]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/test_nelder_mead_min_1.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / test_nelder_mead_min_1.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' works, by making it minimize a
17 ## quadratic function.
18
19 ok = 1;
20 cnt = 1;
21
22 if ! exist ("verbose"), verbose = 0; end
23 if verbose, printf ("  test_nelder_mead : \n"); end
24
25 if ! exist ("inspect"), inspect = 0; end
26
27 tol = 100*sqrt (eps);
28
29 R = 3 ;
30 C = 2;
31
32 if verbose,
33   printf ("  optimization problem has dimension %i\n",R*C);
34 end
35
36 function c = my_quad_func (x,y,z)
37   c = 1 + sum (vec(x-y)'*z*(vec(x-y)));
38 end
39
40 function c = non_quad_func_1 (x,y,z)
41   tmp = sum (vec(x-y)'*z*(vec(x-y)));
42   c = 1 + 1.1*tmp + sin (sqrt(tmp));
43 end
44
45 function c = non_quad_func_2 (x,y,z)
46   tmp1 = sum (vec(x-y)'*z*(vec(x-y)));
47   tmp2 = max (abs (vec(x-y)))^2;
48   c = 1 + 1.1*tmp1 + tmp2 ;
49 end
50
51 ##       dt = mytic()
52 ##
53 ## Returns the cputime since last call to 'mytic'.
54
55 function dt = mytic()
56    persistent last_mytic = 0 ;
57    [t,u,s] = cputime() ;
58    dt = t - last_mytic ;
59    last_mytic = t ;
60 endfunction
61
62 fnames = { "my_quad_func", "non_quad_func_1", "non_quad_func_2"};
63
64 x0 = randn(R,C) ;
65 x1 = x0 + randn(R,C) ;
66 z = randn (R*C); z = z*z';
67
68 for i = 1:length (fnames)
69   fname = fnames{i};
70   if verbose, 
71     printf ("trying to minimize '%s'\n", fname);
72   end
73   ctl = nan*zeros (1,6);
74
75   mytic ();
76   [x2,v,nf] = nelder_mead_min (fname, {x1,x0,z}, ctl) ;
77   t0 = mytic ();
78
79   if any (abs (x2-x0)(:) > 100*tol),
80     if verbose || inspect, printf ("not ok %i\n",cnt); end
81     [max(abs (x2-x0)(:)), 100*tol]
82     if inspect, keyboard; end
83     ok = 0 ;
84   else 
85     if verbose, 
86       printf ("ok %i\n  function evaluations = %i\n",cnt,nf); 
87     end
88   end
89   cnt++;
90
91                                 # Use vanilla nelder_mead_min
92   mytic ();
93   [x2,v,nf] = nelder_mead_min (fname, {x1,x0,z}) ;
94   t1 = mytic ();
95
96   if any (abs (x2-x0)(:) > 100*tol),
97     if verbose || inspect, printf ("not ok %i\n",cnt); end
98     [max(abs (x2-x0)(:)), 100*tol]
99     if inspect, keyboard; end
100     ok = 0 ;
101   else 
102     if verbose, 
103       printf ("ok %i\n  function evaluations = %i\n",cnt,nf); 
104     end
105   end
106   cnt++;
107
108
109                                 # Optimize wrt 2nd arg. 
110   ctl = nan * zeros (1,6);
111   ctl(6) = 0;
112   ctl(3) = 2;
113
114   mytic ();
115   [x2,v,nf] = nelder_mead_min (fname, {x1,x0,z}, ctl) ;
116   t0 = mytic ();
117
118   if any (abs (x2-x1)(:) > 100*tol),
119     if verbose || inspect, printf ("not ok %i\n",cnt); end
120     [max(abs (x2-x0)(:)), 100*tol]
121     if inspect, keyboard; end
122     ok = 0 ;
123   else 
124     if verbose, 
125       printf ("ok %i\n  function evaluations = %i\n",cnt,nf); 
126     end
127   end
128   cnt++;
129
130                                 # Optimize wrt 2nd arg. 
131   ctl = nan * zeros (1,6);
132   ctl(3) = 2;
133
134   mytic ();
135   [x2,v,nf] = nelder_mead_min (fname, {x1,x0,z}, ctl) ;
136   t1 = mytic ();
137
138   if any (abs (x2-x1)(:) > tol),
139     if verbose || inspect, printf ("not ok %i\n",cnt); end
140     [max(abs (x2-x0)(:)), 100*tol]
141     if inspect, keyboard; end
142     ok = 0 ;
143   else 
144     if verbose, 
145       printf ("ok %i\n  function evaluations = %i\n",cnt,nf); 
146     end
147   end
148   cnt++;
149   if 0
150                                 # Check with struct control variable
151     ctls = struct ("narg", 2);
152     [x2bis,vbis,nfbis] = nelder_mead_min (fname, {x1,x0,z}, ctls) ;
153     t1 = mytic ();
154     ## [nf,nfbis]
155     if any ((x2-x2bis)(:))
156       if verbose || inspect, printf ("not ok %i\n",cnt); end
157       printf ("  struct ctl : x2 - x2bis -> %g\n", max(abs (x2-x2bis)(:)));
158       if inspect, keyboard; end
159       ok = 0 ;
160     else 
161       if verbose, 
162         printf ("ok %i\n  function evaluations = %i\n",cnt,nfbis); 
163       end
164     end
165     cnt++;
166     
167                                 # Check with named args
168     [x2bis,vbis,nfbis] = nelder_mead_min (fname, {x1,x0,z}, "narg", 2) ;
169     t1 = mytic ();
170     ## [nf,nfbis]
171     if any ((x2-x2bis)(:))
172       if verbose || inspect, printf ("not ok %i\n",cnt); end
173       printf ("  named arg  : x2 - x2bis -> %g\n", max(abs (x2-x2bis)(:)));
174       if inspect, keyboard; end
175       ok = 0 ;
176     else 
177       if verbose, 
178         printf ("ok %i\n  function evaluations = %i\n",cnt,nfbis); 
179       end
180     end
181     cnt++;
182   end
183 end
184
185 if verbose && ok
186   printf ("All tests ok\n");
187 end
188
189