1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
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
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
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/>.
16 ## Checks wether the function 'nelder_mead_min' works, by making it minimize a
17 ## quadratic function.
22 if ! exist ("verbose"), verbose = 0; end
23 if verbose, printf (" test_nelder_mead : \n"); end
25 if ! exist ("inspect"), inspect = 0; end
33 printf (" optimization problem has dimension %i\n",R*C);
36 function c = my_quad_func (x,y,z)
37 c = 1 + sum (vec(x-y)'*z*(vec(x-y)));
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));
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 ;
53 ## Returns the cputime since last call to 'mytic'.
56 persistent last_mytic = 0 ;
62 fnames = { "my_quad_func", "non_quad_func_1", "non_quad_func_2"};
65 x1 = x0 + randn(R,C) ;
66 z = randn (R*C); z = z*z';
68 for i = 1:length (fnames)
71 printf ("trying to minimize '%s'\n", fname);
73 ctl = nan*zeros (1,6);
76 [x2,v,nf] = nelder_mead_min (fname, {x1,x0,z}, ctl) ;
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
86 printf ("ok %i\n function evaluations = %i\n",cnt,nf);
91 # Use vanilla nelder_mead_min
93 [x2,v,nf] = nelder_mead_min (fname, {x1,x0,z}) ;
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
103 printf ("ok %i\n function evaluations = %i\n",cnt,nf);
109 # Optimize wrt 2nd arg.
110 ctl = nan * zeros (1,6);
115 [x2,v,nf] = nelder_mead_min (fname, {x1,x0,z}, ctl) ;
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
125 printf ("ok %i\n function evaluations = %i\n",cnt,nf);
130 # Optimize wrt 2nd arg.
131 ctl = nan * zeros (1,6);
135 [x2,v,nf] = nelder_mead_min (fname, {x1,x0,z}, ctl) ;
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
145 printf ("ok %i\n function evaluations = %i\n",cnt,nf);
150 # Check with struct control variable
151 ctls = struct ("narg", 2);
152 [x2bis,vbis,nfbis] = nelder_mead_min (fname, {x1,x0,z}, ctls) ;
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
162 printf ("ok %i\n function evaluations = %i\n",cnt,nfbis);
167 # Check with named args
168 [x2bis,vbis,nfbis] = nelder_mead_min (fname, {x1,x0,z}, "narg", 2) ;
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
178 printf ("ok %i\n function evaluations = %i\n",cnt,nfbis);
186 printf ("All tests ok\n");