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 -->
14function y = rosenbrock ( x )
15  y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
16endfunction
17//
18// Test basic use without parameters
19//
20[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] );
21assert_checkalmostequal ( x , [1.0   1.0], 1e-4 );
22assert_checkalmostequal ( fval , 0.0 , [] , 1e-4 );
23assert_checkequal ( exitflag , 1 );
24assert_checkequal ( output.iterations , 85 );
25assert_checkequal ( output.algorithm , "Nelder-Mead simplex direct search" );
26assert_checkequal ( output.funcCount , 159 );
27assert_checkequal ( output.message(1) , "Optimization terminated:");
28assert_checkequal ( output.message(2) , " the current x satisfies the termination criteria using OPTIONS.TolX of 0.0001");
29assert_checkequal ( output.message(3) , " and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 0.0001");
30//
31// fminsearch with incorrect number of input arguments
32//
33cmd = "fminsearch ( )";
34assert_checkerror(cmd,"%s: Wrong number of input arguments: %d or %d expected.\n",[], "fminsearch",2,3);
35//
36// Check that tolerance on X is correctly taken into account
37//
38opt = optimset ( "TolX" , 1.e-2 );
39[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt );
40assert_checkalmostequal ( x , [1.0 1.0], 1.e-2 );
41assert_checkalmostequal ( fval , 0.0 , [] ,  1e-4 );
42assert_checkequal ( exitflag , 1 );
43assert_checkequal ( output.iterations , 70 );
44assert_checkequal ( output.funcCount , 130 );
45//
46// Check that tolerance on F is correctly taken into account
47//
48opt = optimset ( "TolFun" , 1.e-10 );
49[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt );
50assert_checkalmostequal ( x , [1.0 1.0], 1.e-4 );
51assert_checkalmostequal ( fval , 0.0 , [] ,  1e-2 );
52assert_checkequal ( exitflag , 1 );
53assert_checkequal ( output.iterations , 90 );
54assert_checkequal ( output.funcCount , 168 );
55//
56// Check that maximum number of iterations is correctly taken into account
57//
58opt = optimset ( "MaxIter" , 10 );
59[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt );
60fminsearch: Exiting: Maximum number of iterations has been exceeded
61         - increase MaxIter option.
62         Current function value: 4.1355598
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 );
73fminsearch: Exiting: Maximum number of function evaluations has been exceeded
74          - increase MaxFunEvals option.
75         Current function value: 4.3813601
76assert_checkalmostequal ( x , [1.0 1.0], 1.e1 );
77assert_checkalmostequal ( fval , 0.0 , [] ,  1e1 );
78assert_checkequal ( exitflag , 0 );
79assert_checkequal ( output.iterations , 5 );
80assert_checkequal ( output.funcCount , 11 );
81//
82// Check that Display is correctly used in mode "final"
83//
84opt = optimset ( "Display" , "final" );
85[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt );
86Optimization terminated:
87 the current x satisfies the termination criteria using OPTIONS.TolX of 0.0001
88 and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 0.0001
89assert_checkalmostequal ( x , [1.0 1.0], 1.e-4 );
90assert_checkalmostequal ( fval , 0.0 , [] ,  1.e-4 );
91assert_checkequal ( exitflag , 1 );
92assert_checkequal ( output.iterations , 85 );
93assert_checkequal ( output.funcCount , 159 );
94//
95// Check that Display is correctly used in mode "iter"
96//
97opt = optimset ( "Display" , "iter" );
98[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt );
99 Iteration   Func-count     min f(x)         Procedure
100     0            3             24.2
101     1            3            20.05         initial simplex
102     2            5         5.161796         expand
103     3            7         4.497796         reflect
104     4            9         4.497796         contract outside
105     5           11        4.3813601         contract inside
106     6           13        4.2452728         contract inside
107     7           15        4.2176247         reflect
108     8           17        4.2112906         contract inside
109     9           19        4.1355598         expand
110    10           21        4.1355598         contract inside
111    11           23        4.0127268         expand
112    12           25        3.9373812         expand
113    13           27         3.602606         expand
114    14           28         3.602606         reflect
115    15           30        3.4662211         reflect
116    16           32        3.2160547         expand
117    17           34        3.1649126         reflect
118    18           36        2.7068692         expand
119    19           37        2.7068692         reflect
120    20           39        2.0021824         expand
121    21           41        2.0021824         contract inside
122    22           43        2.0021824         contract inside
123    23           45        1.8154337         expand
124    24           47        1.7348144         contract outside
125    25           49        1.3169723         expand
126    26           50        1.3169723         reflect
127    27           51        1.3169723         reflect
128    28           53        1.1595038         reflect
129    29           55        1.0767387         contract inside
130    30           57        0.8834921         reflect
131    31           59        0.8834921         contract inside
132    32           61        0.6691654         expand
133    33           63        0.6691654         contract inside
134    34           64        0.6691654         reflect
135    35           66        0.5367289         reflect
136    36           68        0.5367289         contract inside
137    37           70         0.423294         expand
138    38           72         0.423294         contract outside
139    39           74        0.3985272         reflect
140    40           76        0.3144704         expand
141    41           77        0.3144704         reflect
142    42           79        0.1903167         expand
143    43           81        0.1903167         contract inside
144    44           82        0.1903167         reflect
145    45           84        0.1369602         reflect
146    46           86        0.1369602         contract outside
147    47           88        0.1131281         contract outside
148    48           90        0.1105304         contract inside
149    49           92        0.1023402         reflect
150    50           94        0.1011837         contract inside
151    51           96        0.0794969         expand
152    52           97        0.0794969         reflect
153    53           98        0.0794969         reflect
154    54          100        0.0569294         expand
155    55          102        0.0569294         contract inside
156    56          104        0.0344855         expand
157    57          106        0.0179534         expand
158    58          108        0.0169469         contract outside
159    59          110        0.0040146         reflect
160    60          112        0.0040146         contract inside
161    61          113        0.0040146         reflect
162    62          115          0.00037         reflect
163    63          117          0.00037         contract inside
164    64          118          0.00037         reflect
165    65          120          0.00037         contract inside
166    66          122         0.000059         contract outside
167    67          124        0.0000337         contract inside
168    68          126        0.0000337         contract outside
169    69          128        0.0000189         contract outside
170    70          130        0.0000085         contract inside
171    71          132        0.0000029         contract inside
172    72          133        0.0000029         reflect
173    73          135        0.0000007         contract inside
174    74          137        0.0000007         contract inside
175    75          139        0.0000006         contract inside
176    76          141        0.0000002         contract outside
177    77          143        0.0000001         contract inside
178    78          145        5.235D-08         contract inside
179    79          147        5.035D-08         contract inside
180    80          149        2.004D-08         contract inside
181    81          151        1.123D-09         contract inside
182    82          153        1.123D-09         contract outside
183    83          155        1.123D-09         contract inside
184    84          157        1.108D-09         contract outside
185    85          159        8.178D-10         contract inside
186Optimization terminated:
187 the current x satisfies the termination criteria using OPTIONS.TolX of 0.0001
188 and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 0.0001
189assert_checkalmostequal ( x , [1.0 1.0], 1.e-4 );
190assert_checkalmostequal ( fval , 0.0 , [] ,  1.e-4 );
191assert_checkequal ( exitflag , 1 );
192assert_checkequal ( output.iterations , 85 );
193assert_checkequal ( output.funcCount , 159 );
194//
195// Check that Display is correctly used in mode "off" (no message at all)
196//
197opt = optimset ( "Display" , "off" );
198[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt );
199assert_checkalmostequal ( x , [1.0 1.0], 1.e-4 );
200assert_checkalmostequal ( fval , 0.0 , [] ,  1.e-4 );
201assert_checkequal ( exitflag , 1 );
202assert_checkequal ( output.iterations , 85 );
203assert_checkequal ( output.funcCount , 159 );
204//
205// Check that Display is correctly used in mode "notify" (display only problem messages)
206//
207opt = optimset ( "Display" , "notify" );
208[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt );
209assert_checkalmostequal ( x , [1.0 1.0], 1.e-4 );
210assert_checkalmostequal ( fval , 0.0 , [] ,  1.e-4 );
211assert_checkequal ( exitflag , 1 );
212assert_checkequal ( output.iterations , 85 );
213assert_checkequal ( output.funcCount , 159 );
214//
215// Check that Display is correctly used in mode "off" (no message at all), when there is a maximum number of iterations reached
216//
217opt = optimset ( "Display" , "off" , "MaxIter" , 10 );
218[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt );
219assert_checkalmostequal ( x , [1.0 1.0], 1.e1 );
220assert_checkalmostequal ( fval , 0.0 , [] ,  1e1 );
221assert_checkequal ( exitflag , 0 );
222assert_checkequal ( output.iterations , 10 );
223assert_checkequal ( output.funcCount , 21 );
224//
225// Check that Display is correctly used in mode "notify" (display only problem messages), when there is a maximum number of iterations reached
226//
227opt = optimset ( "Display" , "notify" , "MaxIter" , 10 );
228[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt );
229fminsearch: Exiting: Maximum number of iterations has been exceeded
230         - increase MaxIter option.
231         Current function value: 4.1355598
232assert_checkalmostequal ( x , [1.0 1.0], 1.e1 );
233assert_checkalmostequal ( fval , 0.0 , [] ,  1e1 );
234assert_checkequal ( exitflag , 0 );
235assert_checkequal ( output.iterations , 10 );
236assert_checkequal ( output.funcCount , 21 );
237//
238// Check that Display is correctly used in mode "iter", when there is a maximum number of iterations reached
239//
240opt = optimset ( "Display" , "iter" , "MaxIter" , 10 );
241[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt );
242 Iteration   Func-count     min f(x)         Procedure
243     0            3             24.2
244     1            3            20.05         initial simplex
245     2            5         5.161796         expand
246     3            7         4.497796         reflect
247     4            9         4.497796         contract outside
248     5           11        4.3813601         contract inside
249     6           13        4.2452728         contract inside
250     7           15        4.2176247         reflect
251     8           17        4.2112906         contract inside
252     9           19        4.1355598         expand
253    10           21        4.1355598         contract inside
254fminsearch: Exiting: Maximum number of iterations has been exceeded
255         - increase MaxIter option.
256         Current function value: 4.1355598
257assert_checkalmostequal ( x , [1.0 1.0], 1.e1 );
258assert_checkalmostequal ( fval , 0.0 , [] ,  1e1 );
259assert_checkequal ( exitflag , 0 );
260assert_checkequal ( output.iterations , 10 );
261assert_checkequal ( output.funcCount , 21 );
262//
263// Check that Display is correctly used in mode "final", when there is a maximum number of iterations reached
264//
265opt = optimset ( "Display" , "final" , "MaxIter" , 10 );
266[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt );
267fminsearch: Exiting: Maximum number of iterations has been exceeded
268         - increase MaxIter option.
269         Current function value: 4.1355598
270assert_checkalmostequal ( x , [1.0 1.0], 1.e1 );
271assert_checkalmostequal ( fval , 0.0 , [] ,  1e1 );
272assert_checkequal ( exitflag , 0 );
273assert_checkequal ( output.iterations , 10 );
274assert_checkequal ( output.funcCount , 21 );
275//
276// Test basic use with column x0
277//
278[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1].' );
279assert_checkalmostequal ( x , [1.0   1.0], 1e-4 );
280//
281// Test extra arguments
282function y = bananaext (x,a,b)
283  y = a*(x(2)-x(1)^2)^2 + (b-x(1))^2;
284endfunction
285a = 100;
286b = 1;
287[x fval] = fminsearch ( list(bananaext,a,b) , [-1.2 1] );
288assert_checkalmostequal ( x , [1.0   1.0], 1e-4 );
289assert_checkalmostequal ( fval , 0, [], 1e-5 );
290//
291a = 100;
292b = 12;
293[x fval] = fminsearch ( list(bananaext,a,b) , [10 100] );
294assert_checkalmostequal ( x , [12  144], 1e-4 );
295assert_checkalmostequal ( fval , 0, [], 1e-5 );
296