forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 9:21 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  [ 2 posts ] 
Author Message
 Post subject: Question about the optimization package
PostPosted: Wed Nov 10, 2010 8:25 pm 
Offline

Joined: Wed Nov 10, 2010 8:19 pm
Posts: 1
I tried to use the lbsgs optimization function. And the parameters in function1_grad might be changed in my project.
My question is how can use the void* ptr to pass the parameters in the main function.
A simple sample would be great.
Thanks.

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

using namespace alglib;
void function1_grad(const real_1d_array &x, double &func, real_1d_array &grad, void *ptr)
{
//
// this callback calculates f(x0,x1) = 100*(x0+3)^4 + (x1-3)^4
// and its derivatives df/d0 and df/dx1
//
func = 100*pow(x[0]+3,4) + pow(x[1]-3,4);
grad[0] = 400*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
// using LBFGS method.
//
real_1d_array x = "[0,0]";
double epsg = 0.0000000001;
double epsf = 0;
double epsx = 0;
ae_int_t maxits = 0;
minlbfgsstate state;
minlbfgsreport rep;

minlbfgscreate(1, x, state);
minlbfgssetcond(state, epsg, epsf, epsx, maxits);
minlbfgsoptimize(state, function1_grad);
minlbfgsresults(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 about the optimization package
PostPosted: Thu Nov 11, 2010 2:58 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
You can either pass argv as ptr parameter of minlbfgsoptimize - function1_grad will be called with ptr equal to argv.
Alternatively, you can create proxy structure which stores both argc and argv - and pass pointer to this structure.


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

All times are UTC


Who is online

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