/usr/share/octave/packages/optim-1.4.0/private/__d2_min__.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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | ## 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/>.
## -*- texinfo -*-
## @deftypefn{Function File} {[@var{p_res}, @var{objf}, @var{cvg}, @var{outp}] =} __d2_min__ (@var{f}, @var{pin}, @var{hook})
## Undocumented internal function.
## @end deftypefn
function [p_res, objf, cvg, outp] = __d2_min__ (f, pin, hook)
### modified by Olaf Till <i7tiol@t-online.de>
n = length (pin);
## constants
maxinner = 30;
tcoeff = 0.5; # Discount on total weight
ncoeff = 0.5; # Discount on weight of newton
ocoeff = 1.5; # Factor for outwards searching
## passed function for gradient of objective function
grad_f = hook.dfdp;
## passed function for hessian of objective function
if (isempty (hess_f = hook.hessian))
error ("this backend requires a supplied Hessian function");
endif
## is it the inverse of the hessian?
inverse_hessian = hook.inverse_hessian;
## passed options
ftol = hook.TolFun;
if (isempty (utol = hook.TolX)) utol = 10 * sqrt (eps); endif
if (isempty (maxout = hook.MaxIter)) maxout = 1000; endif
fixed = hook.fixed;
verbose = strcmp (hook.Display, "iter");
prudent = strcmp (hook.FunValCheck, "on");
user_interaction = hook.user_interaction;
## some useful variables derived from passed variables
n = numel (pin);
## backend-specific checking of options and constraints
if (all (fixed))
error ("no free parameters");
endif
## fill constant fields of hook for derivative-functions; some fields
## may be backend-specific
dfdp_hook.fixed = fixed; # this may be handled by the frontend, but
# the backend still may add to it
## set up for iterations
p = pbest = pin;
vf = fbest = eval_objf (f, pin, prudent);
nobjf = 1;
if (([stop, outp.user_interaction] = ...
__do_user_interaction__ (user_interaction, p,
struct ("iteration", 0,
"fval", vf),
"init")))
p_res = p;
outp.niter = 0;
outp.nobj = nobjf;
objf = vf;
cvg = -1;
return;
endif
for (niter = 1 : maxout)
[grad, hessian] = ...
eval_grad_hessian (grad_f, hess_f, p, n, prudent,
setfield (dfdp_hook, "f", vf));
grad = grad(:);
if (inverse_hessian)
inv_hessian = hessian;
else
inv_hessian = pinv (hessian);
endif
fold = vf;
if (verbose)
printf ("d2_min: niter=%d, objf=%8.3g\n", niter, vf);
endif
dnewton = - inv_hessian * grad; # Newton step
if (dnewton' * grad > 0)
## Heuristic for negative hessian
dnewton = -100 * grad;
endif
wn = 1; # Weight of Newton step
wt = 1; # Total weight
done_inner = false; # false = not found, true = ready to quit inner loop
for (ninner = 1 : maxinner) # inner loop
dp = wt * (wn * dnewton - (1 - wn) * grad);
pnew = p + dp;
if (verbose)
printf ("total weight: %8.3g, newtons weight: %8.3g, objf=%8.3g, newton norm: %8.3g, deriv norm: %8.3g\n",...
wt, wn, fbest, norm (wt * wn * dnewton),
norm (wt * (1 - wn) * d));
endif
fnew = eval_objf (f, pnew, prudent);
nobjf++;
if (fnew < fbest)
dbest = dp;
fbest = fnew;
pbest = pnew;
done_inner = true; # will go out at next increase
if (verbose)
printf ("d2_min: found better value\n");
endif
elseif (done_inner)
if (verbose)
printf ("d2_min: quitting %d th inner loop\n", ninner);
endif
break;
endif
wt *= tcoeff; # reduce norm of proposed step
wn *= ncoeff; # and bring it closer to derivative
endfor # end of inner loop
if (ninner == maxinner)
printf ("d2_min: too many inner loops (objf: %8.3g)\n", fnew);
wbest = 0;
else
## look for improvement along dbest
wbest = 1;
wn = ocoeff;
pnew = p + wn * dbest;
fnew = eval_objf (f, pnew, prudent);
nobjf++;
while (fnew < fbest)
fbest = fnew;
wbest = wn;
pbest = pnew;
wn *= ocoeff;
pnew = p + wn * dbest;
fnew = eval_objf (f, pnew, prudent);
nobjf++;
if (verbose)
printf ("d2_min: looking further: objf: %8.3g\n", fnew);
endif
endwhile
endif
if (verbose)
printf ("d2_min: inner loop: fbest: %8.5g, fold: %8.5g\n",
fbest, fold);
endif
if (fbest < fold)
## improvement found
vf = fbest;
p = pbest;
endif
if (([stop, outp.user_interaction] = ...
__do_user_interaction__ (user_interaction, p,
struct ("iteration", niter,
"fval", vf),
"iter")))
p_res = p;
outp.niter = niter;
outp.nobjf = nobjf;
objf = vf;
cvg = -1;
return;
endif
if (fold - fbest < ((abs (fold) + sqrt (eps)) * abs (ftol)))
if (verbose)
printf ("d2_min: quitting, niter: %-3d, objf: %8.3g, fold: %8.3g\n",
niter, vf, fold);
endif
cvg = 3;
break;
elseif (max (abs (wbest * dbest)) < ...
(max (abs (pbest)) + sqrt (eps)) * abs (utol))
cvg = 2;
break;
elseif (niter == maxout)
cvg = 0
endif
pbest = p;
endfor
## return result
p_res = pbest;
objf = fbest;
outp.niter = niter;
outp.nobjf = nobjf;
if (([stop, outp.user_interaction] = ...
__do_user_interaction__ (user_interaction, p_res,
struct ("iteration", niter,
"fval", objf),
"done")))
cvg = -1;
endif
endfunction
function ret = eval_objf (f, p, prudent)
ret = f (p);
if (prudent && (! isnumeric (ret) || isnan (ret) || numel (ret) > 1))
error ("objective function returns inadequate output");
endif
endfunction
function [grad, hessian] = ...
eval_grad_hessian (grad_f, hess_f, p, n, prudent, hook)
persistent first_call = true;
grad = grad_f (p, hook);
hessian = hess_f (p);
if (first_call)
first_call = false;
if (prudent && (! isnumeric (grad) || numel (grad) != n))
error ("gradient function returns inadequate output");
endif
if (prudent && (! isnumeric (hessian) || any (size (hessian) != n)))
error ("hessian function returns inadequate output");
endif
endif
endfunction
|