SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
GridSearchModelSelection.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2011-2012 Heiko Strathmann
8  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
9  */
10 
15 #include <shogun/machine/Machine.h>
16 
17 using namespace shogun;
18 
20 {
21 }
22 
24  CMachineEvaluation* machine_eval,
25  CModelSelectionParameters* model_parameters)
26  : CModelSelection(machine_eval, model_parameters)
27 {
28 }
29 
31 {
32 }
33 
35 {
36  if (print_state)
37  SG_PRINT("Generating parameter combinations\n")
38 
39  /* Retrieve all possible parameter combinations */
40  CDynamicObjectArray* combinations=
42 
44 
45  CParameterCombination* best_combination=NULL;
47  {
48  if (print_state) SG_PRINT("Direction is maximize\n")
49  best_result->mean=CMath::ALMOST_NEG_INFTY;
50  }
51  else
52  {
53  if (print_state) SG_PRINT("Direction is minimize\n")
54  best_result->mean=CMath::ALMOST_INFTY;
55  }
56 
57  /* underlying learning machine */
59 
60  /* apply all combinations and search for best one */
61  for (index_t i=0; i<combinations->get_num_elements(); ++i)
62  {
63  CParameterCombination* current_combination=(CParameterCombination*)
64  combinations->get_element(i);
65 
66  /* eventually print */
67  if (print_state)
68  {
69  SG_PRINT("trying combination:\n")
70  current_combination->print_tree();
71  }
72 
73  current_combination->apply_to_modsel_parameter(
75 
76  /* note that this may implicitly lock and unlockthe machine */
77  CCrossValidationResult* result=
79 
80  if (result->get_result_type() != CROSSVALIDATION_RESULT)
81  SG_ERROR("Evaluation result is not of type CCrossValidationResult!")
82 
83  if (print_state)
84  result->print_result();
85 
86  /* check if current result is better, delete old combinations */
88  {
89  if (result->mean>best_result->mean)
90  {
91  if (best_combination)
92  SG_UNREF(best_combination);
93 
94  best_combination=(CParameterCombination*)
95  combinations->get_element(i);
96 
97  SG_REF(result);
98  SG_UNREF(best_result);
99  best_result=result;
100  }
101  else
102  {
104  combinations->get_element(i);
105  SG_UNREF(combination);
106  }
107  }
108  else
109  {
110  if (result->mean<best_result->mean)
111  {
112  if (best_combination)
113  SG_UNREF(best_combination);
114 
115  best_combination=(CParameterCombination*)
116  combinations->get_element(i);
117 
118  SG_REF(result);
119  SG_UNREF(best_result);
120  best_result=result;
121  }
122  else
123  {
125  combinations->get_element(i);
126  SG_UNREF(combination);
127  }
128  }
129 
130  SG_UNREF(result);
131  SG_UNREF(current_combination);
132  }
133 
134  SG_UNREF(best_result);
135  SG_UNREF(machine);
136  SG_UNREF(combinations);
137 
138  return best_combination;
139 }
virtual EEvaluationResultType get_result_type() const
int32_t index_t
Definition: common.h:62
void apply_to_modsel_parameter(Parameter *parameter) const
#define SG_ERROR(...)
Definition: SGIO.h:129
void print_tree(int prefix_num=0) const
type to encapsulate the results of an evaluation run.
#define SG_REF(x)
Definition: SGObject.h:54
A generic learning machine interface.
Definition: Machine.h:143
Class to select parameters and their ranges for model selection. The structure is organized as a tree...
virtual CEvaluationResult * evaluate()=0
static const float64_t ALMOST_NEG_INFTY
almost neg (log) infinity
Definition: Math.h:2052
Abstract base class for model selection.
CModelSelectionParameters * m_model_parameters
CMachineEvaluation * m_machine_eval
#define SG_PRINT(...)
Definition: SGIO.h:137
CMachine * get_machine() const
Machine Evaluation is an abstract class that evaluates a machine according to some criterion...
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
Class that holds ONE combination of parameters for a learning machine. The structure is organized as ...
EEvaluationDirection get_evaluation_direction()
Parameter * m_model_selection_parameters
Definition: SGObject.h:549
virtual CParameterCombination * select_model(bool print_state=false)
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CDynamicObjectArray * get_combinations(index_t prefix_num=1)
static const float64_t ALMOST_INFTY
Definition: Math.h:2049
CSGObject * get_element(int32_t index) const

SHOGUN Machine Learning Toolbox - Documentation