diff options
author | Shashank | 2017-05-29 12:40:26 +0530 |
---|---|---|
committer | Shashank | 2017-05-29 12:40:26 +0530 |
commit | 0345245e860375a32c9a437c4a9d9cae807134e9 (patch) | |
tree | ad51ecbfa7bcd3cc5f09834f1bb8c08feaa526a4 /modules/optimization/demos/neldermead | |
download | scilab_for_xcos_on_cloud-0345245e860375a32c9a437c4a9d9cae807134e9.tar.gz scilab_for_xcos_on_cloud-0345245e860375a32c9a437c4a9d9cae807134e9.tar.bz2 scilab_for_xcos_on_cloud-0345245e860375a32c9a437c4a9d9cae807134e9.zip |
CMSCOPE changed
Diffstat (limited to 'modules/optimization/demos/neldermead')
28 files changed, 2757 insertions, 0 deletions
diff --git a/modules/optimization/demos/neldermead/fminsearch_display.sce b/modules/optimization/demos/neldermead/fminsearch_display.sce new file mode 100755 index 000000000..0f462a2ff --- /dev/null +++ b/modules/optimization/demos/neldermead/fminsearch_display.sce @@ -0,0 +1,50 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009 - DIGITEO - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +function demo_fmin_display() + + 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); + + // + // Load this script into the editor + // + m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal") + if(m == 1) + filename = "fminsearch_display.sce"; + dname = get_absolute_file_path(filename); + editor ( dname + filename, "readonly" ); + end +endfunction + +demo_fmin_display(); +clear demo_fmin_display; + + + + + + + + + diff --git a/modules/optimization/demos/neldermead/fminsearch_optimplotfunccount.sce b/modules/optimization/demos/neldermead/fminsearch_optimplotfunccount.sce new file mode 100755 index 000000000..400df74a7 --- /dev/null +++ b/modules/optimization/demos/neldermead/fminsearch_optimplotfunccount.sce @@ -0,0 +1,39 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009 - DIGITEO - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +function demo_funccount() + + 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 ); + demo_viewCode("fminsearch_optimplotfunccount.sce"); + + // + // Display results + // + mprintf("x = %s\n", strcat(string(x)," ")); + mprintf("fval = %e\n", fval); + +endfunction + +demo_funccount(); +clear demo_funccount; + + + + + + diff --git a/modules/optimization/demos/neldermead/fminsearch_optimplotfval.sce b/modules/optimization/demos/neldermead/fminsearch_optimplotfval.sce new file mode 100755 index 000000000..034d8befa --- /dev/null +++ b/modules/optimization/demos/neldermead/fminsearch_optimplotfval.sce @@ -0,0 +1,38 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009 - DIGITEO - Michael Baudin +// Copyright (C) 2009 - DIGITEO - Allan CORNET +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + + +function demo_plotfval() + + 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 ); + demo_viewCode("fminsearch_optimplotfval.sce"); + // + // Display results + // + mprintf("x = %s\n",strcat(string(x)," ")); + mprintf("fval = %e\n",fval); +endfunction + +demo_plotfval(); +clear demo_plotfval; + + + + + + diff --git a/modules/optimization/demos/neldermead/fminsearch_optimplotx.sce b/modules/optimization/demos/neldermead/fminsearch_optimplotx.sce new file mode 100755 index 000000000..116ba56dc --- /dev/null +++ b/modules/optimization/demos/neldermead/fminsearch_optimplotx.sce @@ -0,0 +1,37 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009 - DIGITEO - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2012- Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + + +function demo_plotx() + + 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 ); + demo_viewCode("fminsearch_optimplotx.sce"); + + // + // Display results + // + mprintf("x=%s\n",strcat(string(x)," ")); + mprintf("fval=%e\n",fval); +endfunction + +demo_plotx(); +clear demo_plotx; + + + + diff --git a/modules/optimization/demos/neldermead/fminsearch_outputfunction.sce b/modules/optimization/demos/neldermead/fminsearch_outputfunction.sce new file mode 100755 index 000000000..2857d9b9c --- /dev/null +++ b/modules/optimization/demos/neldermead/fminsearch_outputfunction.sce @@ -0,0 +1,69 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009 - 2011 - DIGITEO - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +function demo_outputfunction() + + mprintf(_("Running optimization ...\n")); + + function y = banana (x) + y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2; + endfunction + + function stop = 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 ) + stop = %f + endfunction + + opt = optimset ( "OutputFcn" , outfun ); + [x,fval,exitflag,output ] = fminsearch ( banana , [-1.2 1] , opt ); + + // + // Display results + // + mprintf("x = %s\n",strcat(string(x)," ")); + mprintf("fval = %e\n",fval); + + 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 + // + m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal") + if(m == 1) + filename = "fminsearch_outputfunction.sce"; + dname = get_absolute_file_path(filename); + editor ( dname + filename, "readonly" ); + end +endfunction + +demo_outputfunction(); +clear demo_outputfunction; + + + + + + + + diff --git a/modules/optimization/demos/neldermead/fminsearch_rosenbrock.sce b/modules/optimization/demos/neldermead/fminsearch_rosenbrock.sce new file mode 100755 index 000000000..979ef72c4 --- /dev/null +++ b/modules/optimization/demos/neldermead/fminsearch_rosenbrock.sce @@ -0,0 +1,59 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009 - DIGITEO - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +function demo_fmin_rosenbrock() + 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]; + [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); + 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 + // + m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal") + if(m == 1) + filename = "fminsearch_rosenbrock.sce"; + dname = get_absolute_file_path(filename); + editor ( dname + filename, "readonly" ); + end +endfunction + +demo_fmin_rosenbrock(); +clear demo_fmin_rosenbrock; + + + + + + + + diff --git a/modules/optimization/demos/neldermead/fminsearch_tolx.sce b/modules/optimization/demos/neldermead/fminsearch_tolx.sce new file mode 100755 index 000000000..3b8e778e6 --- /dev/null +++ b/modules/optimization/demos/neldermead/fminsearch_tolx.sce @@ -0,0 +1,62 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009 - DIGITEO - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + + +function demo_fmin_tolx() + + 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 + // + m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal") + if(m == 1) + filename = "fminsearch_tolx.sce"; + dname = get_absolute_file_path(filename); + editor ( dname + filename, "readonly" ); + end +endfunction + +demo_fmin_tolx(); +clear demo_fmin_tolx; + + + + + + + + diff --git a/modules/optimization/demos/neldermead/neldermead.dem.gateway.sce b/modules/optimization/demos/neldermead/neldermead.dem.gateway.sce new file mode 100755 index 000000000..2229c851a --- /dev/null +++ b/modules/optimization/demos/neldermead/neldermead.dem.gateway.sce @@ -0,0 +1,36 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008 - INRIA +// Copyright (C) 2010 - DIGITEO - Yann COLLETTE +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// +// This file is released under the 3-clause BSD license. See COPYING-BSD. + +subdemolist = [_("fminsearch - Rosenbrock"), "fminsearch_rosenbrock.sce"; ... +_("fminsearch - tolx"), "fminsearch_tolx.sce"; ... +_("fminsearch - Output Function"), "fminsearch_outputfunction.sce"; ... +_("fminsearch - Option Display"), "fminsearch_display.sce"; ... +_("fminsearch - Plot Func Count"), "fminsearch_optimplotfunccount.sce"; ... +_("fminsearch - Plot F"), "fminsearch_optimplotfval.sce"; ... +_("fminsearch - Plot X"), "fminsearch_optimplotx.sce"; ... +_("neldermead - Rosenbrock Variable"), "neldermead_rosenbrock.sce"; ... +_("neldermead - Output Command"), "neldermead_outputcmd.sce"; ... +_("neldermead - Dimensionality"), "neldermead_dimension.sce"; ... +_("nmplot - Han #1"), "nmplot_han1.sce"; ... +_("nmplot - Han #2"), "nmplot_han2.sce"; ... +_("nmplot - McKinnon #1"), "nmplot_mckinnon.sce"; ... +_("nmplot - McKinnon #2"), "nmplot_mckinnon2.sce"; ... +_("nmplot - Quadratic Fixed #1"), "nmplot_quadratic.fixed.sce"; ... +_("nmplot - Quadratic Variable#1"), "nmplot_quadratic.variable.sce"; ... +_("nmplot - Quadratic Fixed #2"), "nmplot_quadratic.fixed2.sce"; ... +_("nmplot - Quadratic Variable#2"), "nmplot_quadratic.variable2.sce"; ... +_("nmplot - Rosenbrock Fixed"), "nmplot_rosenbrock.fixed.sce"; ... +_("nmplot - Rosenbrock Variable"), "nmplot_rosenbrock.sce"; ... +_("neldermead - Box A"), "neldermead_boxproblemA.sce"; ... +_("neldermead - Box B"), "neldermead_boxproblemB.sce"; ... +_("neldermead - Box Bounds"), "neldermead_boxbounds.sce"; ... +_("neldermead - Box Post"), "neldermead_boxpost.sce"; ... +_("neldermead - Polygon"), "polygon.sce"; ... +]; + +subdemolist(:,2) = SCI + "/modules/optimization/demos/neldermead/" + subdemolist(:,2); + diff --git a/modules/optimization/demos/neldermead/neldermead_boxbounds.sce b/modules/optimization/demos/neldermead/neldermead_boxbounds.sce new file mode 100755 index 000000000..c7940f1d5 --- /dev/null +++ b/modules/optimization/demos/neldermead/neldermead_boxbounds.sce @@ -0,0 +1,118 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2010 - DIGITEO - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2011 - DIGITEO - Michael Baudin +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +// +// neldermeadBounds.sce -- +// Show a simple neldermead session with bounds. +// + +function demo_boxbounds() + + filename = "neldermead_boxbounds.sce"; + dname = get_absolute_file_path(filename); + + mprintf(_("Illustrates Box'' algorithm on a simply bounds-constrained optimization problem.\n")); + + // A simple quadratic function + function [ f , index ] = myquad ( x , index ) + f = x(1)^2 + x(2)^2 + endfunction + function y = myquadC ( x1 , x2 ) + y = myquad ( [x1 , x2] , 2 ) + endfunction + + // + // Initialize the random number generator, so that the results are always the + // same. + // + rand("seed" , 0) + + x0 = [1.2 1.9].'; + // Compute f(x0) : should be close to -2351244.0 + [ fx0 , index ] = myquad ( x0 , 2 ); + mprintf("Computed fx0 = %e (expected = %e)\n",fx0 , 5.05 ); + + xopt = [1 1].'; + // Compute f(xopt) : should be 2 + [ fopt , index ] = myquad ( xopt , 2 ); + mprintf("Computed fopt = %e (expected = %e)\n", fopt , 2 ); + + nm = nmplot_new (); + nm = nmplot_configure(nm,"-numberofvariables",2); + nm = nmplot_configure(nm,"-function",myquad); + nm = nmplot_configure(nm,"-x0",x0); + nm = nmplot_configure(nm,"-method","box"); + nm = nmplot_configure(nm,"-boundsmin",[1 1]); + nm = nmplot_configure(nm,"-boundsmax",[2 2]); + + // + // Check that the cost function is correctly connected. + // + [ nm , f ] = nmplot_function ( nm , x0 ); + simplexfn = fullfile(TMPDIR , "history.simplex.txt") + nm = nmplot_configure(nm,"-simplexfn",simplexfn); + + // + // Perform optimization + // + mprintf(_("Searching (please wait) ...\n")); + nm = nmplot_search(nm); + mprintf(_("...Done\n")); + // + // Print a summary + // + exec(fullfile(dname,"nmplot_summary.sci"),-1); + nmplot_summary(nm) + mprintf("==========================\n"); + xcomp = nmplot_get(nm,"-xopt"); + mprintf("x expected = %s\n",strcat(string(xopt), " ")); + shift = norm(xcomp-xopt)/norm(xopt); + mprintf("Shift = %f\n",shift); + fcomp = nmplot_get(nm,"-fopt"); + mprintf("f expected = %f\n",fopt); + shift = abs(fcomp-fopt)/abs(fopt); + mprintf("Shift =%f\n",shift); + + // + // Plot + // + mprintf(_("Plot contour (please wait)...\n")); + xmin = 0.5 ; + xmax = 2.1 ; + ymin = 0.5 ; + ymax = 2.1 ; + nx = 50 ; + ny = 50; + xdata=linspace(xmin,xmax,nx); + ydata=linspace(ymin,ymax,ny); + scf(); + xset("fpf"," ") + drawlater(); + contour ( xdata , ydata , myquadC , 10 ) + nmplot_simplexhistory ( nm ); + drawnow(); + demo_viewCode(filename); + // + // Cleanup + deletefile(simplexfn) + nm = nmplot_destroy(nm); + mprintf(_("End of demo.\n")); +endfunction + +demo_boxbounds(); +clear demo_boxbounds; + + + + + + diff --git a/modules/optimization/demos/neldermead/neldermead_boxpost.sce b/modules/optimization/demos/neldermead/neldermead_boxpost.sce new file mode 100755 index 000000000..f09a4e545 --- /dev/null +++ b/modules/optimization/demos/neldermead/neldermead_boxpost.sce @@ -0,0 +1,165 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-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 +// + +function demo_boxpost() + + filename = "neldermead_boxpost.sce"; + dname = get_absolute_file_path(filename); + + mprintf(_("Illustrates Box'' algorithm on Rosenbrock''s Post Office Problem.\n")); + mprintf(_("Defining Rosenbrock Post Office function...\n")); + + // + // Reference: + // + // Algorithm 454 + // The complex method for constrained + // optimization + // Richardson, Kuester + // 1971 + // + // An automatic method for finding the + // greatest or least value of a function + // Rosenbrock + // 1960 + // + // Richardson and Kuester Results : + // F=3456 + // X1 = 24.01 + // X2 = 12.00 + // X3 = 12.00 + // Iterations : 72 + // + // + + // + // fpostoffice -- + // Computes the Post Office cost function and + // inequality constraints. + // + // Arguments + // x: the point where to compute the function + // index : the stuff to compute + // + + function [ f , c , index ] = fpostoffice ( x , index ) + f = [] + c = [] + if ( index==2 | index==6 ) then + f = -x(1) * x(2) * x(3) + end + + if ( index==5 | index==6 ) then + c1 = x(1) + 2 * x(2) + 2 * x(3) + c2 = 72 - c1 + c = [c1 c2] + end + endfunction + // + // Initialize the random number generator, so that the results are always the + // same. + // + rand("seed" , 0) + + x0 = [1.0 1.0 1.0].'; + // Compute f(x0) : should be close to -1 + fx0 = fpostoffice ( x0 , 2 ); + mprintf("Computed fx0 = %e (expected = %e)\n",fx0 , -1 ); + [ fx0 , cx0, index ] = fpostoffice ( x0 , 6 ); + mprintf("Computed Constraints(x0) = [%e %e]\n", .. + cx0(1), cx0(2) ); + mprintf("Expected Constraints(x0) = [%e %e]\n", .. + 5 , 67 ); + + xopt = [24 12 12].'; + // Compute f(xopt) : should be 3456 + fopt = fpostoffice ( xopt ); + mprintf("Computed fopt = %e (expected = %e)\n", fopt , -3456 ); + + nm = neldermead_new (); + nm = neldermead_configure(nm,"-numberofvariables",3); + nm = neldermead_configure(nm,"-function",fpostoffice); + nm = neldermead_configure(nm,"-x0",x0); + nm = neldermead_configure(nm,"-maxiter",300); + nm = neldermead_configure(nm,"-maxfunevals",500); + nm = neldermead_configure(nm,"-method","box"); + nm = neldermead_configure(nm,"-verbose",1); + logfile = TMPDIR + "/postoffice.txt"; + nm = neldermead_configure(nm,"-logfile" , logfile ); + nm = neldermead_configure(nm,"-verbosetermination",1); + nm = neldermead_configure(nm,"-boundsmin",[0.0 0.0 0.0]); + nm = neldermead_configure(nm,"-boundsmax",[42.0 42.0 42.0]); + // Configure like Box + nm = neldermead_configure(nm,"-simplex0method","randbounds"); + nm = neldermead_configure(nm,"-nbineqconst",2); + 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. + // + [ nm , result ] = neldermead_function ( nm , x0 ); + + // + // Perform optimization + // + mprintf(_("Searching (please wait) ...\n")); + nm = neldermead_search(nm); + // + // Print a summary + // + exec(fullfile(dname,"neldermead_summary.sci"),-1); + neldermead_summary(nm) + mprintf("==========================\n"); + xcomp = neldermead_get(nm,"-xopt"); + 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 expected = %f\n",fopt); + shift = abs(fcomp-fopt)/abs(fopt); + mprintf("Shift = %f\n",shift); + nm = neldermead_destroy(nm); + deletefile ( logfile ) + mprintf(_("End of demo.\n")); + + // + // Load this script into the editor + // + m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal") + if(m == 1) + editor ( dname + filename, "readonly" ); + end +endfunction + +demo_boxpost(); +clear demo_boxpost; + + + + + + + + + + diff --git a/modules/optimization/demos/neldermead/neldermead_boxproblemA.sce b/modules/optimization/demos/neldermead/neldermead_boxproblemA.sce new file mode 100755 index 000000000..a80384e13 --- /dev/null +++ b/modules/optimization/demos/neldermead/neldermead_boxproblemA.sce @@ -0,0 +1,218 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2009-2010 - DIGITEO - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +// +// nmplot_boxproblemA.sce -- +// Show that the Box algorithm is able to reproduce the +// numerical experiment presented in Box's paper. +// + +function demo_boxproblemA() + + filename = "neldermead_boxproblemA.sce"; + dname = get_absolute_file_path(filename); + + mprintf(_("Illustrates Box'' algorithm on Box problem A.\n")); + + mprintf("M.J. Box, \n"); + mprintf(_("""A new method of constrained optimization \n")); + mprintf(_("and a comparison with other methods"".\n")); + mprintf("The Computer Journal, Volume 8, Number 1, 1965, 42--52\n"); + mprintf(_("Problem A\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 + // + + // + // boxproblemA -- + // Computes the Box problem A 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 + // + function [ f , c , index ] = boxproblemA ( x , index , data ) + f = [] + c = [] + b = x(2) + 0.01 * x(3) + x6 = (data.k1 + data.k2 * x(2) ... + + data.k3 * x(3) + data.k4 * x(4) + data.k5 * x(5)) * x(1) + y1 = data.k6 + data.k7 * x(2) + data.k8 * x(3) ... + + data.k9 * x(4) + data.k10 * x(5) + y2 = data.k11 + data.k12 * x(2) + data.k13 * x(3) ... + + data.k14 * x(4) + data.k15 * x(5) + y3 = data.k16 + data.k17 * x(2) + data.k18 * x(3) ... + + data.k19 * x(4) + data.k20 * x(5) + y4 = data.k21 + data.k22 * x(2) + data.k23 * x(3) ... + + data.k24 * x(4) + data.k25 * x(5) + x7 = ( y1 + y2 + y3 ) * x(1) + x8 = (data.k26 + data.k27 * x(2) + data.k28 * x(3) ... + + data.k29 * x(4) ... + + data.k30 * x(5) ) * x(1) + x6 + x7 + + if ( index == 2 | index == 6 ) 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) ... + + data.k34 * x(4) + data.k35 * x(5)) * x(1) ... + - 24345 + data.a1 * x6 + f = -f + end + + if ( index == 5 | index == 6 ) then + c1 = x6 + c2 = 294000 - x6 + c3 = x7 + c4 = 294000 - x7 + c5 = x8 + c6 = 277200 - x8 + c = [c1 c2 c3 c4 c5 c6] + 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; + + // + // 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 , c , index ] = boxproblemA ( x0 , 2 , boxparams ); + mprintf("Computed fx0 = %e (expected = %e)\n",fx0 , -2351244. ); + + xopt = [4.53743 2.4 60.0 9.3 7.0].'; + // Compute f(xopt) : should be -5280334.0 + [ fopt , c , index ] = boxproblemA ( xopt , 2 , boxparams ); + mprintf("Computed fopt = %e (expected = %e)\n", fopt , -5280334.0 ); + + nm = neldermead_new (); + nm = neldermead_configure(nm,"-numberofvariables",5); + nm = neldermead_configure(nm,"-function",list(boxproblemA,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"); + + // 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. + // + [ nm , f ] = neldermead_function ( nm , x0 ); + + // + // Perform optimization + // + mprintf(_("Searching (please wait)...\n")); + nm = neldermead_search(nm, "off"); + mprintf(_("...Done\n")); + // + // Print a summary + // + exec(fullfile(dname,"neldermead_summary.sci"),-1); + neldermead_summary(nm) + mprintf("==========================\n"); + xcomp = neldermead_get(nm,"-xopt"); + mprintf("x expected = [%s]\n",strcat(string(xopt)," ")); + shift = norm(xcomp-xopt)/norm(xopt); + mprintf(_("Relative error on x = %e\n"),shift); + fcomp = neldermead_get(nm,"-fopt"); + mprintf("f expected = %f\n",fopt); + shift = abs(fcomp-fopt)/abs(fopt); + mprintf("Relative error on f = %e\n",shift); + nm = neldermead_destroy(nm); + mprintf(_("End of demo.\n")); + + // + // Load this script into the editor + // + m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal") + if(m == 1) + editor ( dname + filename, "readonly" ); + end +endfunction + +demo_boxproblemA(); +clear demo_boxproblemA; + + + + + + + + diff --git a/modules/optimization/demos/neldermead/neldermead_boxproblemB.sce b/modules/optimization/demos/neldermead/neldermead_boxproblemB.sce new file mode 100755 index 000000000..3785fd7d6 --- /dev/null +++ b/modules/optimization/demos/neldermead/neldermead_boxproblemB.sce @@ -0,0 +1,173 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2009-2010 - DIGITEO - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +// +// nmplot_boxproblemA.sce -- +// Show that the Box algorithm is able to reproduce the +// numerical experiment presented in Box's paper. +// + +function demo_boxproblemB() + + filename = "neldermead_boxproblemB.sce"; + dname = get_absolute_file_path(filename); + + mprintf(_("Illustrates Box'' algorithm on Box problem B.\n")); + + mprintf("M.J. Box, \n"); + mprintf(_("""A new method of constrained optimization \n")); + mprintf(_("and a comparison with other methods"".\n")); + mprintf("The Computer Journal, Volume 8, Number 1, 1965, 42--52\n"); + mprintf(_("Problem B\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 + // + function [ f , c , index ] = boxproblemB ( x , index ) + f = [] + c = [] + x3 = x(1) + sqrt(3.0) * x(2) + + if ( index==2 | index==6 ) then + f = -(9.0 - (x(1) - 3.0) ^ 2) * x(2) ^ 3 / 27.0 / sqrt(3.0) + end + + if ( index==5 | index==6 ) 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 = boxproblemB ( x0 , 2 ); + mprintf("Computed fx0 = %e (expected = %e)\n",fx0 , -0.0133645895646 ); + [fx0 , cx0 , index ] = boxproblemB ( x0 , 6 ); + mprintf("Computed Constraints(x0) = [%e %e %e]\n", ... + cx0(1), cx0(2), cx0(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 , 2 ); + 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,"-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. + // + [ nm , f ] = neldermead_function ( nm , x0 ); + + // + // Perform optimization + // + mprintf(_("Searching (please wait) ...\n")); + nm = neldermead_search(nm); + // + // Print a summary + // + exec(fullfile(dname,"neldermead_summary.sci"),-1); + neldermead_summary(nm) + mprintf("==========================\n"); + xcomp = neldermead_get(nm,"-xopt"); + mprintf("x expected = [%s]\n", strcat(string(xopt), " ")); + shift = norm(xcomp-xopt)/norm(xopt); + mprintf(_("Relative error on x = %e\n"), shift); + fcomp = neldermead_get(nm,"-fopt"); + mprintf(_("f expected = %f\n"), fopt); + shift = abs(fcomp-fopt)/abs(fopt); + mprintf(_("Relative error on f = %e\n"), shift); + nm = neldermead_destroy(nm); + mprintf(_("End of demo.\n")); + + // + // Load this script into the editor + // + m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal") + if(m == 1) + editor ( dname + filename, "readonly" ); + end +endfunction + +demo_boxproblemB(); +clear demo_boxproblemB; + + + + + + + + diff --git a/modules/optimization/demos/neldermead/neldermead_dimension.sce b/modules/optimization/demos/neldermead/neldermead_dimension.sce new file mode 100755 index 000000000..18da8011f --- /dev/null +++ b/modules/optimization/demos/neldermead/neldermead_dimension.sce @@ -0,0 +1,113 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2009-2010 - DIGITEO - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + + +function demo_dimension() + + mprintf(_("Illustrates the sensitivity to dimension of the Nelder-Mead algorithm\n")); + mprintf(_("Han and Neumann. ""Effect of dimensionality on the nelder-mead simplex method."" Optimization Methods and Software, 2006.\n")); + + function [ f , index ] = quadracticn ( x , index ) + f = sum(x.^2); + endfunction + + // + // solvepb -- + // Find the solution for the given number of dimensions + // + function [nbfevals , niter , rho] = solvepb ( n ) + rand("seed", 0) + nm = neldermead_new (); + nm = neldermead_configure(nm, "-numberofvariables", n); + nm = neldermead_configure(nm, "-function", quadracticn); + nm = neldermead_configure(nm, "-x0", zeros(n,1)); + nm = neldermead_configure(nm, "-maxiter", 2000); + nm = neldermead_configure(nm, "-maxfunevals", 2000); + nm = neldermead_configure(nm, "-tolxmethod", %f); + nm = neldermead_configure(nm, "-tolsimplexizerelative", 0.0); + nm = neldermead_configure(nm, "-tolsimplexizeabsolute", 1.e-8); + nm = neldermead_configure(nm, "-simplex0method", "given"); + coords (1,1:n) = zeros(1,n); + for i = 2:n+1 + coords (i,1:n) = 2.0 * rand(1,n) - 1.0; + end + nm = neldermead_configure(nm, "-coords0", coords); + // + // Perform optimization + // + nm = neldermead_search(nm); + si0 = neldermead_get ( nm , "-simplex0" ); + sigma0 = optimsimplex_size ( si0 , "sigmaplus" ); + siopt = neldermead_get ( nm , "-simplexopt" ); + sigmaopt = optimsimplex_size ( siopt , "sigmaplus" ); + niter = neldermead_get ( nm , "-iterations" ); + rho = (sigmaopt/sigma0)^(1.0/niter); + nbfevals = neldermead_get ( nm , "-funevals" ); + mprintf ( "%d %d %d %f\n", n , nbfevals , niter , rho ); + nm = neldermead_destroy(nm); + endfunction + + nmax = 20; + mprintf(_("Maximum dimension:%d\n"),nmax); + mprintf(_("Column #1: number of dimensions\n")); + mprintf(_("Column #2: number of function evaluations\n")); + mprintf(_("Column #3: number of iterations\n")); + mprintf(_("Column #4: convergence rate (lower is better)\n")); + + for n = 1:nmax + [nbfevals niter rho] = solvepb ( n ); + array_rho(n) = rho; + array_nbfevals(n) = nbfevals; + array_niter(n) = niter; + end + + // Plot rate of convergence + hh = scf(); + plot(1:nmax,array_rho) + hh.children.x_label.text = _("Number of parameters") + hh.children.y_label.text = _("Rate of convergence") + hh.children.children.children.mark_mode = "on"; + hh.children.children.children.mark_style = 9; + hh.children.children.children.mark_size = 10; + + // Plot number of function evaluations + hh = scf(); + plot(1:nmax,array_nbfevals) + hh.children.x_label.text = _("Number of parameters") + hh.children.y_label.text = _("Number of function evaluations") + hh.children.children.children.mark_mode = "on"; + hh.children.children.children.mark_style = 9; + hh.children.children.children.mark_size = 10; + mprintf(_("End of demo.\n")); + + // + //Load this script into the editor + // + + m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal") + if(m == 1) + filename = "neldermead_dimension.sce"; + dname = get_absolute_file_path(filename); + editor ( dname + filename, "readonly" ); + end +endfunction + +demo_dimension(); +clear demo_dimension; + + + + + + + + diff --git a/modules/optimization/demos/neldermead/neldermead_outputcmd.sce b/modules/optimization/demos/neldermead/neldermead_outputcmd.sce new file mode 100755 index 000000000..6882b07d1 --- /dev/null +++ b/modules/optimization/demos/neldermead/neldermead_outputcmd.sce @@ -0,0 +1,90 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + + +function demo_outputcmd() + + function [ y , index ] = rosenbrock ( x , index ) + y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2; + endfunction + + + // + // myoutputcmd -- + // This command is called back by the Nelder-Mead + // algorithm. + // Arguments + // state : the current state of the algorithm + // "init", "iter", "done" + // data : the data at the current state + // This is a tlist with the following entries: + // * x : the optimal vector of parameters + // * fval : the minimum function value + // * simplex : the simplex, as a simplex object + // * iteration : the number of iterations performed + // * funccount : the number of function evaluations + // stop : set to %t to interrupt the algorithm + // + + function stop = myoutputcmd ( state , data ) + iter = data.iteration + if ( state == "init" ) then + mprintf ( "=================================\n"); + mprintf ( _("Initialization\n")); + elseif ( state == "done" ) then + mprintf ( "=================================\n"); + mprintf ( _("End of Optimization\n")); + end + fc = data.funccount + fval = data.fval + x = data.x + simplex = data.simplex + // Simplex is a data structure, which can be managed + // by the optimsimplex class. + ssize = optimsimplex_size ( simplex ) + mprintf ( "Iteration #%d, Feval #%d, Fval = %e, x = %s, Size = %e\n", iter, fc, fval, strcat(string(x)," "), ssize); + stop = %f + endfunction + + + nm = neldermead_new (); + nm = neldermead_configure(nm,"-numberofvariables",2); + nm = neldermead_configure(nm,"-function",rosenbrock); + nm = neldermead_configure(nm,"-x0",[-1.2 1.0]'); + nm = neldermead_configure(nm,"-maxiter",200); + nm = neldermead_configure(nm,"-maxfunevals",350); + nm = neldermead_configure(nm,"-tolfunrelative",10*%eps); + nm = neldermead_configure(nm,"-tolxrelative",10*%eps); + nm = neldermead_configure(nm,"-outputcommand",myoutputcmd); + nm = neldermead_search(nm); + nm = neldermead_destroy(nm); + mprintf(_("End of demo.\n")); + + // + // Load this script into the editor + // + m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal") + if(m == 1) + filename = "neldermead_outputcmd.sce"; + dname = get_absolute_file_path(filename); + editor ( dname + filename, "readonly" ); + end +endfunction + +demo_outputcmd() +clear demo_outputcmd; + + + + + + + diff --git a/modules/optimization/demos/neldermead/neldermead_rosenbrock.sce b/modules/optimization/demos/neldermead/neldermead_rosenbrock.sce new file mode 100755 index 000000000..05056d645 --- /dev/null +++ b/modules/optimization/demos/neldermead/neldermead_rosenbrock.sce @@ -0,0 +1,59 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +function demo_nelder_rosen() + + mprintf(_("Defining Rosenbrock function ...\n")); + + function [ y , index ] = rosenbrock ( x , index ) + y = 100*(x(2)-x(1)^2)^2+(1-x(1))^2; + endfunction + + x0 = [-1.2 1.0]'; + + nm = neldermead_new(); + nm = neldermead_configure(nm, "-numberofvariables", 2); + nm = neldermead_configure(nm, "-function", rosenbrock); + nm = neldermead_configure(nm, "-x0", x0); + nm = neldermead_configure(nm, "-maxiter", 300); + nm = neldermead_configure(nm, "-maxfunevals", 300); + + mprintf(_("Searching (please wait) ...\n")); + nm = neldermead_search(nm); + + fx0 = neldermead_get(nm, "-fx0"); + mprintf("f(x0) = %f, x0=[%s]\n" , fx0 , strcat(string(x0), " ")); + xopt = neldermead_get(nm, "-xopt"); + fopt = neldermead_get(nm, "-fopt"); + mprintf("f(xopt) = %f, xopt=[%s]\n" , fopt , strcat(string(xopt), " ")); + nm = neldermead_destroy(nm); + mprintf(_("End of demo.\n")); + + // + // Load this script into the editor + // + m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal") + if(m == 1) + filename = "neldermead_rosenbrock.sce"; + dname = get_absolute_file_path(filename); + editor ( dname + filename, "readonly" ); + end +endfunction + +demo_nelder_rosen(); +clear demo_nelder_rosen; + + + + + + + diff --git a/modules/optimization/demos/neldermead/neldermead_summary.sci b/modules/optimization/demos/neldermead/neldermead_summary.sci new file mode 100755 index 000000000..37854bd52 --- /dev/null +++ b/modules/optimization/demos/neldermead/neldermead_summary.sci @@ -0,0 +1,29 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2011 - 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.1-en.txt + +function neldermead_summary(nm) + function str = _strvec ( x ) + str = strcat(string(x)," ") + endfunction + + xopt = neldermead_get(nm,"-xopt") + fopt = neldermead_get(nm,"-fopt") + funevals = neldermead_get(nm,"-funevals") + iter = neldermead_get(nm,"-iterations") + status = neldermead_get(nm,"-status") + restartnb = neldermead_get(nm,"-restartnb") + mprintf("Xopt: [%s]\n",_strvec(xopt)); + mprintf("Fopt: %s\n",string(fopt)); + mprintf("Function evaluations: %d\n",funevals); + mprintf("Iterations: %d\n",iter); + mprintf("Status: %s\n",status); + mprintf("Restartnb: %d\n",restartnb) +endfunction diff --git a/modules/optimization/demos/neldermead/nmplot_han1.sce b/modules/optimization/demos/neldermead/nmplot_han1.sce new file mode 100755 index 000000000..2ebf7dc1c --- /dev/null +++ b/modules/optimization/demos/neldermead/nmplot_han1.sce @@ -0,0 +1,113 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2011 - DIGITEO - Michael Baudin +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +function demo_nmplot_1() + + filename = "nmplot_han1.sce"; + dname = get_absolute_file_path(filename); + + mprintf(_("Illustrates the 1st counter example given by Han et al.\n")); + + // + // han1 -- + // Compute the cost function from the Han Phd Thesis + // which exhibits a failure of the NM method. + // Reference + // Algorithms in Unconstrained Optimization + // Han, Lixing + // 2000 + // Ph.D., The University of Connecticut + // + mprintf(_("Defining Han function...\n")); + + function [ f , index ] = han1 ( x , index ) + f = x(1)^2 + x(2) * (x(2) + 2.0) * (x(2) - 0.5) * (x(2) - 2.0); + endfunction + function y = han1C ( x1 , x2 ) + y = han1 ( [x1 , x2] , 2 ) + endfunction + + + mprintf(_("Defining initial simplex coordinates...\n")); + coords0 = [ + 0. -1. + 0. 1. + 1. 0. + ]; + + + mprintf(_("Creating nmplot object...\n")); + nm = nmplot_new (); + mprintf(_("Configuring nmplot object...\n")); + nm = nmplot_configure(nm, "-numberofvariables", 2); + nm = nmplot_configure(nm, "-function", han1); + nm = nmplot_configure(nm, "-x0", [1.0 1.0]'); + nm = nmplot_configure(nm, "-maxiter", 50); + nm = nmplot_configure(nm, "-maxfunevals", 300); + nm = nmplot_configure(nm, "-tolfunrelative", 10*%eps); + nm = nmplot_configure(nm, "-tolxrelative", 10*%eps); + nm = nmplot_configure(nm, "-simplex0method", "given"); + nm = nmplot_configure(nm, "-coords0", coords0); + + // + // Setup output files + // + mprintf(_("Setup output files...\n")); + simplexfn = TMPDIR + filesep() + "history.simplex.txt"; + nm = nmplot_configure(nm, "-simplexfn", simplexfn); + + // + // Perform optimization + // + mprintf(_("Searching (please wait)...\n")); + nm = nmplot_search(nm); + // + // Print a summary + // + exec(fullfile(dname,"nmplot_summary.sci"),-1); + nmplot_summary(nm) + + // + // Plot the history of the simplex + // + mprintf(_("Plotting contour (please wait)...\n")); + xmin = -0.2 ; + xmax = 1.2 ; + ymin = -2.0 ; + ymax = 2.0 ; + nx = 50 ; + ny = 50; + xdata=linspace(xmin,xmax,nx); + ydata=linspace(ymin,ymax,ny); + scf(); + drawlater(); + contour ( xdata , ydata , han1C , [-5 -4 -2 -1 0 1 1.5] ) + nmplot_simplexhistory ( nm ); + drawnow(); + demo_viewCode(filename); + + // + // Clean-up + // + deletefile(simplexfn); + nm = nmplot_destroy(nm); + mprintf(_("End of demo.\n")); +endfunction + +demo_nmplot_1(); +clear demo_nmplot_1; + + + + + + diff --git a/modules/optimization/demos/neldermead/nmplot_han2.sce b/modules/optimization/demos/neldermead/nmplot_han2.sce new file mode 100755 index 000000000..795a30671 --- /dev/null +++ b/modules/optimization/demos/neldermead/nmplot_han2.sce @@ -0,0 +1,112 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2011 - DIGITEO - Michael Baudin +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + + +function demo_nmplot_2() + filename = "nmplot_han2.sce"; + dname = get_absolute_file_path(filename); + + mprintf(_("Illustrates the 2nd counter example given by Han et al.\n")); + + // + // han2 -- + // Compute the cost function from the Han Phd Thesis + // which exhibits a failure of the NM method. + // Reference + // Algorithms in Unconstrained Optimization + // Han, Lixing + // 2000 + // Ph.D., The University of Connecticut + // + function [ f , index ] = han2 ( x , index ) + if abs(x(2)) <= 1.0 then + rho = 0.0 + elseif x(2) > 1.0 then + rho = x(2) - 1 + else + rho = -x(2) - 1 + end + f = x(1)^2 + rho; + endfunction + function y = han2C ( x1 , x2 ) + y = han2 ( [x1 , x2] , 2 ) + endfunction + + + coords0 = [ + 0. 0.5 + 0. -0.5 + 1. 0.]; + + mprintf(_("Creating nmplot object ...\n")); + nm = nmplot_new (); + nm = nmplot_configure(nm, "-numberofvariables", 2); + nm = nmplot_configure(nm, "-function", han2); + nm = nmplot_configure(nm, "-x0", [1.0 1.0]'); + nm = nmplot_configure(nm, "-maxiter", 50); + nm = nmplot_configure(nm, "-maxfunevals", 300); + nm = nmplot_configure(nm, "-tolfunrelative", 10*%eps); + nm = nmplot_configure(nm, "-tolxrelative", 10*%eps); + nm = nmplot_configure(nm, "-simplex0method", "given"); + nm = nmplot_configure(nm, "-coords0", coords0); + + // + // Setup output files + // + simplexfn = TMPDIR + filesep() + "history.simplex.txt"; + nm = nmplot_configure(nm, "-simplexfn", simplexfn); + + // + // Perform optimization + // + mprintf(_("Searching (please wait) ...\n")); + nm = nmplot_search(nm); + // + // Print a summary + // + exec(fullfile(dname,"nmplot_summary.sci"),-1); + nmplot_summary(nm) + + // + // Plot + // + mprintf(_("Plotting contour (please wait) ...\n")); + xmin = -0.2 ; + xmax = 1.2 ; + ymin = -1.5 ; + ymax = 1.5 ; + nx = 50 ; + ny = 50; + scf(); + xset("fpf"," ") + drawlater(); + xdata=linspace(xmin,xmax,nx); + ydata=linspace(ymin,ymax,ny); + contour ( xdata , ydata , han2C , [0.1 0.2 0.5 1.0 1.5 1.9] ) + nmplot_simplexhistory ( nm ); + drawnow(); + demo_viewCode(filename); + // + // Cleanup + deletefile(simplexfn); + nm = nmplot_destroy(nm); + mprintf(_("End of demo.\n")); +endfunction + +demo_nmplot_2() +clear demo_nmplot_2; + + + + + + diff --git a/modules/optimization/demos/neldermead/nmplot_mckinnon.sce b/modules/optimization/demos/neldermead/nmplot_mckinnon.sce new file mode 100755 index 000000000..9cc8969d4 --- /dev/null +++ b/modules/optimization/demos/neldermead/nmplot_mckinnon.sce @@ -0,0 +1,195 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2011 - DIGITEO - Michael Baudin +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +function demo_mckinnon2() + filename = "nmplot_mckinnon.sce"; + dname = get_absolute_file_path(filename); + + mprintf(_("Defining McKinnon function...\n")); + //% MCKINNON computes the McKinnon function. + // + // Discussion: + // + // This function has a global minimizer: + // + // X* = ( 0.0, -0.5 ), F(X*) = -0.25 + // + // There are three parameters, TAU, THETA and PHI. + // + // 1 < TAU, then F is strictly convex. + // and F has continuous first derivatives. + // 2 < TAU, then F has continuous second derivatives. + // 3 < TAU, then F has continuous third derivatives. + // + // However, this function can cause the Nelder-Mead optimization + // algorithm to "converge" to a point which is not the minimizer + // of the function F. + // + // Sample parameter values which cause problems for Nelder-Mead + // include: + // + // TAU = 1, THETA = 15, PHI = 10; + // TAU = 2, THETA = 6, PHI = 60; + // TAU = 3, THETA = 6, PHI = 400; + // + // To get the bad behavior, we also assume the initial simplex has the form + // + // X1 = (0,0), + // X2 = (1,1), + // X3 = (A,B), + // + // where + // + // A = (1+sqrt(33))/8 = 0.84307... + // B = (1-sqrt(33))/8 = -0.59307... + // + // Licensing: + // + // This code is distributed under the GNU LGPL license. + // + // Modified: + // + // 09 February 2008 + // + // Author: + // + // John Burkardt + // + // Reference: + // + // Ken McKinnon, + // Convergence of the Nelder-Mead simplex method to a nonstationary point, + // SIAM Journal on Optimization, + // Volume 9, Number 1, 1998, pages 148-158. + // + // Parameters: + // + // Input, real X(2), the argument of the function. + // + // Output, real F, the value of the function at X. + // + // Copyright (C) 2009 - INRIA - Michael Baudin, Scilab port + + function [ f , index ] = mckinnon3 ( x , index ) + + if ( length ( x ) ~= 2 ) + error (_("Error: function expects a two dimensional input\n")); + end + + tau = 3.0; + theta = 6.0; + phi = 400.0; + + if ( x(1) <= 0.0 ) + f = theta * phi * abs ( x(1) ).^tau + x(2) * ( 1.0 + x(2) ); + else + f = theta * x(1).^tau + x(2) * ( 1.0 + x(2) ); + end + endfunction + function y = mckinnon3C ( x1 , x2 ) + y = mckinnon3 ( [x1 , x2] , 2 ) + endfunction + + + lambda1 = (1.0 + sqrt(33.0))/8.0; + lambda2 = (1.0 - sqrt(33.0))/8.0; + coords0 = [ + 1.0 1.0 + 0.0 0.0 + lambda1 lambda2 + ]; + + + x0 = [1.0 1.0]'; + mprintf(_("x0 = %s\n"),strcat(string(x0)," ")); + mprintf(_("Creating object ...\n")); + nm = nmplot_new (); + nm = nmplot_configure(nm, "-numberofvariables",2); + nm = nmplot_configure(nm, "-function",mckinnon3); + nm = nmplot_configure(nm, "-x0",x0); + nm = nmplot_configure(nm, "-maxiter",200); + nm = nmplot_configure(nm, "-maxfunevals",300); + nm = nmplot_configure(nm, "-tolfunrelative",10*%eps); + nm = nmplot_configure(nm, "-tolxrelative",10*%eps); + nm = nmplot_configure(nm, "-simplex0method","given"); + nm = nmplot_configure(nm, "-coords0",coords0); + // + // Setup output files + // + simplexfn = TMPDIR + filesep() + "history.simplex.txt"; + fbarfn = TMPDIR + filesep() + "history.fbar.txt"; + foptfn = TMPDIR + filesep() + "history.fopt.txt"; + sigmafn = TMPDIR + filesep() + "history.sigma.txt"; + nm = nmplot_configure(nm, "-simplexfn",simplexfn); + nm = nmplot_configure(nm, "-fbarfn",fbarfn); + nm = nmplot_configure(nm, "-foptfn",foptfn); + nm = nmplot_configure(nm, "-sigmafn",sigmafn); + // + // Perform optimization + // + mprintf(_("Searching (please wait) ...\n")); + nm = nmplot_search(nm); + // + // Print a summary + // + exec(fullfile(dname,"nmplot_summary.sci"),-1); + nmplot_summary(nm) + // + // Plot + // + mprintf(_("Plot contour (please wait) ...\n")); + xmin = -0.2; + xmax = 1.2 ; + ymin = -2.0 ; + ymax = 2.0 ; + nx = 50 ; + ny = 50; + xdata=linspace(xmin,xmax,nx); + ydata=linspace(ymin,ymax,ny); + scf(); + f = gcf(); + f.axes_size = [710, 560]; + subplot(2,2,1) + xset("fpf"," ") + drawlater(); + contour ( xdata , ydata , mckinnon3C , [-0.2 0.0 1.0 2.0 5.0 10.0 20.0] ) + nmplot_simplexhistory ( nm ); + drawnow(); + subplot(2,2,2) + mytitle = _("Function Value Average"); + myxlabel = _("Iterations"); + nmplot_historyplot ( nm , fbarfn, mytitle , myxlabel ); + subplot(2,2,3) + mytitle = _("Minimum Function Value") ; + myxlabel = _("Iterations"); + nmplot_historyplot ( nm , foptfn, mytitle , myxlabel ); + subplot(2,2,4) + mytitle = _("Maximum Oriented length") ; + myxlabel = _("Iterations") ; + nmplot_historyplot ( nm , sigmafn, mytitle , myxlabel ); + demo_viewCode(filename); + deletefile(simplexfn); + deletefile(fbarfn); + deletefile(foptfn); + deletefile(sigmafn); + nm = nmplot_destroy(nm); + mprintf(_("End of demo.\n")); +endfunction + +demo_mckinnon2() +clear demo_mckinnon2; + + + + + + diff --git a/modules/optimization/demos/neldermead/nmplot_mckinnon2.sce b/modules/optimization/demos/neldermead/nmplot_mckinnon2.sce new file mode 100755 index 000000000..7a21a0535 --- /dev/null +++ b/modules/optimization/demos/neldermead/nmplot_mckinnon2.sce @@ -0,0 +1,190 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2011 - DIGITEO - Michael Baudin +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + + +function demo_mckinnon2() + + mprintf(_("Defining McKinnon function...\n")); + + //% MCKINNON computes the McKinnon function. + // + // Discussion: + // + // This function has a global minimizer: + // + // X* = ( 0.0, -0.5 ), F(X*) = -0.25 + // + // There are three parameters, TAU, THETA and PHI. + // + // 1 < TAU, then F is strictly convex. + // and F has continuous first derivatives. + // 2 < TAU, then F has continuous second derivatives. + // 3 < TAU, then F has continuous third derivatives. + // + // However, this function can cause the Nelder-Mead optimization + // algorithm to "converge" to a point which is not the minimizer + // of the function F. + // + // Sample parameter values which cause problems for Nelder-Mead + // include: + // + // TAU = 1, THETA = 15, PHI = 10; + // TAU = 2, THETA = 6, PHI = 60; + // TAU = 3, THETA = 6, PHI = 400; + // + // To get the bad behavior, we also assume the initial simplex has the form + // + // X1 = (0,0), + // X2 = (1,1), + // X3 = (A,B), + // + // where + // + // A = (1+sqrt(33))/8 = 0.84307... + // B = (1-sqrt(33))/8 = -0.59307... + // + // Licensing: + // + // This code is distributed under the GNU LGPL license. + // + // Modified: + // + // 09 February 2008 + // + // Author: + // + // John Burkardt + // + // Reference: + // + // Ken McKinnon, + // Convergence of the Nelder-Mead simplex method to a nonstationary point, + // SIAM Journal on Optimization, + // Volume 9, Number 1, 1998, pages 148-158. + // + // Parameters: + // + // Input, real X(2), the argument of the function. + // + // Output, real F, the value of the function at X. + // + // Copyright (C) 2009 - INRIA - Michael Baudin, Scilab port + + function [ f , index ] = mckinnon3 ( x , index ) + + if ( length ( x ) ~= 2 ) + error (_("Error: function expects a two dimensional input\n")); + end + + tau = 3.0; + theta = 6.0; + phi = 400.0; + + if ( x(1) <= 0.0 ) + f = theta * phi * abs ( x(1) ).^tau + x(2) * ( 1.0 + x(2) ); + else + f = theta * x(1).^tau + x(2) * ( 1.0 + x(2) ); + end + endfunction + function y = mckinnon3C ( x1 , x2 ) + y = mckinnon3 ( [x1 , x2] , 2 ) + endfunction + + lambda1 = (1.0 + sqrt(33.0))/8.0; + lambda2 = (1.0 - sqrt(33.0))/8.0; + coords0 = [ + 1.0 1.0 + 0.0 0.0 + lambda1 lambda2 + ]; + + + x0 = [1.0 1.0]'; + mprintf(_("x0=%s\n"), strcat(string(x0)," ")); + mprintf(_("Creating object...\n")); + nm = nmplot_new (); + nm = nmplot_configure(nm, "-numberofvariables",2); + nm = nmplot_configure(nm, "-function",mckinnon3); + nm = nmplot_configure(nm, "-x0",x0); + nm = nmplot_configure(nm, "-maxiter",200); + nm = nmplot_configure(nm, "-maxfunevals",300); + nm = nmplot_configure(nm, "-tolsimplexizerelative",1.e-6); + nm = nmplot_configure(nm, "-simplex0method","given"); + nm = nmplot_configure(nm, "-coords0",coords0); + nm = nmplot_configure(nm, "-kelleystagnationflag",%t); + nm = nmplot_configure(nm, "-restartflag",%t); + nm = nmplot_configure(nm, "-restartdetection","kelley"); + // + // Setup output files + // + simplexfn = TMPDIR + filesep() + "history.simplex.txt"; + fbarfn = TMPDIR + filesep() + "history.fbar.txt"; + foptfn = TMPDIR + filesep() + "history.fopt.txt"; + sigmafn = TMPDIR + filesep() + "history.sigma.txt"; + nm = nmplot_configure(nm, "-simplexfn",simplexfn); + nm = nmplot_configure(nm, "-fbarfn",fbarfn); + nm = nmplot_configure(nm, "-foptfn",foptfn); + nm = nmplot_configure(nm, "-sigmafn",sigmafn); + // + // Perform optimization + // + mprintf(_("Searching (please wait) ...\n")); + nm = nmplot_search(nm); + disp(nm); + + // + // Plot + // + mprintf(_("Plot contour (please wait) ...\n")); + xmin = -0.2; + xmax = 1.2 ; + ymin = -2.0 ; + ymax = 2.0 ; + nx = 50 ; + ny = 50; + xdata=linspace(xmin,xmax,nx); + ydata=linspace(ymin,ymax,ny); + scf(); + f = gcf(); + f.axes_size = [710, 560]; + subplot(2,2,1) + xset("fpf"," ") + drawlater(); + contour ( xdata , ydata , mckinnon3C , [-0.2 0.0 1.0 2.0 5.0 10.0 20.0] ) + nmplot_simplexhistory ( nm ); + drawnow(); + subplot(2,2,2) + mytitle = _("Function Value Average"); + myxlabel = _("Iterations"); + nmplot_historyplot ( nm , fbarfn, mytitle , myxlabel ); + subplot(2,2,3) + mytitle = _("Minimum Function Value") ; + myxlabel = _("Iterations"); + nmplot_historyplot ( nm , foptfn, mytitle , myxlabel ); + subplot(2,2,4) + mytitle = _("Maximum Oriented length") ; + myxlabel = _("Iterations") ; + nmplot_historyplot ( nm , sigmafn, mytitle , myxlabel ); + demo_viewCode("nmplot_mckinnon2.sce"); + deletefile(simplexfn); + deletefile(fbarfn); + deletefile(foptfn); + deletefile(sigmafn); + nm = nmplot_destroy(nm); + mprintf(_("End of demo.\n")); +endfunction + +demo_mckinnon2(); +clear demo_mckinnon2 + + + diff --git a/modules/optimization/demos/neldermead/nmplot_quadratic.fixed.sce b/modules/optimization/demos/neldermead/nmplot_quadratic.fixed.sce new file mode 100755 index 000000000..5f1947d63 --- /dev/null +++ b/modules/optimization/demos/neldermead/nmplot_quadratic.fixed.sce @@ -0,0 +1,87 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2011 - DIGITEO - Michael Baudin +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +function demo_nmplot_qfixed() + + filename = "nmplot_quadratic.fixed.sce"; + dname = get_absolute_file_path(filename); + + mprintf(_("Illustrates that the fixed-shape Spendley et al. algorithm performs well on a quadratic test case.\n")); + mprintf(_("Defining quadratic function ...\n")); + + function [ y , index ] = quadratic ( x , index ) + y = x(1)^2 + x(2)^2 - x(1) * x(2); + endfunction + function y = quadraticC ( x1, x2 ) + y = quadratic ( [x1, x2] , 2 ) + endfunction + + mprintf(_("Creating nmplot object ...\n")); + + nm = nmplot_new (); + nm = nmplot_configure(nm, "-numberofvariables",2); + nm = nmplot_configure(nm, "-function",quadratic); + nm = nmplot_configure(nm, "-x0",[2.0 2.0]'); + nm = nmplot_configure(nm, "-maxiter",100); + nm = nmplot_configure(nm, "-maxfunevals",300); + nm = nmplot_configure(nm, "-tolxmethod",%f); + nm = nmplot_configure(nm, "-tolsimplexizerelative",1.e-8); + nm = nmplot_configure(nm, "-simplex0method","spendley"); + nm = nmplot_configure(nm, "-method","fixed"); + // + // Setup output files + // + simplexfn = TMPDIR + filesep() + "history.simplex.txt"; + + nm = nmplot_configure(nm, "-simplexfn",simplexfn); + // + // Perform optimization + // + mprintf(_("Searching (please wait) ...\n")); + nm = nmplot_search(nm); + // + // Print a summary + // + exec(fullfile(dname,"nmplot_summary.sci"),-1); + nmplot_summary(nm) + + // Plot simplex history + scf(); + // Plot the contours of the cost function and the simplex history + mprintf(_("Plotting contour (please wait) ...\n")); + nm = nmplot_configure(nm, "-verbose",0); + xmin = -2.0 ; + xmax = 4.0 ; + ymin = -2.0 ; + ymax = 4.0 ; + nx = 50 ; + ny = 50; + xdata=linspace(xmin,xmax,nx); + ydata=linspace(ymin,ymax,ny); + drawlater(); + contour ( xdata , ydata , quadraticC , [0.1 1.0 2.0 5.0 10.0 15.0 20.0] ) + nmplot_simplexhistory ( nm ); + drawnow(); + demo_viewCode(filename); + + // Clean-up + deletefile(simplexfn); + nm = nmplot_destroy(nm); + mprintf("End of demo.\n"); +endfunction + +demo_nmplot_qfixed(); +clear demo_nmplot_qfixed; + + + + diff --git a/modules/optimization/demos/neldermead/nmplot_quadratic.fixed2.sce b/modules/optimization/demos/neldermead/nmplot_quadratic.fixed2.sce new file mode 100755 index 000000000..ea51f1f91 --- /dev/null +++ b/modules/optimization/demos/neldermead/nmplot_quadratic.fixed2.sce @@ -0,0 +1,87 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2011 - DIGITEO - Michael Baudin +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +function demo_nmplot_qfixed2() + + filename = "nmplot_quadratic.fixed2.sce"; + dname = get_absolute_file_path(filename); + + mprintf(_("Illustrates that the fixed-shape Spendley et al. algorithm performs badly on a badly quadratic test case.\n")); + mprintf(_("Defining quadratic function...\n")); + a = 100; + + function [ y , index ] = quadratic ( x , index ) + y = a * x(1)^2 + x(2)^2; + endfunction + function y = quadraticC ( x1 , x2 ) + y = quadratic ( [x1 , x2] , 2 ) + endfunction + + mprintf(_("Creating nmplot object ...\n")); + nm = nmplot_new (); + nm = nmplot_configure(nm, "-numberofvariables",2); + nm = nmplot_configure(nm, "-function",quadratic); + nm = nmplot_configure(nm, "-x0",[10.0 10.0]'); + nm = nmplot_configure(nm, "-maxiter",400); + nm = nmplot_configure(nm, "-maxfunevals",400); + nm = nmplot_configure(nm, "-tolxmethod",%f); + nm = nmplot_configure(nm, "-tolsimplexizerelative",1.e-8); + nm = nmplot_configure(nm, "-simplex0method","spendley"); + nm = nmplot_configure(nm, "-method","fixed"); + + // + // Setup output files + // + simplexfn = TMPDIR + filesep() + "history.simplex.txt"; + nm = nmplot_configure(nm, "-simplexfn",simplexfn); + + // + // Perform optimization + // + mprintf(_("Searching (please wait) ...\n")); + nm = nmplot_search(nm); + // + // Print a summary + // + exec(fullfile(dname,"nmplot_summary.sci"),-1); + nmplot_summary(nm) + // + // Plot the contours of the cost function and the simplex history + mprintf(_("Plotting contour (please wait) ...\n")); + nm = nmplot_configure(nm, "-verbose", 0); + xmin = -5.0 ; + xmax = 12.0 ; + ymin = -2.0 ; + ymax = 12.0 ; + nx = 50 ; + ny = 50; + scf() + drawlater(); + xdata=linspace(xmin,xmax,nx); + ydata=linspace(ymin,ymax,ny); + contour ( xdata , ydata , quadraticC , [10.0 50 100 1000 2000 5000 10000 20000] ) + nmplot_simplexhistory ( nm ); + drawnow(); + demo_viewCode(filename); + + // Clean-up + deletefile(simplexfn); + nm = nmplot_destroy(nm); + mprintf("End of demo.\n"); +endfunction + +demo_nmplot_qfixed2(); +clear demo_nmplot_qfixed2; + + + + diff --git a/modules/optimization/demos/neldermead/nmplot_quadratic.variable.sce b/modules/optimization/demos/neldermead/nmplot_quadratic.variable.sce new file mode 100755 index 000000000..90222f5d7 --- /dev/null +++ b/modules/optimization/demos/neldermead/nmplot_quadratic.variable.sce @@ -0,0 +1,84 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2011 - DIGITEO - Michael Baudin +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +function demo_nmplot_qvariable() + + filename = "nmplot_quadratic.variable.sce"; + dname = get_absolute_file_path(filename); + + mprintf(_("Illustrates that the variable-shape Nelder-Mead algorithm performs well on a quadratic test case.\n")); + mprintf(_("Defining quadratic function ...\n")); + function [ y , index ] = quadratic ( x , index ) + y = x(1)^2 + x(2)^2 - x(1) * x(2); + endfunction + function y = quadraticC ( x1 , x2 ) + y = quadratic ( [x1 , x2] , 2 ) + endfunction + + mprintf(_("Creating nmplot object...\n")); + nm = nmplot_new (); + nm = nmplot_configure(nm, "-numberofvariables",2); + nm = nmplot_configure(nm, "-function",quadratic); + nm = nmplot_configure(nm, "-x0",[2.0 2.0]'); + nm = nmplot_configure(nm, "-maxiter",100); + nm = nmplot_configure(nm, "-maxfunevals",300); + nm = nmplot_configure(nm, "-tolxmethod",%f); + nm = nmplot_configure(nm, "-tolsimplexizerelative",1.e-8); + nm = nmplot_configure(nm, "-simplex0method","spendley"); + + // + // Setup output files + // + simplexfn = TMPDIR + filesep() + "history.simplex.txt"; + nm = nmplot_configure(nm, "-simplexfn",simplexfn); + + // + // Perform optimization + // + mprintf(_("Searching (please wait) ...\n")); + nm = nmplot_search(nm); + // + // Print a summary + // + exec(fullfile(dname,"nmplot_summary.sci"),-1); + nmplot_summary(nm) + // + // Plot the contours of the cost function and the simplex history + mprintf(_("Plotting contour (please wait) ...\n")); + nm = nmplot_configure(nm, "-verbose", 0); + xmin = -2.0 ; + xmax = 4.0 ; + ymin = -2.0 ; + ymax = 4.0 ; + nx = 50 ; + ny = 50; + xdata=linspace(xmin,xmax,nx); + ydata=linspace(ymin,ymax,ny); + scf() + drawlater(); + contour ( xdata , ydata , quadraticC , [0.1 1.0 2.0 5.0 10.0 15.0 20.0] ) + nmplot_simplexhistory ( nm ); + drawnow(); + demo_viewCode(filename); + + // Clean-up + deletefile(simplexfn); + nm = nmplot_destroy(nm); + mprintf(_("End of demo.\n")); +endfunction + +demo_nmplot_qvariable(); +clear demo_nmplot_qvariable; + + + + diff --git a/modules/optimization/demos/neldermead/nmplot_quadratic.variable2.sce b/modules/optimization/demos/neldermead/nmplot_quadratic.variable2.sce new file mode 100755 index 000000000..114c70ffe --- /dev/null +++ b/modules/optimization/demos/neldermead/nmplot_quadratic.variable2.sce @@ -0,0 +1,84 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2011 - DIGITEO - Michael Baudin +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +function demo_nmplot_qvariable2() + + filename = "nmplot_quadratic.variable2.sce"; + dname = get_absolute_file_path(filename); + + mprintf(_("Illustrates that the Nelder-Mead algorithm performs correctly on a badly quadratic test case.\n")); + mprintf(_("Defining quadratic function ...\n")); + a = 100.0; + function [ y , index ] = quadratic ( x , index ) + y = a * x(1)^2 + x(2)^2; + endfunction + function y = quadraticC ( x1 , x2 ) + [ y , index ] = quadratic ( [x1 , x2] , 2 ) + endfunction + + mprintf(_("Creating nmplot object...\n")); + nm = nmplot_new (); + nm = nmplot_configure(nm, "-numberofvariables",2); + nm = nmplot_configure(nm, "-function",quadratic); + nm = nmplot_configure(nm, "-x0",[10.0 10.0]'); + nm = nmplot_configure(nm, "-maxiter",400); + nm = nmplot_configure(nm, "-maxfunevals",400); + nm = nmplot_configure(nm, "-tolxmethod",%f); + nm = nmplot_configure(nm, "-tolsimplexizerelative",1.e-8); + nm = nmplot_configure(nm, "-simplex0method","spendley"); + + // + // Setup output files + // + simplexfn = TMPDIR + filesep() + "history.simplex.txt"; + nm = nmplot_configure(nm, "-simplexfn",simplexfn); + // + // Perform optimization + // + mprintf(_("Searching (please wait) ...\n")); + nm = nmplot_search(nm); + // + // Print a summary + // + exec(fullfile(dname,"nmplot_summary.sci"),-1); + nmplot_summary(nm) + // + // Plot the contours of the cost function and the simplex history + mprintf(_("Plotting contour (please wait) ...\n")); + nm = nmplot_configure(nm, "-verbose", 0); + xmin = -5.0 ; + xmax = 12.0 ; + ymin = -2.0 ; + ymax = 12.0 ; + nx = 50 ; + ny = 50; + scf() + drawlater(); + xdata=linspace(xmin,xmax,nx); + ydata=linspace(ymin,ymax,ny); + contour ( xdata , ydata , quadraticC , [10.0 50 100 1000 2000 5000 10000 20000] ) + nmplot_simplexhistory ( nm ); + drawnow(); + demo_viewCode(filename); + // + // Clean-up + deletefile(simplexfn); + nm = nmplot_destroy(nm); + mprintf(_("End of demo.\n")); +endfunction + +demo_nmplot_qvariable2(); +clear demo_nmplot_qvariable2; + + + + diff --git a/modules/optimization/demos/neldermead/nmplot_rosenbrock.fixed.sce b/modules/optimization/demos/neldermead/nmplot_rosenbrock.fixed.sce new file mode 100755 index 000000000..46efa9ef6 --- /dev/null +++ b/modules/optimization/demos/neldermead/nmplot_rosenbrock.fixed.sce @@ -0,0 +1,83 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2011 - DIGITEO - Michael Baudin +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +function demo_nmplot_rosen() + + filename = "nmplot_rosenbrock.fixed.sce"; + dname = get_absolute_file_path(filename); + + mprintf(_("Illustrates that the fixed-shape Spendley et al. algorithm does NOT perform well on Rosenbrock test case.\n")); + mprintf(_("Defining Rosenbrock function...\n")); + + function [ y , index ] = rosenbrock ( x , index ) + y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2; + endfunction + function [ y , index ] = rosenbrockC ( x1 , x2 ) + y = rosenbrock ( [x1 , x2] , 2 ) + endfunction + + mprintf(_("Creating nmplot object ...\n")); + nm = nmplot_new (); + nm = nmplot_configure(nm, "-numberofvariables",2); + nm = nmplot_configure(nm, "-function",rosenbrock); + nm = nmplot_configure(nm, "-x0",[-1.2 1.0]'); + nm = nmplot_configure(nm, "-maxiter",100); + nm = nmplot_configure(nm, "-maxfunevals",300); + nm = nmplot_configure(nm, "-tolfunrelative",10*%eps); + nm = nmplot_configure(nm, "-tolxrelative",10*%eps); + nm = nmplot_configure(nm, "-method","fixed"); + + // + // Setup output files + // + simplexfn = TMPDIR + filesep() + "history.simplex.txt"; + nm = nmplot_configure(nm, "-simplexfn",simplexfn); + + // + // Perform optimization + // + mprintf(_("Searching (please wait) ...\n")); + nm = nmplot_search(nm); + // + // Print a summary + // + exec(fullfile(dname,"nmplot_summary.sci"),-1); + nmplot_summary(nm) + + // Plot the contours of the cost function and the simplex history + mprintf(_("Plotting contour (please wait) ...\n")); + xmin = -2.0 ; + xmax = 2.0 ; + ymin = -2.0 ; + ymax = 2.0 ; + nx = 50 ; + ny = 50; + xdata=linspace(xmin,xmax,nx); + ydata=linspace(ymin,ymax,ny); + f = scf(); + drawlater(); + contour ( xdata , ydata , rosenbrockC , [2 10 100 500 1000 2000] ) + nmplot_simplexhistory ( nm ); + drawnow(); + demo_viewCode(filename); + // + deletefile(simplexfn); + nm = nmplot_destroy(nm); + mprintf(_("End of demo.\n")); +endfunction + +demo_nmplot_rosen() +clear demo_nmplot_rosen; + + + + diff --git a/modules/optimization/demos/neldermead/nmplot_rosenbrock.sce b/modules/optimization/demos/neldermead/nmplot_rosenbrock.sce new file mode 100755 index 000000000..7501008d7 --- /dev/null +++ b/modules/optimization/demos/neldermead/nmplot_rosenbrock.sce @@ -0,0 +1,79 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2011 - DIGITEO - Michael Baudin +// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS +// +// 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.1-en.txt + +function demo_nmplot_rosenvar() + + filename = "nmplot_rosenbrock.sce"; + dname = get_absolute_file_path(filename); + mprintf("Illustrates the Nelder-Mead algorithm on Rosenbrock test case.\n"); + mprintf("Defining Rosenbrock function...\n"); + function [ y , index ] = rosenbrock ( x , index ) + y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2; + endfunction + function [ y , index ] = rosenbrockC ( x1 , x2 ) + y = rosenbrock ( [x1 , x2] , 2 ) + endfunction + + mprintf("Creating nmplot object...\n"); + nm = nmplot_new (); + nm = nmplot_configure(nm,"-numberofvariables",2); + nm = nmplot_configure(nm,"-function",rosenbrock); + nm = nmplot_configure(nm,"-x0",[-1.2 1.0]'); + nm = nmplot_configure(nm,"-maxiter",200); + nm = nmplot_configure(nm,"-maxfunevals",300); + nm = nmplot_configure(nm,"-tolfunrelative",10*%eps); + nm = nmplot_configure(nm,"-tolxrelative",10*%eps); + // + // Setup output files + // + simplexfn = TMPDIR + filesep() + "history.simplex.txt"; + nm = nmplot_configure(nm,"-simplexfn",simplexfn); + // + // Perform optimization + // + mprintf("Searching (please wait)...\n"); + nm = nmplot_search(nm); + // + // Print a summary + // + exec(fullfile(dname,"nmplot_summary.sci"),-1); + nmplot_summary(nm) + // + // Plot the contours of the cost function and the simplex history + mprintf("Plotting contour (please wait)...\n"); + xmin = -2.0; + xmax = 2.0 ; + ymin = -1.0 ; + ymax = 2.5 ; + nx = 50 ; + ny = 50; + xdata=linspace(xmin,xmax,nx); + ydata=linspace(ymin,ymax,ny); + scf(); + drawlater(); + contour ( xdata , ydata , rosenbrockC , [2 10 100 500 1000 2000] ) + nmplot_simplexhistory ( nm ); + drawnow(); + demo_viewCode(filename); + // + // Cleanup + deletefile(simplexfn); + nm = nmplot_destroy(nm); + mprintf("End of demo.\n"); +endfunction + +demo_nmplot_rosenvar() +clear demo_nmplot_rosenvar; + + + + diff --git a/modules/optimization/demos/neldermead/nmplot_summary.sci b/modules/optimization/demos/neldermead/nmplot_summary.sci new file mode 100755 index 000000000..9542426e1 --- /dev/null +++ b/modules/optimization/demos/neldermead/nmplot_summary.sci @@ -0,0 +1,27 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2008-2009 - INRIA - Michael Baudin +// Copyright (C) 2010 - DIGITEO - Allan CORNET +// Copyright (C) 2011 - 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.1-en.txt + +function nmplot_summary(nm) + function str = _strvec ( x ) + str = strcat(string(x)," ") + endfunction + + xopt = nmplot_get(nm,"-xopt") + fopt = nmplot_get(nm,"-fopt") + funevals = nmplot_get(nm,"-funevals") + iter = nmplot_get(nm,"-iterations") + status = nmplot_get(nm,"-status") + mprintf("Xopt: [%s]\n",_strvec(xopt)); + mprintf("Fopt: %s\n",string(fopt)); + mprintf("Function evaluations: %d\n",funevals); + mprintf("Iterations: %d\n",iter); + mprintf("Status: %s\n",status); +endfunction diff --git a/modules/optimization/demos/neldermead/polygon.sce b/modules/optimization/demos/neldermead/polygon.sce new file mode 100755 index 000000000..c51b5406d --- /dev/null +++ b/modules/optimization/demos/neldermead/polygon.sce @@ -0,0 +1,261 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2011 - 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.1-en.txt + +mprintf("Finds the largest n-polygon with diameter smaller than 1.\n"); + +function [ f , c , index ] = largesmallpolygon ( x , index ) + // Bibliography + // "Benchmarking Optimization Software with Cops" + // Dolan, Moré, 2001 + // Section 1, "Largest Small Polygon" + // + // "Biggest Little Polygon" + // http://mathworld.wolfram.com/BiggestLittlePolygon.html + // + // Audet, C. "Optimisation globale structurée: propriétés, équivalences et résolution." + // Thèse de Doctorat. Montréal, Canada: École Polytechnique de Montréal, 1997. + // http://www.gerad.ca/Charles.Audet. + // + // Known optimal values are + // A6 = 0.677981 (Wolfram Mathworld) + // A8 = 0.726869 (Wolfram Mathworld) + // A25 = 0.77974 (Dolan & Moré - SNOPT) + // A50 = 0.784016 (Dolan & Moré - SNOPT) + // A75 = 0.784769 (Dolan & Moré - SNOPT) + // A100 = 0.785040 (Dolan & Moré - SNOPT) + // + nv = size(x,"*")/2 + f = [] + c = [] + // nv: number of vertices + // x : a (2*nv)-by-1 matrix of doubles, where + // r is in [0,1] + // t is in [0,pi] + r = x(1:nv) + t = x(nv+1:2*nv) + + //polygon_draw(r ,t , nv); + //pause + + if ( index == 2 | index == 6 ) then + f = polygon_area(r ,t , nv) + f = -f + end + if ( index == 5 | index == 6 ) then + c = zeros(nv^2+nv-1,1) + // Set the diameters + d = polygon_diameters(r ,t , nv) + d = matrix(d,nv^2,1) + c(1:nv^2) = 1-d + // Set the angles + c(nv^2+1:nv^2+nv-1) = t(2:nv)-t(1:nv-1) + // Transpose for neldermead + c = c' + end +endfunction + +function polygon_draw(r ,t , nv) + // Draws a polygon with nv vertices. + // r : a nv-by-1 matrix of doubles, the radius + // t : a nv-by-1 matrix of doubles, the angles + x = r.*cos(t); + y = r.*sin(t); + i = 1; + x($+1) = r(i)*cos(t(i)); + y($+1) = r(i)*sin(t(i)); + plot(x,y,"bo-") + h = gcf() + h.children.isoview = "on" +endfunction + +function polygon_update(h,r ,t , nv) + // Draws a polygon with nv vertices. + // r : a nv-by-1 matrix of doubles, the radius + // t : a nv-by-1 matrix of doubles, the angles + x = r.*cos(t); + y = r.*sin(t); + i = 1; + x($+1) = r(i)*cos(t(i)); + y($+1) = r(i)*sin(t(i)); + h.data = [x y] +endfunction + +function [r,t] = polygon_regular (nv) + // Returns a regular polygon with nv vertices + // and unit radius, centered at origin. + // r : a nv-by-1 matrix of doubles, the radius + // t : a nv-by-1 matrix of doubles, the angles + r = ones(nv,1); + tnv = 2*%pi/nv + t = linspace(-%pi+tnv,%pi,nv)' +endfunction + +function f = polygon_area(r ,t , nv) + // Returns the area of a polygon with nv vertices. + // r : a nv-by-1 matrix of doubles, the radius + // t : a nv-by-1 matrix of doubles, the angles + f = r(nv) * r(1) * sin(t(1)-t(nv)) + for i = 1: nv-1 + f = f + r(i+1) * r(i) * sin(t(i+1)-t(i)) + end + f = 0.5 * f +endfunction + + + +function d = polygon_diameters(r ,t , nv) + // Returns the diameters of a polygon with nv vertices. + // r : a nv-by-1 matrix of doubles, the radius + // t : a nv-by-1 matrix of doubles, the angles + d = zeros(nv,nv) + for i = 1 : nv + for j = 1 : nv + d(i,j) = r(i)^2 + r(j)^2 - 2*r(i)*r(j)*cos(t(i)-t(j)) + d(i,j) = abs(d(i,j)) + d(i,j) = sqrt(d(i,j)) + end + end +endfunction + + + + +///////////////////////////////////////////////// +// +// Maximize the size of the polynomial +// +// +function stop = myoutputcmd(state, data) + stop = %f + iter = data.iteration + if ( state == "init" ) then + mprintf ( "=================================\n"); + mprintf ( "Initialization\n"); + elseif ( state == "done" ) then + mprintf ( "=================================\n"); + mprintf ( "End of Optimization\n"); + end + fc = data.funccount + fval = data.fval + x = data.x + simplex = data.simplex + step = data.step + ssize = optimsimplex_size ( simplex ) + // + // Plot current solution + if ( %t & modulo(iter,10) == 0) then + h = findobj ( "user_data" , "nmpolygon" ); + nv = size(x,"*")/2 + r = x(1:nv) + t = x(nv+1:$) + polygon_update(h.children.children,r ,t , nv) + a = polygon_area(r ,t , nv); + str = msprintf("Largest Small Polygon - Area=%f",a); + h.title.text = str + end + // + if ( modulo(iter,10) == 0 ) then + mprintf ( "Iter. #%3d, Feval #%3d, Fval = %f, S = %.1e\n", .. + iter, fc, fval, ssize); + end +endfunction + + + + + +function [A,r,t] = findlargestpolygon (nv) + // Finds the largest smallest polygon with nv vertices + // A : a 1-by-1 matrix of doubles, the area + // r : a nv-by-1 matrix of doubles, the radius + // t : a nv-by-1 matrix of doubles, the angle + radius = 0.45; + [r,t] = polygon_regular (nv); + r = radius*r; + scf() + polygon_draw(r ,t , nv); + h = gcf(); + h.children.data_bounds = [ + -0.6 -0.6 + 0.6 0.6 + ]; + h.children.user_data = "nmpolygon"; + x0 = [r;t]; + index = 6; + [ f0 , c0 , index ] = largesmallpolygon ( x0 , index ); + mprintf("Current area = %f\n",-f0); + mprintf("Constraint satisfaction = %f (expected positive)\n",min(c0)); + // + // Setup bounds + rmin = zeros(nv,1); + rmax = ones(nv,1); + tmin = -ones(nv,1)*%pi; + tmax = ones(nv,1)*%pi; + xmin=[rmin;tmin]; + xmax=[rmax;tmax]; + // + nm = neldermead_new (); + nm = neldermead_configure(nm,"-numberofvariables",2*nv); + nm = neldermead_configure(nm,"-function",largesmallpolygon); + nm = neldermead_configure(nm,"-x0",x0); + nm = neldermead_configure(nm,"-maxiter",2000); + nm = neldermead_configure(nm,"-maxfunevals",2000); + nm = neldermead_configure(nm,"-method","box"); + nm = neldermead_configure(nm,"-boundsmin",xmin'); + nm = neldermead_configure(nm,"-boundsmax",xmax'); + nm = neldermead_configure(nm,"-simplex0method","randbounds"); + nm = neldermead_configure(nm,"-nbineqconst",nv^2+nv-1); + nm = neldermead_configure(nm,"-outputcommand",myoutputcmd); + // + // Check that the cost function is correctly connected. + [ nm , result ] = neldermead_function ( nm , x0 ); + // + // Perform optimization + nm = neldermead_search(nm, "off"); + fopt = neldermead_get(nm,"-fopt") + A = -fopt + xopt = neldermead_get(nm,"-xopt") + r = xopt(1:nv) + t = xopt(nv+1:$) + nm = neldermead_destroy(nm) +endfunction + +//////////////////////////////////////////////////////// +// Checking area for nv=6 +// Use a regular hexagon (see Graham, page 5, Fig. 5). +// A = 0.64952... +nv = 6; +radius = 0.5; +[r,t] = polygon_regular (nv); +r = radius*r; +h = scf(); +polygon_draw(r ,t , nv); +f = polygon_area(r ,t , nv); +// +// Check this +// A = 3*sqrt(3)*s^2/2, where s is the side length, i.e. the radius +A = 3*sqrt(3)*radius^2/2; + +mprintf("Area =%f (expected = %f)\n",f,A); + +d = polygon_diameters(r ,t , nv); +dmax = max(d); +mprintf("Maximum diameter=%f (expected = %f)\n",dmax,2*radius); +dmin = min(d(d<>0)); +mprintf("Minimum diameter=%f (expected=%f)\n",dmin,radius); +close(h); + +//////////////////////////////////////////////////////// +// +// Solve problem +// +nv = 6; +rand("seed" , 0); +[A,r,t] = findlargestpolygon (nv); +mprintf("Maximum Area =%f (expected = %f)\n",A,0.677981); |