SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RandomSearchModelSelection.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  * Copyright (C) 2011 Heiko Strathmann
8  * Copyright (C) 2012 Sergey Lisitsyn
9  */
10 
16 #include <shogun/machine/Machine.h>
17 
18 using namespace shogun;
19 
21  CModelSelection(NULL, NULL)
22 {
23  set_ratio(0.5);
24 }
25 
27  CModelSelectionParameters* model_parameters,
28  CMachineEvaluation* machine_eval, float64_t ratio) :
29  CModelSelection(model_parameters, machine_eval)
30 {
31  set_ratio(ratio);
32 }
33 
35 {
36 }
37 
39 {
40  if (print_state)
41  SG_PRINT("Generating parameter combinations\n");
42 
43  /* Retrieve all possible parameter combinations */
44  CDynamicObjectArray* all_combinations=
46 
47  int32_t n_all_combinations = all_combinations->get_num_elements();
48  SGVector<index_t> combinations_indices = CStatistics::sample_indices(n_all_combinations*m_ratio, n_all_combinations);
49 
50  CDynamicObjectArray* combinations = new CDynamicObjectArray();
51 
52  for (int32_t i=0; i<combinations_indices.vlen; i++)
53  combinations->append_element(all_combinations->get_element(i));
54 
56 
57  CParameterCombination* best_combination=NULL;
59  {
60  if (print_state) SG_PRINT("Direction is maximize\n");
61  best_result->mean=CMath::ALMOST_NEG_INFTY;
62  }
63  else
64  {
65  if (print_state) SG_PRINT("Direction is maximize\n");
66  best_result->mean=CMath::ALMOST_INFTY;
67  }
68 
69  /* underlying learning machine */
71 
72  /* apply all combinations and search for best one */
73  for (index_t i=0; i<combinations->get_num_elements(); ++i)
74  {
75  CParameterCombination* current_combination=(CParameterCombination*)
76  combinations->get_element(i);
77 
78  /* eventually print */
79  if (print_state)
80  {
81  SG_PRINT("trying combination:\n");
82  current_combination->print_tree();
83  }
84 
85  current_combination->apply_to_modsel_parameter(
87 
88  /* note that this may implicitly lock and unlockthe machine */
89  CCrossValidationResult* result =
91 
92  if (result->get_result_type() != CROSSVALIDATION_RESULT)
93  SG_ERROR("Evaluation result is not of type CCrossValidationResult!");
94 
95  if (print_state)
96  result->print_result();
97 
98  /* check if current result is better, delete old combinations */
100  {
101  if (result->mean>best_result->mean)
102  {
103  if (best_combination)
104  SG_UNREF(best_combination);
105 
106  best_combination=(CParameterCombination*)
107  combinations->get_element(i);
108 
109  SG_UNREF(best_result);
110  SG_REF(result);
111  best_result = result;
112  }
113  else
114  {
116  combinations->get_element(i);
117  SG_UNREF(combination);
118  }
119  }
120  else
121  {
122  if (result->mean<best_result->mean)
123  {
124  if (best_combination)
125  SG_UNREF(best_combination);
126 
127  best_combination=(CParameterCombination*)
128  combinations->get_element(i);
129 
130  SG_UNREF(best_result);
131  SG_REF(result);
132  best_result = result;
133  }
134  else
135  {
137  combinations->get_element(i);
138  SG_UNREF(combination);
139  }
140  }
141 
142  SG_UNREF(result);
143  SG_UNREF(current_combination);
144  }
145 
146  SG_UNREF(best_result);
147  SG_UNREF(machine);
148  SG_UNREF(combinations);
149 
150  return best_combination;
151 }
152 

SHOGUN Machine Learning Toolbox - Documentation