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 16 17function y = rosenbrock ( x ) 18 y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2; 19endfunction 20// 21// Test basic use without parameters 22// 23[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] ); 24assert_checkalmostequal ( x , [1.0 1.0], 1e-4 ); 25assert_checkalmostequal ( fval , 0.0 , [] , 1e-4 ); 26assert_checkequal ( exitflag , 1 ); 27assert_checkequal ( output.iterations , 85 ); 28assert_checkequal ( output.algorithm , "Nelder-Mead simplex direct search" ); 29assert_checkequal ( output.funcCount , 159 ); 30assert_checkequal ( output.message(1) , "Optimization terminated:"); 31assert_checkequal ( output.message(2) , " the current x satisfies the termination criteria using OPTIONS.TolX of 0.0001"); 32assert_checkequal ( output.message(3) , " and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 0.0001"); 33// 34// fminsearch with incorrect number of input arguments 35// 36cmd = "fminsearch ( )"; 37assert_checkerror(cmd,"%s: Wrong number of input arguments: %d or %d expected.\n",[], "fminsearch",2,3); 38// 39// Check that tolerance on X is correctly taken into account 40// 41opt = optimset ( "TolX" , 1.e-2 ); 42[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 43assert_checkalmostequal ( x , [1.0 1.0], 1.e-2 ); 44assert_checkalmostequal ( fval , 0.0 , [] , 1e-4 ); 45assert_checkequal ( exitflag , 1 ); 46assert_checkequal ( output.iterations , 70 ); 47assert_checkequal ( output.funcCount , 130 ); 48// 49// Check that tolerance on F is correctly taken into account 50// 51opt = optimset ( "TolFun" , 1.e-10 ); 52[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 53assert_checkalmostequal ( x , [1.0 1.0], 1.e-4 ); 54assert_checkalmostequal ( fval , 0.0 , [] , 1e-2 ); 55assert_checkequal ( exitflag , 1 ); 56assert_checkequal ( output.iterations , 90 ); 57assert_checkequal ( output.funcCount , 168 ); 58// 59// Check that maximum number of iterations is correctly taken into account 60// 61opt = optimset ( "MaxIter" , 10 ); 62[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 63assert_checkalmostequal ( x , [1.0 1.0], 1.e1 ); 64assert_checkalmostequal ( fval , 0.0 , [] , 1e1 ); 65assert_checkequal ( exitflag , 0 ); 66assert_checkequal ( output.iterations , 10 ); 67assert_checkequal ( output.funcCount , 21 ); 68// 69// Check that maximum number of function evaluations is correctly taken into account 70// 71opt = optimset ( "MaxFunEvals" , 10 ); 72[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 73assert_checkalmostequal ( x , [1.0 1.0], 1.e1 ); 74assert_checkalmostequal ( fval , 0.0 , [] , 1e1 ); 75assert_checkequal ( exitflag , 0 ); 76assert_checkequal ( output.iterations , 5 ); 77assert_checkequal ( output.funcCount , 11 ); 78// 79// Check that Display is correctly used in mode "final" 80// 81opt = optimset ( "Display" , "final" ); 82[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 83assert_checkalmostequal ( x , [1.0 1.0], 1.e-4 ); 84assert_checkalmostequal ( fval , 0.0 , [] , 1.e-4 ); 85assert_checkequal ( exitflag , 1 ); 86assert_checkequal ( output.iterations , 85 ); 87assert_checkequal ( output.funcCount , 159 ); 88// 89// Check that Display is correctly used in mode "iter" 90// 91opt = optimset ( "Display" , "iter" ); 92[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 93assert_checkalmostequal ( x , [1.0 1.0], 1.e-4 ); 94assert_checkalmostequal ( fval , 0.0 , [] , 1.e-4 ); 95assert_checkequal ( exitflag , 1 ); 96assert_checkequal ( output.iterations , 85 ); 97assert_checkequal ( output.funcCount , 159 ); 98// 99// Check that Display is correctly used in mode "off" (no message at all) 100// 101opt = optimset ( "Display" , "off" ); 102[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 103assert_checkalmostequal ( x , [1.0 1.0], 1.e-4 ); 104assert_checkalmostequal ( fval , 0.0 , [] , 1.e-4 ); 105assert_checkequal ( exitflag , 1 ); 106assert_checkequal ( output.iterations , 85 ); 107assert_checkequal ( output.funcCount , 159 ); 108// 109// Check that Display is correctly used in mode "notify" (display only problem messages) 110// 111opt = optimset ( "Display" , "notify" ); 112[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 113assert_checkalmostequal ( x , [1.0 1.0], 1.e-4 ); 114assert_checkalmostequal ( fval , 0.0 , [] , 1.e-4 ); 115assert_checkequal ( exitflag , 1 ); 116assert_checkequal ( output.iterations , 85 ); 117assert_checkequal ( output.funcCount , 159 ); 118// 119// Check that Display is correctly used in mode "off" (no message at all), when there is a maximum number of iterations reached 120// 121opt = optimset ( "Display" , "off" , "MaxIter" , 10 ); 122[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 123assert_checkalmostequal ( x , [1.0 1.0], 1.e1 ); 124assert_checkalmostequal ( fval , 0.0 , [] , 1e1 ); 125assert_checkequal ( exitflag , 0 ); 126assert_checkequal ( output.iterations , 10 ); 127assert_checkequal ( output.funcCount , 21 ); 128// 129// Check that Display is correctly used in mode "notify" (display only problem messages), when there is a maximum number of iterations reached 130// 131opt = optimset ( "Display" , "notify" , "MaxIter" , 10 ); 132[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 133assert_checkalmostequal ( x , [1.0 1.0], 1.e1 ); 134assert_checkalmostequal ( fval , 0.0 , [] , 1e1 ); 135assert_checkequal ( exitflag , 0 ); 136assert_checkequal ( output.iterations , 10 ); 137assert_checkequal ( output.funcCount , 21 ); 138// 139// Check that Display is correctly used in mode "iter", when there is a maximum number of iterations reached 140// 141opt = optimset ( "Display" , "iter" , "MaxIter" , 10 ); 142[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 143assert_checkalmostequal ( x , [1.0 1.0], 1.e1 ); 144assert_checkalmostequal ( fval , 0.0 , [] , 1e1 ); 145assert_checkequal ( exitflag , 0 ); 146assert_checkequal ( output.iterations , 10 ); 147assert_checkequal ( output.funcCount , 21 ); 148// 149// Check that Display is correctly used in mode "final", when there is a maximum number of iterations reached 150// 151opt = optimset ( "Display" , "final" , "MaxIter" , 10 ); 152[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 153assert_checkalmostequal ( x , [1.0 1.0], 1.e1 ); 154assert_checkalmostequal ( fval , 0.0 , [] , 1e1 ); 155assert_checkequal ( exitflag , 0 ); 156assert_checkequal ( output.iterations , 10 ); 157assert_checkequal ( output.funcCount , 21 ); 158 159// 160// Test basic use with column x0 161// 162[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1].' ); 163assert_checkalmostequal ( x , [1.0 1.0], 1e-4 ); 164 165// 166// Test extra arguments 167function y = bananaext (x,a,b) 168 y = a*(x(2)-x(1)^2)^2 + (b-x(1))^2; 169endfunction 170a = 100; 171b = 1; 172[x fval] = fminsearch ( list(bananaext,a,b) , [-1.2 1] ); 173assert_checkalmostequal ( x , [1.0 1.0], 1e-4 ); 174assert_checkalmostequal ( fval , 0, [], 1e-5 ); 175// 176a = 100; 177b = 12; 178[x fval] = fminsearch ( list(bananaext,a,b) , [10 100] ); 179assert_checkalmostequal ( x , [12 144], 1e-4 ); 180assert_checkalmostequal ( fval , 0, [], 1e-5 ); 181