openModeller  Version 1.5.0
garp_best_subsets.cpp
Go to the documentation of this file.
1 
30 #include "garp_best_subsets.hh"
31 #include "garp.hh"
32 
33 #include <openmodeller/om.hh>
34 #include <openmodeller/Random.hh>
35 #include <openmodeller/Sampler.hh>
37 
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <math.h>
41 
42 #ifdef WIN32
43 #include <windows.h>
44 #define SLEEP(secs) Sleep(secs * 1000)
45 #else
46 #include <unistd.h>
47 #define SLEEP(secs) sleep(secs);
48 #endif
49 
50 #define NUM_PARAM 11
51 
52 /************************************/
53 /*** Algorithm parameter metadata ***/
54 
56 {
57  // Best Subsets Parameters
58  {
59  "TrainingProportion", // Id.
60  "Training Proportion", // Name.
61  Real, // Type.
62 
63  // Overview.
64  "Percentage of occurrence data to be used to train models.",
65 
66  // Description.
67  "Percentage of occurrence data to be used to train models.",
68 
69  1, // Not zero if the parameter has lower limit.
70  0, // Parameter's lower limit.
71  1, // Not zero if the parameter has upper limit.
72  100, // Parameter's upper limit.
73  "50" // Parameter's typical (default) value.
74  },
75 
76  {
77  "TotalRuns", // Id.
78  "Total Runs", // Name.
79  Integer, // Type.
80 
81  // Overview.
82  "Maximum number of GARP runs to be performed.",
83 
84  // Description.
85  "Maximum number of GARP runs to be performed.",
86 
87  1, // Not zero if the parameter has lower limit.
88  0, // Parameter's lower limit.
89  1, // Not zero if the parameter has upper limit.
90  10000, // Parameter's upper limit.
91  "20" // Parameter's typical (default) value.
92  },
93 
94  {
95  "HardOmissionThreshold", // Id.
96  "Hard Omission Threshold", // Name.
97  Real, // Type.
98 
99  // Overview.
100  "Maximum acceptable omission error. Set to 100% to use only soft omission.",
101 
102  // Description.
103  "Maximum acceptable omission error. Set to 100% to use only soft omission.",
104 
105  1, // Not zero if the parameter has lower limit.
106  0, // Parameter's lower limit.
107  1, // Not zero if the parameter has upper limit.
108  100, // Parameter's upper limit.
109  "100" // Parameter's typical (default) value.
110  },
111 
112  {
113  "ModelsUnderOmissionThreshold", // Id.
114  "Models Under Omission Threshold", // Name.
115  Integer, // Type.
116 
117  // Overview.
118  "Minimum number of models below omission threshold.",
119 
120  // Description.
121  "Minimum number of models below omission threshold.",
122 
123  1, // Not zero if the parameter has lower limit.
124  0, // Parameter's lower limit.
125  1, // Not zero if the parameter has upper limit.
126  10000, // Parameter's upper limit.
127  "20" // Parameter's typical (default) value.
128  },
129 
130  {
131  "CommissionThreshold", // Id.
132  "Commission Threshold", // Name.
133  Real, // Type.
134 
135  // Overview.
136  "Percentage of distribution of models to be taken regarding commission error.",
137 
138  // Description.
139  "Percentage of distribution of models to be taken regarding commission error.",
140 
141  1, // Not zero if the parameter has lower limit.
142  0, // Parameter's lower limit.
143  1, // Not zero if the parameter has upper limit.
144  100, // Parameter's upper limit.
145  "50" // Parameter's typical (default) value.
146  },
147 
148  {
149  "CommissionSampleSize", // Id.
150  "Commission Sample Size", // Name.
151  Integer, // Type.
152 
153  // Overview.
154  "Number of samples used to calculate commission error.",
155 
156  // Description.
157  "Number of samples used to calculate commission error.",
158 
159  1, // Not zero if the parameter has lower limit.
160  1, // Parameter's lower limit.
161  0, // Not zero if the parameter has upper limit.
162  0, // Parameter's upper limit.
163  "10000" // Parameter's typical (default) value.
164  },
165 
166  {
167  "MaxThreads", // Id.
168  "Maximum Number of Threads", // Name.
169  Integer, // Type.
170 
171  // Overview.
172  "Maximum number of threads of executions to run simultaneously.",
173 
174  // Description.
175  "Maximum number of threads of executions to run simultaneously.",
176 
177  1, // Not zero if the parameter has lower limit.
178  1, // Parameter's lower limit.
179  1, // Not zero if the parameter has upper limit.
180  1024, // Parameter's upper limit.
181  "1" // Parameter's typical (default) value.
182  },
183 
184  // GARP parameters
185  {
186  "MaxGenerations", // Id.
187  "Max generations", // Name.
188  Integer, // Type.
189 
190  // Overview.
191  "Maximum number of iterations run by the Genetic Algorithm.",
192 
193  // Description.
194  "Maximum number of iterations (generations) run by the Genetic\
195  Algorithm.",
196 
197  1, // Not zero if the parameter has lower limit.
198  1, // Parameter's lower limit.
199  0, // Not zero if the parameter has upper limit.
200  0, // Parameter's upper limit.
201  "400" // Parameter's typical (default) value.
202  },
203 
204  {
205  "ConvergenceLimit", // Id.
206  "Convergence limit", // Name.
207  Real, // Type.
208 
209  // Overview.
210  "Defines the convergence value that makes the algorithm stop\
211  (before reaching MaxGenerations).",
212 
213  // Description.
214  "Defines the convergence value that makes the algorithm stop\
215  (before reaching MaxGenerations).",
216 
217  1, // Not zero if the parameter has lower limit.
218  0.0, // Parameter's lower limit.
219  1, // Not zero if the parameter has upper limit.
220  1.0, // Parameter's upper limit.
221  "0.01" // Parameter's typical (default) value.
222  },
223 
224  {
225  "PopulationSize", // Id.
226  "Population size", // Name.
227  Integer, // Type.
228 
229  "Maximum number of rules to be kept in solution.", // Overview.
230  "Maximum number of rules to be kept in solution.", // Description
231 
232  1, // Not zero if the parameter has lower limit.
233  1, // Parameter's lower limit.
234  1, // Not zero if the parameter has upper limit.
235  500, // Parameter's upper limit.
236  "50" // Parameter's typical (default) value.
237  },
238 
239  {
240  "Resamples", // Id.
241  "Resamples", // Name.
242  Integer, // Type.
243 
244  // Overview.
245  "Number of points sampled (with replacement) used to test rules.",
246 
247  // Description.
248  "Number of points sampled (with replacement) used to test rules.",
249 
250  1, // Not zero if the parameter has lower limit.
251  1, // Parameter's lower limit.
252  1, // Not zero if the parameter has upper limit.
253  100000, // Parameter's upper limit.
254  "2500" // Parameter's typical (default) value.
255  }
256 };
257 
258 /************************************/
259 /*** Algorithm's general metadata ***/
260 
262 
263  "GARP_BS", // Id.
264  "GARP with Best Subsets - new openModeller implementation", // Name.
265  "3.0.4", // Version.
266 
267  // Overview.
268  "GARP is a genetic algorithm that creates ecological niche \
269 models for species. The models describe environmental conditions \
270 under which the species should be able to maintain populations. \
271 For input, GARP uses a set of point localities where the species \
272 is known to occur and a set of geographic layers representing \
273 the environmental parameters that might limit the species' \
274 capabilities to survive. This algorithm applies the Best Subsets \
275 procedure using the new openModeller implementation in each GARP \
276 run. Please refer to GARP single run algorithm description for more \
277 information about the differences between DesktopGarp and the new \
278 GARP implementation.",
279 
280  // Description.
281  "GARP is a genetic algorithm that creates ecological niche \
282 models for species. The models describe environmental conditions \
283 under which the species should be able to maintain populations. \
284 For input, GARP uses a set of point localities where the species \
285 is known to occur and a set of geographic layers representing \
286 the environmental parameters that might limit the species' \
287 capabilities to survive. This algorithm applies the Best Subsets \
288 procedure using the new openModeller implementation in each GARP \
289 run. Please refer to GARP single run algorithm description for more \
290 information about the differences between DesktopGarp and the new \
291 GARP implementation.",
292 
293  // Author
294  "Anderson, R. P., D. Lew, D. and A. T. Peterson.",
295 
296  // Bibliography.
297  "Anderson, R. P., D. Lew, and A. T. Peterson. 2003. \
298 Evaluating predictive models of species' distributions: criteria \
299 for selecting optimal models.Ecological Modelling, v. 162, p. 211 232.",
300 
301  "Ricardo Scachetti Pereira", // Code author.
302  "ricardo [at] tdwg . org", // Code author's contact.
303 
304  0, // Does not accept categorical data.
305  1, // Does not need (pseudo)absence points.
306 
307  NUM_PARAM, // Algorithm's parameters.
308  parameters_bs
309 };
310 
311 
312 /****************************************************************/
313 /****************** Algorithm's factory function ****************/
314 
315 OM_ALG_DLL_EXPORT
316 AlgorithmImpl *
318 {
319  return new GarpBestSubsets();
320 }
321 
322 OM_ALG_DLL_EXPORT
323 AlgMetadata const *
325 {
326  return &metadata_bs;
327 }
328 
329 /****************************************************************/
330 /****************** Garp class **********************************/
331 
333  : BestSubsets(& metadata_bs)
334 {
335  _normalizerPtr = new ScaleNormalizer( -1.0, +1.0, true );
336 }
337 
338 // ****************************************************************
340 {
341 }
342 
343 // ****************************************************************
345 {
346  return new Garp();
347 }
348 
349 // ****************************************************************
350 // ************* initialize ***************************************
351 
353 {
354  // GARP parameters
355  if (!getParameter("MaxGenerations", &_max_gen)) {
356  Log::instance()->error("Parameter MaxGenerations not set properly.\n");
357  return 0;
358  }
359 
360  if (!getParameter("ConvergenceLimit", &_conv_limit)) {
361  Log::instance()->error("Parameter ConvergenceLimit not set properly.\n");
362  return 0;
363  }
364 
365  if (!getParameter("PopulationSize", &_popsize)) {
366  Log::instance()->error("Parameter PopulationSize not set properly.\n");
367  return 0;
368  }
369 
370  if (!getParameter("Resamples", &_resamples)) {
371  Log::instance()->error("Parameter Resamples not set properly.\n");
372  return 0;
373  }
374 
375  if (_alg_params)
376  delete[] _alg_params;
377 
378  _nparam = 4;
380 
381  char buffer[1024];
382  sprintf(buffer, "%d", _max_gen);
383  _alg_params[0].setId("MaxGenerations");
384  _alg_params[0].setValue(buffer);
385 
386  sprintf(buffer, "%f", _conv_limit);
387  _alg_params[1].setId("ConvergenceLimit");
388  _alg_params[1].setValue(buffer);
389 
390  sprintf(buffer, "%d", _popsize);
391  _alg_params[2].setId("PopulationSize");
392  _alg_params[2].setValue(buffer);
393 
394  sprintf(buffer, "%d", _resamples);
395  _alg_params[3].setId("Resamples");
396  _alg_params[3].setValue(buffer);
397 
398  return 1;
399 }
400 
AlgParamMetadata parameters_bs[NUM_PARAM]
void setValue(std::string const val)
Definition: AlgParameter.hh:68
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
AlgorithmImpl * getBSAlgorithm()
void error(const char *format,...)
'Error' level.
Definition: Log.cpp:290
int getParameter(std::string const &name, std::string *value)
AlgMetadata metadata_bs
Definition: garp.hh:57
OM_ALG_DLL_EXPORT AlgorithmImpl * algorithmFactory()
void setId(std::string const id)
Definition: AlgParameter.hh:59
OM_ALG_DLL_EXPORT AlgMetadata const * algorithmMetadata()
AlgParameter * _alg_params
Normalizer * _normalizerPtr
Definition: Algorithm.hh:247
#define NUM_PARAM