| 1 | // -*- Mode: C++; tab-width: 2; -*- |
| 2 | // vi: set ts=2: |
| 3 | // |
| 4 | // $Id: strangLBFGS.h,v 1.1.4.6 2007/08/07 09:12:36 aleru Exp $ |
| 5 | // |
| 6 | |
| 7 | #ifndef BALL_MOLMEC_MINIMIZATION_STRANGLBFGS_H |
| 8 | #define BALL_MOLMEC_MINIMIZATION_STRANGLBFGS_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 | /** Limited-memory BFGS minimizer based on the Strang recurrence. |
| 21 | * A minimizer for geometry optimization based on the limited-memory |
| 22 | * BFGS method proposed in |
| 23 | * |
| 24 | * [1] Nocedal J.: Updating quasi-Newton matrices with limited storage. |
| 25 | * Math. Comp. 35 (1980) 773-782. |
| 26 | * |
| 27 | * [2] Liu D.C., Nocedal J.: On the limited memory BFGS method for large |
| 28 | * scale optimization. Math. Programming 45 (1989) 503-528. |
| 29 | * |
| 30 | * If the 'improved' option is set, ideas proposed in |
| 31 | * |
| 32 | * [3] Mehiddin Al-Baali: Improved Hessian approximations for the limited |
| 33 | * memory BFGS method. Numerical Algorithms 22 (1999) 99-112 |
| 34 | * |
| 35 | * are adopted. |
| 36 | * \ingroup MolmecEnergyMinimizer |
| 37 | */ |
| 38 | class BALL_EXPORT StrangLBFGSMinimizer |
| 39 | : public EnergyMinimizer |
| 40 | { |
| 41 | public: |
| 42 | |
| 43 | BALL_CREATE(StrangLBFGSMinimizer) |
| 44 | |
| 45 | /** @name Options and Defaults |
| 46 | */ |
| 47 | //@{ |
| 48 | |
| 49 | /** Symbolic names for option keys. |
| 50 | * This struct contains a symbolic name |
| 51 | * for each recognized key in StrangLBFGSMinimizer::options. \par |
| 52 | * For each symbol the required type is given under parameters. |
| 53 | */ |
| 54 | struct Option |
| 55 | { |
| 56 | /** Should the improved version be used? |
| 57 | */ |
| 58 | static const char* IMPROVED; |
| 59 | |
| 60 | /** Maximum number of stored vector pairs |
| 61 | */ |
| 62 | static const char* NUM_OF_STORED_VECT_PAIRS; |
| 63 | }; |
| 64 | |
| 65 | struct BALL_EXPORT Default |
| 66 | { |
| 67 | /** Should the improved version be used? |
| 68 | * Default is false. |
| 69 | */ |
| 70 | static const bool IMPROVED; |
| 71 | |
| 72 | /** Maximum number of stored vector pairs. |
| 73 | * Default is 5. |
| 74 | */ |
| 75 | static const Size NUM_OF_STORED_VECT_PAIRS; |
| 76 | }; |
| 77 | |
| 78 | //@} |
| 79 | /** @name Constructors and Destructors |
| 80 | */ |
| 81 | //@{ |
| 82 | |
| 83 | /** Default constructor. |
| 84 | */ |
| 85 | StrangLBFGSMinimizer(); |
| 86 | |
| 87 | /** Constructor expecting a valid force field |
| 88 | */ |
| 89 | StrangLBFGSMinimizer(ForceField& force_field); |
| 90 | |
| 91 | /** Constructor expecting a valid force field and a snapshot manager |
| 92 | */ |
| 93 | StrangLBFGSMinimizer(ForceField& force_field, SnapShotManager *ssm); |
| 94 | |
| 95 | /** Constructor expecting a valid force field, a snapshot manager and options |
| 96 | */ |
| 97 | StrangLBFGSMinimizer(ForceField& force_field, SnapShotManager* ssm, const Options& options); |
| 98 | |
| 99 | /** Constructor expecting a valid force field and options |
| 100 | */ |
| 101 | StrangLBFGSMinimizer(ForceField& force_field, const Options& options); |
| 102 | |
| 103 | /** Copy constructor |
| 104 | */ |
| 105 | StrangLBFGSMinimizer(const StrangLBFGSMinimizer& rhs); |
| 106 | |
| 107 | /** Destructor. |
| 108 | */ |
| 109 | virtual ~StrangLBFGSMinimizer(); |
| 110 | |
| 111 | //@} |
| 112 | /** @name Assignments |
| 113 | */ |
| 114 | //@{ |
| 115 | |
| 116 | /** Assignment operator |
| 117 | */ |
| 118 | const StrangLBFGSMinimizer& operator=(const StrangLBFGSMinimizer& rhs); |
| 119 | |
| 120 | //@} |
| 121 | /** @name Setup methods. They do all necessary preparations. |
| 122 | */ |
| 123 | //@{ |
| 124 | |
| 125 | /** Specific setup |
| 126 | */ |
| 127 | virtual bool specificSetup(); |
| 128 | |
| 129 | //@} |
| 130 | /** @name Accessors |
| 131 | */ |
| 132 | //@{ |
| 133 | |
| 134 | /** Set the maximum number of stored vector pairs |
| 135 | */ |
| 136 | void setMaxNumOfStoredVectPairs(Size num); |
| 137 | |
| 138 | /** Return the maximum number of stored vector pairs |
| 139 | */ |
| 140 | Size getMaxNumOfStoredVectPairs() const; |
| 141 | |
| 142 | /** Set the improved flag |
| 143 | */ |
| 144 | void setImprovedFlag(bool flag); |
| 145 | |
| 146 | /** Return the improved flag |
| 147 | */ |
| 148 | bool getImprovedFlag() const; |
| 149 | |
| 150 | /** Calculate the next step. |
| 151 | * First, this method updates the model. Second, it performs a line search |
| 152 | * along the calculated direction afterwards. |
| 153 | * @return double \f$\geq 0\f$ if the line search found an acceptable solution, otherwise -1. |
| 154 | * @see EnergyMinimizer::findStep |
| 155 | */ |
| 156 | virtual double findStep(); |
| 157 | |
| 158 | /** Update the stored vector pairs, either by adding a new pair or if there |
| 159 | * is no space left by replacing the oldest pair. |
| 160 | * Compute the search direction afterwards by using the Strang recurrence formula |
| 161 | * (optional: with an improvement idea of Al-Baali). |
| 162 | */ |
| 163 | virtual void updateDirection(); |
| 164 | |
| 165 | /** Minimize the energy of the system. |
| 166 | * This method executes at most <tt>iterations</tt> minimization steps. |
| 167 | * If the number of iterations is not given, the number specified in the |
| 168 | * options is taken. |
| 169 | * @param iterations the maximum number of iterations |
| 170 | * @param resume <b>true</b> to resume a previous run |
| 171 | * @see EnergyMinimizer::minimize |
| 172 | */ |
| 173 | virtual bool minimize(Size iterations = 0, bool resume = false); |
| 174 | |
| 175 | protected: |
| 176 | |
| 177 | //@} |
| 178 | /** @name Protected Attributes |
| 179 | */ |
| 180 | //@{ |
| 181 | |
| 182 | /** The line search |
| 183 | */ |
| 184 | LineSearch line_search_; |
| 185 | |
| 186 | /** Is this an initial first iteration? |
| 187 | */ |
| 188 | bool first_iter_; |
| 189 | |
| 190 | /** Number of movable atoms. |
| 191 | */ |
| 192 | Size number_of_atoms_; |
| 193 | |
| 194 | /** Maximum number of stored vector pairs. |
| 195 | */ |
| 196 | Size max_num_of_vect_pairs_; |
| 197 | |
| 198 | /** Current number of stored vector pairs. |
| 199 | */ |
| 200 | Size curr_num_of_vect_pairs_; |
| 201 | |
| 202 | /** Should the improved version be used? |
| 203 | */ |
| 204 | bool improved_; |
| 205 | |
| 206 | /** Old and new scaling values. Also used in the Strang recurrence formula. |
| 207 | */ |
| 208 | vector<float> rho_; |
| 209 | |
| 210 | /** Stored former steps. Vectors stored in column order. |
| 211 | */ |
| 212 | vector<Vector3> stored_s_; |
| 213 | |
| 214 | /** Stored former changes in gradients. |
| 215 | * Vectors stored in column order. |
| 216 | */ |
| 217 | vector<Vector3> stored_y_; |
| 218 | |
| 219 | /** Temporarily used memory for saving scalars associated with |
| 220 | * the stored vector pairs. |
| 221 | */ |
| 222 | vector<float> work_val_; |
| 223 | |
| 224 | /** Index of the vector pair which will be used for saving the data |
| 225 | * of the current step (usually by replacing old data). |
| 226 | */ |
| 227 | Size index_of_free_vect_; |
| 228 | |
| 229 | /** Positions of movable atoms when starting an iteration. |
| 230 | * This is used to reduce slightly rounding errors |
| 231 | */ |
| 232 | std::vector<Vector3> initial_atoms_; |
| 233 | |
| 234 | //@} |
| 235 | |
| 236 | }; |
| 237 | } // end of namespace BALL |
| 238 | |
| 239 | #endif // BALL_MOLMEC_MINIMIZATION_STRANGLBFGS_H |