fminsearch: added demos after Serge's comments
Michaƫl Baudin [Thu, 22 Oct 2009 14:39:00 +0000 (16:39 +0200)]
25 files changed:
scilab/modules/optimization/demos/neldermead/fminsearch_display.sce [new file with mode: 0644]
scilab/modules/optimization/demos/neldermead/fminsearch_optimplotfunccount.sce [new file with mode: 0644]
scilab/modules/optimization/demos/neldermead/fminsearch_optimplotfval.sce [new file with mode: 0644]
scilab/modules/optimization/demos/neldermead/fminsearch_optimplotx.sce [new file with mode: 0644]
scilab/modules/optimization/demos/neldermead/fminsearch_outputfunction.sce [new file with mode: 0644]
scilab/modules/optimization/demos/neldermead/fminsearch_rosenbrock.sce [moved from scilab/modules/optimization/demos/neldermead/fminsearch.sce with 84% similarity]
scilab/modules/optimization/demos/neldermead/fminsearch_tolx.sce [new file with mode: 0644]
scilab/modules/optimization/demos/neldermead/neldermead_boxpost.sce [new file with mode: 0644]
scilab/modules/optimization/demos/neldermead/neldermead_boxproblemA.sce
scilab/modules/optimization/demos/neldermead/neldermead_boxproblemB.sce
scilab/modules/optimization/demos/neldermead/neldermead_outputcmd.sce
scilab/modules/optimization/demos/neldermead/neldermead_rosenbrock.sce
scilab/modules/optimization/demos/neldermead/nmplot_han1.sce
scilab/modules/optimization/demos/neldermead/nmplot_han2.sce
scilab/modules/optimization/demos/neldermead/nmplot_mckinnon.sce
scilab/modules/optimization/demos/neldermead/nmplot_mckinnon2.sce
scilab/modules/optimization/demos/neldermead/nmplot_quadratic.fixed.sce
scilab/modules/optimization/demos/neldermead/nmplot_quadratic.fixed2.sce
scilab/modules/optimization/demos/neldermead/nmplot_rosenbrock.fixed.sce
scilab/modules/optimization/demos/neldermead/nmplot_rosenbrock.sce
scilab/modules/optimization/demos/optim/optim_output.sce [new file with mode: 0644]
scilab/modules/optimization/demos/optim/optim_plot.sce [new file with mode: 0644]
scilab/modules/optimization/demos/optim/optim_rosenbrock.sce [new file with mode: 0644]
scilab/modules/optimization/demos/optim/optim_withderivative.sce [new file with mode: 0644]
scilab/modules/optimization/demos/optimization.dem.gateway.sce

diff --git a/scilab/modules/optimization/demos/neldermead/fminsearch_display.sce b/scilab/modules/optimization/demos/neldermead/fminsearch_display.sce
new file mode 100644 (file)
index 0000000..997ea33
--- /dev/null
@@ -0,0 +1,37 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009 - Digiteo - Michael Baudin
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+
+mprintf("Running optimization...\n");
+
+function y = banana (x)
+  y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
+endfunction
+opt = optimset ( "Display" , "iter" );
+[x fval] = fminsearch ( banana , [-1.2 1] , opt );
+//
+// Display results
+//
+mprintf("x=%s\n",strcat(string(x)," "));
+mprintf("fval=%e\n",fval);
+mprintf("exitflag=%d\n",exitflag);
+mprintf("output.message:\n");
+for i =1:3
+  mprintf(output.message(i));
+  mprintf("\n");
+end
+mprintf("output.algorithm:%s\n",output.algorithm);
+mprintf("output.funcCount:%d\n",output.funcCount);
+mprintf("output.iterations:%d\n",output.iterations);
+//
+// Load this script into the editor
+//
+filename = 'fminsearch_display.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
+
diff --git a/scilab/modules/optimization/demos/neldermead/fminsearch_optimplotfunccount.sce b/scilab/modules/optimization/demos/neldermead/fminsearch_optimplotfunccount.sce
new file mode 100644 (file)
index 0000000..ea83fb1
--- /dev/null
@@ -0,0 +1,28 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009 - Digiteo - Michael Baudin
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+
+mprintf("Running optimization...\n");
+
+function y = banana (x)
+  y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
+endfunction
+opt = optimset ( "PlotFcns" , optimplotfunccount );
+[x fval] = fminsearch ( banana , [-1.2 1] , opt );
+//
+// Display results
+//
+mprintf("x=%s\n",strcat(string(x)," "));
+mprintf("fval=%e\n",fval);
+//
+// Load this script into the editor
+//
+filename = 'fminsearch_optimplotfunccount.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
+
diff --git a/scilab/modules/optimization/demos/neldermead/fminsearch_optimplotfval.sce b/scilab/modules/optimization/demos/neldermead/fminsearch_optimplotfval.sce
new file mode 100644 (file)
index 0000000..f4589ca
--- /dev/null
@@ -0,0 +1,28 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009 - Digiteo - Michael Baudin
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+
+mprintf("Running optimization...\n");
+
+function y = banana (x)
+  y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
+endfunction
+opt = optimset ( "PlotFcns" , optimplotfval );
+[x fval] = fminsearch ( banana , [-1.2 1] , opt );
+//
+// Display results
+//
+mprintf("x=%s\n",strcat(string(x)," "));
+mprintf("fval=%e\n",fval);
+//
+// Load this script into the editor
+//
+filename = 'fminsearch_optimplotfval.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
+
diff --git a/scilab/modules/optimization/demos/neldermead/fminsearch_optimplotx.sce b/scilab/modules/optimization/demos/neldermead/fminsearch_optimplotx.sce
new file mode 100644 (file)
index 0000000..fe22473
--- /dev/null
@@ -0,0 +1,28 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009 - Digiteo - Michael Baudin
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+
+mprintf("Running optimization...\n");
+
+function y = banana (x)
+  y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
+endfunction
+opt = optimset ( "PlotFcns" , optimplotx );
+[x fval] = fminsearch ( banana , [-1.2 1] , opt );
+//
+// Display results
+//
+mprintf("x=%s\n",strcat(string(x)," "));
+mprintf("fval=%e\n",fval);
+//
+// Load this script into the editor
+//
+filename = 'fminsearch_optimplotx.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
+
diff --git a/scilab/modules/optimization/demos/neldermead/fminsearch_outputfunction.sce b/scilab/modules/optimization/demos/neldermead/fminsearch_outputfunction.sce
new file mode 100644 (file)
index 0000000..d6d8cc0
--- /dev/null
@@ -0,0 +1,45 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009 - Digiteo - Michael Baudin
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+
+mprintf("Running optimization...\n");
+
+function y = banana (x)
+ y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
+endfunction
+
+function outfun ( x , optimValues , state )
+  fc = optimValues.funccount;
+  fv = optimValues.fval;
+  it = optimValues.iteration;
+  pr = optimValues.procedure;
+  mprintf ( "%d %e %d -%s-\n" , fc , fv , it , pr )
+endfunction
+opt = optimset ( "OutputFcn" , outfun );
+[x fval] = fminsearch ( banana , [-1.2 1] , opt );
+//
+// Display results
+//
+mprintf("x=%s\n",strcat(string(x)," "));
+mprintf("fval=%e\n",fval);
+mprintf("exitflag=%d\n",exitflag);
+mprintf("output.message:\n");
+for i =1:3
+  mprintf(output.message(i));
+  mprintf("\n");
+end
+mprintf("output.algorithm:%s\n",output.algorithm);
+mprintf("output.funcCount:%d\n",output.funcCount);
+mprintf("output.iterations:%d\n",output.iterations);
+//
+// Load this script into the editor
+//
+filename = 'fminsearch_outputfunction.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
+
@@ -7,14 +7,15 @@
 // are also available at
 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 
-mprintf("Defining Rosenbrock function...\n");
+mprintf("Running optimization...\n");
 function y = banana (x)
   y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
 endfunction
 x0 = [-1.2 1]
-mprintf("x0=%s\n",strcat(string(x0)," "));
-mprintf("Running optimization...\n");
 [x , fval , exitflag , output] = fminsearch ( banana , x0 );
+//
+// Display results
+//
 mprintf("x=%s\n",strcat(string(x)," "));
 mprintf("fval=%e\n",fval);
 mprintf("exitflag=%d\n",exitflag);
@@ -26,6 +27,10 @@ end
 mprintf("output.algorithm:%s\n",output.algorithm);
 mprintf("output.funcCount:%d\n",output.funcCount);
 mprintf("output.iterations:%d\n",output.iterations);
-
-
+//
+// Load this script into the editor
+//
+filename = 'fminsearch_rosenbrock.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
 
diff --git a/scilab/modules/optimization/demos/neldermead/fminsearch_tolx.sce b/scilab/modules/optimization/demos/neldermead/fminsearch_tolx.sce
new file mode 100644 (file)
index 0000000..ebd9f5a
--- /dev/null
@@ -0,0 +1,39 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009 - Digiteo - Michael Baudin
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+
+mprintf("Running optimization...\n");
+
+function y = banana (x)
+ y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
+endfunction
+
+x0 = [-1.2 1]
+opt = optimset ( "TolX" , 1.e-2 );
+[x , fval , exitflag , output] = fminsearch ( banana , x0 , opt );
+//
+// Display results
+//
+mprintf("x=%s\n",strcat(string(x)," "));
+mprintf("fval=%e\n",fval);
+mprintf("exitflag=%d\n",exitflag);
+mprintf("output.message:\n");
+for i =1:3
+  mprintf(output.message(i));
+  mprintf("\n");
+end
+mprintf("output.algorithm:%s\n",output.algorithm);
+mprintf("output.funcCount:%d\n",output.funcCount);
+mprintf("output.iterations:%d\n",output.iterations);
+//
+// Load this script into the editor
+//
+filename = 'fminsearch_tolx.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
+
diff --git a/scilab/modules/optimization/demos/neldermead/neldermead_boxpost.sce b/scilab/modules/optimization/demos/neldermead/neldermead_boxpost.sce
new file mode 100644 (file)
index 0000000..b31c43b
--- /dev/null
@@ -0,0 +1,157 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2008-2009 - INRIA - Michael Baudin
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+
+//
+// nmplot_boxpost.sce --
+//   Show that the Box algorithm is able to reproduce the 
+//   numerical experiment presented in Richardson and Kuester's paper.
+//   Rosenbrock's Post Office
+//
+
+mprintf("Illustrates Box'' algorithm on Box problem A.\n");
+mprintf("Defining Box Problem A function...\n");
+
+//
+//  Reference:
+//
+//   M.J.Box, 
+//   "A new method of constrained optimization 
+//   and a comparison with other methods".
+//   The Computer Journal, Volume 8, Number 1, 1965, 42--52
+//   Problem A
+//
+//   Algorithm 454: the complex method for constrained optimization [E4]
+//   Communications of the ACM, Volume 16 ,  Issue 8  (August 1973)
+//   Pages: 487 - 489   
+//
+//   Richardson and Kuester Results :
+//   F=1.0000
+//   X1 = 3.0000
+//   X2 = 1.7320
+//   Iterations : 68
+//
+//
+// Note 
+//    The maximum bound for the parameter x1
+//    is not indicated in Box's paper, but indicated in "Algo 454".
+//    The maximum bound for x2 is set to 100/sqrt(3) and satisfies the constraint on x2.
+//    The original problem was to maximize the cost function.
+//    Here, the problem is to minimize the cost function.
+
+//
+// boxproblemB --
+//   Computes the Box problem B cost function and 
+//   inequality constraints.
+//
+// Arguments
+//   x: the point where to compute the function
+//   index : the stuff to compute
+//   data : the parameters of Box cost function
+// Note
+//  The following protocol is used
+//  * if index=1, or no index, returns the value of the cost 
+//    function (default case)
+//  * if index=2, returns the value of the nonlinear inequality 
+//    constraints, as a row array
+//  * if index=3, returns an array which contains
+//    at index #0, the value of the cost function  
+//    at index #1 to the end is the list of the values of the nonlinear 
+//    constraints
+//  The inequality constraints are expected to be positive.
+//
+function [ f , c , index ] = boxproblemB ( x , index )
+  if (~isdef('index','local')) then
+    index = 1
+  end
+  x3 = x(1) + sqrt(3.0) * x(2)
+  if ( index==1 | index==3 ) then
+    f = -(9.0 - (x(1) - 3.0) ^ 2) * x(2) ^ 3 / 27.0 / sqrt(3.0)
+  end
+  if ( index==2 | index==3 ) then
+      c1 = x(1) / sqrt(3.0) - x(2)
+      c2 = x3
+      c3 = 6.0 - x3
+      c = [c1 c2 c3]
+  end
+endfunction
+
+
+//
+// Initialize the random number generator, so that the results are always the
+// same.
+//
+rand("seed" , 0)
+
+x0 = [1.0 0.5].';
+// Compute f(x0) : should be close to -0.0133645895646
+[ fx0 , c, index ] = boxproblemB ( x0 , 1 );
+mprintf("Computed fx0 = %e (expected = %e)\n",fx0 , -0.0133645895646 );
+result = boxproblemB ( x0 , 2 );
+mprintf("Computed Constraints(x0) = [%e %e %e]\n", ...
+  result(1), result(2), result(3) );
+mprintf("Expected Constraints(x0) = [%e %e %e]\n", ...
+  0.0773503 , 1.8660254 , 4.1339746 );
+
+
+xopt = [3.0 1.7320508075688774].';
+// Compute f(xopt) : should be -1.0
+fopt = boxproblemB ( xopt );
+mprintf("Computed fopt = %e (expected = %e)\n", fopt , -1.0 );
+
+nm = neldermead_new ();
+nm = neldermead_configure(nm,"-numberofvariables",2);
+nm = neldermead_configure(nm,"-function",boxproblemB);
+nm = neldermead_configure(nm,"-x0",x0);
+nm = neldermead_configure(nm,"-maxiter",300);
+nm = neldermead_configure(nm,"-maxfunevals",300);
+nm = neldermead_configure(nm,"-method","box");
+nm = neldermead_configure(nm,"-verbose",1);
+nm = neldermead_configure(nm,"-logfile" , "postoffice.txt" );
+nm = neldermead_configure(nm,"-verbosetermination",1);
+nm = neldermead_configure(nm,"-boundsmin",[0.0 0.0]);
+nm = neldermead_configure(nm,"-boundsmax",[100.0 57.735026918962582]);
+// Configure like Box
+nm = neldermead_configure(nm,"-simplex0method","randbounds");
+nm = neldermead_configure(nm,"-nbineqconst",3);
+nm = neldermead_configure(nm,"-tolxmethod" , %f );
+nm = neldermead_configure(nm,"-tolsimplexizemethod",%f);
+nm = neldermead_configure(nm,"-boxtermination" , %t );
+nm = neldermead_configure(nm,"-boxtolf" , 0.001 );
+nm = neldermead_configure(nm,"-boxboundsalpha" , 0.0001 );
+
+//
+// Check that the cost function is correctly connected.
+// The index must be provided, because the additionnal argument "data"
+// comes after.
+//
+[ nm , result ] = neldermead_function ( nm , x0 , 1 );
+//
+// Perform optimization
+//
+nm = neldermead_search(nm);
+neldermead_display(nm);
+xcomp = neldermead_get(nm,"-xopt");
+mprintf("x computed=%s\n",strcat(string(xcomp)," "));
+mprintf("x expected=%s\n",strcat(string(xopt)," "));
+shift = norm(xcomp-xopt)/norm(xopt);
+mprintf("Shift =%f\n",shift);
+fcomp = neldermead_get(nm,"-fopt");
+mprintf("f computed=%f\n",fcomp);
+mprintf("f expected=%f\n",fopt);
+shift = abs(fcomp-fopt)/abs(fopt);
+mprintf("Shift =%f\n",shift);
+nm = neldermead_destroy(nm);
+deletefile ( "postoffice.txt" )
+//
+// Load this script into the editor
+//
+filename = 'neldermead_boxpost.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
+
index 512e88d..21cd08e 100644 (file)
@@ -178,3 +178,10 @@ shift = abs(fcomp-fopt)/abs(fopt);
 mprintf("Shift =%f\n",shift);
 nm = neldermead_destroy(nm);
 deletefile ( "boxproblemA.txt" )
+//
+// Load this script into the editor
+//
+filename = 'neldermead_boxproblemA.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
+
index 4c93e1f..b4acff3 100644 (file)
@@ -13,7 +13,7 @@
 //   numerical experiment presented in Box's paper.
 //
 
-mprintf("Illustrates Box'' algorithm on Box problem A.\n");
+mprintf("Illustrates Box'' algorithm on Box problem B.\n");
 mprintf("Defining Box Problem A function...\n");
 
 //
@@ -135,4 +135,10 @@ shift = abs(fcomp-fopt)/abs(fopt);
 mprintf("Shift =%f\n",shift);
 nm = neldermead_destroy(nm);
 deletefile ( "boxproblemB.txt" )
+//
+// Load this script into the editor
+//
+filename = 'neldermead_boxproblemB.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
 
index e37e92f..a3a27e7 100644 (file)
@@ -65,4 +65,10 @@ nm = neldermead_configure(nm,"-outputcommand",myoutputcmd);
 nm = neldermead_search(nm);
 nm = neldermead_destroy(nm);
 
+//
+// Load this script into the editor
+//
+filename = 'neldermead_outputcmd.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
 
index eeed1e1..c96ae6e 100644 (file)
@@ -68,4 +68,10 @@ my_handle.children.children(1).children.mark_foreground = 5;
 nm = neldermead_destroy(nm);
 clear nm;
 
+//
+// Load this script into the editor
+//
+filename = 'neldermead_rosenbrock.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
 
index 2117511..dc9e2f9 100644 (file)
@@ -83,4 +83,10 @@ deletefile("han1-history-fopt.txt");
 deletefile("han1-history-sigma.txt");
 nm = nmplot_destroy(nm);
 
+//
+// Load this script into the editor
+//
+filename = 'nmplot_han1.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
 
index cd2d2a6..d412656 100644 (file)
@@ -66,6 +66,7 @@ nmplot_display(nm);
 //
 // Plot
 //
+mprintf("Plotting contour...\n");
 [nm , xdata , ydata , zdata ] = nmplot_contour ( nm , xmin = -0.2 , xmax = 1.2 , ymin = -1.5 , ymax = 1.5 , nx = 50 , ny = 50 );
 f = scf(100001);
 xset("fpf"," ")
@@ -76,4 +77,10 @@ drawnow();
 deletefile("han2-history-simplex.txt");
 nm = nmplot_destroy(nm);
 
+//
+// Load this script into the editor
+//
+filename = 'nmplot_han2.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
 
index ce17448..b23b4ac 100644 (file)
@@ -155,4 +155,10 @@ deletefile("mckinnon.history.fopt.txt");
 deletefile("mckinnon.history.sigma.txt");
 nm = nmplot_destroy(nm);
 
+//
+// Load this script into the editor
+//
+filename = 'nmplot_mckinnon.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
 
index 02a1369..ef715a4 100644 (file)
@@ -157,4 +157,10 @@ deletefile("mckinnon.history.restart.fopt.txt");
 deletefile("mckinnon.history.restart.sigma.txt");
 nm = nmplot_destroy(nm);
 
+//
+// Load this script into the editor
+//
+filename = 'nmplot_mckinnon2.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
 
index 2474685..39449ed 100644 (file)
@@ -80,4 +80,10 @@ deletefile("rosenbrock.fixed.history.fopt.txt");
 deletefile("rosenbrock.fixed.history.sigma.txt");
 nm = nmplot_destroy(nm);
 
+//
+// Load this script into the editor
+//
+filename = 'nmplot_quadratic.fixed.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
 
index 0c0b1de..3301ea8 100644 (file)
@@ -81,4 +81,10 @@ deletefile("rosenbrock.fixed.history.fopt.txt");
 deletefile("rosenbrock.fixed.history.sigma.txt");
 nm = nmplot_destroy(nm);
 
+//
+// Load this script into the editor
+//
+filename = 'nmplot_quadratic.fixed2.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
 
index 6b86fba..07e363c 100644 (file)
@@ -67,4 +67,10 @@ deletefile("rosenbrock.fixed.history.fopt.txt");
 deletefile("rosenbrock.fixed.history.sigma.txt");
 nm = nmplot_destroy(nm);
 
+//
+// Load this script into the editor
+//
+filename = 'nmplot_rosenbrock.fixed.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
 
index ff36d69..aaf8a6d 100644 (file)
@@ -68,4 +68,10 @@ deletefile("rosenbrock.history.fopt.txt");
 deletefile("rosenbrock.history.sigma.txt");
 nm = nmplot_destroy(nm);
 
+//
+// Load this script into the editor
+//
+filename = 'nmplot_rosenbrock.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
 
diff --git a/scilab/modules/optimization/demos/optim/optim_output.sce b/scilab/modules/optimization/demos/optim/optim_output.sce
new file mode 100644 (file)
index 0000000..929d4f8
--- /dev/null
@@ -0,0 +1,40 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009 - Digiteo - Michael Baudin
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+
+mprintf("Running optimization...\n");
+
+xref=[1;2;3];
+x0=[1;-1;1];
+function [f,g,ind] = cost(x,ind)
+  f=0.5*norm(x-xref)^2;
+  g=x-xref;
+endfunction
+function [f,g,ind] = cost(x,ind)
+  if ( ind == 1 | ind == 4 ) then
+    f=0.5*norm(x-xref)^2;
+  end
+  if ( ind == 1 | ind == 4 ) then
+    g=x-xref;
+  end
+  if ( ind == 1 ) then
+    mprintf("===========\n")
+    mprintf("x = %s\n", strcat(string(x)," "))
+    mprintf("f = %e\n", f)
+    g=x-xref;
+    mprintf("g = %s\n", strcat(string(g)," "))
+  end
+endfunction
+[f,xopt]=optim(cost,x0,imp=-1)
+//
+// Load this script into the editor
+//
+filename = 'optim_output.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
+
diff --git a/scilab/modules/optimization/demos/optim/optim_plot.sce b/scilab/modules/optimization/demos/optim/optim_plot.sce
new file mode 100644 (file)
index 0000000..ad1d61a
--- /dev/null
@@ -0,0 +1,61 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009 - Digiteo - Michael Baudin
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+
+mprintf("Running optimization...\n");
+
+// 1. Define rosenbrock
+function [ f , g , ind ] = rosenbrock ( x , ind )
+  if ((ind == 1) | (ind == 4)) then
+    f = 100.0 *(x(2)-x(1)^2)^2 + (1-x(1))^2;
+  end
+  if ((ind == 1) | (ind == 4)) then
+    g(1) = - 400. * ( x(2) - x(1)**2 ) * x(1) -2. * ( 1. - x(1) )
+    g(2) = 200. * ( x(2) - x(1)**2 )
+  end
+  if (ind == 1) then
+    plot ( x(1) , x(2) , "g." )
+  end
+endfunction
+x0 = [-1.2 1.0];
+xopt = [1.0 1.0];
+// 2. Draw the contour of Rosenbrock's function
+mprintf("Draw contours...\n");
+xmin = -2.0
+xmax = 2.0
+stepx = 0.1
+ymin = -1.0
+ymax = 2.0
+stepy = 0.1
+nx = 100
+ny = 100
+stepx = (xmax - xmin)/nx;
+xdata = xmin:stepx:xmax;
+stepy = (ymax - ymin)/ny;
+ydata = ymin:stepy:ymax;
+for ix = 1:length(xdata)
+    for iy = 1:length(ydata)
+      x = [xdata(ix) ydata(iy)];
+      [ f , g , ind ] = rosenbrock ( x , 4 );
+      zdata ( ix , iy ) = f;
+    end
+end
+f = scf();
+contour ( xdata , ydata , zdata , [1 10 100 500 1000])
+plot(x0(1) , x0(2) , "b.")
+plot(xopt(1) , xopt(2) , "r*")
+// 3. Plot the optimization process, during optimization
+mprintf("Plot points during optimization...\n");
+[ fopt , xopt ] = optim ( rosenbrock , x0 , imp = -1)
+//
+// Load this script into the editor
+//
+filename = 'optim_plot.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
+
diff --git a/scilab/modules/optimization/demos/optim/optim_rosenbrock.sce b/scilab/modules/optimization/demos/optim/optim_rosenbrock.sce
new file mode 100644 (file)
index 0000000..08bba88
--- /dev/null
@@ -0,0 +1,30 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009 - Digiteo - Michael Baudin
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+
+mprintf("Running optimization...\n");
+
+function [ f , g , ind ] = rosenbrock ( x , ind )
+  f = 100*(x(2)-x(1)^2)^2+(1-x(1))^2;
+  g(1) = - 400. * ( x(2) - x(1)**2 ) * x(1) -2. * ( 1. - x(1) )
+  g(2) = 200. * ( x(2) - x(1)**2 )
+endfunction
+x0 = [-1.2 1]
+[f,x]=optim(rosenbrock,x0)
+//
+// Display results
+//
+mprintf("x=%s\n",strcat(string(x)," "));
+mprintf("f=%e\n",f);
+//
+// Load this script into the editor
+//
+filename = 'optim_rosenbrock.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
+
diff --git a/scilab/modules/optimization/demos/optim/optim_withderivative.sce b/scilab/modules/optimization/demos/optim/optim_withderivative.sce
new file mode 100644 (file)
index 0000000..c379c73
--- /dev/null
@@ -0,0 +1,36 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009 - Digiteo - Michael Baudin
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+
+mprintf("Running optimization...\n");
+
+function f = rosenbrock ( x )
+  f = 100.0 *(x(2)-x(1)^2)^2 + (1-x(1))^2;
+endfunction
+function [ f , g , ind ] = rosenbrockCost2 ( x , ind )
+  if ((ind == 1) | (ind == 4)) then
+    f = rosenbrock ( x );
+  end
+  if ((ind == 1) | (ind == 4)) then
+    g= derivative ( rosenbrock , x.' , order = 4 );
+  end
+endfunction
+x0 = [-1.2 1.0];
+[ f , x ] = optim ( rosenbrockCost2 , x0 )
+//
+// Display results
+//
+mprintf("x=%s\n",strcat(string(x)," "));
+mprintf("f=%e\n",f);
+//
+// Load this script into the editor
+//
+filename = 'optim_withderivative.sce';
+dname = get_absolute_file_path(filename);
+editor ( dname + filename );
+
index 648e859..7746bda 100644 (file)
@@ -8,16 +8,27 @@
 demopath = get_absolute_file_path("optimization.dem.gateway.sce");
 subdemolist = [
 "Non linear data fitting"         "datafit/datafit.dem.sce"       
-"fminsearch"                      "neldermead/fminsearch.sce"       
+"optim/Rosenbrock"                "optim/optim_rosenbrock.sce"       
+"optim/Output"                    "optim/optim_output.sce"       
+"optim/Plot"                      "optim/optim_plot.sce"       
+"optim/Derivative"                "optim/optim_withderivative.sce"       
+"fminsearch/Rosenbrock"           "neldermead/fminsearch_rosenbrock.sce.sce"       
+"fminsearch/tolx"                 "neldermead/fminsearch_tolx.sce"   
+"fminsearch/Output Function"      "neldermead/fminsearch_outputfunction.sce"   
+"fminsearch/Option Display"       "neldermead/fminsearch_display.sce"   
+"fminsearch/Plot Func Count"      "neldermead/fminsearch_optimplotfunccount.sce"   
+"fminsearch/Plot F"               "neldermead/fminsearch_optimplotfval.sce"   
+"fminsearch/Plot X"               "neldermead/fminsearch_optimplotx.sce"   
 "neldermead/Rosenbrock Variable"  "neldermead/neldermead_rosenbrock.sce"       
 "neldermead/Output Command"       "neldermead/neldermead_outputcmd.sce"       
 "neldermead/Box A"                "neldermead/neldermead_boxproblemA.sce"       
+"neldermead/Box B"                "neldermead/neldermead_boxproblemB.sce"       
 "nmplot/Han #1"             "neldermead/nmplot_han1.sce"       
 "nmplot/Han #2"             "neldermead/nmplot_han2.sce"       
 "nmplot/McKinnon #1"        "neldermead/nmplot_mckinnon.sce"       
 "nmplot/McKinnon #2"        "neldermead/nmplot_mckinnon2.sce"       
-"nmplot/Quadratic Fixed #1"   "neldermead/nmplot_quadratic.fixed.sce"       
-"nmplot/Quadratic Fixed #2"   "neldermead/nmplot_quadratic.fixed2.sce"       
+"nmplot/Quadratic Fixed #1" "neldermead/nmplot_quadratic.fixed.sce"       
+"nmplot/Quadratic Fixed #2" "neldermead/nmplot_quadratic.fixed2.sce"       
 "nmplot/Rosenbrock Fixed"   "neldermead/nmplot_rosenbrock.fixed.sce"       
 "nmplot/Rosenbrock Variable"         "neldermead/nmplot_rosenbrock.sce"       
 ];