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 on Rosenbrock test case
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_configure(nm,"-maxiter",200);
26nm = neldermead_configure(nm,"-maxfunevals",300);
27nm = neldermead_configure(nm,"-tolfunrelative",10*%eps);
28nm = neldermead_configure(nm,"-tolxrelative",10*%eps);
29nm = neldermead_configure(nm,"-tolsimplexizerelative",1.e-6);
30nm = neldermead_configure(nm,"-simplex0method","axes");
31nm = neldermead_configure(nm,"-simplex0length",1.0);
32nm = neldermead_configure(nm,"-method","variable");
33nm = neldermead_configure(nm,"-storehistory",%t);
34nm = neldermead_search(nm);
35// Check optimum point
36xopt = neldermead_get(nm,"-xopt");
37assert_checkalmostequal ( xopt , [1.0;1.0], 1e-6 );
38// Check optimum point value
39fopt = neldermead_get(nm,"-fopt");
40assert_checkalmostequal ( fopt , 0.0 , [] , 1e-6 );
41// Check function value history
42historyfopt = neldermead_get(nm,"-historyfopt");
43iterations = neldermead_get(nm,"-iterations");
44sizefopt = size(historyfopt);
45assert_checkequal ( [iterations 1], sizefopt );
46// Check number of function evaluations
47funevals = neldermead_get(nm,"-funevals");
48assert_checkequal ( (funevals < 300) , %T )
49 ans  =
50  T
51// Check optimum history
52historyxopt = neldermead_get(nm,"-historyxopt");
53sizexopt = size(historyxopt);
54assert_checkequal ( iterations, sizexopt );
55// Check simplex history
56historysimplex = neldermead_get(nm,"-historysimplex");
57sizexsimplex = size(historysimplex);
58assert_checkequal ( [iterations 3 2], sizexsimplex );
59// Check fx0
60fx0 = neldermead_get(nm,"-fx0");
61assert_checkalmostequal ( fx0 , 24.2 , 10*%eps );
62// Check status
63status = neldermead_get(nm,"-status");
64assert_checkequal ( status , "tolsize" );
65// Cleanup
66nm = neldermead_destroy(nm);
67// Check that the verbose mode is functional
68// Few iterations are necessary to check this
69// Many iterations costs a lot more in time.
70nm = neldermead_new ();
71nm = neldermead_configure(nm,"-numberofvariables",2);
72nm = neldermead_configure(nm,"-function",rosenbrock);
73nm = neldermead_configure(nm,"-x0",[-1.2 1.0]');
74nm = neldermead_configure(nm,"-maxiter",5);
75nm = neldermead_configure(nm,"-maxfunevals",300);
76nm = neldermead_configure(nm,"-tolfunrelative",10*%eps);
77nm = neldermead_configure(nm,"-tolxrelative",10*%eps);
78nm = neldermead_configure(nm,"-tolsimplexizerelative",1.e-6);
79nm = neldermead_configure(nm,"-simplex0method","axes");
80nm = neldermead_configure(nm,"-simplex0length",1.0);
81nm = neldermead_configure(nm,"-method","variable");
82nm = neldermead_configure(nm,"-verbose",1);
83nm = neldermead_configure(nm,"-verbosetermination",1);
84nm = neldermead_configure(nm,"-storehistory",%t);
85nm = neldermead_search(nm, "off");
86Function Evaluation #1, index=1, x= [-1.2 1]
87Function Evaluation #2, index=2, x= [-1.2 1]
88Function Evaluation #3, index=2, x= [-1.2 1]
89Function Evaluation #4, index=2, x= [-0.2 1]
90Function Evaluation #5, index=2, x= [-1.2 2]
91Step #1 : order
92=================================================================
93Iteration #1 (total = 1)
94Function Eval #5
95Xopt : [-1.2 1]
96Fopt : 24.2
97DeltaFv : 69.4
98Center : [-0.8666667 1.3333333]
99Size : 1
100Optim Simplex Object:
101=====================
102nbve: 3
103n: 2
104x: 3-by-2 matrix
105fv: 3-by-1 matrix
106Reflect
107xbar=-1.2 1.5
108Function Evaluation #6, index=2, x= [-2.2 2]
109xr=[-2.2 2], f(xr)=816.800000
110Contract - inside
111Function Evaluation #7, index=2, x= [-0.7 1.25]
112xc=-0.7 1.25, f(xc)=60.650000
113  > Perform Inside Contraction
114Sort
115=================================================================
116Iteration #2 (total = 2)
117Function Eval #7
118Xopt : [-1.2 1]
119Fopt : 24.2
120DeltaFv : 36.45
121Center : [-1.0333333 1.4166667]
122Size : 1
123Optim Simplex Object:
124=====================
125nbve: 3
126n: 2
127x: 3-by-2 matrix
128fv: 3-by-1 matrix
129  > Termination ?
130  > iterations=2 >= maxiter=5
131  > funevals=7 >= maxfunevals=300
132  > e(x)=0.186339 < 2.220D-15 * 1.7534886 + 0
133  > Terminate = F, status = continue
134  > simplex size=1 < 0 + 0.000001 * 1
135  > Terminate = F, status = continue
136Reflect
137xbar=-1.2 1.5
138Function Evaluation #8, index=2, x= [-1.7 1.75]
139xr=[-1.7 1.75], f(xr)=137.250000
140Contract - inside
141Function Evaluation #9, index=2, x= [-0.95 1.375]
142xc=-0.95 1.375, f(xc)=26.128125
143  > Perform Inside Contraction
144Sort
145=================================================================
146Iteration #3 (total = 3)
147Function Eval #9
148Xopt : [-1.2 1]
149Fopt : 24.2
150DeltaFv : 12
151Center : [-1.1166667 1.4583333]
152Size : 1
153Optim Simplex Object:
154=====================
155nbve: 3
156n: 2
157x: 3-by-2 matrix
158fv: 3-by-1 matrix
159  > Termination ?
160  > iterations=3 >= maxiter=5
161  > funevals=9 >= maxfunevals=300
162  > e(x)=0.0931695 < 2.220D-15 * 1.8367582 + 0
163  > Terminate = F, status = continue
164  > simplex size=1 < 0 + 0.000001 * 1
165  > Terminate = F, status = continue
166Reflect
167xbar=-1.075 1.1875
168Function Evaluation #10, index=2, x= [-0.95 0.375]
169xr=[-0.95 0.375], f(xr)=31.628125
170Contract - outside
171Function Evaluation #11, index=2, x= [-1.0125 0.78125]
172xc=-1.0125 0.78125, f(xc)=9.999182
173  > Perform Outside Contraction
174Sort
175=================================================================
176Iteration #4 (total = 4)
177Function Eval #11
178Xopt : [-1.0125 0.78125]
179Fopt : 9.9991821
180DeltaFv : 16.128943
181Center : [-1.0541667 1.0520833]
182Size : 0.5970304
183Optim Simplex Object:
184=====================
185nbve: 3
186n: 2
187x: 3-by-2 matrix
188fv: 3-by-1 matrix
189  > Termination ?
190  > iterations=4 >= maxiter=5
191  > funevals=11 >= maxfunevals=300
192  > e(x)=0.4110296 < 2.220D-15 * 1.4893444 + 0
193  > Terminate = F, status = continue
194  > simplex size=0.5970304 < 0 + 0.000001 * 1
195  > Terminate = F, status = continue
196Reflect
197xbar=-1.10625 0.890625
198Function Evaluation #12, index=2, x= [-1.2625 0.40625]
199xr=[-1.2625 0.40625], f(xr)=146.171643
200Contract - inside
201Function Evaluation #13, index=2, x= [-1.028125 1.1328125]
202xc=-1.028125 1.1328125, f(xc)=4.687423
203  > Perform Inside Contraction
204Sort
205=================================================================
206Iteration #5 (total = 5)
207Function Eval #13
208Xopt : [-1.028125 1.1328125]
209Fopt : 4.6874228
210DeltaFv : 19.512577
211Center : [-1.0802083 0.9713542]
212Size : 0.3519096
213Optim Simplex Object:
214=====================
215nbve: 3
216n: 2
217x: 3-by-2 matrix
218fv: 3-by-1 matrix
219  > Termination ?
220  > iterations=5 >= maxiter=5
221  > Terminate = T, status = maxiter
222  > Terminate = T, status = maxiter
223Terminate with status : maxiter
224status = neldermead_get(nm,"-status");
225assert_checkequal ( status , "maxiter" );
226nm = neldermead_destroy(nm);
227