forum.alglib.net
http://forum.alglib.net/

Gradient fitting with more constants
http://forum.alglib.net/viewtopic.php?f=2&t=772
Page 1 of 1

Author:  Eden [ Mon Feb 18, 2013 6:42 pm ]
Post subject:  Gradient fitting with more constants

Hello,

i have problem with nonlinear fitting. If I am using only method without gradient, its all Ok. But then I want to use gradient and I cant ger correct result. What is wrong with my code?

Thanks for answer.

Code:
Code:
void function_cx_1_func(const alglib::real_1d_array &c, const alglib::real_1d_array &x, double &func, void *ptr){
   func = c[0]+c[1]*x[0];
}

void function_cx_1_grad(const alglib::real_1d_array &c, const alglib::real_1d_array &x, double &func, alglib::real_1d_array &grad, void *ptr) {
   func = c[0]+c[1]*x[0];
   grad = "[0, 0]";
   grad[0] = 1;
   grad[1] = x[0];
}


int main(int argc, char **argv){

   alglib::real_2d_array x = "[[1], [2], [3], [4], [5]]";
   alglib::real_1d_array y = "[10, 15, 20, 25, 30]";
   alglib::real_1d_array c = "[1, 1]";;

   double epsf = 0;
    double epsx = 0.00001;
    alglib::ae_int_t maxits = 0;
    alglib::ae_int_t info;
    alglib::lsfitstate state;
    alglib::lsfitreport rep;

    //
    // Fitting without weights
    //
    alglib::lsfitcreatefg(x, y, c, true, state);
    alglib::lsfitsetcond(state, epsf, epsx, maxits);
    alglib::lsfitfit(state, function_cx_1_func, function_cx_1_grad);
    alglib::lsfitresults(state, info, c, rep);
   std::cout<<(int)info<<" "<<rep.rmserror<<" "<<c.tostring(1)<<std::endl;

   return 0;
}

Author:  Eden [ Tue Feb 19, 2013 4:40 pm ]
Post subject:  Re: Gradient fitting with more constants

Problem solved. I just removed line
Code:
grad = "[0, 0]";
. But i dont know why it works, because in next two lines i rewrite grad...(in both cases)

Author:  Sergey.Bochkanov [ Wed Feb 20, 2013 6:49 am ]
Post subject:  Re: Gradient fitting with more constants

I don't know too :( just started investigating this issue...

Author:  Sergey.Bochkanov [ Wed Feb 20, 2013 12:05 pm ]
Post subject:  Re: Gradient fitting with more constants

1. grad parameter is a reference to proxy real_1d_array object. This proxy object redirects all accesses into the memory utilized by ALGLIB computational core. The only thing you should can do is to access individual elements of the array - both reads and writes are possible. However, any attempt to resize it (setlength) should result in exception, because proxy guards itself against anything which may change internal data structures.

2. however, there is a loophole in the implementation of the real_1d_array class (and other similar classes). When you assign string constant to proxy object, it effectively breaks connection between proxy and internal memory used by ALGLIB. New independent array is created, and all operations with grad are performed with this new array. Optimizer is unaware of changes and its copy of grad is full of trash data.

3. I added this issue to bug tracker, it is going to be fixed very soon. Maybe we will make bugfix-only release...

Author:  Raf1600 [ Wed Feb 20, 2013 2:17 pm ]
Post subject:  Re: Gradient fitting with more constants

This sounds quite similar to my problem, as I assign the grad vector from the result of a function.

I think I will try to assing the values of the grad vector row by row.

I haven't try your suggestion yet.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/