mlpack  3.0.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
add_evaluate_with_gradient.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_OPTIMIZERS_FUNCTION_ADD_EVALUATE_WITH_GRADIENT_HPP
14 #define MLPACK_CORE_OPTIMIZERS_FUNCTION_ADD_EVALUATE_WITH_GRADIENT_HPP
15 
16 #include <mlpack/prereqs.hpp>
18 #include "traits.hpp"
19 
20 namespace mlpack {
21 namespace optimization {
22 
28 template<typename FunctionType,
29  // Check if there is at least one non-const Evaluate() or Gradient().
30  bool HasEvaluateGradient = traits::HasNonConstSignatures<
31  FunctionType,
32  traits::HasEvaluate,
36  traits::HasGradient,
40  bool HasEvaluateWithGradient = traits::HasEvaluateWithGradient<
41  FunctionType,
44 {
45  public:
46  // Provide a dummy overload so the name 'EvaluateWithGradient' exists for this
47  // object.
49 };
50 
54 template<typename FunctionType, bool HasEvaluateGradient>
55 class AddEvaluateWithGradient<FunctionType, HasEvaluateGradient, true>
56 {
57  public:
58  // Reflect the existing EvaluateWithGradient().
59  double EvaluateWithGradient(const arma::mat& coordinates, arma::mat& gradient)
60  {
61  return static_cast<FunctionType*>(
62  static_cast<Function<FunctionType>*>(this))->EvaluateWithGradient(
63  coordinates, gradient);
64  }
65 };
66 
71 template<typename FunctionType>
72 class AddEvaluateWithGradient<FunctionType, true, false>
73 {
74  public:
82  double EvaluateWithGradient(const arma::mat& coordinates,
83  arma::mat& gradient)
84  {
85  const double objective =
86  static_cast<Function<FunctionType>*>(this)->Evaluate(coordinates);
87  static_cast<Function<FunctionType>*>(this)->Gradient(coordinates, gradient);
88  return objective;
89  }
90 };
91 
97 template<typename FunctionType,
98  // Check if there is at least one const Evaluate() or Gradient().
99  bool HasEvaluateGradient = traits::HasConstSignatures<
100  FunctionType,
101  traits::HasEvaluate,
104  traits::HasGradient,
107  bool HasEvaluateWithGradient = traits::HasEvaluateWithGradient<
108  FunctionType,
111 {
112  public:
113  // Provide a dummy overload so the name 'EvaluateWithGradient' exists for this
114  // object.
116 };
117 
121 template<typename FunctionType, bool HasEvaluateGradient>
122 class AddEvaluateWithGradientConst<FunctionType, HasEvaluateGradient, true>
123 {
124  public:
125  // Reflect the existing EvaluateWithGradient().
126  double EvaluateWithGradient(const arma::mat& coordinates, arma::mat& gradient)
127  const
128  {
129  return static_cast<const FunctionType*>(
130  static_cast<const Function<FunctionType>*>(this))->EvaluateWithGradient(
131  coordinates, gradient);
132  }
133 };
134 
139 template<typename FunctionType>
140 class AddEvaluateWithGradientConst<FunctionType, true, false>
141 {
142  public:
150  double EvaluateWithGradient(const arma::mat& coordinates,
151  arma::mat& gradient) const
152  {
153  const double objective =
154  static_cast<const Function<FunctionType>*>(this)->Evaluate(coordinates);
155  static_cast<const Function<FunctionType>*>(this)->Gradient(coordinates,
156  gradient);
157  return objective;
158  }
159 };
160 
167 template<typename FunctionType,
168  bool HasEvaluateGradient =
169  traits::HasEvaluate<FunctionType,
170  traits::EvaluateStaticForm>::value &&
171  traits::HasGradient<FunctionType,
173  bool HasEvaluateWithGradient =
174  traits::HasEvaluateWithGradient<FunctionType,
177 {
178  public:
179  // Provide a dummy overload so the name 'EvaluateWithGradient' exists for this
180  // object.
182 };
183 
187 template<typename FunctionType, bool HasEvaluateGradient>
188 class AddEvaluateWithGradientStatic<FunctionType, HasEvaluateGradient, true>
189 {
190  public:
191  // Reflect the existing EvaluateWithGradient().
192  static double EvaluateWithGradient(const arma::mat& coordinates,
193  arma::mat& gradient)
194  {
195  return FunctionType::EvaluateWithGradient(coordinates, gradient);
196  }
197 };
198 
203 template<typename FunctionType>
204 class AddEvaluateWithGradientStatic<FunctionType, true, false>
205 {
206  public:
214  static double EvaluateWithGradient(const arma::mat& coordinates,
215  arma::mat& gradient)
216  {
217  const double objective = FunctionType::Evaluate(coordinates);
218  FunctionType::Gradient(coordinates, gradient);
219  return objective;
220  }
221 };
222 
223 } // namespace optimization
224 } // namespace mlpack
225 
226 #endif
double(*)(const arma::mat &) EvaluateStaticForm
This is the form of a static Evaluate() method.
Definition: traits.hpp:55
double(*)(const arma::mat &, arma::mat &) EvaluateWithGradientStaticForm
This is the form of a static EvaluateWithGradient() method.
Definition: traits.hpp:83
The AddEvaluateWithGradient mixin class will provide an EvaluateWithGradient() method if the given Fu...
double EvaluateWithGradient(const arma::mat &coordinates, arma::mat &gradient)
Return both the evaluated objective function and its gradient, storing the gradient in the given matr...
void(*)(const arma::mat &, arma::mat &) GradientStaticForm
This is the form of a static Gradient() method.
Definition: traits.hpp:68
double(FunctionType::*)(const arma::mat &) const EvaluateConstForm
This is the form of a const Evaluate() method.
Definition: traits.hpp:51
The AddEvaluateWithGradientStatic mixin class will provide a static EvaluateWithGradient() method if ...
void(FunctionType::*)(const arma::mat &, arma::mat &) const GradientConstForm
This is the form of a const Gradient() method.
Definition: traits.hpp:64
double(FunctionType::*)(const arma::mat &, arma::mat &) const EvaluateWithGradientConstForm
This is the form of a const EvaluateWithGradient() method.
Definition: traits.hpp:78
static double EvaluateWithGradient(const arma::mat &coordinates, arma::mat &gradient)
Return both the evaluated objective function and its gradient, storing the gradient in the given matr...
The core includes that mlpack expects; standard C++ includes and Armadillo.
double(FunctionType::*)(const arma::mat &) EvaluateForm
This is the form of a non-const Evaluate() method.
Definition: traits.hpp:46
double EvaluateWithGradient(traits::UnconstructableType &)
void(FunctionType::*)(const arma::mat &, arma::mat &) GradientForm
This is the form of a non-const Gradient() method.
Definition: traits.hpp:59
double EvaluateWithGradient(traits::UnconstructableType &) const
The AddEvaluateWithGradient mixin class will provide an EvaluateWithGradient() const method if the gi...
This is a utility type used to provide unusable overloads from each of the mixin classes.
Definition: traits.hpp:255
double EvaluateWithGradient(const arma::mat &coordinates, arma::mat &gradient) const
Return both the evaluated objective function and its gradient, storing the gradient in the given matr...
static double EvaluateWithGradient(traits::UnconstructableType &)
double(FunctionType::*)(const arma::mat &, arma::mat &) EvaluateWithGradientForm
This is the form of a non-const EvaluateWithGradient() method.
Definition: traits.hpp:73
The Function class is a wrapper class for any FunctionType that will add any possible derived methods...
Definition: function.hpp:22