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

Question on optimization
http://forum.alglib.net/viewtopic.php?f=2&t=280
Page 1 of 1

Author:  evkut [ Sun Jan 23, 2011 7:27 pm ]
Post subject:  Question on optimization

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.

Author:  Sergey.Bochkanov [ Sun Jan 23, 2011 8:12 pm ]
Post subject:  Re: Question on optimization

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;
}

Author:  evkut [ Sun Jan 23, 2011 10:17 pm ]
Post subject:  Re: Question on optimization

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.

Author:  Sergey.Bochkanov [ Mon Jan 24, 2011 7:13 am ]
Post subject:  Re: Question on optimization

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.

Author:  evkut [ Mon Jan 24, 2011 7:51 pm ]
Post subject:  Re: Question on optimization

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

Author:  frainbbs [ Thu Mar 24, 2011 9:47 am ]
Post subject:  Re: Question on optimization

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;
}

Author:  darn [ Sun Apr 15, 2012 4:50 pm ]
Post subject:  Re: Question on optimization

"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]
}

Author:  Sergey.Bochkanov [ Tue Apr 17, 2012 6:50 am ]
Post subject:  Re: Question on optimization

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.

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