Commit f60ecfc3 authored by Federico Allocati's avatar Federico Allocati
Browse files

first version using cmd line args for the parameters

parent 7f4d42df
......@@ -13,10 +13,14 @@ except:
json_ok = False
print "WARNING simplejson not found some function may not work"
def options(opt):
opt.add_option('--qsub', type='string', help='json file to submit to torque', dest='qsub')
def add_create_options(opt):
opt.add_option('--dim_in', type='int', help='dim_in', dest='dim_in')
opt.add_option('--dim_out', type='int', help='dim_out', dest='dim_out')
opt.add_option('--bayes_opt_boptimizer_noise', type='float', help='bayes_opt_boptimizer_noise', dest='bayes_opt_boptimizer_noise')
opt.add_option('--bayes_opt_bobase_stats_enabled', action='store_true', help='bayes_opt_bobase_stats_enabled', dest='bayes_opt_bobase_stats_enabled')
opt.add_option('--init_randomsampling_samples', type='int', help='init_randomsampling_samples', dest='init_randomsampling_samples')
opt.add_option('--stop_maxiterations_iterations', type='int', help='stop_maxiterations_iterations', dest='stop_maxiterations_iterations')
def create_variants(bld, source, uselib_local,
uselib, variants, includes=". ../",
......@@ -43,9 +47,12 @@ def create_variants(bld, source, uselib_local,
use=uselib_local,
defines=deff)
def create_exp(name):
ws_tpl = """
#! /usr/bin/env python
def create_exp(name, opt):
if not os.path.exists('exp'):
os.makedirs('exp')
os.mkdir('exp/' + name)
ws_tpl = """#! /usr/bin/env python
def configure(conf):
pass
......@@ -54,18 +61,116 @@ def options(opt):
def build(bld):
bld(features='cxx cxxprogram',
source='@exp.cpp',
source='@NAME.cpp',
includes='. ../../src',
target='@exp',
target='@NAME',
uselib='BOOST EIGEN TBB LIBCMAES NLOPT',
use='limbo')
"""
if not os.path.exists('exp'):
os.makedirs('exp')
os.mkdir('exp/' + name)
wscript = open('exp/' + name + "/wscript", "w")
wscript.write(ws_tpl.replace('@exp', name))
os.system("cp src/tutorials/basic_example.cpp exp/" + name + "/" + name + ".cpp")
ws_tpl = ws_tpl.replace('@NAME', name)
ws = open('exp/' + name + "/wscript", "w")
ws.write(ws_tpl)
cpp_tpl = """// please see the explanation in the documentation
#include <iostream>
// you can also include <limbo/limbo.hpp> but it will slow down the compilation
#include <limbo/bayes_opt/boptimizer.hpp>
using namespace limbo;
struct Params {
struct bayes_opt_boptimizer : public defaults::bayes_opt_boptimizer {
@BAYES_OPT_BOPTIMIZER_NOISE};
// depending on which internal optimizer we use, we need to import different parameters
#ifdef USE_LIBCMAES
struct opt_cmaes : public defaults::opt_cmaes {
};
#elif defined(USE_NLOPT)
struct opt_nloptnograd : public defaults::opt_nloptnograd {
};
#else
struct opt_gridsearch : public defaults::opt_gridsearch {
};
#endif
struct bayes_opt_bobase : public defaults::bayes_opt_bobase {
@BAYES_OPT_BOBASE_STATS_ENABLED};
struct kernel_exp : public defaults::kernel_exp {
};
struct init_randomsampling : public defaults::init_randomsampling {
@INIT_RANDOMSAMPLING_SAMPLES};
struct stop_maxiterations : public defaults::stop_maxiterations {
@STOP_MAXITERATIONS_ITERATIONS};
// we use the default parameters for acqui_ucb
struct acqui_ucb : public defaults::acqui_ucb {
};
};
struct Eval {
// number of input dimension (x.size())
static constexpr size_t dim_in = @DIM_IN;
// number of dimenions of the result (res.size())
static constexpr size_t dim_out = @DIM_OUT;
// the function to be optimized
Eigen::VectorXd operator()(const Eigen::VectorXd& x) const
{
@CODE_RES_INIT
// YOUR CODE HERE
@CODE_RES_RETURN
}
};
int main()
{
// we use the default acquisition function / model / stat / etc.
bayes_opt::BOptimizer<Params> boptimizer;
// run the evaluation
boptimizer.optimize(Eval());
// the best sample found
std::cout << "Best sample: " << @CODE_BEST_SAMPLE << " - Best observation: " << @CODE_BEST_OBS << std::endl;
return 0;
}
"""
cpp_params = {}
cpp_params['BAYES_OPT_BOPTIMIZER_NOISE'] = ' BO_PARAM(double, noise, ' + str(opt.bayes_opt_boptimizer_noise) + ');\n ' if opt.bayes_opt_boptimizer_noise and opt.bayes_opt_boptimizer_noise >= 0 else ''
cpp_params['BAYES_OPT_BOBASE_STATS_ENABLED'] = '' if opt.bayes_opt_bobase_stats_enabled else ' BO_PARAM(bool, stats_enabled, false);\n '
cpp_params['INIT_RANDOMSAMPLING_SAMPLES'] = ' BO_PARAM(int, samples, ' + str(opt.init_randomsampling_samples) + ');\n ' if opt.init_randomsampling_samples and opt.init_randomsampling_samples > 0 else ''
cpp_params['STOP_MAXITERATIONS_ITERATIONS'] = ' BO_PARAM(int, iterations, ' + str(opt.stop_maxiterations_iterations) + ');\n ' if opt.stop_maxiterations_iterations and opt.stop_maxiterations_iterations > 0 else ''
cpp_params['DIM_IN'] = str(opt.dim_in) if opt.dim_in and opt.dim_in > 1 else '1'
cpp_params['DIM_OUT'] = str(opt.dim_out) if opt.dim_out and opt.dim_out > 1 else '1'
if opt.dim_in and opt.dim_in > 1:
cpp_params['CODE_BEST_SAMPLE'] = 'boptimizer.best_sample().transpose()'
else:
cpp_params['CODE_BEST_SAMPLE'] = 'boptimizer.best_sample()(0)'
if opt.dim_out and opt.dim_out > 1:
cpp_params['CODE_RES_INIT'] = 'Eigen::VectorXd res(' + str(opt.dim_in) + ')'
cpp_params['CODE_RES_RETURN'] = 'return res;'
cpp_params['CODE_BEST_OBS'] = 'boptimizer.best_observation().transpose()'
else:
cpp_params['CODE_RES_INIT'] = 'double y = 0;'
cpp_params['CODE_RES_RETURN'] = '// return a 1-dimensional vector\n return tools::make_vector(y);'
cpp_params['CODE_BEST_OBS'] = 'boptimizer.best_observation()(0)'
for key, value in cpp_params.iteritems():
cpp_tpl = cpp_tpl.replace('@' + key, value)
cpp = open('exp/' + name + "/" + name + ".cpp", "w")
cpp.write(cpp_tpl)
def summary(bld):
lst = getattr(bld, 'utest_results', [])
......
......@@ -29,6 +29,7 @@ def options(opt):
opt.load('libcmaes')
opt.add_option('--create', type='string', help='create a new exp', dest='create_exp')
limbo.add_create_options(opt)
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')
opt.add_option('--oar', type='string', help='config file (json) to submit to oar', dest='oar')
......@@ -153,7 +154,7 @@ def run_benchmark(ctx):
def shutdown(ctx):
if ctx.options.create_exp:
limbo.create_exp(ctx.options.create_exp)
limbo.create_exp(ctx.options.create_exp, ctx.options)
if ctx.options.qsub:
limbo.qsub(ctx.options.qsub)
if ctx.options.oar:
......
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