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 :)

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