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// <-- CLI SHELL MODE -->
14// <-- ENGLISH IMPOSED -->
15function [ y , index ] = rosenbrock ( x , index )
16  y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
17endfunction
18//
19// Test with default NM
20//
21nm = neldermead_new ();
22nm = neldermead_configure(nm,"-numberofvariables",2);
23nm = neldermead_configure(nm,"-function",rosenbrock);
24nm = neldermead_configure(nm,"-x0",[-1.2 1.0].');
25nm = neldermead_search(nm);
26optimbase_terminate: Exiting: Maximum number of function evaluations has been exceeded
27          - increase MaxFunEvals option.
28nm = neldermead_restart(nm);
29optimbase_terminate: Exiting: Maximum number of function evaluations has been exceeded
30          - increase MaxFunEvals option.
31nm = neldermead_destroy(nm);
32//
33// Test with and maximum number of iterations reached
34//
35nm = neldermead_new ();
36nm = neldermead_configure(nm,"-numberofvariables",2);
37nm = neldermead_configure(nm,"-function",rosenbrock);
38nm = neldermead_configure(nm,"-x0",[-1.2 1.0].');
39nm = neldermead_configure(nm,"-maxiter",10);
40nm = neldermead_search(nm);
41optimbase_terminate: Exiting: Maximum number of iterations has been exceeded
42         - increase MaxIter option.
43nm = neldermead_configure(nm,"-maxiter",100);
44nm = neldermead_restart(nm);
45optimbase_terminate: Exiting: Maximum number of function evaluations has been exceeded
46          - increase MaxFunEvals option.
47assert_checktrue(neldermead_get(nm,"-iterations")>10);
48nm = neldermead_destroy(nm);
49function [ f , index ] = objfun ( x , index )
50  f = exp(x(1))*(4*x(1)^2+2*x(2)^2+x(1)*x(2)+2*x(2));
51endfunction
52xopt = [4/31;-33/62];
53fopt = -%e^(4/31)/2;
54//
55// Test with "difficult case"
56//
57nm = neldermead_new ();
58nm = neldermead_configure(nm,"-numberofvariables",2);
59nm = neldermead_configure(nm,"-function",objfun);
60nm = neldermead_configure(nm,"-x0",[-1 1].');
61nm = neldermead_search(nm);
62optimbase_terminate: Exiting: Maximum number of function evaluations has been exceeded
63          - increase MaxFunEvals option.
64nm = neldermead_configure(nm,"-maxfunevals",200);
65nm = neldermead_restart(nm);
66xc = neldermead_get(nm,"-xopt");
67fc = neldermead_get(nm,"-fopt");
68assert_checkalmostequal(xc,xopt,1.e-7);
69assert_checkalmostequal(fc,fopt,1.e-15);
70nm = neldermead_destroy(nm);
71