5 #include "Implementation/ProblemTraits.hpp" 22 template<
typename Problem,
typename Scalar,
typename ErrorHandler>
33 template <
typename ... Args>
35 ErrorHandler& error_handler,
37 : Problem(std::forward<Args>(args)...)
38 , m_error_handler(error_handler)
53 m_derivative_epsilon = derivative_epsilon;
65 m_compare_epsilon = compare_epsilon;
73 return m_derivative_epsilon;
81 return m_compare_epsilon;
102 = Problem::gradient(x);
106 for (Eigen::Index i = 0; i < x.size(); ++i) {
108 x_plus(i) += m_derivative_epsilon;
111 x_minus(i) -= m_derivative_epsilon;
113 if (!x_value_is_ok<Problem>(x_minus, i) || !x_value_is_ok<Problem>(x_plus, i)) {
117 Scalar score_plus = Problem::value(x_plus);
119 Scalar score_minus = Problem::value(x_minus);
121 Scalar numeric_first_derivative =
122 (score_plus - score_minus) / (Scalar{2} * m_derivative_epsilon);
123 numerical_gradient(i) = numeric_first_derivative;
125 if (abs(numeric_first_derivative - g(i)) > m_compare_epsilon) {
132 m_error_handler(x, numerical_gradient, g);
142 typename std::enable_if<!Implementation::problem_traits<U, Scalar>::has_lower_bound,
152 typename std::enable_if<Implementation::problem_traits<U, Scalar>::has_lower_bound,
153 void>::type* =
nullptr>
158 return x(i) >= this->lower_bounds()(i) && x(i) <= this->upper_bounds()(i);
161 ErrorHandler& m_error_handler;
163 Scalar m_derivative_epsilon;
164 Scalar m_compare_epsilon;
Debug wrapper for any problem.
Definition: DebugProblem.hpp:23
const Scalar & getCompareEpsilon() const
Get the epsilon value for comparison of analytical and numerical derivative.
Definition: DebugProblem.hpp:79
void setDerivativeEpsilon(const Scalar &derivative_epsilon=1e-6)
Set the epsilon value for numerical derivative calculation.
Definition: DebugProblem.hpp:51
Implementation::problem_traits< Problem, Scalar >::gradient_type gradient(const Vector< Scalar > &x)
Calculate and check the gradient.
Definition: DebugProblem.hpp:97
const Scalar & getDerivativeEpsilon() const
Get the epsilon value for numerical derivative calculation.
Definition: DebugProblem.hpp:71
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Vector type used.
Definition: Types.hpp:15
void setCompareEpsilon(const Scalar &compare_epsilon=1e-3)
Set the epsilon value for comparison of analytical and numerical derivative.
Definition: DebugProblem.hpp:63
DebugProblem(ErrorHandler &error_handler, Args &&... args)
Construct a new debug problem.
Definition: DebugProblem.hpp:34
decltype(gradient_probe(*dummy_problem)) gradient_type
return type of the gradient function; void if not provided
Definition: ProblemTraits.hpp:77
BFGS optimizations.
Definition: BFGS.hpp:24