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 16function [ y , index ] = squarefun ( x , index ) 17 y = x(1)^2+x(2)^2; 18endfunction 19 20nm = neldermead_new (); 21nm = neldermead_configure(nm,"-numberofvariables",2); 22nm = neldermead_configure(nm,"-x0",[1.0 1.0]'); 23nm = neldermead_configure(nm,"-simplex0method","axes"); 24nm = neldermead_configure(nm,"-simplex0length",0.5); 25nm = neldermead_configure(nm,"-method","fixed"); 26nm = neldermead_configure(nm,"-function",squarefun); 27nm = neldermead_search(nm, "off"); 28xopt = neldermead_get(nm,"-xopt"); 29assert_checkalmostequal ( xopt , [0.0;0.0], 1e-6 ); 30fopt = neldermead_get(nm,"-fopt"); 31assert_checkalmostequal ( fopt , 0.0, 1e-6 ); 32nm = neldermead_destroy(nm); 33 34// 35// Interrupt the algorithm when done. 36 37nm = neldermead_new (); 38nm = neldermead_configure(nm,"-numberofvariables",2); 39nm = neldermead_configure(nm,"-x0",[1.0 1.0]'); 40nm = neldermead_configure(nm,"-method","fixed"); 41nm = neldermead_configure(nm,"-function",squarefun); 42// Set the relative size to zero: it should never stop... 43nm = neldermead_configure(nm,"-tolsimplexizerelative",0.0); 44nm = neldermead_search(nm, "off"); 45xopt = neldermead_get(nm,"-xopt"); 46assert_checkalmostequal ( xopt , [0.0;0.0], 1e-6 ); 47fopt = neldermead_get(nm,"-fopt"); 48assert_checkalmostequal ( fopt , 0.0, 1e-6 ); 49nm = neldermead_destroy(nm); 50// 51// Check verbose mode on a few iterations. 52 53nm = neldermead_new (); 54nm = neldermead_configure(nm,"-numberofvariables",2); 55nm = neldermead_configure(nm,"-x0",[1.0 1.0]'); 56nm = neldermead_configure(nm,"-method","fixed"); 57nm = neldermead_configure(nm,"-function",squarefun); 58nm = neldermead_configure(nm,"-maxiter",5); 59nm = neldermead_configure(nm,"-verbose",1); 60// Set the relative size to zero: it should never stop... 61nm = neldermead_configure(nm,"-tolsimplexizerelative",0.0); 62nm = neldermead_search(nm, "off"); 63xopt = neldermead_get(nm,"-xopt"); 64assert_checkalmostequal ( xopt , [0.0;0.0], 1e-6 ); 65fopt = neldermead_get(nm,"-fopt"); 66assert_checkalmostequal ( fopt , 0.0, 1e-6 ); 67nm = neldermead_destroy(nm); 68 69function [ y , index ] = rosenbrock ( x , index ) 70 y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2; 71endfunction 72 73nm = neldermead_new (); 74nm = neldermead_configure(nm,"-numberofvariables",2); 75nm = neldermead_configure(nm,"-function",rosenbrock); 76nm = neldermead_configure(nm,"-x0",[-1.2 1.0]'); 77nm = neldermead_configure(nm,"-maxiter",200); 78nm = neldermead_configure(nm,"-maxfunevals",200); 79nm = neldermead_configure(nm,"-tolfunrelative",10*%eps); 80nm = neldermead_configure(nm,"-tolxrelative",10*%eps); 81nm = neldermead_configure(nm,"-simplex0method","axes"); 82nm = neldermead_configure(nm,"-simplex0length",1.0); 83nm = neldermead_configure(nm,"-method","fixed"); 84nm = neldermead_search(nm, "off"); 85// With fixed-size simplices, one cannot lead the 86// simplex to the optimum. 87// Check optimum point 88xopt = neldermead_get(nm,"-xopt"); 89assert_checkalmostequal ( xopt , [1.0;1.0], 1e1 ); 90// Check optimum point value 91fopt = neldermead_get(nm,"-fopt"); 92assert_checkalmostequal ( fopt , 0.0 , 1e1 ); 93// Check status 94status = neldermead_get(nm,"-status"); 95assert_checkequal ( status , "maxfuneval" ); 96// Cleanup 97nm = neldermead_destroy(nm); 98 99// Check that the verbose mode is functional 100// Few iterations are necessary to check this 101// Many iterations costs a lot more in time. 102nm = neldermead_new (); 103nm = neldermead_configure(nm,"-numberofvariables",2); 104nm = neldermead_configure(nm,"-function",rosenbrock); 105nm = neldermead_configure(nm,"-x0",[-1.2 1.0]'); 106nm = neldermead_configure(nm,"-maxiter",5); 107nm = neldermead_configure(nm,"-maxfunevals",200); 108nm = neldermead_configure(nm,"-tolfunrelative",10*%eps); 109nm = neldermead_configure(nm,"-tolxrelative",10*%eps); 110nm = neldermead_configure(nm,"-simplex0method","axes"); 111nm = neldermead_configure(nm,"-simplex0length",1.0); 112nm = neldermead_configure(nm,"-method","fixed"); 113nm = neldermead_configure(nm,"-verbose",1); 114nm = neldermead_configure(nm,"-verbosetermination",0); 115nm = neldermead_search(nm, "off"); 116status = neldermead_get(nm,"-status"); 117assert_checkequal ( status , "maxiter" ); 118nm = neldermead_destroy(nm); 119 120