optim - non-linear optimization routine
Non-linear optimization routine for programs without constraints or with bound constraints:
min costf(x) w.r.t x.
costf is an "external" i.e function, or list or Fortran routine (see "external"). This external must return f (costf(x)) and g (gradient of costf) given x.
If costf is a function, the calling sequence for costf must be:
[f,g,ind]=costf(x,ind).
Here, costf is a function which returns f, value (real number) of cost function at x, and g, gradient vector of cost function at x. The variable ind is used by optim and is described below.
If ind=2 (resp. 3, 4), costf must provide f (resp. g, f and g).
If ind=1 nothing is computed (used for display purposes only).
On output, ind<0 means that f cannot be evaluated at x and ind=0 interrupts the optimization.
If costf is a character string, it refers to the name of a Fortran routine which must be linked to Scilab (see examples in the routines foptim.f and e.g. genros.f in the directory SCIDIR/default)
Dynamic link of Fortran routine is also possible (help link).
Here, the generic calling sequence for the Fortran subroutine is: function costf(ind,n,x,f,g,ti,tr,td)
ind has the same meaning as above if set to 0,1,2 but the values ind=10 and ind=11 are now valid. These values are used for initializations (see below).
n is the dimension of x, x is an n vector, ti,tr,td are working arrays.
The Fortran function costf must return f and the vector g, given x, ind, n, ti, tr, td.
If costf is given as a Fortran routine, it is possible to initialize parameters or to send Scilab variables to this routine.
This facility is managed by the parameter 'in.
If the string 'in' is present, initialization is done by Fortran: optim makes two calls to the Fortran function costf, once with ind=10 and once with ind=11. In this case, for ind=10, costf must set the dimensions nti, ntr, ntd of ti, tr, td in the common/nird/nti, ntr, ntd and, for ind=11, costf must initialize the vectors ti , tr, td (integer vector, real vector, double precision vector respectively).
In the calling sequence of optim, the string 'in' can be replaced by 'ti', valti ,'td' , valtd. Then, the Fortran function costf(ind, x, f, g, ti, tr, td) is evaluated with ti=valti and td=valtd whatever the value of ind. Thus, the Scilab variables valti and valtd (integer vector and real vector) are sent to the Fortran function costf.
It is also possible to save the content of of the working arrays ti and td. This is possible by adding the strings 'si' and/or 'sd' at the ned of the calling sequence of optim. Then, the output variables must be: [f,[x,[g],[to]]],[ti],[td]].
xref=[1;2;3];x0=[1;-1;1] deff('[f,g,ind]=cost(x,ind)','f=0.5*norm(x-xref)^2,g=x-xref'); [f,xopt]=optim(cost,x0) //Simplest call [f,xopt,gopt]=optim(cost,x0,'gc') // By conjugate gradient [f,xopt,gopt]=optim(cost,x0,'nd') //Seen as non differentiable [f,xopt,gopt]=optim(cost,'b',[-1;0;2],[0.5;1;4],x0) // Bounds on x [f,xopt,gopt]=optim(cost,'b',[-1;0;2],[0.5;1;4],x0,'gc') // Bounds on x [f,xopt,gopt]=optim(cost,'b',[-1;0;2],[0.5;1;4],x0,'gc','ar',3) // Here, 3 calls to cost are allowed. // Now calling the Fortran subroutine "genros" in SCIDIR/default/Ex-optim.f // See also the link function for dynamically linking an objective function [f,xopt,gopt]=optim('genros',[1;2;3]) //Rosenbrock's function