| 1 | tim | 1902 | /* | 
| 2 |  |  | * Copyright (C) 2000-2004  Object Oriented Parallel Simulation Engine (OOPSE) project | 
| 3 |  |  | * | 
| 4 |  |  | * Contact: oopse@oopse.org | 
| 5 |  |  | * | 
| 6 |  |  | * This program is free software; you can redistribute it and/or | 
| 7 |  |  | * modify it under the terms of the GNU Lesser General Public License | 
| 8 |  |  | * as published by the Free Software Foundation; either version 2.1 | 
| 9 |  |  | * of the License, or (at your option) any later version. | 
| 10 |  |  | * All we ask is that proper credit is given for our work, which includes | 
| 11 |  |  | * - but is not limited to - adding the above copyright notice to the beginning | 
| 12 |  |  | * of your source code files, and to any copyright notice that you may distribute | 
| 13 |  |  | * with programs based on this work. | 
| 14 |  |  | * | 
| 15 |  |  | * This program is distributed in the hope that it will be useful, | 
| 16 |  |  | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 17 |  |  | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 18 |  |  | * GNU Lesser General Public License for more details. | 
| 19 |  |  | * | 
| 20 |  |  | * You should have received a copy of the GNU Lesser General Public License | 
| 21 |  |  | * along with this program; if not, write to the Free Software | 
| 22 |  |  | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
| 23 |  |  | * | 
| 24 |  |  | */ | 
| 25 |  |  |  | 
| 26 |  |  | #ifndef MINIMIZERS_OOPSEMINIMIZER_HPP | 
| 27 |  |  | #define MINIMIZERS_OOPSEMINIMIZER_HPP | 
| 28 |  |  |  | 
| 29 |  |  | #include <iostream> | 
| 30 |  |  |  | 
| 31 |  |  | #include "integrators/Integrator.hpp" | 
| 32 |  |  | #include "io/DumpWriter.hpp" | 
| 33 |  |  | #include "io/StatWriter.hpp" | 
| 34 |  |  | #include "minimizers/MinimizerParameterSet.hpp" | 
| 35 |  |  | #include "brains/ForceManager.hpp" | 
| 36 |  |  |  | 
| 37 |  |  | // base class of minimizer | 
| 38 |  |  |  | 
| 39 |  |  | namespace oopse { | 
| 40 |  |  |  | 
| 41 |  |  | /** @todo need refactorying */ | 
| 42 |  |  | const int MIN_LSERROR = -1; | 
| 43 |  |  | const int MIN_MAXITER = 0; | 
| 44 |  |  | const int MIN_CONVERGE = 1; | 
| 45 |  |  |  | 
| 46 |  |  | const int CONVG_UNCONVG = 0; | 
| 47 |  |  | const int CONVG_FTOL = 1; | 
| 48 |  |  | const int CONVG_GTOL = 2; | 
| 49 |  |  | const int CONVG_ABSGTOL = 3; | 
| 50 |  |  | const int CONVG_STEPTOL = 4; | 
| 51 |  |  |  | 
| 52 |  |  | const int LS_SUCCEED =1; | 
| 53 |  |  | const int LS_ERROR  = -1; | 
| 54 |  |  |  | 
| 55 |  |  | /** @todo move to math module */ | 
| 56 |  |  | double dotProduct(const std::vector<double>& v1, const std::vector<double>& v2); | 
| 57 |  |  |  | 
| 58 |  |  | /** | 
| 59 |  |  | * @class Minimizer | 
| 60 |  |  | * base minimizer class | 
| 61 |  |  | */ | 
| 62 |  |  | class Minimizer { | 
| 63 |  |  | public: | 
| 64 |  |  |  | 
| 65 |  |  | Minimizer(SimInfo *rhs); | 
| 66 |  |  |  | 
| 67 |  |  | virtual ~Minimizer(); | 
| 68 |  |  |  | 
| 69 |  |  | // | 
| 70 |  |  | virtual void init() {} | 
| 71 |  |  |  | 
| 72 |  |  | //driver function of minimization method | 
| 73 |  |  | virtual void minimize(); | 
| 74 |  |  |  | 
| 75 |  |  | // | 
| 76 |  |  | virtual int step() = 0; | 
| 77 |  |  |  | 
| 78 |  |  | // | 
| 79 |  |  | virtual void prepareStep() {}; | 
| 80 |  |  |  | 
| 81 |  |  | //line search algorithm, for the time being, we use back track algorithm | 
| 82 |  |  | virtual int doLineSearch(std::vector<double>& direction, double stepSize); | 
| 83 |  |  |  | 
| 84 |  |  | virtual int checkConvg() = 0; | 
| 85 |  |  |  | 
| 86 |  |  | //save the result when minimization method is done | 
| 87 |  |  | virtual void saveResult(){} | 
| 88 |  |  |  | 
| 89 |  |  | //get the status of minimization | 
| 90 |  |  | int getMinStatus() {return minStatus;} | 
| 91 |  |  |  | 
| 92 |  |  | // get the dimension of the model | 
| 93 |  |  | int getDim() {  return ndim;  } | 
| 94 |  |  |  | 
| 95 |  |  | //get the name of minimizer method | 
| 96 |  |  | std::string getMinimizerName() {  return minimizerName;  } | 
| 97 |  |  |  | 
| 98 |  |  | //return number of  current Iteration | 
| 99 |  |  | int getCurIter() {  return curIter;  } | 
| 100 |  |  |  | 
| 101 |  |  | // set the verbose mode of minimizer | 
| 102 |  |  | void setVerbose(bool verbose) {  bVerbose = verbose;} | 
| 103 |  |  |  | 
| 104 |  |  | //get and set the coordinate | 
| 105 |  |  | std::vector<double> getX() {  return curX;  } | 
| 106 |  |  | void setX(std::vector<double>& x); | 
| 107 |  |  |  | 
| 108 |  |  | //get and set the value of object function | 
| 109 |  |  | double getF() {  return curF;  } | 
| 110 |  |  | void setF(double f)  { curF = f;  } | 
| 111 |  |  |  | 
| 112 |  |  | std::vector<double> getG() {  return curG;  } | 
| 113 |  |  | void setG(std::vector<double>& g); | 
| 114 |  |  |  | 
| 115 |  |  | //get and set the gradient | 
| 116 |  |  | std::vector<double> getGrad() {  return curG;  } | 
| 117 |  |  | void setGrad(std::vector<double>& g) {  curG = g;  } | 
| 118 |  |  |  | 
| 119 |  |  | //interal function to evaluate the energy and gradient in OOPSE | 
| 120 |  |  | void calcEnergyGradient(std::vector<double>& x,  std::vector<double>& grad, double& | 
| 121 |  |  | energy, int& status); | 
| 122 |  |  |  | 
| 123 |  |  | //calculate the value of object function | 
| 124 |  |  | virtual void calcF(); | 
| 125 |  |  | virtual void calcF(std::vector<double>& x, double&f, int& status); | 
| 126 |  |  |  | 
| 127 |  |  | //calculate the gradient | 
| 128 |  |  | virtual void calcG(); | 
| 129 |  |  | virtual void calcG(std::vector<double>& x,  std::vector<double>& g, double& f, int& status); | 
| 130 |  |  |  | 
| 131 |  |  | //calculate the hessian | 
| 132 |  |  | //virtual void calcH(int& status); | 
| 133 |  |  | //virtual void calcH(vector<double>& x,  std::vector<dobule>& g, SymMatrix& h, int& status); | 
| 134 |  |  |  | 
| 135 |  |  | friend std::ostream& operator<<(std::ostream& os, const Minimizer& minimizer); | 
| 136 |  |  |  | 
| 137 |  |  | protected: | 
| 138 |  |  |  | 
| 139 |  |  | // transfrom cartesian and rotational coordinates into minimization coordinates | 
| 140 |  |  | std::vector<double> getCoor(); | 
| 141 |  |  |  | 
| 142 |  |  | // transfrom minimization coordinates into cartesian and rotational coordinates | 
| 143 |  |  | void setCoor(std::vector<double>& x); | 
| 144 |  |  |  | 
| 145 |  |  |  | 
| 146 |  |  |  | 
| 147 |  |  | //constraint the bonds; | 
| 148 |  |  | int shakeR() { return 0;} | 
| 149 |  |  |  | 
| 150 |  |  | //remove the force component along the bond direction | 
| 151 |  |  | int shakeF() { return 0;} | 
| 152 |  |  |  | 
| 153 |  |  | double calcPotential(); | 
| 154 |  |  |  | 
| 155 |  |  | SimInfo* info; | 
| 156 |  |  |  | 
| 157 |  |  | ForceManager* forceMan; | 
| 158 |  |  |  | 
| 159 |  |  | //parameter set of minimization method | 
| 160 |  |  | MinimizerParameterSet* paramSet; | 
| 161 |  |  |  | 
| 162 |  |  | //flag of turning on shake algorithm | 
| 163 |  |  | bool usingShake; | 
| 164 |  |  |  | 
| 165 |  |  | // dimension of the model | 
| 166 |  |  | int ndim; | 
| 167 |  |  |  | 
| 168 |  |  | //name of the minimizer | 
| 169 |  |  | std::string minimizerName; | 
| 170 |  |  |  | 
| 171 |  |  | // current iteration number | 
| 172 |  |  | int curIter; | 
| 173 |  |  | //status of minimization | 
| 174 |  |  | int minStatus; | 
| 175 |  |  |  | 
| 176 |  |  | //flag of verbose mode | 
| 177 |  |  | bool bVerbose; | 
| 178 |  |  |  | 
| 179 |  |  | //status of energy and gradient evaluation | 
| 180 |  |  | int egEvalStatus; | 
| 181 |  |  |  | 
| 182 |  |  | //initial coordinates | 
| 183 |  |  | //vector<double> initX; | 
| 184 |  |  |  | 
| 185 |  |  | //current value  of the function | 
| 186 |  |  | double curF; | 
| 187 |  |  |  | 
| 188 |  |  | // current coordinates | 
| 189 |  |  | std::vector<double> curX; | 
| 190 |  |  |  | 
| 191 |  |  | //gradient at curent coordinates | 
| 192 |  |  | std::vector<double> curG; | 
| 193 |  |  |  | 
| 194 |  |  | //hessian at current coordinates | 
| 195 |  |  | //SymMatrix curH; | 
| 196 |  |  |  | 
| 197 |  |  | private: | 
| 198 |  |  |  | 
| 199 |  |  | //calculate the dimension od the model for minimization | 
| 200 |  |  | void calcDim(); | 
| 201 |  |  |  | 
| 202 |  |  | }; | 
| 203 |  |  |  | 
| 204 |  |  | } | 
| 205 |  |  | #endif | 
| 206 |  |  |  |