/usr/include/madness/tensor/solvers.cc is in libmadness-dev 0.10-3.
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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | /*
This file is part of MADNESS.
Copyright (C) 2007,2010 Oak Ridge National Laboratory
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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For more information please contact:
Robert J. Harrison
Oak Ridge National Laboratory
One Bethel Valley Road
P.O. Box 2008, MS-6367
email: harrisonrj@ornl.gov
tel: 865-241-3937
fax: 865-572-0680
$Id$
*/
#include <madness/tensor/solvers.h>
namespace madness {
double OptimizationTargetInterface::test_gradient(Tensor<double>& x, double value_precision, bool doprint) {
const double eps = pow(value_precision,0.3333);
if (doprint) {
printf("\n");
printf("Testing gradient eps=%.1e\n----------------\n", eps);
printf(" i f- f+ ganalytic gnumeric err\n");
printf(" ---- ------------------ ------------------ ------------------ ------------------ -------\n");
}
Tensor<double> tt = gradient(x);
int n = int(tt.dim(0));
double maxerr = 0.0;
for (int i=0; i<n; ++i) {
x[i] += eps;
double fp = value(x);
x[i] -= 2.0*eps;
double fm = value(x);
x[i] += eps;
double gg = 0.5*(fp-fm)/eps;
if (doprint)
printf("% 5d%20.12e%20.12e%20.12e%20.12e %.1e\n", i, fm, fp, tt(i), gg, std::abs(tt(i)-gg));
maxerr = std::max(std::abs(gg-tt(i)),maxerr);
}
if (doprint) printf("\n");
return maxerr;
}
SteepestDescent::SteepestDescent(const std::shared_ptr<OptimizationTargetInterface>& tar,
double tol,
double value_precision,
double gradient_precision)
: target(tar)
, tol(tol)
, f(tol*1e16)
, gnorm(tol*1e16)
{
if (!target->provides_gradient()) throw "Steepest descent requires the gradient";
}
bool SteepestDescent::optimize(Tensor<double>& x) {
double step = 10.0;
double fnew;
Tensor<double> g;
target->value_and_gradient(x,f,g);
gnorm = g.normf();
for (int i=0; i<100; ++i) {
while (1) {
Tensor<double> gnew;
x.gaxpy(1.0, g, -step);
target->value_and_gradient(x,fnew,gnew);
if (fnew < f) {
f = fnew;
g = gnew;
break;
}
x.gaxpy(1.0, g, step);
step *= 0.5;
print("reducing step size", f, fnew, step);
}
Tensor<double> g = target->gradient(x);
gnorm = g.normf();
print("iteration",i,"value",f,"gradient",gnorm);
if (converged()) break;
}
return converged();
}
bool SteepestDescent::converged() const {return gnorm < tol;}
double SteepestDescent::gradient_norm() const {return gnorm;}
double SteepestDescent::value() const {return f;}
double QuasiNewton::line_search(double a1, double f0, double dxgrad,
const Tensor<double>& x, const Tensor<double>& dx) const {
double f1, f2p;
double hess, a2;
const char* lsmode = "";
if (dxgrad*a1 > 0.0) {
print(" line search gradient +ve ", a1, dxgrad);
a1 = -a1;
}
f1 = target->value(x + a1 * dx);
// Fit to a parabola using f0, g0, f1
hess = 2.0*(f1-f0-a1*dxgrad)/(a1*a1);
a2 = -dxgrad/hess;
if (std::abs(f1-f0) < value_precision) { // Insufficient precision
a2 = a1;
lsmode = "fixed";
}
else if (hess > 0.0) { // Positive curvature
if ((f1 - f0) <= -value_precision) { // Downhill
lsmode = "downhill";
if (std::abs(a2) > 4.0*std::abs(a1)) {
lsmode = "restrict";
a2 = 4.0*a1;
}
}
else { // Uphill
lsmode = "bracket";
}
}
else { // Negative curvature
if ((f1 - f0) < value_precision) { // Downhill
lsmode = "negative";
a2 = 2e0*a1;
}
else {
lsmode = "punt";
a2 = a1;
}
}
f2p = f0 + dxgrad*a2 + 0.5*hess*a2*a2;
printf(" line search grad=%.2e hess=%.2e mode=%s newstep=%.3f\n", dxgrad, hess, lsmode, a2);
printf(" predicted %.12e\n", f2p);
return a2;
}
void QuasiNewton::hessian_update_sr1(const Tensor<double>& s,
const Tensor<double>& y, Tensor<double>& hessian) {
Tensor<double> q = y - inner(hessian,s);
double qds = q.trace(s);
if (std::abs(qds) > 1e-8 * s.normf() * q.normf()) {
hessian += outer(q,q).scale(1.0/qds);
}
else {
printf(" SR1 not updating\n");
}
}
void QuasiNewton::hessian_update_bfgs(const Tensor<double>& dx,
const Tensor<double>& dg, Tensor<double>& hessian) {
/*
Apply the BFGS update to the approximate Hessian h[][].
h[][] = Hessian matrix from previous iteration
dx[] = Step from previous iteration
. (dx[] = x[] - xp[] where xp[] is the previous point)
dg[] = gradient difference (dg = g - gp)
*/
Tensor<double> hdx = inner(hessian,dx);
double dxhdx = dx.trace(hdx);
double dxdx = dx.trace(dx);
double dxdg = dx.trace(dg);
double dgdg = dg.trace(dg);
const int n=hessian.dim(0);
if ( (dxdx > 0.0) && (dgdg > 0.0) && (std::abs(dxdg/std::sqrt(dxdx*dgdg)) > 1.e-8) ) {
for (int i=0; i<n; ++i) {
for (int j=0; j<n; ++j) {
hessian(i,j) += dg[i]*dg[j]/dxdg - hdx[i]*hdx[j]/dxhdx;
}
}
}
else {
printf(" BFGS not updating dxdg (%e), dgdg (%e), dxhdx (%f), dxdx(%e)\n" , dxdg, dgdg, dxhdx, dxdx);
}
}
Tensor<double> QuasiNewton::new_search_direction(const Tensor<double>& g) const {
Tensor<double> dx, s;
double tol = gradient_precision;
double trust = 1.0; // This applied in spectral basis
Tensor<double> v, e;
syev(h, v, e);
// Transform gradient into spectral basis
Tensor<double> gv = inner(g,v);
// Take step applying restriction
int nneg=0, nsmall=0, nrestrict=0;
for (int i=0; i<n; ++i) {
if (e[i] < -tol) {
if (printtest) printf(" forcing negative eigenvalue to be positive %d %.1e\n", i, e[i]);
nneg++;
//e[i] = -2.0*e[i]; // Enforce positive search direction
e[i] = -0.1*e[i]; // Enforce positive search direction
}
else if (e[i] < tol) {
if (printtest) printf(" forcing small eigenvalue to be positive %d %.1e\n", i, e[i]);
nsmall++;
e[i] = tol;
}
gv[i] = -gv[i] / e[i];
if (std::abs(gv[i]) > trust) { // Step restriction
double gvnew = trust*std::abs(gv(i))/gv[i];
if (printtest) printf(" restricting step in spectral direction %d %.1e --> %.1e\n", i, gv[i], gvnew);
nrestrict++;
gv[i] = gvnew;
}
}
if (nneg || nsmall || nrestrict) printf(" nneg=%d nsmall=%d nrestrict=%d\n", nneg, nsmall, nrestrict);
// Transform back from spectral basis
return inner(v,gv);
}
QuasiNewton::QuasiNewton(const std::shared_ptr<OptimizationTargetInterface>& tar,
int maxiter,
double tol,
double value_precision,
double gradient_precision)
: update("BFGS")
, target(tar)
, maxiter(maxiter)
, tol(tol)
, value_precision(value_precision)
, gradient_precision(gradient_precision)
, f(tol*1e16)
, gnorm(tol*1e16)
, n(0)
, printtest(false)
{
if (!target->provides_gradient()) throw "QuasiNewton requires the gradient";
}
void QuasiNewton::set_update(const std::string& method) {
if (method == "BFGS" || method == "SR1") update=method;
else throw "QuasiNewton: unknown update mthod";
}
void QuasiNewton::set_test(const bool& test_level) {
printtest = test_level;
}
bool QuasiNewton::optimize(Tensor<double>& x) {
// int maxiter = 20; // !!!!!!!!! dumb
if (n != x.dim(0)) {
n = x.dim(0);
h = Tensor<double>();
}
if(printtest) target->test_gradient(x, value_precision);
bool h_is_identity = (h.size() == 0);
if (h_is_identity) {
h = Tensor<double>(n,n);
for (int i=0; i<n; ++i) h(i,i) = 1.0;
}
// the previous gradient
Tensor<double> gp;
// the displacement
Tensor<double> dx;
for (int iter=0; iter<maxiter; ++iter) {
Tensor<double> g;
target->value_and_gradient(x, f, g);
gnorm = g.normf();
printf(" QuasiNewton iteration %2d value %.12e gradient %.2e\n",iter,f,gnorm);
if (converged()) break;
if (iter == 1 && h_is_identity) {
// Default initial Hessian is scaled identity but
// prefer to reuse any existing approximation.
h.scale(g.trace(gp)/gp.trace(dx));
}
if (iter > 0) {
if (update == "BFGS") hessian_update_bfgs(dx, g-gp,h);
else hessian_update_sr1(dx, g-gp,h);
}
dx = new_search_direction(g);
double step = line_search(1.0, f, dx.trace(g), x, dx);
dx.scale(step);
x += dx;
gp = g;
}
if (printtest) {
print("final hessian");
print(h);
}
return converged();
}
bool QuasiNewton::converged() const {return gnorm < tol;}
double QuasiNewton::value() const {return f;}
double QuasiNewton::gradient_norm() const {return gnorm;}
}
|