optimizer.hpp 1.11 KB
Newer Older
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef LIMBO_OPT_OPTIMIZER_HPP
#define LIMBO_OPT_OPTIMIZER_HPP

#include <tuple>

#include <Eigen/Core>

#include <boost/optional.hpp>

namespace limbo {
    namespace opt {
      // return type of the function to optimize
      typedef std::pair<double, boost::optional<Eigen::VectorXd> > eval_t;

      // return with opt::no_grand(your_val) if no gradient is available
      eval_t no_grad(double x) { return eval_t{x, boost::optional<Eigen::VectorXd>{} } ;}

      // get the gradient from a function evaluation
      const Eigen::VectorXd& grad(const eval_t& fg) {
        assert(std::get<1>(fg).is_initialized());
        return std::get<1>(fg).get();
      }

      // get the value from a function evaluation
      double fun(const eval_t& fg) {
        return std::get<0>(fg);
      }

      // eval f without gradient
      template<typename F>
      double eval(const F& f, const Eigen::VectorXd& x) {
        return std::get<0>(f(x, false));
      }

      // eval f with gradient
      template<typename F>
      eval_t eval_grad(const F& f, const Eigen::VectorXd& x) {
        return f(x, true);
      }

    }
}

#endif