forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 8:13 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: Question on optimization
PostPosted: Sun Jan 23, 2011 7:27 pm 
Offline

Joined: Sun Jan 23, 2011 7:12 pm
Posts: 3
Sorry.
I can not understand how to modify programs using alglib.
Consider some examples. If the function of the family
"void function1_func (...)" instead "func = 100*pow(x[0]+3,4) + pow (x[1]-3.4)"
to compute "func = Z*pow(x[0]+3,4) + pow(x[1]-3.4)", where Z is calculated in another module.

Thank you.
Eugene.


Top
 Profile  
 
 Post subject: Re: Question on optimization
PostPosted: Sun Jan 23, 2011 8:12 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
You can do as follows:

Code:
#include <stdafx.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <optimization.h>

using namespace alglib;

double function_z(void *data_for_calculation)
{
    return 1.2345678;
}

void function1_grad(const real_1d_array &x, double &func, real_1d_array &grad, void *ptr)
{
    // ptr contains pointer to data required to calculate Z, you can pass it to external function
    func = function_z(ptr)*pow(x[0]+3,4) + pow(x[1]-3,4);
    grad[0] = 4*function_z(ptr)*pow(x[0]+3,3);
    grad[1] = 4*pow(x[1]-3,3);
}

int main(int argc, char **argv)
{
    //
    // This example demonstrates minimization of f(x,y) = 100*(x+3)^4+(y-3)^4
    // with nonlinear conjugate gradient method.
    //
    real_1d_array x = "[0,0]";
    double epsg = 0.0000000001;
    double epsf = 0;
    double epsx = 0;
    ae_int_t maxits = 0;
    mincgstate state;
    mincgreport rep;

    void *data_for_calculation = .... ; // pointer to data required to calculate Z

    mincgcreate(x, state);
    mincgsetcond(state, epsg, epsf, epsx, maxits);
    mincgoptimize(state, function1_grad, NULL, data_for_calculation); // you pass pointer to additional data required to calculate Z
    mincgresults(state, x, rep);

    printf("%d\n", int(rep.terminationtype)); // EXPECTED: 4
    printf("%s\n", x.tostring(2).c_str()); // EXPECTED: [-3,3]
    return 0;
}


Top
 Profile  
 
 Post subject: Re: Question on optimization
PostPosted: Sun Jan 23, 2011 10:17 pm 
Offline

Joined: Sun Jan 23, 2011 7:12 pm
Posts: 3
Thank you very much for your prompt response.
For this particular case it is exhaustive.
But the main interest are the cases the calculation
of arbitrary functions with an arbitrary number of parameters.
For example, in my case
f = sum( Ai*exp( -c*( (x-mi).Sigma.(x-mi) )/2 ), i=0..N ),
where x - vector of dimension M, Sigma - matrix of dimension MxM.
M = 7, order N >= 500. All mi read from file.
Ai, c, Sigma - calculated by mi.

Gradient - respectively.
What to do in this case?
Modification of the function "
Code:
void function1_grad(const real_1d_array &L, double &func, real_1d_array &grad, void *ptr)
"
to "
Code:
void function1_grad(const real_1d_array &L, double &func, real_1d_array &grad, void *ptr, ... + params)
"
the compiler does not accept.
On a single pointer (*ptr) all the calculations do not hang. Although the use of alglib it would be preferable.

Thank you.
Eugene.


Top
 Profile  
 
 Post subject: Re: Question on optimization
PostPosted: Mon Jan 24, 2011 7:13 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
You can use *ptr to pass pointer to some complex structure (declared by you), which stores Ai, mi, Sigma, filenames, etc. It is possible without modification of ALGLIB.


Top
 Profile  
 
 Post subject: Re: Question on optimization
PostPosted: Mon Jan 24, 2011 7:51 pm 
Offline

Joined: Sun Jan 23, 2011 7:12 pm
Posts: 3
All made in accordance with the foregoing, and manual.
An error while variable initializing. Append simple project for MSVS2008.
Thank you, Eugene.


Attachments:
File comment: Project for MSVS2008
OptimDistrib1.rar [782.35 KiB]
Downloaded 736 times
Top
 Profile  
 
 Post subject: Re: Question on optimization
PostPosted: Thu Mar 24, 2011 9:47 am 
Offline

Joined: Mon Feb 28, 2011 7:23 am
Posts: 9
int main(int argc, char **argv)
{
//
// This example demonstrates minimization of f(x,y) = 100*(x+3)^4+(y-3)^4
// with nonlinear conjugate gradient method.
//
real_1d_array x = "[0,0]";
double epsg = 0.0000000001;
double epsf = 0;
double epsx = 0;
ae_int_t maxits = 0;
mincgstate state;
mincgreport rep;

void *data_for_calculation = .... ; // pointer to data required to calculate Z
// this can not pass data to function
// double *data = new double [10] ;
// if(NULL==data) return -1;
// void *data_for_calculation = data ;
// this is wrong.VS2008 can not compile it,"error C2036: “void *”: ?????"

mincgcreate(x, state);
mincgsetcond(state, epsg, epsf, epsx, maxits);
mincgoptimize(state, function1_grad, NULL, data_for_calculation); // you pass pointer to additional data required to calculate Z
mincgresults(state, x, rep);

printf("%d\n", int(rep.terminationtype)); // EXPECTED: 4
printf("%s\n", x.tostring(2).c_str()); // EXPECTED: [-3,3]
return 0;
}


Top
 Profile  
 
 Post subject: Re: Question on optimization
PostPosted: Sun Apr 15, 2012 4:50 pm 
Offline

Joined: Sun Apr 15, 2012 4:26 pm
Posts: 1
"You can use *ptr to pass pointer to some complex structure (declared by you), which stores Ai, mi, Sigma, filenames, etc. It is possible without modification of ALGLIB."

Hi there, I am relatively new to c++, so i have a basic question about the statement above: how do I do this? I would like to pass an object to the function to be optimized, but when i do it through the void *ptr, the object isn't recognized as such within the function. My code is below. Any suggestions would be greatly appreciated.

Thanks

void function1_fvec(const alglib::real_1d_array &x, alglib::real_1d_array &fi, void *my_obj)
{
//
// this callback calculates
// f0(x0,x1) = (a number)*100*(x0+3)^4,
// f1(x0,x1) = (x1-3)^4
//

fi[0] = my_obj->A[2]*10*pow(x[0]+3,2);
fi[1] = pow(x[1]-3,2);
}


int main(void)
{




alglib::real_1d_array x = "[0,0,3,6,-4.368]";
double epsg = 0.0000000001;
double epsf = 0;
double epsx = 0;
alglib::ae_int_t maxits = 0;
alglib::minlmstate state;
alglib::minlmreport rep;

my_type *my_obj = new my_type(.....);

minlmcreatev(3, x, 0.0001, state);
minlmsetcond(state, epsg, epsf, epsx, maxits);
minlmoptimize(state, function1_fvec,NULL, my_obj);
minlmresults(state, x, rep);

printf("%d\n", int(rep.terminationtype)); // EXPECTED: 4
printf("%s\n", x.tostring(2).c_str()); // EXPECTED: [-3,+3]
}


Top
 Profile  
 
 Post subject: Re: Question on optimization
PostPosted: Tue Apr 17, 2012 6:50 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
You have to cast my_obj from void* back to my_type* before using it in function1_fvec. C++ is strongly typed language and can't determine actual type of the object automatically.


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 6 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