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