Public Types | Public Member Functions | Public Attributes | Protected Attributes | List of all members
stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile > Class Template Reference

#include "stan/optimization/bfgs.hpp"

Public Types

typedef Eigen::Matrix< Scalar, DimAtCompile, 1 > VectorT
 
typedef Eigen::Matrix< Scalar, DimAtCompile, DimAtCompile > HessianT
 

Public Member Functions

QNUpdateType & get_qnupdate ()
 
const QNUpdateType & get_qnupdate () const
 
const Scalar & curr_f () const
 
const VectorTcurr_x () const
 
const VectorTcurr_g () const
 
const VectorTcurr_p () const
 
const Scalar & prev_f () const
 
const VectorTprev_x () const
 
const VectorTprev_g () const
 
const VectorTprev_p () const
 
Scalar prev_step_size () const
 
Scalar rel_grad_norm () const
 
Scalar rel_obj_decrease () const
 
const Scalar & alpha0 () const
 
const Scalar & alpha () const
 
const size_t iter_num () const
 
const std::stringnote () const
 
std::string get_code_string (int retCode)
 
 BFGSMinimizer (FunctorType &f)
 
void initialize (const VectorT &x0)
 
int step ()
 
int minimize (VectorT &x0)
 

Public Attributes

LSOptions< Scalar > _ls_opts
 
ConvergenceOptions< Scalar > _conv_opts
 

Protected Attributes

FunctorType & _func
 
VectorT _gk
 
VectorT _gk_1
 
VectorT _xk_1
 
VectorT _xk
 
VectorT _pk
 
VectorT _pk_1
 
Scalar _fk
 
Scalar _fk_1
 
Scalar _alphak_1
 
Scalar _alpha
 
Scalar _alpha0
 
size_t _itNum
 
std::string _note
 
QNUpdateType _qn
 

Detailed Description

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
class stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >

Definition at line 70 of file bfgs.hpp.

Member Typedef Documentation

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
typedef Eigen::Matrix<Scalar, DimAtCompile, DimAtCompile> stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::HessianT

Definition at line 73 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
typedef Eigen::Matrix<Scalar, DimAtCompile, 1> stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::VectorT

Definition at line 72 of file bfgs.hpp.

Constructor & Destructor Documentation

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::BFGSMinimizer ( FunctorType &  f)
inlineexplicit

Definition at line 147 of file bfgs.hpp.

Member Function Documentation

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
const Scalar& stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::alpha ( ) const
inline

Definition at line 112 of file bfgs.hpp.

Referenced by stan::services::optimize::bfgs(), and stan::services::optimize::lbfgs().

112 { return _alpha; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
const Scalar& stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::alpha0 ( ) const
inline

Definition at line 111 of file bfgs.hpp.

Referenced by stan::services::optimize::bfgs(), and stan::services::optimize::lbfgs().

111 { return _alpha0; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
const Scalar& stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::curr_f ( ) const
inline

Definition at line 91 of file bfgs.hpp.

Referenced by TEST().

91 { return _fk; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
const VectorT& stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::curr_g ( ) const
inline

Definition at line 93 of file bfgs.hpp.

Referenced by stan::services::optimize::bfgs(), stan::services::optimize::lbfgs(), and TEST().

93 { return _gk; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
const VectorT& stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::curr_p ( ) const
inline

Definition at line 94 of file bfgs.hpp.

Referenced by TEST().

94 { return _pk; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
const VectorT& stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::curr_x ( ) const
inline

Definition at line 92 of file bfgs.hpp.

Referenced by TEST().

92 { return _xk; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
std::string stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::get_code_string ( int  retCode)
inline

Definition at line 117 of file bfgs.hpp.

Referenced by stan::services::optimize::bfgs(), and stan::services::optimize::lbfgs().

117  {
118  switch (retCode) {
119  case TERM_SUCCESS:
120  return std::string("Successful step completed");
121  case TERM_ABSF:
122  return std::string("Convergence detected: absolute change "
123  "in objective function was below tolerance");
124  case TERM_RELF:
125  return std::string("Convergence detected: relative change "
126  "in objective function was below tolerance");
127  case TERM_ABSGRAD:
128  return std::string("Convergence detected: "
129  "gradient norm is below tolerance");
130  case TERM_RELGRAD:
131  return std::string("Convergence detected: relative "
132  "gradient magnitude is below tolerance");
133  case TERM_ABSX:
134  return std::string("Convergence detected: "
135  "absolute parameter change was below tolerance");
136  case TERM_MAXIT:
137  return std::string("Maximum number of iterations hit, "
138  "may not be at an optima");
139  case TERM_LSFAIL:
140  return std::string("Line search failed to achieve a sufficient "
141  "decrease, no more progress can be made");
142  default:
143  return std::string("Unknown termination code");
144  }
145  }
enum BeamMode string
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
QNUpdateType& stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::get_qnupdate ( )
inline

Definition at line 88 of file bfgs.hpp.

Referenced by stan::services::optimize::lbfgs().

88 { return _qn; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
const QNUpdateType& stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::get_qnupdate ( ) const
inline

Definition at line 89 of file bfgs.hpp.

89 { return _qn; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
void stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::initialize ( const VectorT x0)
inline

Definition at line 149 of file bfgs.hpp.

Referenced by TEST().

149  {
150  int ret;
151  _xk = x0;
152  ret = _func(_xk, _fk, _gk);
153  if (ret) {
154  throw std::runtime_error("Error evaluating initial BFGS point.");
155  }
156  _pk = -_gk;
157 
158  _itNum = 0;
159  _note = "";
160  }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
const size_t stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::iter_num ( ) const
inline

Definition at line 113 of file bfgs.hpp.

Referenced by stan::services::optimize::bfgs(), stan::services::optimize::lbfgs(), and TEST().

113 { return _itNum; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
int stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::minimize ( VectorT x0)
inline

Definition at line 277 of file bfgs.hpp.

277  {
278  int retcode;
279  initialize(x0);
280  while (!(retcode = step()))
281  continue;
282  x0 = _xk;
283  return retcode;
284  }
void initialize(const VectorT &x0)
Definition: bfgs.hpp:149
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
const std::string& stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::note ( ) const
inline

Definition at line 115 of file bfgs.hpp.

Referenced by stan::services::optimize::bfgs(), and stan::services::optimize::lbfgs().

115 { return _note; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
const Scalar& stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::prev_f ( ) const
inline

Definition at line 96 of file bfgs.hpp.

96 { return _fk_1; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
const VectorT& stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::prev_g ( ) const
inline

Definition at line 98 of file bfgs.hpp.

98 { return _gk_1; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
const VectorT& stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::prev_p ( ) const
inline

Definition at line 99 of file bfgs.hpp.

99 { return _pk_1; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
Scalar stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::prev_step_size ( ) const
inline

Definition at line 100 of file bfgs.hpp.

Referenced by stan::services::optimize::bfgs(), and stan::services::optimize::lbfgs().

100 { return _pk_1.norm()*_alphak_1; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
const VectorT& stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::prev_x ( ) const
inline

Definition at line 97 of file bfgs.hpp.

97 { return _xk_1; }
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
Scalar stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::rel_grad_norm ( ) const
inline

Definition at line 102 of file bfgs.hpp.

102  {
103  return -_pk.dot(_gk) / std::max(std::fabs(_fk), _conv_opts.fScale);
104  }
T max(const caf::Proxy< T > &a, T b)
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
ConvergenceOptions< Scalar > _conv_opts
Definition: bfgs.hpp:86
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
Scalar stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::rel_obj_decrease ( ) const
inline

Definition at line 105 of file bfgs.hpp.

105  {
106  return std::fabs(_fk_1 - _fk) / std::max(std::fabs(_fk_1),
108  _conv_opts.fScale));
109  }
T max(const caf::Proxy< T > &a, T b)
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
ConvergenceOptions< Scalar > _conv_opts
Definition: bfgs.hpp:86
template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
int stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::step ( )
inline

Definition at line 162 of file bfgs.hpp.

Referenced by stan::services::optimize::bfgs(), stan::services::optimize::lbfgs(), and TEST().

162  {
163  Scalar gradNorm, stepNorm;
164  VectorT sk, yk;
165  int retCode(0);
166  int resetB(0);
167 
168  _itNum++;
169 
170  if (_itNum == 1) {
171  resetB = 1;
172  _note = "";
173  } else {
174  resetB = 0;
175  _note = "";
176  }
177 
178  while (true) {
179  if (resetB) {
180  // Reset the Hessian approximation
181  _pk.noalias() = -_gk;
182  }
183 
184  // Get an initial guess for the step size (alpha)
185  if (_itNum > 1 && resetB != 2) {
186  // use cubic interpolation based on the previous step
187  _alpha0 = _alpha = std::min(1.0,
188  1.01*CubicInterp(_gk_1.dot(_pk_1),
189  _alphak_1,
190  _fk - _fk_1,
191  _gk.dot(_pk_1),
192  _ls_opts.minAlpha,
193  1.0));
194  } else {
195  // On the first step (or, after a reset) use the default step size
196  _alpha0 = _alpha = _ls_opts.alpha0;
197  }
198 
199  // Perform the line search. If successful, the results are in the
200  // variables: _xk_1, _fk_1 and _gk_1.
202  _pk, _xk, _fk, _gk,
203  _ls_opts.c1, _ls_opts.c2,
204  _ls_opts.minAlpha);
205  if (retCode) {
206  // Line search failed...
207  if (resetB) {
208  // did a Hessian reset and it still failed,
209  // and nothing left to try
211  return retCode;
212  } else {
213  // try resetting the Hessian approximation
214  resetB = 2;
215  _note += "LS failed, Hessian reset";
216  continue;
217  }
218  } else {
219  break;
220  }
221  }
222 
223  // Swap things so that k is the most recent iterate
224  std::swap(_fk, _fk_1);
225  _xk.swap(_xk_1);
226  _gk.swap(_gk_1);
227  _pk.swap(_pk_1);
228 
229  sk.noalias() = _xk - _xk_1;
230  yk.noalias() = _gk - _gk_1;
231 
232  gradNorm = _gk.norm();
233  stepNorm = sk.norm();
234 
235  // Update QN approximation
236  if (resetB) {
237  // If the QN approximation was reset, automatically scale it
238  // and update the step-size accordingly
239  Scalar B0fact = _qn.update(yk, sk, true);
240  _pk_1 /= B0fact;
241  _alphak_1 = _alpha*B0fact;
242  } else {
243  _qn.update(yk, sk);
244  _alphak_1 = _alpha;
245  }
246  // Compute search direction for next step
247  _qn.search_direction(_pk, _gk);
248 
249  // Check for convergence
250  if (std::fabs(_fk_1 - _fk) < _conv_opts.tolAbsF) {
251  // Objective function improvement wasn't sufficient
252  retCode = TERM_ABSF;
253  } else if (gradNorm < _conv_opts.tolAbsGrad) {
254  retCode = TERM_ABSGRAD; // Gradient norm was below threshold
255  } else if (stepNorm < _conv_opts.tolAbsX) {
256  retCode = TERM_ABSX; // Change in x was too small
257  } else if (_itNum >= _conv_opts.maxIts) {
258  retCode = TERM_MAXIT; // Max number of iterations hit
259  } else if (rel_obj_decrease()
260  < _conv_opts.tolRelF
262  // Relative improvement in objective function wasn't sufficient
263  retCode = TERM_RELF;
264  } else if (rel_grad_norm()
265  < _conv_opts.tolRelGrad
267  // Relative gradient norm was below threshold
269  } else {
270  // Step was successful more progress to be made
272  }
273 
274  return retCode;
275  }
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
int WolfeLineSearch(FunctorType &func, Scalar &alpha, XType &x1, Scalar &f1, XType &gradx1, const XType &p, const XType &x0, const Scalar &f0, const XType &gradx0, const Scalar &c1, const Scalar &c2, const Scalar &minAlpha)
Eigen::Matrix< Scalar, DimAtCompile, 1 > VectorT
Definition: bfgs.hpp:72
void swap(art::HLTGlobalStatus &lhs, art::HLTGlobalStatus &rhs)
Scalar rel_obj_decrease() const
Definition: bfgs.hpp:105
LSOptions< Scalar > _ls_opts
Definition: bfgs.hpp:85
ConvergenceOptions< Scalar > _conv_opts
Definition: bfgs.hpp:86
Scalar CubicInterp(const Scalar &df0, const Scalar &x1, const Scalar &f1, const Scalar &df1, const Scalar &loX, const Scalar &hiX)
double epsilon
Scalar rel_grad_norm() const
Definition: bfgs.hpp:102
T min(const caf::Proxy< T > &a, T b)

Member Data Documentation

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
Scalar stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_alpha
protected

Definition at line 79 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
Scalar stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_alpha0
protected

Definition at line 79 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
Scalar stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_alphak_1
protected

Definition at line 78 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
ConvergenceOptions<Scalar> stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_conv_opts

Definition at line 86 of file bfgs.hpp.

Referenced by stan::services::optimize::bfgs(), stan::services::optimize::lbfgs(), and TEST().

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
Scalar stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_fk
protected

Definition at line 78 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
Scalar stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_fk_1
protected

Definition at line 78 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
FunctorType& stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_func
protected

Definition at line 76 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
VectorT stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_gk
protected

Definition at line 77 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
VectorT stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_gk_1
protected

Definition at line 77 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
size_t stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_itNum
protected

Definition at line 80 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
LSOptions<Scalar> stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_ls_opts

Definition at line 85 of file bfgs.hpp.

Referenced by stan::services::optimize::bfgs(), and stan::services::optimize::lbfgs().

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
std::string stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_note
protected

Definition at line 81 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
VectorT stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_pk
protected

Definition at line 77 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
VectorT stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_pk_1
protected

Definition at line 77 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
QNUpdateType stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_qn
protected

Definition at line 82 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
VectorT stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_xk
protected

Definition at line 77 of file bfgs.hpp.

template<typename FunctorType, typename QNUpdateType, typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
VectorT stan::optimization::BFGSMinimizer< FunctorType, QNUpdateType, Scalar, DimAtCompile >::_xk_1
protected

Definition at line 77 of file bfgs.hpp.


The documentation for this class was generated from the following file: