bpp-core3  3.0.0
BfgsMultiDimensions.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_NUMERIC_FUNCTION_BFGSMULTIDIMENSIONS_H
6 #define BPP_NUMERIC_FUNCTION_BFGSMULTIDIMENSIONS_H
7 
8 
9 #include "../VectorTools.h"
10 #include "AbstractOptimizer.h"
11 #include "DirectionFunction.h"
12 
13 namespace bpp
14 {
25  public AbstractOptimizer
26 {
27 protected:
28  // double gtol_;
29  double slope_;
30 
31  // vectors of the Lower & Upper bounds of the parameters
33 
34  mutable Vdouble p_, gradient_, xi_, dg_, hdg_;
35  mutable VVdouble hessian_;
36 
37  mutable std::shared_ptr<DirectionFunction> f1dim_;
38 
39 public:
40  BfgsMultiDimensions(std::shared_ptr<FirstOrderDerivable> function);
41 
42  virtual ~BfgsMultiDimensions() {}
43 
44  BfgsMultiDimensions* clone() const override { return new BfgsMultiDimensions(*this); }
45 
46 public:
52  void doInit(const ParameterList& params) override;
53 
54  double doStep() override;
58  {
59  if (function_)
60  {
61  return *dynamic_pointer_cast<const FirstOrderDerivable>(function_);
62  }
63  else
64  {
65  throw NullPointerException("BfgsMultiDimensions::firstOrderDerivableFunction() : no function associated to this optimizer.");
66  }
67  }
68 
70  {
71  if (function_)
72  {
73  return *dynamic_pointer_cast<FirstOrderDerivable>(function_);
74  }
75  else
76  {
77  throw NullPointerException("BfgsMultiDimensions::firstOrderDerivableFunction() : no function associated to this optimizer.");
78  }
79  }
80 
81  std::shared_ptr<const FirstOrderDerivable> getFirstOrderDerivableFunction() const
82  {
83  return dynamic_pointer_cast<const FirstOrderDerivable>(function_);
84  }
85 
86  std::shared_ptr<FirstOrderDerivable> getFirstOrderDerivableFunction()
87  {
88  return dynamic_pointer_cast<FirstOrderDerivable>(function_);
89  }
90 
91  void getGradient(std::vector<double>& gradient) const;
92 
93 private:
98  void setDirection();
99 };
100 } // end of namespace bpp.
101 #endif // BPP_NUMERIC_FUNCTION_BFGSMULTIDIMENSIONS_H
const FirstOrderDerivable & firstOrderDerivableFunction() const
This is the abstract class for first order derivable functions.
Definition: Functions.h:96
BfgsMultiDimensions(std::shared_ptr< FirstOrderDerivable > function)
std::shared_ptr< FirstOrderDerivable > getFirstOrderDerivableFunction()
std::shared_ptr< FunctionInterface > function_
The function to optimize.
void doInit(const ParameterList &params) override
This function is called by the init() method and contains all calculations.
The parameter list object.
Definition: ParameterList.h:27
std::shared_ptr< DirectionFunction > f1dim_
FirstOrderDerivable & firstOrderDerivableFunction()
double doStep() override
This function is called by the step() method and contains all calculations.
std::vector< double > Vdouble
Definition: VectorTools.h:34
The base class exception for NULL pointer error. This exception may be thrown when an unexpected NULL...
Definition: Exceptions.h:56
Broyden–Fletcher–Goldfarb–Shanno (BFGS) optimization method.
std::shared_ptr< const FirstOrderDerivable > getFirstOrderDerivableFunction() const
Partial implementation of the Optimizer interface.
void getGradient(std::vector< double > &gradient) const
BfgsMultiDimensions * clone() const override
Create a copy of this object and send a pointer to it.
std::vector< Vdouble > VVdouble
Definition: VectorTools.h:35