forum.alglib.net

ALGLIB forum
It is currently Tue Apr 16, 2024 1:08 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Gradient fitting with more constants
PostPosted: Mon Feb 18, 2013 6:42 pm 
Offline

Joined: Mon Feb 18, 2013 6:32 pm
Posts: 3
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;
}


Top
 Profile  
 
 Post subject: Re: Gradient fitting with more constants
PostPosted: Tue Feb 19, 2013 4:40 pm 
Offline

Joined: Mon Feb 18, 2013 6:32 pm
Posts: 3
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)


Top
 Profile  
 
 Post subject: Re: Gradient fitting with more constants
PostPosted: Wed Feb 20, 2013 6:49 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 906
I don't know too :( just started investigating this issue...


Top
 Profile  
 
 Post subject: Re: Gradient fitting with more constants
PostPosted: Wed Feb 20, 2013 12:05 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 906
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...


Top
 Profile  
 
 Post subject: Re: Gradient fitting with more constants
PostPosted: Wed Feb 20, 2013 2:17 pm 
Offline

Joined: Thu Jan 24, 2013 7:25 am
Posts: 8
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group