Mantis - ALGLIB
Viewing Issue Advanced Details
505 General minor always 2013-02-20 16:07 2013-05-21 17:16
SergeyB  
SergeyB  
normal  
resolved  
fixed  
none    
none 3.8.0  
Unspecified
0000505: FIXED: minor bug in the implementation of proxy array/matrix object
Fixed minor bug which appeared when user of the ALGLIB optimizers tried to reallocate array which was passed to the callback function. Optimizer was unaware of the new array which was allocated and was unable to track changes in the function gradient (and thus - to converge).

This bug was marked as minor, because a) it is very rare use case, b) it is impossible to overlook (optimizer does not work at all 100% of the time).

==================

Reported by Eden (http://forum.alglib.net/viewtopic.php?f=2&t=772)

0. This bug appears in ALL C++ code which works with proxy array/matrix objects which are passed to user-defined callbacks

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.


=============================================
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;
}
Issue History
2013-02-20 16:07 SergeyB New Issue
2013-02-20 16:07 SergeyB Status new => assigned
2013-02-20 16:07 SergeyB Assigned To => SergeyB
2013-02-20 16:07 SergeyB Programming language => Unspecified
2013-05-21 17:16 SergeyB Summary Minor bug in the implementation of proxy array/matrix object => FIXED: minor bug in the implementation of proxy array/matrix object
2013-05-21 17:16 SergeyB Description Updated
2013-05-21 17:16 SergeyB Status assigned => resolved
2013-05-21 17:16 SergeyB Fixed in Version => 3.8.0
2013-05-21 17:16 SergeyB Resolution open => fixed

There are no notes attached to this issue.