/usr/share/octave/packages/optim-1.4.0/test_nelder_mead_min_1.m is in octave-optim 1.4.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.
## Checks wether the function 'nelder_mead_min' works, by making it minimize a
## quadratic function.
ok = 1;
cnt = 1;
if ! exist ("verbose"), verbose = 0; end
if verbose, printf (" test_nelder_mead : \n"); end
if ! exist ("inspect"), inspect = 0; end
tol = 100*sqrt (eps);
R = 3 ;
C = 2;
if verbose,
printf (" optimization problem has dimension %i\n",R*C);
end
function c = my_quad_func (x,y,z)
c = 1 + sum (vec(x-y)'*z*(vec(x-y)));
end
function c = non_quad_func_1 (x,y,z)
tmp = sum (vec(x-y)'*z*(vec(x-y)));
c = 1 + 1.1*tmp + sin (sqrt(tmp));
end
function c = non_quad_func_2 (x,y,z)
tmp1 = sum (vec(x-y)'*z*(vec(x-y)));
tmp2 = max (abs (vec(x-y)))^2;
c = 1 + 1.1*tmp1 + tmp2 ;
end
## dt = mytic()
##
## Returns the cputime since last call to 'mytic'.
function dt = mytic()
persistent last_mytic = 0 ;
[t,u,s] = cputime() ;
dt = t - last_mytic ;
last_mytic = t ;
endfunction
fnames = { "my_quad_func", "non_quad_func_1", "non_quad_func_2"};
x0 = randn(R,C) ;
x1 = x0 + randn(R,C) ;
z = randn (R*C); z = z*z';
for i = 1:length (fnames)
fname = fnames{i};
if verbose,
printf ("trying to minimize '%s'\n", fname);
end
ctl = nan*zeros (1,6);
mytic ();
[x2,v,nf] = nelder_mead_min (fname, {x1,x0,z}, ctl) ;
t0 = mytic ();
if any (abs (x2-x0)(:) > 100*tol),
if verbose || inspect, printf ("not ok %i\n",cnt); end
[max(abs (x2-x0)(:)), 100*tol]
if inspect, keyboard; end
ok = 0 ;
else
if verbose,
printf ("ok %i\n function evaluations = %i\n",cnt,nf);
end
end
cnt++;
# Use vanilla nelder_mead_min
mytic ();
[x2,v,nf] = nelder_mead_min (fname, {x1,x0,z}) ;
t1 = mytic ();
if any (abs (x2-x0)(:) > 100*tol),
if verbose || inspect, printf ("not ok %i\n",cnt); end
[max(abs (x2-x0)(:)), 100*tol]
if inspect, keyboard; end
ok = 0 ;
else
if verbose,
printf ("ok %i\n function evaluations = %i\n",cnt,nf);
end
end
cnt++;
# Optimize wrt 2nd arg.
ctl = nan * zeros (1,6);
ctl(6) = 0;
ctl(3) = 2;
mytic ();
[x2,v,nf] = nelder_mead_min (fname, {x1,x0,z}, ctl) ;
t0 = mytic ();
if any (abs (x2-x1)(:) > 100*tol),
if verbose || inspect, printf ("not ok %i\n",cnt); end
[max(abs (x2-x0)(:)), 100*tol]
if inspect, keyboard; end
ok = 0 ;
else
if verbose,
printf ("ok %i\n function evaluations = %i\n",cnt,nf);
end
end
cnt++;
# Optimize wrt 2nd arg.
ctl = nan * zeros (1,6);
ctl(3) = 2;
mytic ();
[x2,v,nf] = nelder_mead_min (fname, {x1,x0,z}, ctl) ;
t1 = mytic ();
if any (abs (x2-x1)(:) > tol),
if verbose || inspect, printf ("not ok %i\n",cnt); end
[max(abs (x2-x0)(:)), 100*tol]
if inspect, keyboard; end
ok = 0 ;
else
if verbose,
printf ("ok %i\n function evaluations = %i\n",cnt,nf);
end
end
cnt++;
if 0
# Check with struct control variable
ctls = struct ("narg", 2);
[x2bis,vbis,nfbis] = nelder_mead_min (fname, {x1,x0,z}, ctls) ;
t1 = mytic ();
## [nf,nfbis]
if any ((x2-x2bis)(:))
if verbose || inspect, printf ("not ok %i\n",cnt); end
printf (" struct ctl : x2 - x2bis -> %g\n", max(abs (x2-x2bis)(:)));
if inspect, keyboard; end
ok = 0 ;
else
if verbose,
printf ("ok %i\n function evaluations = %i\n",cnt,nfbis);
end
end
cnt++;
# Check with named args
[x2bis,vbis,nfbis] = nelder_mead_min (fname, {x1,x0,z}, "narg", 2) ;
t1 = mytic ();
## [nf,nfbis]
if any ((x2-x2bis)(:))
if verbose || inspect, printf ("not ok %i\n",cnt); end
printf (" named arg : x2 - x2bis -> %g\n", max(abs (x2-x2bis)(:)));
if inspect, keyboard; end
ok = 0 ;
else
if verbose,
printf ("ok %i\n function evaluations = %i\n",cnt,nfbis);
end
end
cnt++;
end
end
if verbose && ok
printf ("All tests ok\n");
end
|