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
|
// CppNumericalSolver
#ifndef MORETHUENTE_H_
#define MORETHUENTE_H_
#include "../meta.h"
#include <cmath>
namespace cppoptlib {
template<typename ProblemType, int Ord>
class MoreThuente {
public:
using Scalar = typename ProblemType::Scalar;
using TVector = typename ProblemType::TVector;
/**
* @brief use MoreThuente Rule for (strong) Wolfe conditiions
* @details [long description]
*
* @param searchDir search direction for next update step
* @param objFunc handle to problem
*
* @return step-width
*/
static Scalar linesearch(const TVector &x, const TVector &searchDir, ProblemType &objFunc, const Scalar alpha_init = 1.0) {
// assume step width
Scalar ak = alpha_init;
Scalar fval = objFunc.value(x);
TVector g = x.eval();
objFunc.gradient(x, g);
TVector s = searchDir.eval();
TVector xx = x.eval();
cvsrch(objFunc, xx, fval, g, ak, s);
return ak;
}
static int cvsrch(ProblemType &objFunc, TVector &x, Scalar f, TVector &g, Scalar &stp, TVector &s) {
// we rewrite this from MIN-LAPACK and some MATLAB code
int info = 0;
int infoc = 1;
const Scalar xtol = 1e-15;
const Scalar ftol = 1e-4;
const Scalar gtol = 1e-2;
const Scalar stpmin = 1e-15;
const Scalar stpmax = 1e15;
const Scalar xtrapf = 4;
const int maxfev = 20;
int nfev = 0;
Scalar dginit = g.dot(s);
if (dginit >= 0.0) {
// no descent direction
// TODO: handle this case
return -1;
}
bool brackt = false;
bool stage1 = true;
Scalar finit = f;
Scalar dgtest = ftol * dginit;
Scalar width = stpmax - stpmin;
Scalar width1 = 2 * width;
TVector wa = x.eval();
Scalar stx = 0.0;
Scalar fx = finit;
Scalar dgx = dginit;
Scalar sty = 0.0;
Scalar fy = finit;
Scalar dgy = dginit;
Scalar stmin;
Scalar stmax;
while (true) {
// make sure we stay in the interval when setting min/max-step-width
if (brackt) {
stmin = std::min(stx, sty);
stmax = std::max(stx, sty);
} else {
stmin = stx;
stmax = stp + xtrapf * (stp - stx);
}
// Force the step to be within the bounds stpmax and stpmin.
stp = std::max(stp, stpmin);
stp = std::min(stp, stpmax);
// Oops, let us return the last reliable values
if (
(brackt && ((stp <= stmin) || (stp >= stmax)))
|| (nfev >= maxfev - 1 ) || (infoc == 0)
|| (brackt && ((stmax - stmin) <= (xtol * stmax)))) {
stp = stx;
}
// test new point
x = wa + stp * s;
f = objFunc.value(x);
objFunc.gradient(x, g);
nfev++;
Scalar dg = g.dot(s);
Scalar ftest1 = finit + stp * dgtest;
// all possible convergence tests
if ((brackt & ((stp <= stmin) | (stp >= stmax))) | (infoc == 0))
info = 6;
if ((stp == stpmax) & (f <= ftest1) & (dg <= dgtest))
info = 5;
if ((stp == stpmin) & ((f > ftest1) | (dg >= dgtest)))
info = 4;
if (nfev >= maxfev)
info = 3;
if (brackt & (stmax - stmin <= xtol * stmax))
info = 2;
if ((f <= ftest1) & (fabs(dg) <= gtol * (-dginit)))
info = 1;
// terminate when convergence reached
if (info != 0)
return -1;
if (stage1 & (f <= ftest1) & (dg >= std::min(ftol, gtol)*dginit))
stage1 = false;
if (stage1 & (f <= fx) & (f > ftest1)) {
Scalar fm = f - stp * dgtest;
Scalar fxm = fx - stx * dgtest;
Scalar fym = fy - sty * dgtest;
Scalar dgm = dg - dgtest;
Scalar dgxm = dgx - dgtest;
Scalar dgym = dgy - dgtest;
cstep( stx, fxm, dgxm, sty, fym, dgym, stp, fm, dgm, brackt, stmin, stmax, infoc);
fx = fxm + stx * dgtest;
fy = fym + sty * dgtest;
dgx = dgxm + dgtest;
dgy = dgym + dgtest;
} else {
// this is ugly and some variables should be moved to the class scope
cstep( stx, fx, dgx, sty, fy, dgy, stp, f, dg, brackt, stmin, stmax, infoc);
}
if (brackt) {
if (fabs(sty - stx) >= 0.66 * width1)
stp = stx + 0.5 * (sty - stx);
width1 = width;
width = fabs(sty - stx);
}
}
return 0;
}
static int cstep(Scalar& stx, Scalar& fx, Scalar& dx, Scalar& sty, Scalar& fy, Scalar& dy, Scalar& stp,
Scalar& fp, Scalar& dp, bool& brackt, Scalar& stpmin, Scalar& stpmax, int& info) {
info = 0;
bool bound = false;
// Check the input parameters for errors.
if ((brackt & ((stp <= std::min(stx, sty) ) | (stp >= std::max(stx, sty)))) | (dx * (stp - stx) >= 0.0)
| (stpmax < stpmin)) {
return -1;
}
Scalar sgnd = dp * (dx / fabs(dx));
Scalar stpf = 0;
Scalar stpc = 0;
Scalar stpq = 0;
if (fp > fx) {
info = 1;
bound = true;
Scalar theta = 3. * (fx - fp) / (stp - stx) + dx + dp;
Scalar s = std::max(theta, std::max(dx, dp));
Scalar gamma = s * sqrt((theta / s) * (theta / s) - (dx / s) * (dp / s));
if (stp < stx)
gamma = -gamma;
Scalar p = (gamma - dx) + theta;
Scalar q = ((gamma - dx) + gamma) + dp;
Scalar r = p / q;
stpc = stx + r * (stp - stx);
stpq = stx + ((dx / ((fx - fp) / (stp - stx) + dx)) / 2.) * (stp - stx);
if (fabs(stpc - stx) < fabs(stpq - stx))
stpf = stpc;
else
stpf = stpc + (stpq - stpc) / 2;
brackt = true;
} else if (sgnd < 0.0) {
info = 2;
bound = false;
Scalar theta = 3 * (fx - fp) / (stp - stx) + dx + dp;
Scalar s = std::max(theta, std::max(dx, dp));
Scalar gamma = s * sqrt((theta / s) * (theta / s) - (dx / s) * (dp / s));
if (stp > stx)
gamma = -gamma;
Scalar p = (gamma - dp) + theta;
Scalar q = ((gamma - dp) + gamma) + dx;
Scalar r = p / q;
stpc = stp + r * (stx - stp);
stpq = stp + (dp / (dp - dx)) * (stx - stp);
if (fabs(stpc - stp) > fabs(stpq - stp))
stpf = stpc;
else
stpf = stpq;
brackt = true;
} else if (fabs(dp) < fabs(dx)) {
info = 3;
bound = 1;
Scalar theta = 3 * (fx - fp) / (stp - stx) + dx + dp;
Scalar s = std::max(theta, std::max( dx, dp));
Scalar gamma = s * sqrt(std::max(static_cast<Scalar>(0.), (theta / s) * (theta / s) - (dx / s) * (dp / s)));
if (stp > stx)
gamma = -gamma;
Scalar p = (gamma - dp) + theta;
Scalar q = (gamma + (dx - dp)) + gamma;
Scalar r = p / q;
if ((r < 0.0) & (gamma != 0.0)) {
stpc = stp + r * (stx - stp);
} else if (stp > stx) {
stpc = stpmax;
} else {
stpc = stpmin;
}
stpq = stp + (dp / (dp - dx)) * (stx - stp);
if (brackt) {
if (fabs(stp - stpc) < fabs(stp - stpq)) {
stpf = stpc;
} else {
stpf = stpq;
}
} else {
if (fabs(stp - stpc) > fabs(stp - stpq)) {
stpf = stpc;
} else {
stpf = stpq;
}
}
} else {
info = 4;
bound = false;
if (brackt) {
Scalar theta = 3 * (fp - fy) / (sty - stp) + dy + dp;
Scalar s = std::max(theta, std::max(dy, dp));
Scalar gamma = s * sqrt((theta / s) * (theta / s) - (dy / s) * (dp / s));
if (stp > sty)
gamma = -gamma;
Scalar p = (gamma - dp) + theta;
Scalar q = ((gamma - dp) + gamma) + dy;
Scalar r = p / q;
stpc = stp + r * (sty - stp);
stpf = stpc;
} else if (stp > stx)
stpf = stpmax;
else {
stpf = stpmin;
}
}
if (fp > fx) {
sty = stp;
fy = fp;
dy = dp;
} else {
if (sgnd < 0.0) {
sty = stx;
fy = fx;
dy = dx;
}
stx = stp;
fx = fp;
dx = dp;
}
stpf = std::min(stpmax, stpf);
stpf = std::max(stpmin, stpf);
stp = stpf;
if (brackt & bound) {
if (sty > stx) {
stp = std::min(stx + static_cast<Scalar>(0.66) * (sty - stx), stp);
} else {
stp = std::max(stx + static_cast<Scalar>(0.66) * (sty - stx), stp);
}
}
return 0;
}
};
}
#endif /* MORETHUENTE_H_ */
|