[0b990d] | 1 | //
|
---|
| 2 | // efc.cc
|
---|
| 3 | //
|
---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
|
---|
| 5 | //
|
---|
| 6 | // Author: Edward Seidl <seidl@janed.com>
|
---|
| 7 | // Maintainer: LPS
|
---|
| 8 | //
|
---|
| 9 | // This file is part of the SC Toolkit.
|
---|
| 10 | //
|
---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify
|
---|
| 12 | // it under the terms of the GNU Library General Public License as published by
|
---|
| 13 | // the Free Software Foundation; either version 2, or (at your option)
|
---|
| 14 | // any later version.
|
---|
| 15 | //
|
---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful,
|
---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | // GNU Library General Public License for more details.
|
---|
| 20 | //
|
---|
| 21 | // You should have received a copy of the GNU Library General Public License
|
---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
|
---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 24 | //
|
---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7.
|
---|
| 26 | //
|
---|
| 27 |
|
---|
| 28 | #ifdef __GNUC__
|
---|
| 29 | #pragma implementation
|
---|
| 30 | #endif
|
---|
| 31 |
|
---|
| 32 | #include <math.h>
|
---|
| 33 | #include <float.h>
|
---|
| 34 |
|
---|
| 35 | #include <util/state/stateio.h>
|
---|
| 36 | #include <math/optimize/efc.h>
|
---|
| 37 | #include <util/misc/formio.h>
|
---|
| 38 | #include <util/keyval/keyval.h>
|
---|
| 39 | #include <math/scmat/local.h>
|
---|
| 40 |
|
---|
| 41 | using namespace std;
|
---|
| 42 | using namespace sc;
|
---|
| 43 |
|
---|
| 44 | /////////////////////////////////////////////////////////////////////////
|
---|
| 45 | // EFCOpt
|
---|
| 46 |
|
---|
| 47 | static ClassDesc EFCOpt_cd(
|
---|
| 48 | typeid(EFCOpt),"EFCOpt",2,"public Optimize",
|
---|
| 49 | 0, create<EFCOpt>, create<EFCOpt>);
|
---|
| 50 |
|
---|
| 51 | EFCOpt::EFCOpt(const Ref<KeyVal>&keyval):
|
---|
| 52 | Optimize(keyval),
|
---|
| 53 | maxabs_gradient(-1.0)
|
---|
| 54 | {
|
---|
| 55 | update_ << keyval->describedclassvalue("update");
|
---|
| 56 |
|
---|
| 57 | accuracy_ = keyval->doublevalue("accuracy");
|
---|
| 58 | if (keyval->error() != KeyVal::OK) accuracy_ = 0.0001;
|
---|
| 59 |
|
---|
| 60 | tstate = keyval->booleanvalue("transition_state");
|
---|
| 61 | if (keyval->error() != KeyVal::OK) tstate = 0;
|
---|
| 62 |
|
---|
| 63 | modef = keyval->booleanvalue("mode_following");
|
---|
| 64 | if (keyval->error() != KeyVal::OK) modef = 0;
|
---|
| 65 |
|
---|
| 66 | if (tstate)
|
---|
| 67 | ExEnv::out0() << endl << indent
|
---|
| 68 | << "performing a transition state search\n\n";
|
---|
| 69 |
|
---|
| 70 | RefSymmSCMatrix hessian(dimension(),matrixkit());
|
---|
| 71 | // get a guess hessian from function
|
---|
| 72 | function()->guess_hessian(hessian);
|
---|
| 73 |
|
---|
| 74 | // see if any hessian matrix elements have been given in the input
|
---|
| 75 | if (keyval->exists("hessian")) {
|
---|
| 76 | int n = hessian.n();
|
---|
| 77 | for (int i=0; i<n; i++) {
|
---|
| 78 | if (keyval->exists("hessian",i)) {
|
---|
| 79 | for (int j=0; j<=i; j++) {
|
---|
| 80 | double tmp = keyval->doublevalue("hessian",i,j);
|
---|
| 81 | if (keyval->error() == KeyVal::OK) hessian(i,j) = tmp;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | hessian_ = hessian;
|
---|
| 87 | last_mode_ = 0;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | EFCOpt::EFCOpt(StateIn&s):
|
---|
| 91 | SavableState(s),
|
---|
| 92 | Optimize(s)
|
---|
| 93 | {
|
---|
| 94 | s.get(tstate);
|
---|
| 95 | s.get(modef);
|
---|
| 96 | hessian_ = matrixkit()->symmmatrix(dimension());
|
---|
| 97 | hessian_.restore(s);
|
---|
| 98 | update_ << SavableState::restore_state(s);
|
---|
| 99 | last_mode_ = matrixkit()->vector(dimension());
|
---|
| 100 | last_mode_.restore(s);
|
---|
| 101 | if (s.version(::class_desc<EFCOpt>()) < 2) {
|
---|
| 102 | double convergence;
|
---|
| 103 | s.get(convergence);
|
---|
| 104 | }
|
---|
| 105 | s.get(accuracy_);
|
---|
| 106 | s.get(maxabs_gradient);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | EFCOpt::~EFCOpt()
|
---|
| 110 | {
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | void
|
---|
| 114 | EFCOpt::save_data_state(StateOut&s)
|
---|
| 115 | {
|
---|
| 116 | Optimize::save_data_state(s);
|
---|
| 117 | s.put(tstate);
|
---|
| 118 | s.put(modef);
|
---|
| 119 | hessian_.save(s);
|
---|
| 120 | SavableState::save_state(update_.pointer(),s);
|
---|
| 121 | last_mode_.save(s);
|
---|
| 122 | s.put(accuracy_);
|
---|
| 123 | s.put(maxabs_gradient);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | void
|
---|
| 127 | EFCOpt::init()
|
---|
| 128 | {
|
---|
| 129 | Optimize::init();
|
---|
| 130 | maxabs_gradient = -1.0;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | int
|
---|
| 134 | EFCOpt::update()
|
---|
| 135 | {
|
---|
| 136 | int i,j;
|
---|
| 137 |
|
---|
| 138 | // these are good candidates to be input options
|
---|
| 139 | const double maxabs_gradient_to_desired_accuracy = 0.05;
|
---|
| 140 | const double maxabs_gradient_to_next_desired_accuracy = 0.005;
|
---|
| 141 | const double roundoff_error_factor = 1.1;
|
---|
| 142 |
|
---|
| 143 | // the gradient convergence criterion.
|
---|
| 144 | double old_maxabs_gradient = maxabs_gradient;
|
---|
| 145 | RefSCVector xcurrent;
|
---|
| 146 | RefSCVector gcurrent;
|
---|
| 147 |
|
---|
| 148 | ExEnv::out0().flush();
|
---|
| 149 |
|
---|
| 150 | // get the next gradient at the required level of accuracy.
|
---|
| 151 | // usually only one pass is needed, unless we happen to find
|
---|
| 152 | // that the accuracy was set too low.
|
---|
| 153 | int accurate_enough;
|
---|
| 154 | do {
|
---|
| 155 | // compute the current point
|
---|
| 156 | function()->set_desired_gradient_accuracy(accuracy_);
|
---|
| 157 |
|
---|
| 158 | xcurrent = function()->get_x();
|
---|
| 159 | gcurrent = function()->gradient().copy();
|
---|
| 160 |
|
---|
| 161 | // compute the gradient convergence criterion now so i can see if
|
---|
| 162 | // the accuracy needs to be tighter
|
---|
| 163 | maxabs_gradient = gcurrent.maxabs();
|
---|
| 164 | // compute the required accuracy
|
---|
| 165 | accuracy_ = maxabs_gradient * maxabs_gradient_to_desired_accuracy;
|
---|
| 166 |
|
---|
| 167 | if (accuracy_ < DBL_EPSILON) accuracy_ = DBL_EPSILON;
|
---|
| 168 |
|
---|
| 169 | // The roundoff_error_factor is thrown in to allow for round off making
|
---|
| 170 | // the current gcurrent.maxabs() a bit smaller than the previous,
|
---|
| 171 | // which would make the current required accuracy less than the
|
---|
| 172 | // gradient's actual accuracy and cause everything to be recomputed.
|
---|
| 173 | accurate_enough = (function()->actual_gradient_accuracy() <=
|
---|
| 174 | accuracy_*roundoff_error_factor);
|
---|
| 175 |
|
---|
| 176 | if (!accurate_enough) {
|
---|
| 177 | ExEnv::out0() << indent
|
---|
| 178 | << "NOTICE: function()->actual_gradient_accuracy() > accuracy_:\n"
|
---|
| 179 | << indent << scprintf(
|
---|
| 180 | " function()->actual_gradient_accuracy() = %15.8e",
|
---|
| 181 | function()->actual_gradient_accuracy()) << endl
|
---|
| 182 | << scprintf(
|
---|
| 183 | " accuracy_ = %15.8e",
|
---|
| 184 | accuracy_) << endl;
|
---|
| 185 | }
|
---|
| 186 | } while(!accurate_enough);
|
---|
| 187 |
|
---|
| 188 | if (old_maxabs_gradient >= 0.0 && old_maxabs_gradient < maxabs_gradient) {
|
---|
| 189 | ExEnv::out0() << indent
|
---|
| 190 | << scprintf("NOTICE: maxabs_gradient increased from %8.4e to %8.4e",
|
---|
| 191 | old_maxabs_gradient, maxabs_gradient) << endl;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | // update the hessian
|
---|
| 195 | if (update_.nonnull()) {
|
---|
| 196 | update_->update(hessian_,function(),xcurrent,gcurrent);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | // begin efc junk
|
---|
| 200 | // first diagonalize hessian
|
---|
| 201 | RefSCMatrix evecs(dimension(),dimension(),matrixkit());
|
---|
| 202 | RefDiagSCMatrix evals(dimension(),matrixkit());
|
---|
| 203 |
|
---|
| 204 | hessian_.diagonalize(evals,evecs);
|
---|
| 205 | //evals.print("hessian eigenvalues");
|
---|
| 206 | //evecs.print("hessian eigenvectors");
|
---|
| 207 |
|
---|
| 208 | // form gradient to local hessian modes F = Ug
|
---|
| 209 | RefSCVector F = evecs.t() * gcurrent;
|
---|
| 210 | //F.print("F");
|
---|
| 211 |
|
---|
| 212 | // figure out if hessian has the right number of negative eigenvalues
|
---|
| 213 | int ncoord = evals.n();
|
---|
| 214 | int npos=0,nneg=0;
|
---|
| 215 | for (i=0; i < ncoord; i++) {
|
---|
| 216 | if (evals.get_element(i) >= 0.0) npos++;
|
---|
| 217 | else nneg++;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | RefSCVector xdisp(dimension(),matrixkit());
|
---|
| 221 | xdisp.assign(0.0);
|
---|
| 222 |
|
---|
| 223 | // for now, we always take the P-RFO for tstate (could take NR if
|
---|
| 224 | // nneg==1, but we won't make that an option yet)
|
---|
| 225 | if (tstate) {
|
---|
| 226 | int mode = 0;
|
---|
| 227 |
|
---|
| 228 | if (modef) {
|
---|
| 229 | // which mode are we following. find mode with maximum overlap with
|
---|
| 230 | // last mode followed
|
---|
| 231 | if (last_mode_.nonnull()) {
|
---|
| 232 | double overlap=0;
|
---|
| 233 | for (i=0; i < ncoord; i++) {
|
---|
| 234 | double S=0;
|
---|
| 235 | for (j=0; j < ncoord; j++) {
|
---|
| 236 | S += last_mode_.get_element(j)*evecs.get_element(j,i);
|
---|
| 237 | }
|
---|
| 238 | S = fabs(S);
|
---|
| 239 | if (S > overlap) {
|
---|
| 240 | mode = i;
|
---|
| 241 | overlap = S;
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 | } else {
|
---|
| 245 | last_mode_ = matrixkit()->vector(dimension());
|
---|
| 246 |
|
---|
| 247 | // find mode with max component = coord 0 which should be the
|
---|
| 248 | // mode being followed
|
---|
| 249 | double comp=0;
|
---|
| 250 | for (i=0; i < ncoord; i++) {
|
---|
| 251 | double S = fabs(evecs.get_element(0,i));
|
---|
| 252 | if (S>comp) {
|
---|
| 253 | mode=i;
|
---|
| 254 | comp=S;
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | for (i=0; i < ncoord; i++)
|
---|
| 260 | last_mode_(i) = evecs(i,mode);
|
---|
| 261 |
|
---|
| 262 | ExEnv::out0() << endl << indent << "\n following mode " << mode << endl;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | double bk = evals(mode);
|
---|
| 266 | double Fk = F(mode);
|
---|
| 267 | double lambda_p = 0.5*bk + 0.5*sqrt(bk*bk + 4*Fk*Fk);
|
---|
| 268 |
|
---|
| 269 | double lambda_n;
|
---|
| 270 | double nlambda=1.0;
|
---|
| 271 | do {
|
---|
| 272 | lambda_n=nlambda;
|
---|
| 273 | nlambda=0;
|
---|
| 274 | for (i=0; i < ncoord; i++) {
|
---|
| 275 | if (i==mode) continue;
|
---|
| 276 |
|
---|
| 277 | nlambda += F.get_element(i)*F.get_element(i) /
|
---|
| 278 | (lambda_n - evals.get_element(i));
|
---|
| 279 | }
|
---|
| 280 | } while(fabs(nlambda-lambda_n) > 1.0e-8);
|
---|
| 281 |
|
---|
| 282 | ExEnv::out0()
|
---|
| 283 | << indent << scprintf("lambda_p = %8.5g",lambda_p) << endl
|
---|
| 284 | << indent << scprintf("lambda_n = %8.5g",lambda_n) << endl;
|
---|
| 285 |
|
---|
| 286 | // form Xk
|
---|
| 287 | double Fkobkl = F(mode)/(evals(mode)-lambda_p);
|
---|
| 288 | for (j=0; j < F.n(); j++)
|
---|
| 289 | xdisp(j) = xdisp(j) - evecs(j,mode) * Fkobkl;
|
---|
| 290 |
|
---|
| 291 | // form displacement x = sum -Fi*Vi/(bi-lam)
|
---|
| 292 | for (i=0; i < F.n(); i++) {
|
---|
| 293 | if (i==mode) continue;
|
---|
| 294 |
|
---|
| 295 | double Fiobil = F(i) / (evals(i)-lambda_n);
|
---|
| 296 | for (j=0; j < F.n(); j++) {
|
---|
| 297 | xdisp(j) = xdisp(j) - evecs(j,i) * Fiobil;
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | // minimum search
|
---|
| 302 | } else {
|
---|
| 303 | // evaluate lambda
|
---|
| 304 | double lambda;
|
---|
| 305 | double nlambda=1.0;
|
---|
| 306 | do {
|
---|
| 307 | lambda=nlambda;
|
---|
| 308 | nlambda=0;
|
---|
| 309 | for (i=0; i < F.n(); i++) {
|
---|
| 310 | double Fi = F(i);
|
---|
| 311 | nlambda += Fi*Fi / (lambda - evals.get_element(i));
|
---|
| 312 | }
|
---|
| 313 | } while(fabs(nlambda-lambda) > 1.0e-8);
|
---|
| 314 |
|
---|
| 315 | ExEnv::out0() << indent << scprintf("lambda = %8.5g", lambda) << endl;
|
---|
| 316 |
|
---|
| 317 | // form displacement x = sum -Fi*Vi/(bi-lam)
|
---|
| 318 | for (i=0; i < F.n(); i++) {
|
---|
| 319 | double Fiobil = F(i) / (evals(i)-lambda);
|
---|
| 320 | for (j=0; j < F.n(); j++) {
|
---|
| 321 | xdisp(j) = xdisp(j) - evecs(j,i) * Fiobil;
|
---|
| 322 | }
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | // scale the displacement vector if it's too large
|
---|
| 327 | double tot = sqrt(xdisp.scalar_product(xdisp));
|
---|
| 328 | if (tot > max_stepsize_) {
|
---|
| 329 | double scal = max_stepsize_/tot;
|
---|
| 330 | ExEnv::out0() << endl << indent
|
---|
| 331 | << scprintf("stepsize of %f is too big, scaling by %f",tot,scal)
|
---|
| 332 | << endl;
|
---|
| 333 | xdisp.scale(scal);
|
---|
| 334 | tot *= scal;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | //xdisp.print("xdisp");
|
---|
| 338 |
|
---|
| 339 | // try steepest descent
|
---|
| 340 | // RefSCVector xdisp = -1.0*gcurrent;
|
---|
| 341 | RefSCVector xnext = xcurrent + xdisp;
|
---|
| 342 |
|
---|
| 343 | conv_->reset();
|
---|
| 344 | conv_->get_grad(function());
|
---|
| 345 | conv_->get_x(function());
|
---|
| 346 | conv_->set_nextx(xnext);
|
---|
| 347 |
|
---|
| 348 | // check for conergence before resetting the geometry
|
---|
| 349 | int converged = conv_->converged();
|
---|
| 350 | if (converged)
|
---|
| 351 | return converged;
|
---|
| 352 |
|
---|
| 353 | ExEnv::out0() << endl
|
---|
| 354 | << indent << scprintf("taking step of size %f",tot) << endl;
|
---|
| 355 |
|
---|
| 356 | function()->set_x(xnext);
|
---|
| 357 | Ref<NonlinearTransform> t = function()->change_coordinates();
|
---|
| 358 | apply_transform(t);
|
---|
| 359 |
|
---|
| 360 | // make the next gradient computed more accurate, since it will
|
---|
| 361 | // be smaller
|
---|
| 362 | accuracy_ = maxabs_gradient * maxabs_gradient_to_next_desired_accuracy;
|
---|
| 363 |
|
---|
| 364 | return converged;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | void
|
---|
| 368 | EFCOpt::apply_transform(const Ref<NonlinearTransform> &t)
|
---|
| 369 | {
|
---|
| 370 | if (t.null()) return;
|
---|
| 371 | Optimize::apply_transform(t);
|
---|
| 372 | if (last_mode_.nonnull()) t->transform_gradient(last_mode_);
|
---|
| 373 | if (hessian_.nonnull()) t->transform_hessian(hessian_);
|
---|
| 374 | if (update_.nonnull()) update_->apply_transform(t);
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 378 |
|
---|
| 379 | // Local Variables:
|
---|
| 380 | // mode: c++
|
---|
| 381 | // c-file-style: "ETS"
|
---|
| 382 | // End:
|
---|