Commit 385f2ad7 authored by Laurent Belcour's avatar Laurent Belcour

Updating the compound function interface to mask the vector

data and expose only relevant functions.

Updating the non linear NLOpt algorithm. It is not currently providing
good results.
parent dee59a35
......@@ -269,13 +269,15 @@ class arguments
#endif
int argc = cmd_vec.size();
char* argv[argc];
char** argv;
argv = new char*[argc];
for(int i=0; i<argc; ++i)
{
argv[i] = &cmd_vec[i][0];
}
arguments current_args(argc, argv);
delete argv;
return current_args;
}
......
......@@ -143,3 +143,29 @@ double function::Linf_distance(const data* d) const
return linf_dist;
}
//! \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());
for(int i=0; i<nbParameters(); ++i)
{
M[i] = std::numeric_limits<double>::max();
}
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());
for(int i=0; i<nbParameters(); ++i)
{
m[i] = -std::numeric_limits<double>::max();
}
return m;
}
This diff is collapsed.
......@@ -284,7 +284,7 @@ function* plugins_manager::get_function(const arguments& args)
//! For each args_vec element, create a function object and add
//! it to the compound one.
for(int i=0; i<args_vec.size(); ++i)
for(unsigned int i=0; i<args_vec.size(); ++i)
{
std::string n("--func ");
n.append(args_vec[i]);
......
......@@ -480,7 +480,7 @@ void rational_function::save_matlab(const std::string& filename, const arguments
// Export the numerator of the jth color channel
file << "\tp(" << j+1 << ",:) = ";
for(unsigned int i=0; i<np; ++i)
for(int i=0; i<np; ++i)
{
if(i > 0 && a[i] >= 0.0)
{
......@@ -503,7 +503,7 @@ void rational_function::save_matlab(const std::string& filename, const arguments
// Export the denominator of the jth color channel
file << "\tq(" << j+1 << ",:) = ";
for(unsigned int i=0; i<nq; ++i)
for(int i=0; i<nq; ++i)
{
if(i > 0 && b[i] >= 0.0)
{
......@@ -595,7 +595,7 @@ void rational_function::save_cpp(const std::string& filename, const arguments& a
// Export the numerator of the jth color channel
file << "\tp = ";
for(unsigned int i=0; i<np; ++i)
for(int i=0; i<np; ++i)
{
if(i > 0 && a[i] >= 0.0)
{
......@@ -617,7 +617,7 @@ void rational_function::save_cpp(const std::string& filename, const arguments& a
// Export the denominator of the jth color channel
file << "\tq = ";
for(unsigned int i=0; i<nq; ++i)
for(int i=0; i<nq; ++i)
{
if(i > 0 && b[i] >= 0.0)
{
......@@ -687,7 +687,7 @@ void rational_function::save_call(std::ostream& out, const arguments& args) cons
vec a = rf->getP();
vec b = rf->getQ();
for(unsigned int i=0; i<np; ++i)
for(int i=0; i<np; ++i)
{
std::vector<int> index = rf->index2degree(i) ;
for(unsigned int j=0; j<index.size(); ++j)
......@@ -697,7 +697,7 @@ void rational_function::save_call(std::ostream& out, const arguments& args) cons
out << a[i] << std::endl ;
}
for(unsigned int i=0; i<nq; ++i)
for(int i=0; i<nq; ++i)
{
std::vector<int> index = rf->index2degree(i) ;
for(unsigned int j=0; j<index.size(); ++j)
......@@ -724,7 +724,7 @@ void rational_function::save(const std::string& filename) const
vec a = rf->getP();
vec b = rf->getQ();
for(unsigned int i=0; i<np; ++i)
for(int i=0; i<np; ++i)
{
std::vector<int> index = rf->index2degree(i) ;
for(unsigned int j=0; j<index.size(); ++j)
......@@ -734,7 +734,7 @@ void rational_function::save(const std::string& filename) const
file << a[i] << std::endl ;
}
for(unsigned int i=0; i<nq; ++i)
for(int i=0; i<nq; ++i)
{
std::vector<int> index = rf->index2degree(i) ;
for(unsigned int j=0; j<index.size(); ++j)
......
......@@ -30,8 +30,6 @@ void vertical_segment::load(const std::string& filename, const arguments& args)
_nX = 0 ; _nY = 0 ;
std::vector<int> vs ; int current_vs = 0 ;
double x, y, dy ;
while(file.good())
{
std::string line ;
......
......@@ -18,7 +18,7 @@ ALTA_DLL_EXPORT fitter* provide_fitter()
return new nonlinear_fitter_nlopt();
}
void print_nlopt_error(nlopt_result res, char* string)
void print_nlopt_error(nlopt_result res, const std::string& string)
{
if(res == NLOPT_FAILURE)
{
......@@ -150,6 +150,7 @@ bool nonlinear_fitter_nlopt::fit_data(const data* d, function* fit, const argume
// Check the optimizer to use
nlopt_algorithm algorithm;
nlopt_result res;
std::string optimizerName = args["nlopt-optimizer"];
if(optimizerName == "COBYLA")
{
......@@ -189,25 +190,32 @@ bool nonlinear_fitter_nlopt::fit_data(const data* d, function* fit, const argume
return false;
}
// Set the bounds to the parameters
vec p_min = nf->getParametersMin();
vec p_max = nf->getParametersMax();
nlopt_set_lower_bounds(opt, &p_min[0]);
nlopt_set_upper_bounds(opt, &p_max[0]);
// Set the stopping criterion to a maximum number of evaluation
if(args.is_defined("nlop-max-num-iterations"))
{
res = nlopt_set_maxeval(opt, args.get_int("nlop-max-num-iterations", 10));
if(res < 0) { print_nlopt_error(res, "nlopt_set_maxeval"); }
}
nlopt_set_xtol_rel(opt, 1e-4);
// Create the problem
void* dat[2];
dat[0] = (void*)nf;
dat[1] = (void*)d;
nlopt_result res = nlopt_set_min_objective(opt, f, dat);
res = nlopt_set_min_objective(opt, f, dat);
if(res < 0)
{
print_nlopt_error(res, "nlopt_set_min_objective");
return false;
}
// Set the stopping criterion to a maximum number of evaluation
if(args.is_defined("nlop-max-num-iterations"))
{
res = nlopt_set_maxeval(opt, args.get_int("nlop-max-num-iterations", 10));
if(res < 0) { print_nlopt_error(res, "nlopt_set_maxeval"); }
}
nlopt_set_xtol_rel(opt, 1e-4);
// Launch minimization
double f_end;
......
......@@ -143,6 +143,21 @@ vec isotropic_lafortune_function::parameters() const
return res;
}
//! \brief get the min values for the parameters
vec isotropic_lafortune_function::getParametersMin() const
{
vec res((3*_n)*dimY());
for(int n=0; n<_n; ++n)
for(int i=0; i<dimY(); ++i)
{
res[(n*dimY() + i)*3 + 0] = -std::numeric_limits<double>::max();
res[(n*dimY() + i)*3 + 1] = -std::numeric_limits<double>::max();
res[(n*dimY() + i)*3 + 2] = 0.0;
}
return res;
}
//! Update the vector of parameters for the function
void isotropic_lafortune_function::setParameters(const vec& p)
{
......
......@@ -48,7 +48,7 @@ class isotropic_lafortune_function : public nonlinear_function
//! \brief Export function
virtual void save_call(std::ostream& out, const arguments& args) const;
virtual void save_body(std::ostream& out, const arguments& args) const;
virtual void save_body(std::ostream& out, const arguments& args) const;
//! \brief Boostrap the function by defining the diffuse term
//!
......@@ -61,6 +61,9 @@ class isotropic_lafortune_function : public nonlinear_function
//! \brief Get the vector of parameters for the function
virtual vec parameters() const ;
//! \brief get the min values for the parameters
virtual vec getParametersMin() const;
//! \brief Update the vector of parameters for the function
virtual void setParameters(const vec& p) ;
......
......@@ -249,7 +249,7 @@ void rational_function_chebychev::save_cpp(const std::string& filename, const ar
std::vector<int> degree = rf->index2degree(i);
for(unsigned int k=0; k<degree.size(); ++k)
{
file << "*l(2.0*((x\[" << k << "\]-c[" << k << "])*s[" << k << "] - 0.5), " << degree[k] << ")" ;
file << "*l(2.0*((x[" << k << "]-c[" << k << "])*s[" << k << "] - 0.5), " << degree[k] << ")" ;
}
}
file << ";" << std::endl;
......@@ -271,7 +271,7 @@ void rational_function_chebychev::save_cpp(const std::string& filename, const ar
std::vector<int> degree = rf->index2degree(i);
for(unsigned int k=0; k<degree.size(); ++k)
{
file << "*l(2.0*((x\[" << k << "\]-c[" << k << "])*s[" << k << "] - 0.5), " << degree[k] << ")" ;
file << "*l(2.0*((x[" << k << "]-c[" << k << "])*s[" << k << "] - 0.5), " << degree[k] << ")" ;
}
}
file << ";" << std::endl;
......
Markdown is supported
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