Commit 1316381c authored by pacanows's avatar pacanows
Browse files

Merge

parents 5a571a58 48909b31
env = Environment()
env.SConscript('external/SConscript')
env.SConscript('sources/SConscript')
import os
env = Environment()
##
# CERES dependancies
##
execfile('obtain_ceres.py')
##
# QuadProg++
##
qp_target = 'build' + os.sep + 'lib' + os.sep + 'quaprog++'
qp_base = 'quadprog++' + os.sep
qp_sources = [qp_base + 'Array.cc', qp_base + 'QuadProg++.cc']
env.Append(CPPPATH = ['build' + os.sep + 'include'])
env.StaticLibrary(target = qp_target, source = qp_sources)
env = Environment()
env.SConscript('SConscript')
import urllib
import os
import sys
import shutil
# Check if the build dir exists
if not os.path.exists('build'):
os.mkdir('build')
#end
# Download a file
def download(url, filename):
urllib.urlretrieve(url, filename)
#end
# Uncompress the archive
def uncompress(filename):
cmd = '';
if os.name == 'posix':
cmd = 'tar -xf '
elif os.name == 'nt':
cmd = '7zip '
cmd += filename
ret = os.system(cmd)
#end
# Obtain a package and uncompress it
def obtain(name, rep, url, filename):
if not os.path.exists(rep):
print '<<INSTALL>> Obtaining ' + name
download(url, filename)
uncompress(filename)
else:
print '<<INSTALL>> ' + name + ' already available'
#end
#end
def configure_build(rep):
os.chdir(rep)
ret = os.system('./configure -q --prefix=' + os.getcwd() + os.sep + os.pardir + os.sep + 'build')
if ret != 0:
print '<<ERROR>> unable to configure package'
exit
#end
ret = os.system('make -s && make -s install')
if ret != 0:
print '<<ERROR>> unable to build & install package'
exit
#end
os.chdir(os.pardir)
#end
import obtain
import os
import sys
import shutil
# Download GLOG
obtain.obtain('GLOG v0.3.3', 'glog-0.3.3', 'http://google-glog.googlecode.com/files/glog-0.3.3.tar.gz', 'glog-0.3.3.tar.gz')
if os.name == 'nt':
print '<<WARNING>> no automatic installation for this package'
else:
print '<<INSTALL>> configure and build GLOG v0.3.3'
obtain.configure_build('glog-0.3.3')
#end
# Download Eigen3
execfile('obtain_eigen.py')
# Download CERES
obtain.obtain('CERES v1.7.0', 'ceres-solver-1.7.0', 'http://ceres-solver.googlecode.com/files/ceres-solver-1.7.0.tar.gz', 'ceres-solver-1.7.0.tar.gz')
print '<<WARNING>> CERES installation requires CMake. You need to run it yourself'
import obtain
import os
import sys
import shutil
# Download Eigen3
obtain.obtain('Eigen v3.2.1', 'eigen-eigen-ffa86ffb5570', 'http://bitbucket.org/eigen/eigen/get/3.2.0.tar.gz', 'eigen-3.2.1.tar.gz')
rep = 'build' + os.sep + 'include' + os.sep + 'Eigen'
if not os.path.exists(rep):
shutil.copytree('eigen-eigen-ffa86ffb5570' + os.sep + 'Eigen', rep)
#end
SConscript('core/SConscript')
#SConscript('plugins/SConscript')
#SConscript('softs/SConscript')
SConscript('softs/SConscript')
import os
env = Environment()
env.Append(CPPPATH = '/home/belcour/Sources/eigen')
env.Append(CPPPATH = '../../external/build/include')
# Library sources
sources = ['common.cpp',
......@@ -9,5 +11,9 @@ sources = ['common.cpp',
'rational_function.cpp',
'vertical_segment.cpp']
libs = []
if(os.name == 'posix'):
libs = ['rt', 'dl']
#end
env.StaticLibrary('../build/core', sources)
env.StaticLibrary('../build/core', sources, LIBS=libs)
......@@ -73,13 +73,13 @@ vec product(const vec& a, const vec& b)
}
else
{
#ifdef DEBUG
#ifndef DEBUG
assert(a.size() == b.size());
#endif
vec res(b.size());
for(int i=0; i<b.size(); ++i)
{
res[i] = a[0]*b[i];
res[i] = a[i]*b[i];
}
return res;
}
......
#include "function.h"
#include "common.h"
#include "params.h"
#include "plugins_manager.h"
/*--- Functions implementation ----*/
void function::bootstrap(const data*, const arguments& args)
{
// If the bootstrap option contains a filename, load it
......@@ -11,7 +14,10 @@ void function::bootstrap(const data*, const arguments& args)
std::ifstream in(args["bootstrap"].c_str());
if(in.is_open())
{
load(in);
if(!load(in))
{
std::cerr << "<<ERROR>> cannot bootstrap from file \"" << args["bootstrap"] << "\"" << std::endl;
}
}
}
}
......@@ -121,7 +127,7 @@ void function::save_header(std::ostream& out, const arguments& args) const
out << "#CMD " << args.get_cmd() << std::endl;
out << "#DIM " << _nX << " " << _nY << std::endl;
out << "#PARAM_IN " << params::get_name(input_parametrization()) << std::endl;
//out << "#PARAM_OUT " << params::get_name(output_parametrization()) << std::endl;*
//out << "#PARAM_OUT " << params::get_name(output_parametrization()) << std::endl;*
if(args.is_defined("export-append")) {
out << args["export-append"] << std::endl;
}
......@@ -184,6 +190,73 @@ double function::Linf_distance(const data* d) const
}
/*--- Non-linear functions implementation ----*/
bool nonlinear_function::load(std::istream& in)
{
// Parse line until the next comment
while(in.peek() != '#')
{
char line[256];
in.getline(line, 256);
// If we cross the end of the file, or the badbit is
// set, the file cannot be loaded
if(!in.good())
return false;
}
// Checking for the comment line #FUNC nonlinear_function_phong
std::string token;
in >> token;
if(token.compare("#FUNC") != 0)
{
std::cerr << "<<ERROR>> parsing the stream. The #FUNC is not the next line defined." << std::endl;
#ifdef DEBUG
std::cout << "<<DEBUG>> got: \"" << token << "\"" << std::endl;
#endif
return false;
}
in >> token;
if(token.compare("nonlinear_function") != 0)
{
std::cerr << "<<ERROR>> parsing the stream. A function name is defined." << std::endl;
std::cerr << "<<ERROR>> did you forget to specify the plugin used to export?" << std::endl;
return false;
}
int nb_params = nbParameters();
vec p(nb_params);
for(int i=0; i<nb_params; ++i)
{
in >> token >> p[i];
}
setParameters(p);
return true;
}
void nonlinear_function::save_call(std::ostream& out, const arguments& args) const
{
if(!args.is_defined("export"))
{
// Dump a #FUNC nonlinear
out << "#FUNC nonlinear_function" << std::endl;
// Dump the parameters in order
vec p = parameters();
for(int i=0; i<p.size(); ++i)
{
out << "param_" << i+1 << "\t" << p[i] << std::endl;
}
out << std::endl;
}
function::save_call(out, args);
}
void nonlinear_function::bootstrap(const data* d, const arguments& args)
{
if(args.is_vec("bootstrap"))
......@@ -197,9 +270,6 @@ void nonlinear_function::bootstrap(const data* d, const arguments& args)
}
}
//! \brief get the maximum value for all the parameters in a vector
//! format. The maximum vector and the parameter vector have the same
//! indexing.
vec nonlinear_function::getParametersMax() const
{
vec M(nbParameters());
......@@ -210,9 +280,6 @@ vec nonlinear_function::getParametersMax() const
return M;
}
//! \brief get the minimum value for all the parameters in a vector
//! format. The minimum vector and the parameter vector have the same
//! indexing.
vec nonlinear_function::getParametersMin() const
{
vec m(nbParameters());
......@@ -223,48 +290,427 @@ vec nonlinear_function::getParametersMin() const
return m;
}
/*--- Compound functions implementation ----*/
vec compound_function::operator()(const vec& x) const
{
return value(x);
}
vec compound_function::value(const vec& x) const
{
vec res(_nY);
res = vec::Zero(_nY);
for(unsigned int i=0; i<fs.size(); ++i)
{
if(fs[i]->input_parametrization() != input_parametrization())
{
vec temp_x(fs[i]->dimX());
params::convert(&x[0], input_parametrization(), fs[i]->input_parametrization(), &temp_x[0]);
res = res + fs[i]->value(temp_x);
}
else
{
res = res + fs[i]->value(x);
}
}
return res;
}
void compound_function::push_back(nonlinear_function* f, const arguments& f_args)
{
// Update the input param
if(input_parametrization() == params::UNKNOWN_INPUT)
{
setParametrization(f->input_parametrization());
}
else if(input_parametrization() != f->input_parametrization())
{
setParametrization(params::CARTESIAN);
}
// Update the output param
if(output_parametrization() == params::UNKNOWN_OUTPUT)
{
setParametrization(f->output_parametrization());
}
else if(output_parametrization() != f->output_parametrization())
{
std::cerr << "Creating a compound function with different output dimensions, this is not allowed" << std::endl;
throw;
}
fs.push_back(f);
fs_args.push_back(f_args);
is_fixed.push_back(f_args.is_defined("fixed"));
}
nonlinear_function* compound_function::operator[](int i) const
{
#ifdef DEBUG
assert(i >= 0 && i < fs.size());
#endif
return fs[i];
}
void compound_function::bootstrap(const ::data* d, const arguments& args)
{
const bool global_bootstrap = args.is_defined("bootstrap");
const bool global_bootstrap = args.is_defined("bootstrap");
// Check if the bootstrap is global
if(global_bootstrap)
{
if(args.is_vec("bootstrap"))
{
vec p = args.get_vec("bootstrap", nbParameters());
setParameters(p);
}
else
{
std::ifstream file;
file.open(args["bootstrap"].c_str()) ;
if(file.is_open())
{
// Skip the header
std::string line ;
do
{
std::getline(file, line) ;
}
while(line != "#ALTA HEADER END");
// Load the file
this->load(file);
}
else
{
std::cerr << "<<ERROR>> you must provide a vector of parameters or a file to load with the bootstrap" << std::endl;
}
}
}
// Check if the bootstrap is global
if(global_bootstrap)
{
if(args.is_vec("bootstrap"))
{
vec p = args.get_vec("bootstrap", nbParameters());
setParameters(p);
}
else
{
std::ifstream file;
file.open(args["bootstrap"].c_str()) ;
if(file.is_open())
{
// Skip the header
std::string line ;
do
{
std::getline(file, line) ;
}
while(line != "#ALTA HEADER END");
// Per function bootstrap
for(unsigned int i=0; i<fs.size(); ++i)
{
if(!global_bootstrap)
{
fs[i]->bootstrap(d, fs_args[i]);
}
}
// Load the file
// For each individual element, the input file is parsed
// in order. if the function cannot parse it, the file is
// restored to the previous state and the loading continues
for(unsigned int i=0; i<fs.size(); ++i)
{
std::streampos pos = file.tellg();
// If the function cannot be loaded, put the input stream
// in the previous state and bootstrap normaly this function.
if(!fs[i]->load(file))
{
file.seekg(pos);
// Bootstrap the function as if it was not loaded
fs[i]->bootstrap(d, fs_args[i]);
}
}
}
else
{
std::cerr << "<<ERROR>> you must provide a vector of parameters or a file to load with the bootstrap" << std::endl;
}
}
}
else
{
for(unsigned int i=0; i<fs.size(); ++i)
{
fs[i]->bootstrap(d, fs_args[i]);
}
}
}
/*--- Product functions implementation ----*/
product_function::product_function(nonlinear_function* g1, nonlinear_function* g2)
{
this->f1 = g1;
this->f2 = g2;
}
vec product_function::operator()(const vec& x) const
{
return value(x);
}
vec product_function::value(const vec& x) const
{
// Get the first function value
vec f1res = f1->value(x);
// Convert input space to the 2nd function parametreization
vec xf2(f2->dimX());
params::convert(&x[0], f1->input_parametrization(), f2->input_parametrization(), &xf2[0]);
vec f2res = f2->value(xf2);
return product(f1res, f2res);
}
bool product_function::load(std::istream& in)
{
bool loaded_f1,loaded_f2;
std::streampos pos = in.tellg();
// Load the first function
if(f1 != NULL)
{
loaded_f1 = f1->load(in);
if(! loaded_f1)
{
in.seekg(pos);
}
}
pos = in.tellg();
// Load the second function
if(f2 != NULL)
{
loaded_f2 = f2->load(in);
if(! loaded_f2)
{
in.seekg(pos);
}
}
return loaded_f1 || loaded_f2;
}
void product_function::bootstrap(const data* d, const arguments& args)
{
const bool global_bootstrap = args.is_defined("bootstrap");
// Check if the bootstrap is global
if(global_bootstrap)
{
if(args.is_vec("bootstrap"))
{
vec p = args.get_vec("bootstrap", nbParameters());
setParameters(p);
}
else
{
std::ifstream file;
file.open(args["bootstrap"].c_str()) ;
if(file.is_open())
{
// Skip the header
std::string line ;
do
{
std::getline(file, line) ;
}
while(line != "#ALTA HEADER END");
std::streampos pos = file.tellg();
// If the first function cannot be loaded, put the input stream
// in the previous state and bootstrap normaly this function.
if(!f1->load(file))
{
file.seekg(pos);
// Bootstrap the function as if it was not loaded
f1->bootstrap(d, args);
}
// If the second function cannot be loaded, put the input stream
// in the previous state and bootstrap normaly this function.
if(!f2->load(file))
{
file.seekg(pos);
// Bootstrap the function as if it was not loaded
f2->bootstrap(d, args);
}
}
else
{
std::cerr << "<<ERROR>> you must provide a vector of parameters or a file to load with the bootstrap" << std::endl;
}
}
}
else
{
f1->bootstrap(d, args);
f2->bootstrap(d, args);
}
}
void product_function::setDimX(int nX)
{
f1->setDimX(nX);
f2->setDimX(nX);
function::setDimX(f1->dimX());
}
void product_function::setDimY(int nY)
{
f1->setDimY(nY);
f2->setDimY(nY);
function::setDimY(f1->dimY());
}
void product_function::setMin(const vec& min)
{
function::setMin(min);
f1->setMin(min);
f2->setMin(min);
}
void product_function::setMax(const vec& max)
{
function::setMax(max);
f1->setMax(max);
f2->setMax(max);
}
int product_function::nbParameters() const
{
return f1->nbParameters() + f2->nbParameters();
}
vec product_function::parameters() const
{
int nb_f1_params = f1->nbParameters();
int nb_f2_params = f2->nbParameters();
int nb_params = nb_f1_params + nb_f2_params;
vec params(nb_params);
vec f1_params = f1->parameters();
for(int i=0; i<nb_f1_params; ++i)
{