38 CNLOPTMinimizer::CNLOPTMinimizer()
44 CNLOPTMinimizer::~CNLOPTMinimizer()
54 void CNLOPTMinimizer::init()
58 set_nlopt_parameters();
59 SG_ADD(&m_max_iterations,
"CNLOPTMinimizer__m_max_iterations",
61 SG_ADD(&m_variable_tolerance,
"CNLOPTMinimizer__m_variable_tolerance",
63 SG_ADD(&m_function_tolerance,
"CNLOPTMinimizer__m_function_tolerance",
65 SG_ADD(&m_nlopt_algorithm_id,
"CNLOPTMinimizer__m_nlopt_algorithm_id",
75 nlopt_opt opt=nlopt_create(NLOPTAlgorithmHelper::get_nlopt_algorithm(m_nlopt_algorithm_id),
76 m_target_variable.vlen);
81 if(bound_constraints_fun)
86 nlopt_set_lower_bounds1(opt, bound[0]);
88 else if (bound.
vlen>1)
91 "The length of target variable (%d) and the length of lower bound (%d) do not match\n",
92 m_target_variable.vlen, bound.
vlen);
93 nlopt_set_lower_bounds(opt, bound.
vector);
99 nlopt_set_upper_bounds1(opt, bound[0]);
101 else if (bound.
vlen>1)
104 "The length of target variable (%d) and the length of upper bound (%d) do not match\n",
105 m_target_variable.vlen, bound.
vlen);
106 nlopt_set_upper_bounds(opt, bound.
vector);
111 nlopt_set_maxeval(opt, m_max_iterations);
113 nlopt_set_xtol_abs1(opt, m_variable_tolerance);
114 nlopt_set_ftol_abs(opt, m_function_tolerance);
116 nlopt_set_min_objective(opt, CNLOPTMinimizer::nlopt_function,
this);
124 nlopt_result error_code=nlopt_optimize(opt, m_target_variable.vector, &cost);
127 SG_SWARNING(
"Error(s) happened and NLopt failed during minimization (error code:%d)\n",
139 void CNLOPTMinimizer::set_nlopt_parameters(nlopt_algorithm algorithm,
144 m_nlopt_algorithm_id=NLOPTAlgorithmHelper::get_nlopt_algorithm_id(algorithm);
145 m_max_iterations=max_iterations;
146 m_variable_tolerance=variable_tolerance;
147 m_function_tolerance=function_tolerance;
150 double CNLOPTMinimizer::nlopt_function(
unsigned dim,
const double* variable,
double* gradient,
153 CNLOPTMinimizer* obj_prt=
static_cast<CNLOPTMinimizer *
>(func_data);
154 REQUIRE(obj_prt,
"The instance object passed to NLopt optimizer should not be NULL\n");
156 double cost=obj_prt->m_fun->get_cost();
162 "The length of gradient (%d) and the length of variable (%d) do not match\n",
169 void CNLOPTMinimizer::init_minimization()
171 REQUIRE(m_fun,
"Cost function not set!\n");
172 m_target_variable=m_fun->obtain_variable_reference();
173 REQUIRE(m_target_variable.vlen>0,
"Target variable from cost function must not empty!\n");
177 #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.