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

#include "stan/optimization/bfgs.hpp"

Inheritance diagram for stan::optimization::BFGSLineSearch< M, QNUpdateType, Scalar, DimAtCompile >:
stan::optimization::BFGSMinimizer< ModelAdaptor< M >, QNUpdateType, Scalar, DimAtCompile >

Public Types

typedef BFGSMinimizer< ModelAdaptor< M >, QNUpdateType, Scalar, DimAtCompile > BFGSBase
 
typedef BFGSBase::VectorT vector_t
 
typedef stan::math::index_type< vector_t >::type idx_t
 
typedef Eigen::Matrix< Scalar, DimAtCompile, 1 > VectorT
 
typedef Eigen::Matrix< Scalar, DimAtCompile, DimAtCompile > HessianT
 

Public Member Functions

 BFGSLineSearch (M &model, const std::vector< double > &params_r, const std::vector< int > &params_i, std::ostream *msgs=0)
 
void initialize (const std::vector< double > &params_r)
 
size_t grad_evals ()
 
double logp ()
 
double grad_norm ()
 
void grad (std::vector< double > &g)
 
void params_r (std::vector< double > &x)
 
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)
 
void initialize (const VectorT &x0)
 
int step ()
 
int minimize (VectorT &x0)
 

Public Attributes

LSOptions< Scalar > _ls_opts
 
ConvergenceOptions< Scalar > _conv_opts
 

Protected Attributes

ModelAdaptor< M > & _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
 

Private Attributes

ModelAdaptor< M > _adaptor
 

Detailed Description

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

Definition at line 385 of file bfgs.hpp.

Member Typedef Documentation

template<typename M , typename QNUpdateType , typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
typedef BFGSMinimizer<ModelAdaptor<M>, QNUpdateType, Scalar, DimAtCompile> stan::optimization::BFGSLineSearch< M, QNUpdateType, Scalar, DimAtCompile >::BFGSBase

Definition at line 393 of file bfgs.hpp.

typedef Eigen::Matrix<Scalar, DimAtCompile, DimAtCompile> stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::HessianT
inherited

Definition at line 73 of file bfgs.hpp.

template<typename M , typename QNUpdateType , typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
typedef stan::math::index_type<vector_t>::type stan::optimization::BFGSLineSearch< M, QNUpdateType, Scalar, DimAtCompile >::idx_t

Definition at line 395 of file bfgs.hpp.

template<typename M , typename QNUpdateType , typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
typedef BFGSBase::VectorT stan::optimization::BFGSLineSearch< M, QNUpdateType, Scalar, DimAtCompile >::vector_t

Definition at line 394 of file bfgs.hpp.

typedef Eigen::Matrix<Scalar, DimAtCompile, 1> stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::VectorT
inherited

Definition at line 72 of file bfgs.hpp.

Constructor & Destructor Documentation

template<typename M , typename QNUpdateType , typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
stan::optimization::BFGSLineSearch< M, QNUpdateType, Scalar, DimAtCompile >::BFGSLineSearch ( M &  model,
const std::vector< double > &  params_r,
const std::vector< int > &  params_i,
std::ostream *  msgs = 0 
)
inline

Definition at line 397 of file bfgs.hpp.

References stan::services::util::initialize().

401  : BFGSBase(_adaptor),
402  _adaptor(model, params_i, msgs) {
404  }
void initialize(const std::vector< double > &params_r)
Definition: bfgs.hpp:406
void params_r(std::vector< double > &x)
Definition: bfgs.hpp:423
BFGSMinimizer< ModelAdaptor< M >, QNUpdateType, Scalar, DimAtCompile > BFGSBase
Definition: bfgs.hpp:393
ModelAdaptor< M > _adaptor
Definition: bfgs.hpp:389
const XML_Char XML_Content * model
Definition: expat.h:151

Member Function Documentation

const Scalar& stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::alpha ( ) const
inlineinherited
const Scalar& stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::alpha0 ( ) const
inlineinherited
const Scalar& stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::curr_f ( ) const
inlineinherited
const VectorT& stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::curr_g ( ) const
inlineinherited
const VectorT& stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::curr_p ( ) const
inlineinherited
const VectorT& stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::curr_x ( ) const
inlineinherited
std::string stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::get_code_string ( int  retCode)
inlineinherited

Definition at line 117 of file bfgs.hpp.

References string, stan::optimization::TERM_ABSF, stan::optimization::TERM_ABSGRAD, stan::optimization::TERM_ABSX, stan::optimization::TERM_LSFAIL, stan::optimization::TERM_MAXIT, stan::optimization::TERM_RELF, stan::optimization::TERM_RELGRAD, and stan::optimization::TERM_SUCCESS.

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
QNUpdateType& stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::get_qnupdate ( )
inlineinherited
const QNUpdateType& stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::get_qnupdate ( ) const
inlineinherited
template<typename M , typename QNUpdateType , typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
void stan::optimization::BFGSLineSearch< M, QNUpdateType, Scalar, DimAtCompile >::grad ( std::vector< double > &  g)
inline

Definition at line 417 of file bfgs.hpp.

References MECModelEnuComparisons::i.

417  {
418  const vector_t &cg(this->curr_g());
419  g.resize(cg.size());
420  for (idx_t i = 0; i < cg.size(); i++)
421  g[i] = -cg[i];
422  }
BFGSBase::VectorT vector_t
Definition: bfgs.hpp:394
stan::math::index_type< vector_t >::type idx_t
Definition: bfgs.hpp:395
template<typename M , typename QNUpdateType , typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
size_t stan::optimization::BFGSLineSearch< M, QNUpdateType, Scalar, DimAtCompile >::grad_evals ( )
inline

Definition at line 414 of file bfgs.hpp.

References stan::optimization::ModelAdaptor< M >::fevals().

414 { return _adaptor.fevals(); }
ModelAdaptor< M > _adaptor
Definition: bfgs.hpp:389
template<typename M , typename QNUpdateType , typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
double stan::optimization::BFGSLineSearch< M, QNUpdateType, Scalar, DimAtCompile >::grad_norm ( )
inline

Definition at line 416 of file bfgs.hpp.

void stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::initialize ( const VectorT x0)
inlineinherited
template<typename M , typename QNUpdateType , typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
void stan::optimization::BFGSLineSearch< M, QNUpdateType, Scalar, DimAtCompile >::initialize ( const std::vector< double > &  params_r)
inline

Definition at line 406 of file bfgs.hpp.

References MECModelEnuComparisons::i, stan::services::util::initialize(), and submit_syst::x.

406  {
407  Eigen::Matrix<double, Eigen::Dynamic, 1> x;
408  x.resize(params_r.size());
409  for (size_t i = 0; i < params_r.size(); i++)
410  x[i] = params_r[i];
412  }
void params_r(std::vector< double > &x)
Definition: bfgs.hpp:423
const size_t stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::iter_num ( ) const
inlineinherited
template<typename M , typename QNUpdateType , typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
double stan::optimization::BFGSLineSearch< M, QNUpdateType, Scalar, DimAtCompile >::logp ( )
inline

Definition at line 415 of file bfgs.hpp.

int stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::minimize ( VectorT x0)
inlineinherited
const std::string& stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::note ( ) const
inlineinherited
template<typename M , typename QNUpdateType , typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
void stan::optimization::BFGSLineSearch< M, QNUpdateType, Scalar, DimAtCompile >::params_r ( std::vector< double > &  x)
inline

Definition at line 423 of file bfgs.hpp.

References MECModelEnuComparisons::i.

423  {
424  const vector_t &cx(this->curr_x());
425  x.resize(cx.size());
426  for (idx_t i = 0; i < cx.size(); i++)
427  x[i] = cx[i];
428  }
BFGSBase::VectorT vector_t
Definition: bfgs.hpp:394
stan::math::index_type< vector_t >::type idx_t
Definition: bfgs.hpp:395
const Scalar& stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::prev_f ( ) const
inlineinherited
const VectorT& stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::prev_g ( ) const
inlineinherited
const VectorT& stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::prev_p ( ) const
inlineinherited
Scalar stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::prev_step_size ( ) const
inlineinherited
const VectorT& stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::prev_x ( ) const
inlineinherited
Scalar stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::rel_grad_norm ( ) const
inlineinherited
Scalar stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::rel_obj_decrease ( ) const
inlineinherited
int stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::step ( )
inlineinherited

Definition at line 162 of file bfgs.hpp.

References stan::optimization::LSOptions< Scalar >::alpha0, stan::optimization::LSOptions< Scalar >::c1, stan::optimization::LSOptions< Scalar >::c2, stan::optimization::CubicInterp(), epsilon, stan::math::fabs(), stan::optimization::ConvergenceOptions< Scalar >::maxIts, std::min(), stan::optimization::LSOptions< Scalar >::minAlpha, runNovaSAM::retCode, std::swap(), stan::optimization::TERM_ABSF, stan::optimization::TERM_ABSGRAD, stan::optimization::TERM_ABSX, stan::optimization::TERM_LSFAIL, stan::optimization::TERM_MAXIT, stan::optimization::TERM_RELF, stan::optimization::TERM_RELGRAD, stan::optimization::TERM_SUCCESS, stan::optimization::ConvergenceOptions< Scalar >::tolAbsF, stan::optimization::ConvergenceOptions< Scalar >::tolAbsGrad, stan::optimization::ConvergenceOptions< Scalar >::tolAbsX, stan::optimization::ConvergenceOptions< Scalar >::tolRelF, stan::optimization::ConvergenceOptions< Scalar >::tolRelGrad, and stan::optimization::WolfeLineSearch().

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)
void swap(art::HLTGlobalStatus &lhs, art::HLTGlobalStatus &rhs)
Scalar CubicInterp(const Scalar &df0, const Scalar &x1, const Scalar &f1, const Scalar &df1, const Scalar &loX, const Scalar &hiX)
double epsilon
T min(const caf::Proxy< T > &a, T b)

Member Data Documentation

template<typename M , typename QNUpdateType , typename Scalar = double, int DimAtCompile = Eigen::Dynamic>
ModelAdaptor<M> stan::optimization::BFGSLineSearch< M, QNUpdateType, Scalar, DimAtCompile >::_adaptor
private

Definition at line 389 of file bfgs.hpp.

Scalar stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_alpha
protectedinherited

Definition at line 79 of file bfgs.hpp.

Scalar stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_alpha0
protectedinherited

Definition at line 79 of file bfgs.hpp.

Scalar stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_alphak_1
protectedinherited

Definition at line 78 of file bfgs.hpp.

ConvergenceOptions<Scalar> stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_conv_opts
inherited

Definition at line 86 of file bfgs.hpp.

Scalar stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_fk
protectedinherited

Definition at line 78 of file bfgs.hpp.

Scalar stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_fk_1
protectedinherited

Definition at line 78 of file bfgs.hpp.

ModelAdaptor< M > & stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_func
protectedinherited

Definition at line 76 of file bfgs.hpp.

VectorT stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_gk
protectedinherited

Definition at line 77 of file bfgs.hpp.

VectorT stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_gk_1
protectedinherited

Definition at line 77 of file bfgs.hpp.

size_t stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_itNum
protectedinherited

Definition at line 80 of file bfgs.hpp.

LSOptions<Scalar> stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_ls_opts
inherited

Definition at line 85 of file bfgs.hpp.

std::string stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_note
protectedinherited

Definition at line 81 of file bfgs.hpp.

VectorT stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_pk
protectedinherited

Definition at line 77 of file bfgs.hpp.

VectorT stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_pk_1
protectedinherited

Definition at line 77 of file bfgs.hpp.

QNUpdateType stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_qn
protectedinherited

Definition at line 82 of file bfgs.hpp.

VectorT stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_xk
protectedinherited

Definition at line 77 of file bfgs.hpp.

VectorT stan::optimization::BFGSMinimizer< ModelAdaptor< M > , QNUpdateType, Scalar, DimAtCompile >::_xk_1
protectedinherited

Definition at line 77 of file bfgs.hpp.


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