16 using namespace shogun;
22 double CGradientModelSelection::nlopt_function(
unsigned n,
23 const double *x,
double *grad,
void *my_func_data)
25 nlopt_package* pack = (nlopt_package*)my_func_data;
30 pack->m_current_combination;
32 bool print_state = pack->print_state;
39 SG_SERROR(
"Evaluation result not a GradientEvaluationResult!");
43 SG_SERROR(
"Mismatch between total variables in result and variables in " \
57 shogun::CMapNode<TParameter*, SGVector<float64_t> >*
node =
71 for (
index_t j = 0; j < length; j++)
76 SG_SERROR(
"Parameter %s not found in combination \
92 for (
index_t j = 0; j < length; j++)
97 SG_SERROR(
"Parameter %s not found in combination \
102 curr_index += length;
110 SG_SERROR(
"Parameter %s not found in combination tree.\n",
128 SG_SERROR(
"Evaluation result not a GradientEvaluationResult!");
135 shogun::CMapNode<TParameter*, SGVector<float64_t> >* node =
138 for(
index_t j = 0; j < node->data.vlen; j++)
139 grad[curr_index+j] = node->data[j];
141 curr_index += node->data.vlen;
150 return function_value;
162 void CGradientModelSelection::init()
166 m_current_combination = NULL;
187 void CGradientModelSelection::test_gradients()
206 shogun::CMapNode<TParameter*, SGVector<float64_t> >* node =
213 for (
index_t j = 0; j < node->data.vlen; j++)
215 true_grad = node->data[j];
229 new_value = orig_value+
delta;
235 param->
m_name, new_value, parent, index))
237 SG_ERROR(
"Parameter %s not found in combination tree.\n",
249 approx_grad = (new_eval-orig_eval)/delta;
251 if (abs(approx_grad - true_grad) > error_tol)
253 SG_ERROR(
"Gradient of function with respect to variable %i of %s" \
255 "True value is approximately %f, but calculated value is" \
256 " %f\n", j, param->
m_name,
257 approx_grad, true_grad);
261 param->
m_name, orig_value, parent, index))
263 SG_ERROR(
"Parameter %s not found in combination tree.\n",
286 SG_REF(m_current_combination);
303 SG_ERROR(
"Evaluation result not a GradientEvaluationResult!");
315 shogun::CMapNode<TParameter*, SGVector<float64_t> >* node =
327 SG_ERROR(
"Could not find parameter %s "\
328 "in Parameter Combination\n", param->
m_name);
331 if (final->m_datatype.m_ctype == CT_VECTOR)
336 index_t length = *(
final->m_datatype.m_length_y);
338 for (
index_t j = 0; j < length; j++)
339 x[cur_index+j] = *((
float64_t**)(
final->m_parameter))[j];
344 else if (final->m_datatype.m_ctype == CT_SGVECTOR)
349 index_t length = *(
final->m_datatype.m_length_y);
351 for (
index_t j = 0; j < length; j++)
352 x[cur_index+j] = (*(
float64_t**)(
final->m_parameter))[j];
359 x[cur_index] = *((
float64_t*)(final->m_parameter));
373 shogun::CMapNode<TParameter*, SGVector<float64_t> >* node =
385 SG_ERROR(
"Could not find parameter %s "\
386 "in Parameter Combination\n", param->
m_name);
389 if (final->m_datatype.m_ctype == CT_VECTOR)
394 index_t length = *(
final->m_datatype.m_length_y);
396 for (
index_t j = 0; j < length; j++)
397 lb[cur_index+j] = *((
float64_t**)(
final->m_parameter))[j];
402 else if (final->m_datatype.m_ctype == CT_SGVECTOR)
407 index_t length = *(
final->m_datatype.m_length_y);
409 for (
index_t j = 0; j < length; j++)
410 lb[cur_index+j] = (*(
float64_t**)(
final->m_parameter))[j];
417 lb[cur_index] = *((
float64_t*)(final->m_parameter));
432 opt = nlopt_create(NLOPT_LD_MMA, n);
435 nlopt_set_lower_bounds(opt, lb);
440 SG_SPRINT(
"Minimizing Objective Function\n");
442 nlopt_set_min_objective(opt, nlopt_function, &pack);
448 SG_SPRINT(
"Maximizing Objective Function\n");
450 nlopt_set_max_objective(opt, nlopt_function, &pack);
458 if (nlopt_optimize(opt, x, &minf) < 0)
475 SG_REF(m_current_combination);
482 SG_PRINT(
"Shogun not configured for NLOPT. Returning NULL combination\n");