Commit 4113ddf4 authored by Konstantinos Chatzilygeroudis's avatar Konstantinos Chatzilygeroudis
Browse files

First attempt to provide support for NLOpt

parent 0d79ff02
#ifndef LIMBO_OPT_NLOPT_HPP
#define LIMBO_OPT_NLOPT_HPP
#include <Eigen/Core>
#include <vector>
#include <nlopt.hpp>
namespace limbo {
namespace opt {
template <typename Params, nlopt::algorithm Algorithm = nlopt::LD_MMA>
struct NLOpt {
public:
template <typename F>
Eigen::VectorXd operator()(const F& f) const
{
nlopt::opt opt(Algorithm, f.param_size());
opt.set_min_objective(this->nlopt_func, NULL);
std::vector<double> x(f.init().size());
for(int i=0;i<f.init().size();i++)
x[i] = f.init()(i);
opt.set_ftol_rel(1e-4);
double min;
opt.optimize(x, min);
return Eigen::VectorXd::Map(x.data(), x.size());
}
protected:
static double nlopt_func(const std::vector<double> &x, std::vector<double> &grad, void *my_func_data)
{
Eigen::VectorXd params = Eigen::VectorXd::Map(x.data(), x.size());
double v = x[0]*x[0]+x[1]*x[1];
if(!grad.empty())
{
Eigen::VectorXd g;
grad[0] = 2*x[0];
grad[1] = 2*x[1];
}
return v;
}
};
}
}
#endif
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE nlopt_test
#include <boost/test/unit_test.hpp>
#include <limbo/limbo.hpp>
#include <limbo/opt/nlopt.hpp>
struct Params {
};
struct TestOpt {
public:
TestOpt() {}
double utility(const Eigen::VectorXd& params) const
{
return params(0)*params(0)+params(1)*params(1);
}
std::pair<double, Eigen::VectorXd> utility_and_grad(const Eigen::VectorXd& params) const
{
double v = params(0)*params(0)+params(1)*params(1);
Eigen::VectorXd grad(2);
grad(0) = 2*params(0);
grad(1) = 2*params(1);
return std::make_pair(v,grad);
}
size_t param_size() const
{
return 2;
}
Eigen::VectorXd init() const
{
return (Eigen::VectorXd::Random(param_size()).array() - 1);
}
};
BOOST_AUTO_TEST_CASE(test_nlopt_simple)
{
TestOpt util;
Eigen::VectorXd g = limbo::opt::NLOpt<Params>()(util);
std::cout<<g<<std::endl;
}
......@@ -41,6 +41,12 @@ def build(bld):
target='test_boptimizer',
uselib='BOOST EIGEN TBB SFERES',
use='limbo')
bld.program(features='cxx test',
source='test_nlopt.cpp',
includes='. .. ../../',
target='test_nlopt',
uselib='BOOST EIGEN TBB NLOPT',
use='limbo')
def build_extensive_tests(ctx):
......
#! /usr/bin/env python
# encoding: utf-8
# Konstantinos Chatzilygeroudis - 2015
"""
Quick n dirty nlopt detection
"""
import os, glob, types
from waflib.Configure import conf
def options(opt):
opt.add_option('--nlopt', type='string', help='path to nlopt', dest='nlopt')
@conf
def check_nlopt(conf):
if conf.options.nlopt:
conf.env.INCLUDES_NLOPT = [conf.options.nlopt]
conf.env.LIBPATH_NLOPT = [conf.options.nlopt]
conf.env.LIB_NLOPT = ['nlopt_cxx']
else:
conf.env.INCLUDES_NLOPT = [os.environ['RESIBOTS_DIR'] + '/include', '/usr/local/include']
conf.env.LIBPATH_NLOPT = [os.environ['RESIBOTS_DIR'] + '/lib', '/usr/local/lib']
conf.env.LIB_NLOPT = ['nlopt_cxx']
res = conf.find_file('nlopt.hpp', conf.env.INCLUDES_NLOPT)
return 1
......@@ -25,6 +25,7 @@ def options(opt):
opt.load('sferes')
opt.load('limbo')
opt.load('openmp')
opt.load('nlopt')
#opt.load('ode')
opt.add_option('--exp', type='string', help='exp(s) to build, separate by comma', dest='exp')
opt.add_option('--qsub', type='string', help='config file (json) to submit to torque', dest='qsub')
......@@ -43,6 +44,7 @@ def configure(conf):
conf.load('openmp')
conf.load('mkl')
conf.load('xcode')
conf.load('nlopt')
if conf.env.CXX_NAME in ["icc", "icpc"]:
common_flags = "-Wall -std=c++11"
......@@ -62,6 +64,7 @@ def configure(conf):
conf.check_sferes()
conf.check_openmp()
conf.check_mkl()
conf.check_nlopt()
#conf.check_ode()
if conf.env['CXXFLAGS_ODE']:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment