1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab 2// Copyright (C) 2008-2009 - INRIA - Michael Baudin 3// Copyright (C) 2011 - DIGITEO - Michael Baudin 4// 5// Copyright (C) 2012 - 2016 - Scilab Enterprises 6// 7// This file is hereby licensed under the terms of the GNU GPL v2.0, 8// pursuant to article 5.3.4 of the CeCILL v.2.1. 9// This file was originally licensed under the terms of the CeCILL v2.1, 10// and continues to be available under such terms. 11// For more information, see the COPYING file which you should have received 12// along with this program. 13 14// <-- CLI SHELL MODE --> 15// <-- ENGLISH IMPOSED --> 16 17 18function [ y , index ] = myquad ( x , index ) 19 y = x(1)^2 + x(2)^2 20endfunction 21 22 23// 24// myoutputcmd -- 25// This command is called back by the Nelder-Mead 26// algorithm. 27// Arguments 28// state : the current state of the algorithm 29// "init", "iter", "done" 30// data : the data at the current state 31// This is a tlist with the following entries: 32// * x : the optimal vector of parameters 33// * fval : the minimum function value 34// * simplex : the simplex, as a simplex object 35// * iteration : the number of iterations performed 36// * funccount : the number of function evaluations 37// * step : the type of step in the previous iteration 38// stop: set to true to stop algorithm 39// 40function stop = myoutputcmd ( state , data ) 41 global _OUTPUCMDFLAG_ 42 // Unload the array, just to make sure that the minimum is there 43 iter = data.iteration 44 fc = data.funccount 45 fval = data.fval 46 x = data.x 47 simplex = data.simplex 48 step = data.step 49 // Simplex is a data structure, which can be managed 50 // by the simplex class. 51 v = optimsimplex_dirmat ( simplex ) 52 condv = cond ( v ) 53 _OUTPUCMDFLAG_ = 1 54 stop = %f 55endfunction 56 57 58// 59// Test the variable algorithm 60// 61global _OUTPUCMDFLAG_; 62_OUTPUCMDFLAG_ = 0; 63 64nm = neldermead_new (); 65nm = neldermead_configure(nm,"-numberofvariables",2); 66nm = neldermead_configure(nm,"-function",myquad); 67nm = neldermead_configure(nm,"-x0",[-1.2 1.0]'); 68nm = neldermead_configure(nm,"-maxiter",10); 69nm = neldermead_configure(nm,"-method","variable"); 70nm = neldermead_configure(nm,"-outputcommand",myoutputcmd); 71nm = neldermead_search(nm, "off"); 72iter = neldermead_get(nm,"-iterations"); 73assert_checkequal ( iter , 10 ); 74// We are here, that means that the output command has been correctly 75// called 76assert_checkequal ( _OUTPUCMDFLAG_ , 1 ); 77nm = neldermead_destroy(nm); 78 79 80// 81// Test the fixed algorithm 82// 83global _OUTPUCMDFLAG_; 84_OUTPUCMDFLAG_ = 0; 85 86nm = neldermead_new (); 87nm = neldermead_configure(nm,"-numberofvariables",2); 88nm = neldermead_configure(nm,"-function",myquad); 89nm = neldermead_configure(nm,"-x0",[-1.2 1.0]'); 90nm = neldermead_configure(nm,"-maxiter",10); 91nm = neldermead_configure(nm,"-method","fixed"); 92nm = neldermead_configure(nm,"-outputcommand",myoutputcmd); 93nm = neldermead_search(nm, "off"); 94iter = neldermead_get(nm,"-iterations"); 95assert_checkequal ( iter , 10 ); 96// We are here, that means that the output command has been correctly 97// called 98assert_checkequal ( _OUTPUCMDFLAG_ , 1 ); 99nm = neldermead_destroy(nm); 100 101 102// 103// Test the Box algorithm 104// 105global _OUTPUCMDFLAG_; 106_OUTPUCMDFLAG_ = 0; 107 108nm = neldermead_new (); 109nm = neldermead_configure(nm,"-numberofvariables",2); 110nm = neldermead_configure(nm,"-function",myquad); 111nm = neldermead_configure(nm,"-x0",[-1.2 1.0]'); 112nm = neldermead_configure(nm,"-maxiter",10); 113nm = neldermead_configure(nm,"-method","box"); 114nm = neldermead_configure(nm,"-boundsmin",[-10.0 -10.0]); 115nm = neldermead_configure(nm,"-boundsmax",[10.0 10.0]); 116nm = neldermead_configure(nm,"-outputcommand",myoutputcmd); 117nm = neldermead_search(nm, "off"); 118iter = neldermead_get(nm,"-iterations"); 119assert_checkequal ( iter , 10 ); 120// We are here, that means that the output command has been correctly 121// called 122assert_checkequal ( _OUTPUCMDFLAG_ , 1 ); 123nm = neldermead_destroy(nm); 124 125 126function [ y , index ] = rosenbrock ( x , index ) 127 y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2; 128endfunction 129 130// 131// myoutputcmd2 -- 132// This command is called back by the Nelder-Mead 133// algorithm. 134// Arguments 135// state : the current state of the algorithm 136// "init", "iter", "done" 137// data : the data at the current state 138// This is a tlist with the following entries: 139// * x : the optimal vector of parameters 140// * fval : the minimum function value 141// * simplex : the simplex, as a simplex object 142// * iteration : the number of iterations performed 143// * funccount : the number of function evaluations 144// myobj : a user-defined data structure 145// stop: set to true to stop algorithm 146// 147function stop = myoutputcmd2 ( state , data , myobj ) 148 global _OUTPUCMDFLAG_ 149 // Unload the array, just to make sure that the minimum is there 150 iter = data.iteration 151 fc = data.funccount 152 fval = data.fval 153 x = data.x 154 simplex = data.simplex 155 // Simplex is a data structure, which can be managed 156 // by the simplex class. 157 v = optimsimplex_dirmat ( simplex ) 158 condv = cond ( v ) 159 _OUTPUCMDFLAG_ = myobj.myarg 160 stop = %f 161endfunction 162 163global _OUTPUCMDFLAG_; 164_OUTPUCMDFLAG_ = 0; 165 166myobj = tlist(["T_MYSTUFF","myarg"]); 167myobj.myarg = 12; 168 169nm = neldermead_new (); 170nm = neldermead_configure(nm,"-numberofvariables",2); 171nm = neldermead_configure(nm,"-function",rosenbrock); 172nm = neldermead_configure(nm,"-x0",[-1.2 1.0]'); 173nm = neldermead_configure(nm,"-maxiter",10); 174nm = neldermead_configure(nm,"-method","variable"); 175nm = neldermead_configure(nm,"-outputcommand",list(myoutputcmd2,myobj)); 176nm = neldermead_search(nm, "off"); 177iter = neldermead_get(nm,"-iterations"); 178assert_checkequal ( iter , 10 ); 179assert_checkequal ( _OUTPUCMDFLAG_ , 12 ); 180nm = neldermead_destroy(nm); 181 182 183 184