]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/test_nelder_mead_min_2.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / test_nelder_mead_min_2.m
1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## Checks wether the function 'nelder_mead_min' accepts options properly
17
18 ok = 1;
19 cnt = 1;
20
21 if ! exist ("verbose"), verbose = 0; end
22 if ! exist ("inspect"), inspect = 0; end
23
24 if verbose,
25   printf (["test_nelder_mead_2\n",\
26            "  Check whether nelder_mead_min accepts options properly\n\n"]);
27 end
28
29 N = 2;
30 x1 = zeros (1,N);
31 small = 1e-3;
32 vol = (small^N) / factorial (N);
33
34 ## Define simple 2D function : [x,y] -> x^2, start from [0,0]
35 ## 
36
37 function c = my_func (x)
38   c = x(1)^2;
39 end
40
41 ######################################################################
42 ## Test using volume #################################################
43
44 ## Choose vtol and initial simplex so that algo should stop immediately.
45 ctl = struct ("verbose",verbose, "isz",small, "vtol",vol*1.01, "rst",0);
46
47 [x2,v,nev] = nelder_mead_min ("my_func", x1, ctl);
48
49 if nev != N+1
50   if verbose || inspect, printf ("not ok %i\n",cnt); end
51   if inspect, keyboard; end
52   ok = 0 ;
53 else 
54   if verbose, 
55     printf ("ok %i\n",cnt); 
56   end
57 end
58 cnt++;
59
60 ## Choose vtol and initial simplex so that algo should stop after one
61 ## iteration (should be a reflexion and a tentative extension). Total is 5
62 ## evaluations. 
63 ctl = struct ("verbose",verbose, "isz",small, "vtol",vol*0.99, "rst",0);
64
65 x1 = [0,0];
66
67 [x2,v,nev] = nelder_mead_min ("my_func", x1, ctl);
68
69 if nev != N+3
70   if verbose || inspect, printf ("not ok %i\n",cnt); end
71   if inspect, keyboard; end
72   ok = 0 ;
73 else 
74   if verbose, 
75     printf ("ok %i\n",cnt);
76   end
77 end
78 cnt++;
79
80 ######################################################################
81 ## Test using radius #################################################
82
83 ## Choose rtol and initial simplex so that algo stops immediately.
84 ctl = struct ("verbose",verbose, "isz",small, "rtol",small*2.01, "rst",0);
85
86 [x2,v,nev] = nelder_mead_min ("my_func", x1, ctl);
87
88 if nev != N+1
89   if verbose || inspect, printf ("not ok %i\n",cnt); end
90   if inspect, keyboard; end
91   ok = 0 ;
92 else 
93   if verbose, 
94     printf ("ok %i\n",cnt); 
95   end
96 end
97 cnt++;
98
99 ## Choose rtol and initial simplex so that algo does not stop immediately.
100 ctl = struct ("verbose",verbose, "isz",small, "rtol",small*1.99, "rst",0);
101
102 [x2,v,nev] = nelder_mead_min ("my_func", x1, ctl);
103
104 if nev <= N+1
105   if verbose || inspect, printf ("not ok %i\n",cnt); end
106   if inspect, keyboard; end
107   ok = 0 ;
108 else 
109   if verbose, 
110     printf ("ok %i\n",cnt); 
111   end
112 end
113 cnt++;
114
115 ######################################################################
116 ## Test using values #################################################
117
118 ## Choose rtol and initial simplex so that algo should stop immediately.
119 ctl = struct ("verbose",verbose, "isz",small, "ftol",1.01*small^2, "rst",0);
120
121 [x2,v,nev] = nelder_mead_min ("my_func", x1, ctl);
122
123 if nev != N+1
124   if verbose || inspect, printf ("not ok %i\n",cnt); end
125   if inspect, keyboard; end
126   ok = 0 ;
127 else 
128   if verbose, 
129     printf ("ok %i\n",cnt); 
130   end
131 end
132 cnt++;
133
134 ## Choose rtol and initial simplex so that algo does not stop immediately.
135 ctl = struct ("verbose",verbose, "isz",small, "ftol",0.99*small^2, "rst",0);
136
137 [x2,v,nev] = nelder_mead_min ("my_func", x1, ctl);
138
139 if nev <= N+1
140   if verbose || inspect, printf ("not ok %i\n",cnt); end
141   if inspect, keyboard; end
142   ok = 0 ;
143 else 
144   if verbose
145     printf ("ok %i\n",cnt); 
146   end
147 end
148 cnt++;
149
150 cnt--;
151 if verbose && ok
152   printf ("All %i tests ok\n", cnt);
153 end