[Scilab-users] 'optim' vs 'Nelder-Mead' ... or difficulties to use 'optim'

Paul Bignier paul.bignier at scilab-enterprises.com
Mon Jan 16 10:22:31 CET 2017


Hello Paul,

Rewriting the function as follows:

function f=target(x)
     val_lin = lineaire(x,1,2);
     val_rac = racine(x,10,6);
//        f = abs(val_lin - val_rac);
     f = sqrt((val_lin - val_rac)^2);
     disp(f)
     if ~isreal(f) then pause; end
endfunction

will show you that at a certain iteraton 'x=-2' so "racine(x,10,6)" 
returns a complex number and in turn sqrt() does, when abs() returned 
the modulus (I added a 'pause' so you can play with the variables).

Regards,

Paul


On 01/15/2017 06:04 PM, paul.carrico at free.fr wrote:
> Hi Stephane,
> You're right, I forgot my basis :-)
> Nevertheless when I re-write the target function as the following, it 
> fails after some iterations and I do not understand the origin (??)
> Paul
> #######################################
> function  f=target(x)
>      val_lin  =  lineaire(x,1,2);
>      val_rac  =  racine(x,10,6);
> // f = abs(val_lin - val_rac);
>      f  =  sqrt((val_lin  -  val_rac)^2);  
>      printf("f = %g\n",f);
> endfunction
> ######################################
> Console message:
> *********** qnbd (with bound cstr) ****************
> dimension= 1, epsq= 0.2220446049250313E-15, verbosity level: imp= 3
> max number of iterations allowed: iter= 1000
> max number of calls to costf allowed: nap= 1000
> ------------------------------------------------
> f = 23.6393
> f = 25.1965
> f = 22.0911
> qnbd : iter= 1 f= 0.2363932D+02
> qnbd : nbre fact 1 defact 0 total var factorisees 1
> rlbd tp= 0.6440E+02 tmax= 0.1000E+11 dh0/dt=-0.6027E+00
> f = 21.6468
> f = 23.1924
> f = 20.111
> es t= 0.3318E+01 h=-0.1992E+01 dh/dt=-0.5981E+00 dfh/dt=-0.6005E+00 dt 
> 0.3E+01
> f = 4
> f = 6.47214
> f = 6
> al_parameters,'qn','ar',nocf,niter,imp=3)
> !--error 98
> La variable retournée par la fonction Scilab passée en argument n'est 
> pas valide.
> at line 36 of exec file called by :
> RFACE/Rosembrock/fonctions_test.sce', -1
>
> t= 0.6440E+02 h=-0.1992E+01 dh/dt=-0.5981E+00 dfh/dt= 0.0000E+00 dt 
> 0.6E+02
> qnbd : indqn= 0
> Le 2017-01-15 12:59, Stéphane Mottelet a écrit :
>> Hello
>>
>> Your target function is not differrentiable (Because of the absolute
>> value). That explains why optim has some difficulties. Using a square
>> instead should give the advantage to optim against nelder-mead.
>>
>> S.
>>
>>
>>
>>> Le 15 janv. 2017 à 11:39, paul.carrico at free.fr 
>>> <mailto:paul.carrico at free.fr> a écrit :
>>>
>>> Hi all
>>>
>>> Well, from a "macro" point of view, gradient based methods (order 1 
>>> method) should be more efficient than Nelder-Mead one (order 0) 
>>> especially  for 1D topic, shouldn't be?
>>>
>>> In the case here bellow, when I use 'Optim' it is not the case in 
>>> comparison to 'NelderMead'... I don'y know what I'm doing wrong but 
>>> I decide to frop off 'optim' in favor tp Nelder-Mead - nevertheless 
>>> the later method requires much more iterations :-(
>>>
>>> I'll be interested by some feedback on such topic, either in the 
>>> mailing list (or in MP in order to not pollute it).
>>>
>>> Paul
>>> ########################################"
>>> 'optim'
>>> ########################################
>>> mode(0)
>>>
>>> function f=lineaire(x, a2, b2)
>>>     f = a2*x+b2;
>>> endfunction
>>>
>>> function g=racine(x, a1, b1)
>>>     g = sqrt(a1*x) + b1;
>>> endfunction
>>>
>>> function f=target(x)
>>>     val_lin = lineaire(x,1,2);
>>>     val_rac = racine(x,10,6);
>>>     f = abs(val_lin - val_rac);
>>> endfunction
>>>
>>> // Cost function :
>>> function [f, g, ind]=cost(x, ind)
>>>     f = target(x);
>>> //    g = numderivative(target, x.');
>>> //    g = numderivative(target, x.',0.1,order = 2);
>>>     g = numderivative(target, x.',order = 2);  // better but why ??? 
>>> h automatic works better ???
>>> //    g = numderivative(target, x.',order = 4);
>>> //    g = numderivative(target, x.',0.1, order = 4);
>>> endfunction
>>>
>>> // optimisation avec optim
>>>     initial_parameters = [50]
>>>     lower_bounds = [0];
>>>     upper_bounds = [1000];
>>>     nocf = 1000;      // number of call of f
>>>     niter = 1000;    // number of iterations
>>>     [fopt, xopt, gopt, work, iters, evals, err] = 
>>> optim(cost,'b',lower_bounds,upper_bounds,initial_parameters,'qn','ar',nocf,niter,imp=3);
>>>     xopt
>>>     fopt
>>>     err
>>>
>>> // traçage courbes
>>> x = linspace(0,50,1000)';
>>> plot_raci = racine(x,10,6);
>>> plot_lin = lineaire(x,1,2);
>>>
>>> scf(1);
>>> drawlater();
>>> xgrid(3);
>>> f = gcf();
>>> //f
>>> f.figure_size = [1000, 1000];
>>> f.background = color(255,255,255);
>>> a = gca();
>>> //a
>>> a.font_size = 2;
>>> a.x_label.text = "X axis" ;
>>> a.x_location="bottom";
>>> a.x_label.font_angle=0;
>>> a.x_label.font_size = 4;
>>> a.y_label.text = "Y axis";
>>> a.y_location="left";
>>> a.y_label.font_angle=-90;
>>> a.Y_label.font_size = 4;
>>> a.title.text = "Title";
>>> a.title.font_size = 5;
>>> a.line_style = 1;
>>>
>>> // début des courbes
>>> plot(x,plot_lin);
>>> e1 = gce();
>>> p1 = e1.children;
>>> p1.thickness = 1;
>>> p1.line_style = 1;
>>> p1.foreground = 3;
>>>
>>> plot(x,plot_raci);
>>> e2 = gce();
>>> p2 = e2.children;
>>> p2.thickness = 1;
>>> p2.line_style = 1;
>>> p2.foreground = 2;
>>> drawnow();
>>>
>>> #######################################
>>> Nelder Mead
>>> #######################################
>>> mode(0)
>>>
>>> function f=lineaire(x, a2, b2)
>>>     f = a2*x+b2;
>>> endfunction
>>>
>>> function g=racine(x, a1, b1)
>>>     g = sqrt(a1*x) + b1;
>>> endfunction
>>>
>>> function [f, index]=target(x, index)
>>>     val_lin = lineaire(x,1,2);
>>>     val_rac = racine(x,10,6);
>>>     f = abs(val_lin - val_rac);
>>> endfunction
>>>
>>> // optimisation avec optim
>>>     initial_parameters = [50]
>>>     lower_bounds = [0];
>>>     upper_bounds = [1000];
>>>
>>> nm = neldermead_new ();
>>> nm = neldermead_configure(nm,"-numberofvariables",1);
>>> nm = neldermead_configure(nm,"-function",target);
>>> nm = neldermead_configure(nm,"-x0",initial_parameters);
>>> nm = neldermead_configure(nm,"-maxiter",1000);
>>> nm = neldermead_configure(nm,"-maxfunevals",1000);
>>> nm = neldermead_configure(nm,"-tolfunrelative",10*%eps);
>>> nm = neldermead_configure(nm,"-tolxrelative",10*%eps);
>>> nm = neldermead_configure(nm,"-method","box");
>>> nm = neldermead_configure(nm,"-boundsmin",lower_bounds);
>>> nm = neldermead_configure(nm,"-boundsmax", upper_bounds);
>>> nm = neldermead_search(nm);
>>> nm = neldermead_restart(nm);
>>> xopt = neldermead_get(nm,"-xopt")
>>> fopt = neldermead_get(nm,"-fopt")
>>> nm = neldermead_destroy(nm);
>>>
>>> // traçage courbes
>>> x = linspace(0,50,1000)';
>>> a1 = 10; b1 = 6;  // b1 > b2 here
>>> a2 = 1; b2 = 2;
>>>
>>> plot_raci = racine(x,a1,b1);
>>> plot_lin = lineaire(x,a2,b2);
>>>
>>> scf(1);
>>> drawlater();
>>> xgrid(3);
>>> f = gcf();
>>> //f
>>> f.figure_size = [1000, 1000];
>>> f.background = color(255,255,255);
>>> a = gca();
>>> //a
>>> a.font_size = 2;
>>> a.x_label.text = "X axis" ;
>>> a.x_location="bottom";
>>> a.x_label.font_angle=0;
>>> a.x_label.font_size = 4;
>>> a.y_label.text = "Y axis";
>>> a.y_location="left";
>>> a.y_label.font_angle=-90;
>>> a.Y_label.font_size = 4;
>>> a.title.text = "Title";
>>> a.title.font_size = 5;
>>> a.line_style = 1;
>>>
>>> // début des courbes
>>> plot(x,plot_lin);
>>> e1 = gce();
>>> p1 = e1.children;
>>> p1.thickness = 1;
>>> p1.line_style = 1;
>>> p1.foreground = 3;
>>>
>>> plot(x,plot_raci);
>>> e2 = gce();
>>> p2 = e2.children;
>>> p2.thickness = 1;
>>> p2.line_style = 1;
>>> p2.foreground = 2;
>>> drawnow();
>>> _______________________________________________
>>> users mailing list
>>> users at lists.scilab.org <mailto:users at lists.scilab.org>
>>> http://lists.scilab.org/mailman/listinfo/users
>>
>> _______________________________________________
>> users mailing list
>> users at lists.scilab.org <mailto:users at lists.scilab.org>
>> http://lists.scilab.org/mailman/listinfo/users
>
>
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users

-- 
Paul BIGNIER
Development engineer
-----------------------------------------------------------
Scilab Enterprises
143bis rue Yves Le Coz - 78000 Versailles, France
Phone: +33.1.80.77.04.68
http://www.scilab-enterprises.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20170116/17466e48/attachment.htm>


More information about the users mailing list