| 1 | // -*- Mode: C++; tab-width: 2; -*- |
| 2 | // vi: set ts=2: |
| 3 | // |
| 4 | // $Id: conjugateGradient.h,v 1.29.20.6 2007/08/07 09:12:33 aleru Exp $ |
| 5 | // |
| 6 | |
| 7 | #ifndef BALL_MOLMEC_MINIMIZATION_CONJUGATEGRADIENT_H |
| 8 | #define BALL_MOLMEC_MINIMIZATION_CONJUGATEGRADIENT_H |
| 9 | |
| 10 | #ifndef BALL_MOLMEC_MINIMIZATION_ENERGYMINIMIZER_H |
| 11 | # include <BALL/MOLMEC/MINIMIZATION/energyMinimizer.h> |
| 12 | #endif |
| 13 | |
| 14 | #ifndef BALL_MOLMEC_MINIMIZATION_LINESEARCH_H |
| 15 | # include <BALL/MOLMEC/MINIMIZATION/lineSearch.h> |
| 16 | #endif |
| 17 | |
| 18 | namespace BALL |
| 19 | { |
| 20 | /** A minimizer for geometry optimization based on different |
| 21 | * conjugate gradient (CG) algorithms. See |
| 22 | * |
| 23 | * [1] Fletcher, R. and Reeves, C.M. "Function Minimization by |
| 24 | * Conjugate Gradients" Comp. J., 7, 149-154, 1964 |
| 25 | * |
| 26 | * [2] David Shanno "Conjugate Gradient Methods With Inexact Searches" |
| 27 | * Mathematics of Operations Research, Vol. 3, No. 3, August 1978, pp. 244-256 |
| 28 | * |
| 29 | * [3] S. Watowich et. al "A Stable Rapidly Converging |
| 30 | * Conjugate Gradient Method for Energy Minimization" Journal of Computational |
| 31 | * Chemistry, Vol. 9, No. 6, pp. 650-661 (1988) |
| 32 | * |
| 33 | * [4] M.J.D. Powell "Convergence properties of algorithms for nonlinear |
| 34 | * optimization" Report No. DAMPT 1985/NA1, University of Cambridge, 1985 |
| 35 | * |
| 36 | * [5] D.F. Shanno "Globally convergent conjugate gradient algorithms" |
| 37 | * Mathematical Programming 33 (1985), pp. 61-67 |
| 38 | * \ingroup MolmecEnergyMinimizer |
| 39 | */ |
| 40 | class BALL_EXPORT ConjugateGradientMinimizer |
| 41 | : public EnergyMinimizer |
| 42 | { |
| 43 | public: |
| 44 | |
| 45 | /** @name Enums |
| 46 | */ |
| 47 | //@{ |
| 48 | |
| 49 | /** The different conjugate gradient methods implemented. |
| 50 | * @see updateDirection for details on the implementation and references |
| 51 | */ |
| 52 | enum UpdateMethod |
| 53 | { |
| 54 | /** Polak-Ribiere method |
| 55 | */ |
| 56 | POLAK_RIBIERE = 1, |
| 57 | |
| 58 | /** Fletcher-Reeves method |
| 59 | */ |
| 60 | FLETCHER_REEVES = 2, |
| 61 | |
| 62 | /** Shanno |
| 63 | */ |
| 64 | SHANNO = 3 |
| 65 | }; |
| 66 | |
| 67 | //@} |
| 68 | /** @name Options and Defaults |
| 69 | */ |
| 70 | //@{ |
| 71 | |
| 72 | /** Options names |
| 73 | */ |
| 74 | struct Option |
| 75 | { |
| 76 | /** The initial step length used in the line search |
| 77 | */ |
| 78 | static const char* UPDATE_METHOD; |
| 79 | }; |
| 80 | |
| 81 | /** Defaults for all options |
| 82 | */ |
| 83 | struct BALL_EXPORT Default |
| 84 | { |
| 85 | /** The initial step length used in the line search |
| 86 | */ |
| 87 | static const Size UPDATE_METHOD; |
| 88 | }; |
| 89 | |
| 90 | //@} |
| 91 | /** @name Constructors and Destructors |
| 92 | */ |
| 93 | //@{ |
| 94 | |
| 95 | BALL_CREATE(ConjugateGradientMinimizer) |
| 96 | |
| 97 | /** Default constructor. |
| 98 | */ |
| 99 | ConjugateGradientMinimizer(); |
| 100 | |
| 101 | /** Constructor expecting a valid force field |
| 102 | */ |
| 103 | ConjugateGradientMinimizer(ForceField& force_field); |
| 104 | |
| 105 | /** Constructor expecting a valid force field and a snapshot manager |
| 106 | */ |
| 107 | ConjugateGradientMinimizer(ForceField& force_field,SnapShotManager *ssm); |
| 108 | |
| 109 | /** Constructor expecting a valid force field, a snapshot manager and options |
| 110 | */ |
| 111 | ConjugateGradientMinimizer(ForceField& force_field, |
| 112 | SnapShotManager* ssm, const Options& options); |
| 113 | |
| 114 | /** Constructor expecting a valid force field and options |
| 115 | */ |
| 116 | ConjugateGradientMinimizer(ForceField& force_field, const Options& options); |
| 117 | |
| 118 | /** Copy constructor |
| 119 | */ |
| 120 | ConjugateGradientMinimizer(const ConjugateGradientMinimizer& rhs); |
| 121 | |
| 122 | /** Destructor. |
| 123 | */ |
| 124 | virtual ~ConjugateGradientMinimizer(); |
| 125 | |
| 126 | //@} |
| 127 | /** @name Assignments |
| 128 | */ |
| 129 | //@{ |
| 130 | |
| 131 | /** Assignment operator |
| 132 | */ |
| 133 | const ConjugateGradientMinimizer& operator = (const ConjugateGradientMinimizer& rhs); |
| 134 | |
| 135 | //@} |
| 136 | /** @name Setup methods. They do all necessary preparations. |
| 137 | */ |
| 138 | //@{ |
| 139 | |
| 140 | /** Specific setup |
| 141 | */ |
| 142 | virtual bool specificSetup(); |
| 143 | |
| 144 | //@} |
| 145 | /** @name Accessors |
| 146 | */ |
| 147 | //@{ |
| 148 | |
| 149 | /** Set explicitly the criterion used for updateDirection. If the |
| 150 | * method is not recognized, this function fails. |
| 151 | */ |
| 152 | void setUpdateMethod(UpdateMethod method); |
| 153 | |
| 154 | /** Returns the current method for updateDirection. |
| 155 | */ |
| 156 | UpdateMethod getUpdateMethod() const; |
| 157 | |
| 158 | /** Calculate the next step. |
| 159 | * First, this method updates the model. Second, it performs a line search |
| 160 | * along the calculated direction afterwards. |
| 161 | * @return double \f$\geq 0\f$ if the line search found an acceptable solution, otherwise -1. |
| 162 | * @see EnergyMinimizer::findStep |
| 163 | */ |
| 164 | virtual double findStep(); |
| 165 | |
| 166 | /** Update the search direction. |
| 167 | * This method updates the search direction. |
| 168 | * It uses the different conjugate gradient caculations |
| 169 | * dependend on the options. |
| 170 | */ |
| 171 | virtual void updateDirection(); |
| 172 | |
| 173 | /** Minimize the energy of the system. |
| 174 | * This method executes at most <tt>iterations</tt> minimization steps. |
| 175 | * If the number of iterations is not given, the number specified in the |
| 176 | * options is taken. |
| 177 | * @param iterations the maximum number of iterations |
| 178 | * @param resume <b>true</b> to resume a previous run |
| 179 | * @see EnergyMinimizer::minimize |
| 180 | */ |
| 181 | virtual bool minimize(Size iterations = 0, bool resume = false); |
| 182 | |
| 183 | protected: |
| 184 | |
| 185 | //@} |
| 186 | /** @name Protected Attributes |
| 187 | */ |
| 188 | //@{ |
| 189 | |
| 190 | /** The line search |
| 191 | */ |
| 192 | LineSearch line_search_; |
| 193 | |
| 194 | /** Unscaled last search direction |
| 195 | */ |
| 196 | Gradient unscaled_direction_; |
| 197 | |
| 198 | /** Number of movable atoms. |
| 199 | */ |
| 200 | Size number_of_atoms_; |
| 201 | |
| 202 | /** Update method used for the CG |
| 203 | */ |
| 204 | Size updt_method_; |
| 205 | |
| 206 | /** Is this the (initial) first iteration? |
| 207 | */ |
| 208 | bool first_iter_; |
| 209 | |
| 210 | /** \f$g^T \cdot g\f$ where \f$g\f$ is the gradient of the last iteration |
| 211 | */ |
| 212 | double old_gtg_; |
| 213 | |
| 214 | /** Variable needed for the Shanno direction calculation |
| 215 | */ |
| 216 | vector<Vector3> a_i_; |
| 217 | |
| 218 | /** Variable needed for the Shanno direction calculation |
| 219 | */ |
| 220 | vector<Vector3> b_i_; |
| 221 | |
| 222 | /** Variable needed for the Shanno direction calculation |
| 223 | */ |
| 224 | vector<Vector3> p_t_; |
| 225 | |
| 226 | /** Variable needed for the Shanno direction calculation |
| 227 | */ |
| 228 | vector<Vector3> y_t_; |
| 229 | |
| 230 | /** Variable needed for the Shanno direction calculation |
| 231 | */ |
| 232 | vector<Vector3> p_i_; |
| 233 | |
| 234 | /** Variable needed for the Shanno direction calculation |
| 235 | */ |
| 236 | vector<Vector3> y_i_; |
| 237 | |
| 238 | /** Variable needed for the Shanno direction calculation |
| 239 | */ |
| 240 | double D_1_; |
| 241 | |
| 242 | /** Variable needed for the Shanno direction calculation |
| 243 | */ |
| 244 | double D_4_; |
| 245 | |
| 246 | /** Frequency for restarts. |
| 247 | */ |
| 248 | Size restart_frequency_; |
| 249 | |
| 250 | /** We count the iterations since the last restart. |
| 251 | * There are other necessities for a restart so the |
| 252 | * iteration counter may not coincide (by modulo) with the |
| 253 | * restart frequency. |
| 254 | */ |
| 255 | Size last_restart_iter_; |
| 256 | |
| 257 | //@} |
| 258 | |
| 259 | }; |
| 260 | } // end of namespace BALL |
| 261 | |
| 262 | #endif // BALL_MOLMEC_MINIMIZATION_CONJUGATEGRADIENT_H |