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

Optimization with non-static members
http://forum.alglib.net/viewtopic.php?f=2&t=3813
Page 1 of 1

Author:  MMario [ Fri Sep 08, 2017 10:51 am ]
Post subject:  Optimization with non-static members

Hello all,

I am trying to optimize function within an object I create. However, I am having a lot of problems as the compiler does not like when I try to optimize with objects. Take the following class:

class my_class: mother_class
{
public:
...
void Calibrate();
...
private:
void Objective_Function(const alglib::real_1d_array &x, double &func, void *ptr);
...
}


When I create an object, I want to make a call to Calibrate() within my object:

void my_class::Calibrate() // It calibrates all the MPRs
{
alglib::real_1d_array x = "[1]"; //Initial Value for the MPR
alglib::real_1d_array bndl = "[-10]"; //Lower Bound
alglib::real_1d_array bndu = "[+10]"; // Upper Bound
alglib::minbcstate state;
alglib::minbcreport rep;

double epsg = 0.000001;
double epsf = 0;
double epsx = 0;
alglib::ae_int_t maxits = 0;

double diffstep = 1.0e-6;

minbccreatef(x, diffstep, state);
minbcsetbc(state, bndl, bndu);
minbcsetcond(state, epsg, epsf, epsx, maxits);
minbcoptimize(state, Objective_Function);
minbcresults(state, x, rep);

printf("%d\n", int(rep.terminationtype));
printf("%s\n", x.tostring(10).c_str());
}
}


The bold Objective_Function above gives a compile error but when I declare the function as global or as static within my_class, it does not give a compile error however in both cases I can't access the elements of my class which means I can't solve the optimization problem.

I hope it is clear and that you will be able to help me.

Thank you very much,

Mario

Author:  Sergey.Bochkanov [ Tue Sep 12, 2017 1:36 pm ]
Post subject:  Re: Optimization with non-static members

Hi!

You should define proxy function (traditional one, not class member) which accepts your object as pointer (ptr parameter) and pass your object to minbcoptimize() as ptr parameter. As result, your function will be called with your object as additional parameter. Inside your proxy function, you can call my_class::Objective_Function().

Author:  MMario [ Wed Sep 13, 2017 11:42 am ]
Post subject:  Re: Optimization with non-static members

Dear Sergey,

Thank you very much for your response!

I have tried what you said but I think I am getting something wrong. Could you please take a look at the following? I tried different alternatives consider the following:

class my_class: mother_class
{
public:
...
void Calibrate();
...
private:
void Objective_Function(const alglib::real_1d_array &x, double &func, void *ptr);
...
}



void Proxy_Function(const alglib::real_1d_array &x, double &func, void *ptr)
{
my_class::Objective_Function(const alglib::real_1d_array &x, double &func, void *ptr);
//Or, I tried the following as well:
ptr->Objective_Function(x, func, ptr);

}

void my_class::Objective_Function(const alglib::real_1d_array &x, double &func, void *ptr)
{
...
...
func=...;

}


void my_class::Calibrate() // It calibrates all the MPRs
{
alglib::real_1d_array x = "[1]"; //Initial Value for the MPR
alglib::real_1d_array bndl = "[-10]"; //Lower Bound
alglib::real_1d_array bndu = "[+10]"; // Upper Bound
alglib::minbcstate state;
alglib::minbcreport rep;

double epsg = 0.000001;
double epsf = 0;
double epsx = 0;
alglib::ae_int_t maxits = 0;

double diffstep = 1.0e-6;

minbccreatef(x, diffstep, state);
minbcsetbc(state, bndl, bndu);
minbcsetcond(state, epsg, epsf, epsx, maxits);
minbcoptimize(state, Proxy_Function,NULL, this);
minbcresults(state, x, rep);

printf("%d\n", int(rep.terminationtype));
printf("%s\n", x.tostring(10).c_str());
}
}


I get various compile errors and I think I am getting it completely wrong.

Thank you,

Mario

Author:  Sergey.Bochkanov [ Wed Sep 13, 2017 1:12 pm ]
Post subject:  Re: Optimization with non-static members

Does

Code:
((myclass*)ptr)->Objective_Function(x, func, ptr);


works without warnings?

Author:  MMario [ Wed Sep 13, 2017 1:36 pm ]
Post subject:  Re: Optimization with non-static members

Sergey,

Amazing! You made my day :)

Author:  Lzr [ Mon Dec 18, 2017 5:08 pm ]
Post subject:  Re: Optimization with non-static members

I have kind of a similar problem.

I want to optimize a function that computes doubles from class variables that are private.

To put it simply my : void function that is defined computes the fi array this way :

fi[i] = double_from_data(class variables[i]) - fonction_target(x[0],x[1], x[2] // the parameters
, class variables[i])

let's say the define it like this :

void Class1::function1_fvec(const real_1d_array &x, real_1d_array &fi, void *ptr)
{ /* some computation on class variables*/

for (int i =0; i<N ; i++)
{
fi[i] = double_from_data(class variables[i]) - fonctiontarget(x[0],x[1], x[2] // the parameters
, class variables);

}

}


now when i write a calibration function that would be :
vector <double> calibrate (vector <double> initial_guess)
{
real_1d_array x;

double y[2];
y[0] = 2.0;
y[1] = 2.0;
x.setcontent(2, y);

double epsx = 0.0000000001;


ae_int_t maxits = 100;
minlmstate state;
minlmreport rep;

minlmcreatev(N ,
x,
0.0001,
state);

minlmsetcond(state, epsx, maxits);
alglib::minlmoptimize(state, function1_fvec); // i get an error here
minlmresults(state, x, rep);
}


I don't see how to get around as the same code works for easier functions (like polynomials defined explicitly).

Can someone help me ?


EDIT :

As I tried the solution above. The Class1 remain out of reach, I get accessibility errors. so my question would be how can I use the Alglib with pretty much this ::

fi [i ] = Array_DATA[i] - function of (Array_DATA, other data arrays, data doubles, X (parameters) [i] ;

with the "function of" returning a array (same goes if the function returns a double..

Thank you very much

Author:  Sergey.Bochkanov [ Wed Dec 20, 2017 12:49 pm ]
Post subject:  Re: Optimization with non-static members

Hi!

You can not pass pointer to member function, but you can pass pointer to adapter function, which calls member function. I believe that discussion above already clarified this subject. You may find additional information at https://isocpp.org/wiki/faq/pointers-to-members if you want

Author:  Lzr [ Fri Dec 22, 2017 10:00 am ]
Post subject:  Re: Optimization with non-static members

Thank you very much I will look into it !

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