add_evaluate.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_OPTIMIZERS_FUNCTION_ADD_EVALUATE_HPP
14 #define MLPACK_CORE_OPTIMIZERS_FUNCTION_ADD_EVALUATE_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 #include "traits.hpp"
18 
19 namespace mlpack {
20 namespace optimization {
21 
26 template<typename FunctionType,
27  bool HasEvaluateWithGradient =
28  traits::HasEvaluateWithGradient<FunctionType,
30  bool HasEvaluate =
31  traits::HasEvaluate<FunctionType, traits::EvaluateForm>::value>
33 {
34  public:
35  // Provide a dummy overload so the name 'Evaluate' exists for this object.
37 };
38 
42 template<typename FunctionType, bool HasEvaluateWithGradient>
43 class AddEvaluate<FunctionType, HasEvaluateWithGradient, true>
44 {
45  public:
46  // Reflect the existing Evaluate().
47  double Evaluate(const arma::mat& coordinates)
48  {
49  return static_cast<FunctionType*>(static_cast<Function<FunctionType>*>(
50  this))->Evaluate(coordinates);
51  }
52 };
53 
58 template<typename FunctionType>
59 class AddEvaluate<FunctionType, true, false>
60 {
61  public:
67  double Evaluate(const arma::mat& coordinates)
68  {
69  arma::mat gradient; // This will be ignored.
70  return static_cast<Function<FunctionType>*>(this)->EvaluateWithGradient(
71  coordinates, gradient);
72  }
73 };
74 
80 template<typename FunctionType,
81  bool HasEvaluateWithGradient =
82  traits::HasEvaluateWithGradient<FunctionType,
84  bool HasEvaluate =
85  traits::HasEvaluate<FunctionType,
88 {
89  public:
90  // Provide a dummy overload so the name 'Evaluate' exists for this object.
92 };
93 
97 template<typename FunctionType, bool HasEvaluateWithGradient>
98 class AddEvaluateConst<FunctionType, HasEvaluateWithGradient, true>
99 {
100  public:
101  // Reflect the existing Evaluate().
102  double Evaluate(const arma::mat& coordinates) const
103  {
104  return static_cast<const FunctionType*>(static_cast<const
105 Function<FunctionType>*>(this))->Evaluate(
106  coordinates);
107  }
108 };
109 
114 template<typename FunctionType>
115 class AddEvaluateConst<FunctionType, true, false>
116 {
117  public:
123  double Evaluate(const arma::mat& coordinates) const
124  {
125  arma::mat gradient; // This will be ignored.
126  return static_cast<
127  const Function<FunctionType>*>(this)->EvaluateWithGradient(coordinates,
128  gradient);
129  }
130 };
131 
137 template<typename FunctionType,
138  bool HasEvaluateWithGradient =
139  traits::HasEvaluateWithGradient<FunctionType,
141  bool HasEvaluate =
142  traits::HasEvaluate<FunctionType,
145 {
146  public:
147  // Provide a dummy overload so the name 'Evaluate' exists for this object.
148  static double Evaluate(traits::UnconstructableType&);
149 };
150 
154 template<typename FunctionType, bool HasEvaluateWithGradient>
155 class AddEvaluateStatic<FunctionType, HasEvaluateWithGradient, true>
156 {
157  public:
158  // Reflect the existing Evaluate().
159  static double Evaluate(const arma::mat& coordinates)
160  {
161  return FunctionType::Evaluate(coordinates);
162  }
163 };
164 
169 template<typename FunctionType>
170 class AddEvaluateStatic<FunctionType, true, false>
171 {
172  public:
178  static double Evaluate(const arma::mat& coordinates)
179  {
180  arma::mat gradient; // This will be ignored.
181  return FunctionType::EvaluateWithGradient(coordinates, gradient);
182  }
183 };
184 
185 } // namespace optimization
186 } // namespace mlpack
187 
188 #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
double Evaluate(traits::UnconstructableType &)
double(FunctionType::*)(const arma::mat &) const EvaluateConstForm
This is the form of a const Evaluate() method.
Definition: traits.hpp:51
.hpp
Definition: add_to_po.hpp:21
double(FunctionType::*)(const arma::mat &, arma::mat &) const EvaluateWithGradientConstForm
This is the form of a const EvaluateWithGradient() method.
Definition: traits.hpp:78
The AddEvaluate mixin class will provide an Evaluate() method if the given FunctionType has EvaluateW...
The core includes that mlpack expects; standard C++ includes and Armadillo.
The AddEvaluateStatic mixin class will provide a static Evaluate() method if the given FunctionType h...
double Evaluate(const arma::mat &coordinates)
Return the objective function for the given coordinates.
double Evaluate(const arma::mat &coordinates) const
Return the objective function for the given coordinates.
This is a utility type used to provide unusable overloads from each of the mixin classes.
Definition: traits.hpp:255
static double Evaluate(const arma::mat &coordinates)
Return the objective function for the given coordinates.
The AddEvaluateConst mixin class will provide a const Evaluate() method if the given FunctionType has...
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