Deleted vectorized computation feature. Deleted neldermead_contour. Fixed the demos.
[scilab.git] / scilab / modules / optimization / demos / neldermead / neldermead_boxproblemA.sce
@@ -13,6 +13,9 @@
 //   numerical experiment presented in Box's paper.
 //
 
+mprintf("Illustrates Box''' algorithm on Box problem A.\n");
+mprintf("Defining Box Problem A function...\n");
+
 //
 // boxproblemA --
 //   Computes the Box problem A cost function and 
@@ -34,7 +37,7 @@
 //    constraints
 //  The inequality constraints are expected to be positive.
 //
-function f = boxproblemA ( x , index , data )
+function result = boxproblemA ( x , index , data )
   if (~isdef('index','local')) then
     index = 1
   end
@@ -56,7 +59,7 @@ function f = boxproblemA ( x , index , data )
   if ( index==1 | index==3 ) then
     f = (data.a2 * y1 + data.a3 * y2 + data.a4 * y3 + data.a5 * y4 ...
              + 7840 * data.a6 - 100000 * data.a0 ...
-             - 50800 * $b * data.a7 + data.k31 + data.k32 * x(2) + data.k33 * x(3) ...
+             - 50800 * b * data.a7 + data.k31 + data.k32 * x(2) + data.k33 * x(3) ...
              + data.k34 * x(4) + data.k35 * x(5)) * x(1) ...
              - 24345 + data.a1 * x6
     f = -f
@@ -82,77 +85,101 @@ function f = boxproblemA ( x , index , data )
   end
 endfunction
 
-boxparams = struct()
-boxparams.a0 = 9
-boxparams.a1 = 15
-boxparams.a2 = 50
-boxparams.a3 = 9.583
-boxparams.a4 = 20
-boxparams.a5 = 15
-boxparams.a6 = 6
-boxparams.a7 = 0.75
-boxparams.k1 = -145421.402
-boxparams.k2 = 2931.1506
-boxparams.k3 = -40.427932
-boxparams.k4 = 5106.192
-boxparams.k5 = 15711.36
-boxparams.k6 = -161622.577
-boxparams.k7 = 4176.15328
-boxparams.k8 = 2.8260078
-boxparams.k9 = 9200.476
-boxparams.k10 = 13160.295
-boxparams.k11 = -21686.9194
-boxparams.k12 = 123.56928
-boxparams.k13 = -21.1188894
-boxparams.k14 = 706.834
-boxparams.k15 = 2898.573
-boxparams.k16 = 28298.388
-boxparams.k17 = 60.81096
-boxparams.k18 = 31.242116
-boxparams.k19 = 329.574
-boxparams.k20 = -2882.082
-boxparams.k21 = 74095.3845
-boxparams.k22 = -306.262544
-boxparams.k23 = 16.243649
-boxparams.k24 = -3094.252
-boxparams.k25 = -5566.2628
-boxparams.k26 = -26237.0
-boxparams.k27 = 99.0
-boxparams.k28 = -0.42
-boxparams.k29 = 1300.0
-boxparams.k30 = 2100.0
-boxparams.k31 = 925548.252
-boxparams.k32 = -61968.8432
-boxparams.k33 = 23.3088196
-boxparams.k34 = -27097.648
-boxparams.k35 = -50843.766
-
+boxparams = struct();
+boxparams.a0 = 9;
+boxparams.a1 = 15;
+boxparams.a2 = 50;
+boxparams.a3 = 9.583;
+boxparams.a4 = 20;
+boxparams.a5 = 15;
+boxparams.a6 = 6;
+boxparams.a7 = 0.75;
+boxparams.k1 = -145421.402;
+boxparams.k2 = 2931.1506;
+boxparams.k3 = -40.427932;
+boxparams.k4 = 5106.192;
+boxparams.k5 = 15711.36;
+boxparams.k6 = -161622.577;
+boxparams.k7 = 4176.15328;
+boxparams.k8 = 2.8260078;
+boxparams.k9 = 9200.476;
+boxparams.k10 = 13160.295;
+boxparams.k11 = -21686.9194;
+boxparams.k12 = 123.56928;
+boxparams.k13 = -21.1188894;
+boxparams.k14 = 706.834;
+boxparams.k15 = 2898.573;
+boxparams.k16 = 28298.388;
+boxparams.k17 = 60.81096;
+boxparams.k18 = 31.242116;
+boxparams.k19 = 329.574;
+boxparams.k20 = -2882.082;
+boxparams.k21 = 74095.3845;
+boxparams.k22 = -306.262544;
+boxparams.k23 = 16.243649;
+boxparams.k24 = -3094.252;
+boxparams.k25 = -5566.2628;
+boxparams.k26 = -26237.0;
+boxparams.k27 = 99.0;
+boxparams.k28 = -0.42;
+boxparams.k29 = 1300.0;
+boxparams.k30 = 2100.0;
+boxparams.k31 = 925548.252;
+boxparams.k32 = -61968.8432;
+boxparams.k33 = 23.3088196;
+boxparams.k34 = -27097.648;
+boxparams.k35 = -50843.766;
 
+    //
+    // Initialize the random number generator, so that the results are always the
+    // same.
+    //
+    rand("seed" , 0)
 
+x0 = [2.52 2.0 37.5 9.25 6.8].';
+// Compute f(x0) : should be close to -2351244.0
+fx0 = boxproblemA ( x0 , data = boxparams );
+xopt = [4.53743 2.4 60.0 9.3 7.0].';
+// Compute f(xopt) : should be -5280334.0
+fopt = boxproblemA ( xopt , data = boxparams );
 
-nm = nmplot_new ();
-nm = nmplot_configure(nm,"-numberofvariables",2);
-nm = nmplot_configure(nm,"-function",boxproblemA);
-nm = nmplot_configure(nm,"-costfargument",boxparams);
-nm = nmplot_configure(nm,"-x0",[1.0 1.0]');
-nm = nmplot_configure(nm,"-maxiter",200);
-nm = nmplot_configure(nm,"-maxfunevals",300);
-nm = nmplot_configure(nm,"-simplex0method","given");
-nm = nmplot_configure(nm,"-tolsimplexizerelative",1.e-3);
-nm = nmplot_configure(nm,"-coords0",coords0);
-nm = nmplot_configure(nm,"-simplex0length",1.0);
-nm = nmplot_configure(nm,"-method","box");
-//nm = nmplot_configure(nm,"-verbose",1);
-nm = nmplot_configure(nm,"-verbosetermination",1);
+nm = neldermead_new ();
+nm = neldermead_configure(nm,"-numberofvariables",5);
+nm = neldermead_configure(nm,"-function",boxproblemA);
+nm = neldermead_configure(nm,"-costfargument",boxparams);
+nm = neldermead_configure(nm,"-x0",x0);
+nm = neldermead_configure(nm,"-maxiter",300);
+nm = neldermead_configure(nm,"-maxfunevals",1000);
+nm = neldermead_configure(nm,"-tolsimplexizerelative",1.e-4);
+nm = neldermead_configure(nm,"-method","box");
+//nm = neldermead_configure(nm,"-verbose",1);
+nm = neldermead_configure(nm,"-verbosetermination",1);
 // Configure like Box
 nm = neldermead_configure(nm,"-boundsmin",[0.0 1.2 20.0 9.0 6.0]);
 nm = neldermead_configure(nm,"-boundsmax",[5.0 2.5 60.0 9.3 7.0]);
 nm = neldermead_configure(nm,"-simplex0method","randbounds");
 nm = neldermead_configure(nm,"-nbineqconst",6);
 //
+// 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 = nmplot_search(nm);
-nm = nmplot_destroy(nm);
+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);