24 #ifdef MANGO_NLOPT_AVAILABLE
32 #ifdef MANGO_NLOPT_AVAILABLE
46 nlopt::algorithm mango_nlopt_algorithm;
51 mango_nlopt_algorithm = nlopt::GN_DIRECT;
54 mango_nlopt_algorithm = nlopt::GN_DIRECT_L;
57 mango_nlopt_algorithm = nlopt::GN_DIRECT_L_RAND;
60 mango_nlopt_algorithm = nlopt::GN_DIRECT_NOSCAL;
63 mango_nlopt_algorithm = nlopt::GN_DIRECT_L_NOSCAL;
66 mango_nlopt_algorithm = nlopt::GN_DIRECT_L_RAND_NOSCAL;
69 mango_nlopt_algorithm = nlopt::GN_ORIG_DIRECT;
72 mango_nlopt_algorithm = nlopt::GN_ORIG_DIRECT_L;
75 mango_nlopt_algorithm = nlopt::GN_CRS2_LM;
78 mango_nlopt_algorithm = nlopt::LN_COBYLA;
81 mango_nlopt_algorithm = nlopt::LN_BOBYQA;
84 mango_nlopt_algorithm = nlopt::LN_PRAXIS;
87 mango_nlopt_algorithm = nlopt::LN_NELDERMEAD;
90 mango_nlopt_algorithm = nlopt::LN_SBPLX;
93 mango_nlopt_algorithm = nlopt::LD_MMA;
96 mango_nlopt_algorithm = nlopt::LD_CCSAQ;
99 mango_nlopt_algorithm = nlopt::LD_SLSQP;
102 mango_nlopt_algorithm = nlopt::LD_LBFGS;
105 mango_nlopt_algorithm = nlopt::LD_TNEWTON_PRECOND_RESTART;
108 mango_nlopt_algorithm = nlopt::LD_TNEWTON_PRECOND;
111 mango_nlopt_algorithm = nlopt::LD_TNEWTON_RESTART;
114 mango_nlopt_algorithm = nlopt::LD_TNEWTON;
117 mango_nlopt_algorithm = nlopt::LD_VAR1;
120 mango_nlopt_algorithm = nlopt::LD_VAR2;
125 throw std::runtime_error(
"Error in optimize_nlopt. Unexpected algorithm!");
133 nlopt_opt opt = nlopt_create((nlopt_algorithm)mango_nlopt_algorithm, solver->
N_parameters);
135 nlopt_set_min_objective(opt, (nlopt_func) &mango::Package_nlopt::nlopt_objective_function, (
void*)solver);
137 nlopt_set_maxeval(opt, solver->max_function_and_gradient_evaluations);
139 if (solver->bound_constraints_set) {
140 nlopt_set_lower_bounds(opt, solver->lower_bounds);
141 nlopt_set_upper_bounds(opt, solver->upper_bounds);
144 double final_objective_function;
145 nlopt_result result = nlopt_optimize(opt, solver->state_vector, &final_objective_function);
149 if (solver->verbose > 0) std::cout <<
"nlopt generic success" << std::endl;
151 case nlopt::STOPVAL_REACHED:
152 if (solver->verbose > 0) std::cout <<
"nlopt success: stopval reached." << std::endl;
154 case nlopt::FTOL_REACHED:
155 if (solver->verbose > 0) std::cout <<
"nlopt success: ftol reached." << std::endl;
157 case nlopt::XTOL_REACHED:
158 if (solver->verbose > 0) std::cout <<
"nlopt success: xtol reached." << std::endl;
160 case nlopt::MAXEVAL_REACHED:
161 if (solver->verbose > 0) std::cout <<
"nlopt: maxeval reached" << std::endl;
163 case nlopt::MAXTIME_REACHED:
164 if (solver->verbose > 0) std::cout <<
"nlopt: maxtime reached." << std::endl;
167 if (solver->verbose > 0) std::cerr <<
"WARNING!!! NLOPT reported a generic failure. Results may or may not make sense." << std::endl;
169 case nlopt::INVALID_ARGS:
170 throw std::runtime_error(
"nlopt failure: invalid arguments!");
172 case nlopt::OUT_OF_MEMORY:
173 throw std::runtime_error(
"nlopt out of memory!");
175 case nlopt::ROUNDOFF_LIMITED:
176 if (solver->verbose > 0) std::cerr <<
"nlopt: WARNING! Limited by roundoff. Results may or may not make sense." << std::endl;
178 case nlopt::FORCED_STOP:
179 throw std::runtime_error(
"nlopt forced stop!");
182 throw std::runtime_error(
"nlopt unexpected return value!");
188 throw std::runtime_error(
"Error! A NLOPT algorithm was requested, but Mango was compiled without NLOPT support.");
193 double mango::Package_nlopt::nlopt_objective_function(
unsigned n,
const double* x,
double* grad,
void* f_data) {
198 if (solver->
verbose > 0) std::cout <<
"Hello from nlopt_objective_function" << std::endl << std::flush;
210 if (solver->
verbose > 0) std::cout <<
"Good-bye from nlopt_objective_function" << std::endl << std::flush;
217 throw std::runtime_error(
"Error! mango somehow got to Package_nlopt::optimize_least_squares. This should never happen.");