forum.alglib.net

ALGLIB forum
It is currently Thu Sep 21, 2017 1:31 am

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: 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: 779
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: 779
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  
 
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: Google [Bot] and 2 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group