| 1 | // -*- Mode: C++; tab-width: 2; -*- |
| 2 | // vi: set ts=2: |
| 3 | // |
| 4 | // $Id: lineSearch.h,v 1.20.18.3 2007/08/07 09:12:34 aleru Exp $ |
| 5 | // |
| 6 | |
| 7 | // Line Search Minimizer: A special class for the line search minimization algorithm |
| 8 | |
| 9 | #ifndef BALL_MOLMEC_MINIMIZATION_LINESEARCH_H |
| 10 | #define BALL_MOLMEC_MINIMIZATION_LINESEARCH_H |
| 11 | |
| 12 | #ifndef BALL_COMMON_H |
| 13 | # include <BALL/common.h> |
| 14 | #endif |
| 15 | |
| 16 | namespace BALL |
| 17 | { |
| 18 | class EnergyMinimizer; |
| 19 | |
| 20 | /** Basic line search class. |
| 21 | * This method minimizes the energy of a system along a given |
| 22 | * direction using a two stage algorithm. It is based on |
| 23 | * cubic and quadratic interpolation methods of Jorge J. More |
| 24 | * and David J. Thuente. See |
| 25 | * |
| 26 | * [1] J. More and D. Thuente, "Line search algorithms with guaranteed |
| 27 | * sufficient decrease," ACM Transactions on Mathematical Software 20 |
| 28 | * (1994), no. 3, pp. 286-307. |
| 29 | * |
| 30 | * \ingroup MolmecEnergyMinimizer |
| 31 | */ |
| 32 | class BALL_EXPORT LineSearch |
| 33 | { |
| 34 | public: |
| 35 | |
| 36 | /** @name Constructors and Destructors |
| 37 | */ |
| 38 | //@{ |
| 39 | |
| 40 | BALL_CREATE(LineSearch) |
| 41 | |
| 42 | /** Default constructor. |
| 43 | */ |
| 44 | LineSearch(); |
| 45 | |
| 46 | /** Detailed constructor. |
| 47 | */ |
| 48 | LineSearch(EnergyMinimizer& minimizer); |
| 49 | |
| 50 | /** Copy constructor |
| 51 | */ |
| 52 | LineSearch(const LineSearch& line_search); |
| 53 | |
| 54 | /** Destructor. |
| 55 | */ |
| 56 | virtual ~LineSearch() ; |
| 57 | |
| 58 | //@} |
| 59 | /** @name Assignments |
| 60 | */ |
| 61 | //@{ |
| 62 | |
| 63 | /** Assignment operator |
| 64 | */ |
| 65 | const LineSearch& operator=(const LineSearch& LineSearch); |
| 66 | |
| 67 | //@} |
| 68 | /** @name Accessors |
| 69 | */ |
| 70 | //@{ |
| 71 | |
| 72 | /** Set the parameter alpha (convergence criterion for the line search). |
| 73 | * @see minimize |
| 74 | */ |
| 75 | void setAlpha(double alpha); |
| 76 | |
| 77 | /** Set the parameter beta (convergence criterion for the line search). |
| 78 | * @see minimize |
| 79 | */ |
| 80 | void setBeta(double beta); |
| 81 | |
| 82 | /** Get the parameter alpha (convergence criterion for the line search). |
| 83 | * @see minimize |
| 84 | */ |
| 85 | double getAlpha() const; |
| 86 | |
| 87 | /** Get the parameter beta (convergence criterion for the line search). |
| 88 | * @see minimize |
| 89 | */ |
| 90 | double getBeta() const; |
| 91 | |
| 92 | /** Get the parameter max_steps. |
| 93 | */ |
| 94 | Size getMaxSteps() const; |
| 95 | |
| 96 | /** Set the parameter max_steps. |
| 97 | */ |
| 98 | void setMaxSteps(Size steps); |
| 99 | |
| 100 | /** Set the lower bound on energy values. An estimation for the maximum |
| 101 | * step length is computed based on this value. |
| 102 | */ |
| 103 | void setLowerBound(double lbound); |
| 104 | |
| 105 | /** Get the lower bound on energy values. |
| 106 | */ |
| 107 | double getLowerBound() const; |
| 108 | |
| 109 | /** Set the nonnegative relative tolerance for an acceptable step length. |
| 110 | */ |
| 111 | void setXTol(double xtol); |
| 112 | |
| 113 | /** Get the nonnegative relative tolerance for an acceptable step length. |
| 114 | */ |
| 115 | double getXTol() const; |
| 116 | |
| 117 | /** Set the flag <tt>is_bracketed_</tt>. |
| 118 | * The algorithm will act as if a |
| 119 | * minimizer has already been bracketed (true) or not (false). Warning: |
| 120 | * this can be useful if a user exactly knows what they are doing. Usually, |
| 121 | * this flag should not be touched! The algorithm sets this flag automatically |
| 122 | * if a minimizer could be bracketed. |
| 123 | */ |
| 124 | void setBracketedFlag(bool bracktd); |
| 125 | |
| 126 | /** Return whether a minimizer has already been bracketed. Warning: this returns |
| 127 | * only the flag <tt>is_bracketed_</tt>. If this flag has not been set by the algorithm |
| 128 | * but changed by <tt>setBracketedFlag</tt> this might not be trustworthy! |
| 129 | */ |
| 130 | bool isBracketed() const; |
| 131 | |
| 132 | /** Set the minimizer class which provides the search direction and |
| 133 | * the force field among other things. |
| 134 | */ |
| 135 | void setMinimizer(EnergyMinimizer& minimizer); |
| 136 | |
| 137 | /** Computes a safeguarded step for a search procedure by case differentiation |
| 138 | * dependend on whether a minimum could already be bracketed or not. All |
| 139 | * computations are done by safeguarded quadratic and cubic interpolation. |
| 140 | * The interval that contains a step that satisfies a sufficient decrease and |
| 141 | * the curvature condition is updated. This function is based on the proposed step |
| 142 | * computation of Jorge J. More and David J. Thuente. Don't worry if interval bounds |
| 143 | * are changed after this routine exits. |
| 144 | * |
| 145 | * @param st_a Best step obtained so far and an endpoint of the interval that contains the minimizer. |
| 146 | * @param f_a Energy value at <tt>st_a</tt>. |
| 147 | * @param g_a Directional derivative at <tt>st_a</tt>. |
| 148 | * @param st_b Second endpoint of the interval that contains the minimizer. |
| 149 | * @param f_b Energy value at <tt>st_b</tt>. |
| 150 | * @param g_b Directional derivative at <tt>st_b</tt>. |
| 151 | * @param stp Current step. If <tt>is_bracketed_</tt> is set to <tt>true</tt> then <tt>stp</tt> |
| 152 | * must be between <tt>st_a</tt> and <tt>st_b</tt>. |
| 153 | * @param f Energy value at <tt>stp</tt>. |
| 154 | * @param g Directional derivative at <tt>stp</tt>. |
| 155 | * @param minstp Lower bound for the step. |
| 156 | * @param maxstp Upper bound for the step. |
| 157 | */ |
| 158 | virtual void takeStep(double &st_a, double &f_a, double &g_a, double &st_b, double &f_b, |
| 159 | double &g_b, double &stp, double f, double g, double minstp, double maxstp); |
| 160 | |
| 161 | //@} |
| 162 | /** @name Minimization |
| 163 | */ |
| 164 | //@{ |
| 165 | |
| 166 | /** Perform a line search. |
| 167 | * Find the minimum position for all atoms along the direction provided by the minimization algorithm. |
| 168 | * A two stage algorithm is used proposed by Jorge J. More and David J. Thuente. |
| 169 | * If necessary (the convergence criterion is not fulfilled) a minimizer along |
| 170 | * the given direction is bracketed in the first stage. In the second stage this |
| 171 | * function looks for a minimizer whithin the bracketed interval. |
| 172 | * The routine exits |
| 173 | * |
| 174 | * (1) if the strong Wolfe conditions are satisfied (convergence criterion for the line search), |
| 175 | * that are |
| 176 | * \f[E_{k+1} \leq E_k + \alpha\cdot stp\cdot<g_k, d_k>\f] |
| 177 | * and |
| 178 | * \f[|<g_{k+1}, d_k>| \leq \beta\cdot |<g_k, d_k>|\f] |
| 179 | * where \f$g_k\f$ and \f$g_{k+1}\f$ are the initial and the current gradient and \f$d_k\f$ is the |
| 180 | * search direction, \f$E_{k+1}\f$ is the current and \f$E_k\f$ the initial energy (stp = 0), |
| 181 | * \f$\alpha\f$ and \f$\beta\f$ are two parameters (usually 0.9 and 0.0001). |
| 182 | * |
| 183 | * (2) if the relative tolerance for an acceptable step is reached |
| 184 | * (length of the bracketing interval). |
| 185 | * |
| 186 | * (3) if there has been some numerical problems. In this case the best step length which could |
| 187 | * be found is returned. |
| 188 | * |
| 189 | * @param stp Unused on entry since the line search always starts with stp = 1.0. On exit |
| 190 | * the step length corresponding to a successful step or just the best this function was |
| 191 | * able to find. |
| 192 | * @param keep_gradient If <tt>true</tt>, we will not calculate the gradient for stp = 1 but |
| 193 | * assume that this has already been performed instead. |
| 194 | */ |
| 195 | virtual bool minimize(double& stp, bool keep_gradient = false); |
| 196 | //@} |
| 197 | |
| 198 | protected: |
| 199 | |
| 200 | /*_ Parameter alpha for line search criterion. |
| 201 | */ |
| 202 | double alpha_; |
| 203 | |
| 204 | /*_ Parameter beta for line search criterion. |
| 205 | */ |
| 206 | double beta_; |
| 207 | |
| 208 | /*_ Parameter for the number of interpolation steps |
| 209 | */ |
| 210 | Size max_steps_; |
| 211 | |
| 212 | /*_ Lower bound for energy values. |
| 213 | */ |
| 214 | double lower_energy_bound_; |
| 215 | |
| 216 | /*_ Nonnegative relative tolerance for an acceptable step. |
| 217 | */ |
| 218 | double stptol_; |
| 219 | |
| 220 | /*_ Specifies whether a minimizer has already been bracketed or not. |
| 221 | */ |
| 222 | bool is_bracketed_; |
| 223 | |
| 224 | /*_ Calling minimizer class which provides the search direction among other things. |
| 225 | */ |
| 226 | EnergyMinimizer* minimizer_; |
| 227 | |
| 228 | }; |
| 229 | |
| 230 | } // namespace BALL |
| 231 | |
| 232 | #endif // BALL_MOLMEC_MINIMIZATION/LINESEARCH_H |