39 CNLOPTMinimizer::CNLOPTMinimizer()
45 CNLOPTMinimizer::~CNLOPTMinimizer()
55 void CNLOPTMinimizer::init()
59 set_nlopt_parameters();
60 SG_ADD(&m_max_iterations,
"CNLOPTMinimizer__m_max_iterations",
62 SG_ADD(&m_variable_tolerance,
"CNLOPTMinimizer__m_variable_tolerance",
64 SG_ADD(&m_function_tolerance,
"CNLOPTMinimizer__m_function_tolerance",
66 SG_ADD(&m_nlopt_algorithm_id,
"CNLOPTMinimizer__m_nlopt_algorithm_id",
76 nlopt_opt opt=nlopt_create(get_nlopt_algorithm(m_nlopt_algorithm_id),
77 m_target_variable.vlen);
82 if(bound_constraints_fun)
87 nlopt_set_lower_bounds1(opt, bound[0]);
89 else if (bound.
vlen>1)
92 "The length of target variable (%d) and the length of lower bound (%d) do not match\n",
93 m_target_variable.vlen, bound.
vlen);
94 nlopt_set_lower_bounds(opt, bound.
vector);
100 nlopt_set_upper_bounds1(opt, bound[0]);
102 else if (bound.
vlen>1)
105 "The length of target variable (%d) and the length of upper bound (%d) do not match\n",
106 m_target_variable.vlen, bound.
vlen);
107 nlopt_set_upper_bounds(opt, bound.
vector);
112 nlopt_set_maxeval(opt, m_max_iterations);
114 nlopt_set_xtol_abs1(opt, m_variable_tolerance);
115 nlopt_set_ftol_abs(opt, m_function_tolerance);
117 nlopt_set_min_objective(opt, CNLOPTMinimizer::nlopt_function,
this);
125 nlopt_result error_code=nlopt_optimize(opt, m_target_variable.vector, &cost);
128 SG_SWARNING(
"Error(s) happened and NLopt failed during minimization (error code:%d)\n",
140 int16_t CNLOPTMinimizer::get_nlopt_algorithm_id(ENLOPTALGORITHM method)
142 int16_t method_id=-1;
146 method_id = (int16_t) NLOPT_GN_DIRECT;
149 method_id = (int16_t) NLOPT_GN_DIRECT_L;
151 case GN_DIRECT_L_RAND:
152 method_id = (int16_t) NLOPT_GN_DIRECT_L_RAND;
154 case GN_DIRECT_NOSCAL:
155 method_id = (int16_t) NLOPT_GN_DIRECT_NOSCAL;
157 case GN_DIRECT_L_NOSCAL:
158 method_id = (int16_t) NLOPT_GN_DIRECT_L_NOSCAL;
160 case GN_DIRECT_L_RAND_NOSCAL:
161 method_id = (int16_t) NLOPT_GN_DIRECT_L_RAND_NOSCAL;
164 method_id = (int16_t) NLOPT_GN_ORIG_DIRECT;
166 case GN_ORIG_DIRECT_L:
167 method_id = (int16_t) NLOPT_GN_ORIG_DIRECT_L;
170 method_id = (int16_t) NLOPT_GN_CRS2_LM;
173 method_id = (int16_t) NLOPT_GN_ISRES;
176 method_id = (int16_t) NLOPT_LD_MMA;
179 method_id = (int16_t) NLOPT_LD_LBFGS;
181 case LD_LBFGS_NOCEDAL:
182 method_id = (int16_t) NLOPT_LD_LBFGS_NOCEDAL;
185 method_id = (int16_t) NLOPT_LD_VAR1;
188 method_id = (int16_t) NLOPT_LD_VAR2;
191 method_id = (int16_t) NLOPT_LD_TNEWTON;
193 case LD_TNEWTON_RESTART:
194 method_id = (int16_t) NLOPT_LD_TNEWTON_RESTART;
196 case LD_TNEWTON_PRECOND:
197 method_id = (int16_t) NLOPT_LD_TNEWTON_PRECOND;
199 case LD_TNEWTON_PRECOND_RESTART:
200 method_id = (int16_t) NLOPT_LD_TNEWTON_PRECOND_RESTART;
203 method_id = (int16_t) NLOPT_LD_SLSQP;
206 method_id = (int16_t) NLOPT_LN_PRAXIS;
209 method_id = (int16_t) NLOPT_LN_COBYLA;
212 method_id = (int16_t) NLOPT_LN_NEWUOA;
214 case LN_NEWUOA_BOUND:
215 method_id = (int16_t) NLOPT_LN_NEWUOA_BOUND;
218 method_id = (int16_t) NLOPT_LN_NELDERMEAD;
221 method_id = (int16_t) NLOPT_LN_SBPLX;
224 method_id = (int16_t) NLOPT_LN_BOBYQA;
227 method_id = (int16_t) NLOPT_AUGLAG;
230 method_id = (int16_t) NLOPT_AUGLAG_EQ;
233 method_id = (int16_t) NLOPT_G_MLSL;
236 method_id = (int16_t) NLOPT_G_MLSL_LDS;
239 REQUIRE(method_id>=0,
"Unsupported algorithm\n");
243 void CNLOPTMinimizer::set_nlopt_parameters(ENLOPTALGORITHM algorithm,
248 m_nlopt_algorithm_id=get_nlopt_algorithm_id(algorithm);
249 m_max_iterations=max_iterations;
250 m_variable_tolerance=variable_tolerance;
251 m_function_tolerance=function_tolerance;
254 double CNLOPTMinimizer::nlopt_function(
unsigned dim,
const double* variable,
double* gradient,
257 CNLOPTMinimizer* obj_prt=
static_cast<CNLOPTMinimizer *
>(func_data);
258 REQUIRE(obj_prt,
"The instance object passed to NLopt optimizer should not be NULL\n");
259 REQUIRE((
index_t)dim==(obj_prt->m_target_variable).vlen,
"Length must be matched\n");
261 double *var =
const_cast<double *
>(variable);
262 std::swap_ranges(var, var+dim, (obj_prt->m_target_variable).vector);
264 double cost=obj_prt->m_fun->get_cost();
270 "The length of gradient (%d) and the length of variable (%d) do not match\n",
275 std::swap_ranges(var, var+dim, (obj_prt->m_target_variable).vector);
279 void CNLOPTMinimizer::init_minimization()
281 REQUIRE(m_fun,
"Cost function not set!\n");
282 m_target_variable=m_fun->obtain_variable_reference();
283 REQUIRE(m_target_variable.vlen>0,
"Target variable from cost function must not empty!\n");
287 #endif //USE_GPL_SHOGUN
virtual SGVector< float64_t > get_upper_bound()=0
The first order cost function base class with bound constrains.
The first order cost function base class.
all of classes and functions are contained in the shogun namespace
virtual SGVector< float64_t > get_lower_bound()=0
The first order minimizer base class.