forum.alglib.net

ALGLIB forum
It is currently Fri Jun 22, 2018 10:27 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  [ 8 posts ] 
Author Message
 Post subject: Optimization with non-static members
PostPosted: Fri Sep 08, 2017 10:51 am 
Offline

Joined: Fri Sep 08, 2017 10:38 am
Posts: 3
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


Top
 Profile  
 
 Post subject: Re: Optimization with non-static members
PostPosted: Tue Sep 12, 2017 1:36 pm 
Offline
Site Admin

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


Top
 Profile  
 
 Post subject: Re: Optimization with non-static members
PostPosted: Wed Sep 13, 2017 11:42 am 
Offline

Joined: Fri Sep 08, 2017 10:38 am
Posts: 3
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


Top
 Profile  
 
 Post subject: Re: Optimization with non-static members
PostPosted: Wed Sep 13, 2017 1:12 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 825
Does

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


works without warnings?


Top
 Profile  
 
 Post subject: Re: Optimization with non-static members
PostPosted: Wed Sep 13, 2017 1:36 pm 
Offline

Joined: Fri Sep 08, 2017 10:38 am
Posts: 3
Sergey,

Amazing! You made my day :)


Top
 Profile  
 
 Post subject: Re: Optimization with non-static members
PostPosted: Mon Dec 18, 2017 5:08 pm 
Offline

Joined: Thu Dec 14, 2017 5:30 pm
Posts: 2
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


Top
 Profile  
 
 Post subject: Re: Optimization with non-static members
PostPosted: Wed Dec 20, 2017 12:49 pm 
Offline
Site Admin

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


Top
 Profile  
 
 Post subject: Re: Optimization with non-static members
PostPosted: Fri Dec 22, 2017 10:00 am 
Offline

Joined: Thu Dec 14, 2017 5:30 pm
Posts: 2
Thank you very much I will look into it !


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 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