1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
\r
2 // Copyright (C) 2008-2009 - INRIA - Michael Baudin
\r
3 // Copyright (C) 2010 - DIGITEO - Allan CORNET
\r
4 // Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS
\r
6 // This file must be used under the terms of the CeCILL.
\r
7 // This source file is licensed as described in the file COPYING, which
\r
8 // you should have received as part of this distribution. The terms
\r
9 // are also available at
\r
10 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
\r
13 function demo_outputcmd()
\r
15 function [ y , index ] = rosenbrock ( x , index )
\r
16 y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
\r
22 // This command is called back by the Nelder-Mead
\r
25 // state : the current state of the algorithm
\r
26 // "init", "iter", "done"
\r
27 // data : the data at the current state
\r
28 // This is a tlist with the following entries:
\r
29 // * x : the optimal vector of parameters
\r
30 // * fval : the minimum function value
\r
31 // * simplex : the simplex, as a simplex object
\r
32 // * iteration : the number of iterations performed
\r
33 // * funccount : the number of function evaluations
\r
34 // stop : set to %t to interrupt the algorithm
\r
37 function stop = myoutputcmd ( state , data )
\r
38 iter = data.iteration
\r
39 if ( state == "init" ) then
\r
40 mprintf ( "=================================\n");
\r
41 mprintf ( _("Initialization\n"));
\r
42 elseif ( state == "done" ) then
\r
43 mprintf ( "=================================\n");
\r
44 mprintf ( _("End of Optimization\n"));
\r
49 simplex = data.simplex
\r
50 // Simplex is a data structure, which can be managed
\r
51 // by the optimsimplex class.
\r
52 ssize = optimsimplex_size ( simplex )
\r
53 mprintf ( "Iteration #%d, Feval #%d, Fval = %e, x = %s, Size = %e\n", iter, fc, fval, strcat(string(x)," "), ssize);
\r
58 nm = neldermead_new ();
\r
59 nm = neldermead_configure(nm,"-numberofvariables",2);
\r
60 nm = neldermead_configure(nm,"-function",rosenbrock);
\r
61 nm = neldermead_configure(nm,"-x0",[-1.2 1.0]');
\r
62 nm = neldermead_configure(nm,"-maxiter",200);
\r
63 nm = neldermead_configure(nm,"-maxfunevals",300);
\r
64 nm = neldermead_configure(nm,"-tolfunrelative",10*%eps);
\r
65 nm = neldermead_configure(nm,"-tolxrelative",10*%eps);
\r
66 nm = neldermead_configure(nm,"-outputcommand",myoutputcmd);
\r
67 nm = neldermead_search(nm);
\r
68 nm = neldermead_destroy(nm);
\r
69 mprintf(_("End of demo.\n"));
\r
72 // Load this script into the editor
\r
74 m = messagebox(_("View Code?"), "Question", "question", _(["Yes" "No"]), "modal")
\r
76 filename = 'neldermead_outputcmd.sce';
\r
77 dname = get_absolute_file_path(filename);
\r
78 editor ( dname + filename, "readonly" );
\r
83 clear demo_outputcmd;
\r