openModeller  Version 1.5.0
GarpAlgorithm.cpp
Go to the documentation of this file.
1 /* **************************************
2  * GARP Modeling Package
3  *
4  * **************************************
5  *
6  * Copyright (c), The Center for Research, University of Kansas, 2385 Irving Hill Road, Lawrence, KS 66044-4755, USA.
7  * Copyright (C), David R.B. Stockwell of Symbiotik Pty. Ltd.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the license that is distributed with the software.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * license.txt file included with this software for more details.
16  */
17 
18 // Algorithm.cpp : Implementation of CGarpAlgorithm
19 
20 #ifdef WIN32
21 // avoid warnings caused by problems in VC headers
22 #define _SCL_SECURE_NO_DEPRECATE
23 #endif
24 
25 #include "Rule.h"
26 #include "GarpAlgorithm.h"
27 #include "Utilities.h"
28 #include "EnvCellSet.h"
29 
30 #include <openmodeller/om.hh>
31 #include <openmodeller/Random.hh>
33 
34 #include <time.h>
35 
36 #include <string>
37 
38 #define NUM_PARAM 4
39 
40 /****************************************************************/
41 /*** Algorithm parameter metadata *******************************/
42 
44 {
45  // Metadata of the first parameter.
46  {
47  "MaxGenerations", // Id.
48  "Max generations", // Name.
49  Integer, // Type.
50 
51  // Overview.
52  "Maximum number of iterations run by the Genetic Algorithm.",
53 
54  // Description.
55  "Maximum number of iterations (generations) run by the Genetic Algorithm.",
56 
57  1, // Not zero if the parameter has lower limit.
58  1, // Parameter's lower limit.
59  0, // Not zero if the parameter has upper limit.
60  0, // Parameter's upper limit.
61  "400" // Parameter's typical (default) value.
62  },
63 
64  {
65  "ConvergenceLimit", // Id.
66  "Convergence limit", // Name.
67  Real, // Type.
68 
69  // Overview.
70  "Defines the convergence value that makes the algorithm stop (before reaching MaxGenerations).",
71 
72  // Description.
73  "Defines the convergence value that makes the algorithm stop (before reaching MaxGenerations).",
74 
75  1, // Not zero if the parameter has lower limit.
76  0.0, // Parameter's lower limit.
77  1, // Not zero if the parameter has upper limit.
78  1.0, // Parameter's upper limit.
79  "0.01" // Parameter's typical (default) value.
80  },
81 
82  {
83  "PopulationSize", // Id.
84  "Population size", // Name.
85  Integer, // Type.
86 
87  "Maximum number of rules to be kept in solution.", // Overview.
88  "Maximum number of rules to be kept in solution.", // Description
89 
90  1, // Not zero if the parameter has lower limit.
91  1, // Parameter's lower limit.
92  1, // Not zero if the parameter has upper limit.
93  500, // Parameter's upper limit.
94  "50" // Parameter's typical (default) value.
95  },
96 
97  {
98  "Resamples", // Id.
99  "Resamples", // Name.
100  Integer, // Type.
101 
102  // Overview.
103  "Number of points sampled (with replacement) used to test rules.",
104 
105  // Description.
106  "Number of points sampled (with replacement) used to test rules.",
107 
108  1, // Not zero if the parameter has lower limit.
109  1, // Parameter's lower limit.
110  1, // Not zero if the parameter has upper limit.
111  100000, // Parameter's upper limit.
112  "2500" // Parameter's typical (default) value.
113  }
114 };
115 
116 
117 /*****************************************************************/
118 /*** Algorithm's general metadata ********************************/
119 
121 
122  "DG_GARP", // Id.
123  "GARP (single run) - DesktopGARP implementation", // Name.
124  "1.1 alpha", // Version.
125 
126  // Overview.
127  "This is the 2nd implementation of GARP algorithm, based on the \
128 original C code by David Stockwell. This version correspondss to \
129 the version in use by the DesktopGarp modeling package, with \
130 modifications to use OpenModeller base data access objects.",
131 
132  // Description.
133  "GARP is a genetic algorithm that creates ecological niche \
134 models for species. The models describe environmental conditions \
135 under which the species should be able to maintain populations. \
136 For input, GARP uses a set of point localities where the species \
137 is known to occur and a set of geographic layers representing \
138 the environmental parameters that might limit the species' \
139 capabilities to survive.",
140 
141  // Author
142  "Stockwell, D. R. B., modified by Ricardo Scachetti Pereira",
143 
144  // Bibliography.
145  "Stockwell, D. R. B. 1999. Genetic algorithms II. \
146 Pages 123-144 in A. H. Fielding, editor. \
147 Machine learning methods for ecological applications. \
148 Kluwer Academic Publishers, Boston.\
149 \n\
150 Stockwell, D. R. B., and D. P. Peters. 1999. \
151 The GARP modelling system: Problems and solutions to automated \
152 spatial prediction. International Journal of Geographic \
153 Information Systems 13:143-158.\
154 \n\
155 Stockwell, D. R. B., and I. R. Noble. 1992. \
156 Induction of sets of rules from animal distribution data: \
157 A robust and informative method of analysis. Mathematics and \
158 Computers in Simulation 33:385-390.",
159 
160  "Ricardo Scachetti Pereira", // Code author.
161  "rpereira [at] ku.edu", // Code author's contact.
162 
163  0, // Does not accept categorical data.
164  1, // Does not need (pseudo)absence points.
165 
166  NUM_PARAM, // Algorithm's parameters.
167  parameters
168 };
169 
170 
171 
172 /****************************************************************/
173 /****************** Algorithm's factory function ****************/
174 #ifndef DONT_EXPORT_GARP_FACTORY
175 OM_ALG_DLL_EXPORT
176 AlgorithmImpl *
178 {
179  return new GarpAlgorithm();
180 }
181 
182 OM_ALG_DLL_EXPORT
183 AlgMetadata const *
185 {
186  return &metadata;
187 }
188 #endif
189 
190 
191 // ==========================================================================
192 // ==========================================================================
193 // GarpAlgorithm implementation
194 // ==========================================================================
196  : AlgorithmImpl(& metadata)
197 {
198  _normalizerPtr = new ScaleNormalizer( 1.0, 253.0, true );
199 
201  srand((unsigned)time(NULL));
202 
203  // initialize properties to default values
205 
206  // data points used for training
207  objTrainSet = NULL;
208  //Log::instance()->info("GarpAlgorithm::GarpAlgorithm() at %x\n", this );
209 }
210 
211 // ==========================================================================
213 {
214  //objBest.log();
215 
216  if (objTrainSet)
217  delete objTrainSet;
218 
219  //Log::instance()->info("GarpAlgorithm::~GarpAlgorithm() at %x\n", this );
220 }
221 
222 
223 // ==========================================================================
225 {
226  if (!getParameter("MaxGenerations", &Totalgens)) {
227  Log::instance()->error("Parameter MaxGenerations not set properly.");
228  return 0;
229  }
230 
231  if (!getParameter("ConvergenceLimit", &Conv_limit)) {
232  Log::instance()->error("Parameter ConvergenceLimit not set properly.");
233  return 0;
234  }
235 
236  if (!getParameter("PopulationSize", &Popsize)) {
237  Log::instance()->error("Parameter PopulationSize not set properly.");
238  return 0;
239  }
240 
241  if (!getParameter("Resamples", &Resamples)) {
242  Log::instance()->error("Parameter Resamples not set properly.");
243  return 0;
244  }
245 
246  //Log::instance()->debug("MaxGenerations set to: %d\n", _max_gen);
247  //Log::instance()->debug("ConvergenceLimit set to: %.4f\n", _conv_limit);
248  //Log::instance()->debug("PopulationSize set to: %d\n", _popsize);
249  //Log::instance()->debug("Resamples set to: %d\n", _resamples);
250 
251  int dim = _samp->numIndependent();
252  iActiveGenes = dim + 1;
253  objBest.setDimension(dim + 1);
254  objNew.setDimension(dim + 1);
255 
256  //printf("Dimensions: %d\n", dim);
257 
258  //printf("Presences: %d\nAbsences: %d\n", _samp->numPresence(), _samp->numAbsence());
259 
260  objTrainSet = new EnvCellSet;
262  for (int i = 0; i < Resamples; i++)
263  {
264  OccurrencePtr oc = _samp->getOneSample();
265  Sample sample = (*oc).environment();
266  Scalar dep = ( (*oc).abundance() > 0.0 ) ? 1.0 : 0.0;
267 
268  // transfer values to cell
269  BYTE * values = new BYTE[dim + 2];
270  EnvCell * cell = new EnvCell(dim + 2, values);
271  values[0] = (BYTE) dep;
272  //printf("%5d]: dep=[%d] ", i, values[0]);
273  for (int j = 0; j < dim; j++)
274  {
275  values[j + 1] = (BYTE) sample[j];
276  //printf("%3d (%8.3f) ", values[j + 1], sample[j]);
277  }
278  // Always initialize last element (added by rdg 2009-01-09)
279  values[dim+1] = (BYTE)0;
280 
281  //printf("\n");
282  objTrainSet->add(cell);
283  }
284 
285  // create histograms of occurrence of each layer value for bioclim/range rules
287 
288  // get initial model
290 
291  return 1;
292 }
293 
294 
295 /****************************************************************/
296 /****************** getProgress *********************************/
297 
299 {
300  if (done())
301  { return 1.0; }
302  else
303  {
304  float byIterations = ( Gen / (float) Totalgens );
305  float byConvergence = (float)( Conv_limit / Convergence );
306  float progress = (byIterations > byConvergence) ? byIterations : byConvergence;
307 
308  if (progress > _maxProgress)
309  { _maxProgress = progress; }
310  return _maxProgress;
311  }
312 }
313 
314 
315 /****************************************************************/
316 /****************** getConvergence ******************************/
317 
318 int GarpAlgorithm::getConvergence( Scalar * const val ) const
319 {
320  *val = Convergence;
321  return 0;
322 }
323 
324 
325 /****************************************************************/
326 /****************** getValue ************************************/
327 
329 {
330  return objBest.getValue(x);
331 }
332 
333 // ==========================================================================
335 {
336  int i, j;
337 
338  lVersion = 0;
339 
340  iCPUTime = 0;
341 
342  Improvements = 0;
343 
344  Resamples = 2500;
345  Accuracylimit = 0.0; // The minimium post prob of a rule
346  MinUsage = 0.00;
347  Mortality = 0.9;
348 
349  Experiment = 0; // generations per experiment
350  Totalgens = 2560; // generations per experiment
351  Totaltrials = 0; // trials per experiment
352  Popsize = 50; // population size
353  C_rate = 0.25; // crossover rate
354  M_rate = 0.50; // mutation rate
355  J_rate = 0.25; // join rate
356  I_rate = 0.0;
357  Gapsize = 0.8; // fraction of pop generated from archive
358  Maxspin = 2; // max gens without evals
359  Resampling_f = 1.0 ;
360  Significance = 2.70 ; // the minimum level for inc in best
361  Conv_limit = 0.10 ; // the fractional addition of rules
362  Cutval = 0.0;
363  Trials = 0;
364 
365  // data collection and loop control variables
366  Ave_current_perf = 0.0; // ave perf in current generation
367  Best_current_perf = 0.0; // best perf in current generation
368  Worst_current_perf = 0.0; // worst perf in current generation
369  Best = 0.0; // best performance seen so far
370  Worst = 0.0; // worst performance seen so far
371  Best_guy = 0; // index of best_current_perf
372 
373  Conv = 0; // number of partially coverged genes
374  Doneflag = false;// set when termination conditions hold
375  Gen = 0; // generation counter
376  Lost = 0; // number of totally coverged positions
377  Spin = 0; // number of gens since eval occurred
378  Convergence = 1.0; // the stability of rule set
379  Improvements = 0;// the number of times the best set has been
380  // improved by addition or alteration
381  Resample=1;
382 
383  for (i = 0; i < 2; i++)
384  for (j = 0; j < 5; j++)
385  Heuristic[i][j] = 0; // successes of heuristic operators
386 
387  // flags
388  Sigflag = 1;
389  Postflag = 0;
390  Compflag = 0;
391  Adjustflag = 1;
392 
393  BioclimOnlyFlag = 0;
394  LogitOnlyFlag = 0;
395  RangeRuleFlag = 1;
396  NegatedRuleFlag = 1;
397  AtomicRuleFlag = 1;
398  LogitRuleFlag = 1;
399 
400 
401  // reset gene selection
402  // gene 0 is always active (it is the species gene)
403  iActiveGenes = 1;
404  iGeneIndex[0] = 0;
405  bGeneIsActive[0] = true;
406  for (i = 1; i < MAX_ENV_LAYERS; i++)
407  {
408  bGeneIsActive[i] = true;
409  iGeneIndex[i] = i;
410  }
411 
412  _maxProgress = 0.0;
413 }
414 
415 // ==========================================================================
416 char * GarpAlgorithm::getParameter2(char * strParamName)
417 {
418  const int iStringLen = 512;
419  char * strResult = new char[iStringLen];
420 
421  strcpy(strResult, "");
422 
423  // most used parameters
424  if (strcmp(strParamName, "Gen" ) == 0) sprintf(strResult, "%d", Gen);
425 
426  // flags
427  else if (strcmp(strParamName, "Sigflag" ) == 0) sprintf(strResult, "%d", Sigflag);
428  else if (strcmp(strParamName, "Postflag" ) == 0) sprintf(strResult, "%d", Postflag);
429  else if (strcmp(strParamName, "Compflag" ) == 0) sprintf(strResult, "%d", Compflag);
430  else if (strcmp(strParamName, "Adjustflag" ) == 0) sprintf(strResult, "%d", Adjustflag);
431  else if (strcmp(strParamName, "BioclimOnlyFlag" ) == 0) sprintf(strResult, "%d", BioclimOnlyFlag);
432  else if (strcmp(strParamName, "LogitOnlyFlag" ) == 0) sprintf(strResult, "%d", LogitOnlyFlag);
433  else if (strcmp(strParamName, "RangeRuleFlag" ) == 0) sprintf(strResult, "%d", RangeRuleFlag);
434  else if (strcmp(strParamName, "NegatedRuleFlag" ) == 0) sprintf(strResult, "%d", NegatedRuleFlag);
435  else if (strcmp(strParamName, "AtomicRuleFlag" ) == 0) sprintf(strResult, "%d", AtomicRuleFlag);
436  else if (strcmp(strParamName, "LogitRuleFlag" ) == 0) sprintf(strResult, "%d", LogitRuleFlag);
437 
438  // int parameters
439  else if (strcmp(strParamName, "Version" ) == 0) sprintf(strResult, "%d", static_cast<int>(lVersion));
440  else if (strcmp(strParamName, "CPUTime" ) == 0) sprintf(strResult, "%d", iCPUTime);
441  else if (strcmp(strParamName, "Resamples" ) == 0) sprintf(strResult, "%d", Resamples);
442  else if (strcmp(strParamName, "Totalgens" ) == 0) sprintf(strResult, "%d", Totalgens);
443  else if (strcmp(strParamName, "Totaltrials" ) == 0) sprintf(strResult, "%d", Totaltrials);
444  else if (strcmp(strParamName, "Popsize" ) == 0) sprintf(strResult, "%d", Popsize);
445  else if (strcmp(strParamName, "Maxspin" ) == 0) sprintf(strResult, "%d", Maxspin);
446  else if (strcmp(strParamName, "Best_guy" ) == 0) sprintf(strResult, "%d", Best_guy);
447  else if (strcmp(strParamName, "Resample" ) == 0) sprintf(strResult, "%d", Resample);
448  else if (strcmp(strParamName, "Conv" ) == 0) sprintf(strResult, "%d", Conv);
449  else if (strcmp(strParamName, "Trials" ) == 0) sprintf(strResult, "%d", Trials);
450  else if (strcmp(strParamName, "Experiment" ) == 0) sprintf(strResult, "%d", Experiment);
451  else if (strcmp(strParamName, "Lost" ) == 0) sprintf(strResult, "%d", Lost);
452  else if (strcmp(strParamName, "Spin" ) == 0) sprintf(strResult, "%d", Spin);
453  else if (strcmp(strParamName, "Improvements" ) == 0) sprintf(strResult, "%d", Improvements);
454  else if (strcmp(strParamName, "Doneflag" ) == 0) sprintf(strResult, "%d", (int) Doneflag);
455  else if (strcmp(strParamName, "Heuristic_0" ) == 0) sprintf(strResult, "%d", Heuristic[0][0]);
456  else if (strcmp(strParamName, "Heuristic_1" ) == 0) sprintf(strResult, "%d", Heuristic[0][1]);
457  else if (strcmp(strParamName, "Heuristic_2" ) == 0) sprintf(strResult, "%d", Heuristic[0][2]);
458  else if (strcmp(strParamName, "Heuristic_3" ) == 0) sprintf(strResult, "%d", Heuristic[0][3]);
459  else if (strcmp(strParamName, "Heuristic_4" ) == 0) sprintf(strResult, "%d", Heuristic[0][4]);
460 
461  // double parameters
462  else if (strcmp(strParamName, "Accuracylimit" ) == 0) sprintf(strResult, "%f", Accuracylimit);
463  else if (strcmp(strParamName, "MinUsage" ) == 0) sprintf(strResult, "%f", MinUsage);
464  else if (strcmp(strParamName, "Mortality" ) == 0) sprintf(strResult, "%f", Mortality);
465  else if (strcmp(strParamName, "C_rate" ) == 0) sprintf(strResult, "%f", C_rate);
466  else if (strcmp(strParamName, "M_rate" ) == 0) sprintf(strResult, "%f", M_rate);
467  else if (strcmp(strParamName, "J_rate" ) == 0) sprintf(strResult, "%f", J_rate);
468  else if (strcmp(strParamName, "I_rate" ) == 0) sprintf(strResult, "%f", I_rate);
469  else if (strcmp(strParamName, "Gapsize" ) == 0) sprintf(strResult, "%f", Gapsize);
470  else if (strcmp(strParamName, "Resampling_f" ) == 0) sprintf(strResult, "%f", Resampling_f);
471  else if (strcmp(strParamName, "Significance" ) == 0) sprintf(strResult, "%f", Significance);
472  else if (strcmp(strParamName, "Conv_limit" ) == 0) sprintf(strResult, "%f", Conv_limit);
473  else if (strcmp(strParamName, "Cutval" ) == 0) sprintf(strResult, "%f", Cutval);
474  else if (strcmp(strParamName, "Ave_current_perf" ) == 0) sprintf(strResult, "%f", Ave_current_perf);
475  else if (strcmp(strParamName, "Best_current_perf" ) == 0) sprintf(strResult, "%f", Best_current_perf);
476  else if (strcmp(strParamName, "Worst_current_perf" ) == 0) sprintf(strResult, "%f", Worst_current_perf);
477  else if (strcmp(strParamName, "Best" ) == 0) sprintf(strResult, "%f", Best);
478  else if (strcmp(strParamName, "Worst" ) == 0) sprintf(strResult, "%f", Worst);
479  else if (strcmp(strParamName, "Convergence" ) == 0) sprintf(strResult, "%f", Convergence);
480 
481  // special parameters
482  else if (strcmp(strParamName, "SelectedLayers" ) == 0)
483  {
484  char * strAux = getSelectedLayersAsString();
485  sprintf(strResult, "%s", strAux);
486  delete strAux;
487  }
488 
489  if (strlen(strResult) > static_cast<unsigned int>(iStringLen))
490  throw GarpException(82, "String size exceeded in getParameter::parametersToXML()");
491 
492  return strResult;
493 }
494 
495 // ==========================================================================
496 void GarpAlgorithm::setParameter(char * strParamName, char * strParamValue)
497 {
498  // flags
499  if (strcmp(strParamName, "Sigflag" ) == 0) Sigflag = atoi(strParamValue);
500  else if (strcmp(strParamName, "Postflag" ) == 0) Postflag = atoi(strParamValue);
501  else if (strcmp(strParamName, "Compflag" ) == 0) Compflag = atoi(strParamValue);
502  else if (strcmp(strParamName, "Adjustflag" ) == 0) Adjustflag = atoi(strParamValue);
503  else if (strcmp(strParamName, "BioclimOnlyFlag" ) == 0) BioclimOnlyFlag = atoi(strParamValue);
504  else if (strcmp(strParamName, "LogitOnlyFlag" ) == 0) LogitOnlyFlag = atoi(strParamValue);
505  else if (strcmp(strParamName, "RangeRuleFlag" ) == 0) RangeRuleFlag = atoi(strParamValue);
506  else if (strcmp(strParamName, "NegatedRuleFlag" ) == 0) NegatedRuleFlag = atoi(strParamValue);
507  else if (strcmp(strParamName, "AtomicRuleFlag" ) == 0) AtomicRuleFlag = atoi(strParamValue);
508  else if (strcmp(strParamName, "LogitRuleFlag" ) == 0) LogitRuleFlag = atoi(strParamValue);
509 
510  // int parameters
511  else if (strcmp(strParamName, "Version" ) == 0) lVersion = atoi(strParamValue);
512  else if (strcmp(strParamName, "CPUTime" ) == 0) iCPUTime = atoi(strParamValue);
513  else if (strcmp(strParamName, "Resamples" ) == 0) Resamples = atoi(strParamValue);
514  else if (strcmp(strParamName, "Totalgens" ) == 0) Totalgens = atoi(strParamValue);
515  else if (strcmp(strParamName, "Totaltrials" ) == 0) Totaltrials = atoi(strParamValue);
516  else if (strcmp(strParamName, "Popsize" ) == 0) Popsize = atoi(strParamValue);
517  else if (strcmp(strParamName, "Maxspin" ) == 0) Maxspin = atoi(strParamValue);
518  else if (strcmp(strParamName, "Best_guy" ) == 0) Best_guy = atoi(strParamValue);
519  else if (strcmp(strParamName, "Resample" ) == 0) Resample = atoi(strParamValue);
520  else if (strcmp(strParamName, "Conv" ) == 0) Conv = atoi(strParamValue);
521  else if (strcmp(strParamName, "Trials" ) == 0) Trials = atoi(strParamValue);
522  else if (strcmp(strParamName, "Experiment" ) == 0) Experiment = atoi(strParamValue);
523  else if (strcmp(strParamName, "Gen" ) == 0) Gen = atoi(strParamValue);
524  else if (strcmp(strParamName, "Lost" ) == 0) Lost = atoi(strParamValue);
525  else if (strcmp(strParamName, "Spin" ) == 0) Spin = atoi(strParamValue);
526  else if (strcmp(strParamName, "Improvements" ) == 0) Improvements = atoi(strParamValue);
527  else if (strcmp(strParamName, "Doneflag" ) == 0) Doneflag = ( atoi(strParamValue) ) ? true : false;
528  else if (strcmp(strParamName, "Heuristic_0" ) == 0) Heuristic[0][0] = atoi(strParamValue);
529  else if (strcmp(strParamName, "Heuristic_1" ) == 0) Heuristic[0][1] = atoi(strParamValue);
530  else if (strcmp(strParamName, "Heuristic_2" ) == 0) Heuristic[0][2] = atoi(strParamValue);
531  else if (strcmp(strParamName, "Heuristic_3" ) == 0) Heuristic[0][3] = atoi(strParamValue);
532  else if (strcmp(strParamName, "Heuristic_4" ) == 0) Heuristic[0][4] = atoi(strParamValue);
533 
534  // double parameters
535  else if (strcmp(strParamName, "Accuracylimit" ) == 0) Accuracylimit = atof(strParamValue);
536  else if (strcmp(strParamName, "MinUsage" ) == 0) MinUsage = atof(strParamValue);
537  else if (strcmp(strParamName, "Mortality" ) == 0) Mortality = atof(strParamValue);
538  else if (strcmp(strParamName, "C_rate" ) == 0) C_rate = atof(strParamValue);
539  else if (strcmp(strParamName, "M_rate" ) == 0) M_rate = atof(strParamValue);
540  else if (strcmp(strParamName, "J_rate" ) == 0) J_rate = atof(strParamValue);
541  else if (strcmp(strParamName, "I_rate" ) == 0) I_rate = atof(strParamValue);
542  else if (strcmp(strParamName, "Gapsize" ) == 0) Gapsize = atof(strParamValue);
543  else if (strcmp(strParamName, "Resampling_f" ) == 0) Resampling_f = atof(strParamValue);
544  else if (strcmp(strParamName, "Significance" ) == 0) Significance = atof(strParamValue);
545  else if (strcmp(strParamName, "Conv_limit" ) == 0) Conv_limit = atof(strParamValue);
546  else if (strcmp(strParamName, "Cutval" ) == 0) Cutval = atof(strParamValue);
547  else if (strcmp(strParamName, "Ave_current_perf" ) == 0) Ave_current_perf = atof(strParamValue);
548  else if (strcmp(strParamName, "Best_current_perf" ) == 0) Best_current_perf = atof(strParamValue);
549  else if (strcmp(strParamName, "Worst_current_perf" ) == 0) Worst_current_perf = atof(strParamValue);
550  else if (strcmp(strParamName, "Best" ) == 0) Best = atof(strParamValue);
551  else if (strcmp(strParamName, "Worst" ) == 0) Worst = atof(strParamValue);
552  else if (strcmp(strParamName, "Convergence" ) == 0) Convergence = atof(strParamValue);
553 
554  // special parameters
555  else if (strcmp(strParamName, "SelectedLayers" ) == 0) setSelectedLayers(strParamValue);
556 }
557 
558 // ==========================================================================
560 {
561  const int iStringSize = 1024;
562  char strSelectedLayers[iStringSize], strNextGene[16], * strResult;
563 
564  strcpy(strSelectedLayers, "");
565  for (int i = 1; i < iActiveGenes; i++)
566  {
567  sprintf(strNextGene, ";%d", iGeneIndex[i] - 1);
568  strcat(strSelectedLayers, strNextGene);
569  }
570 
571  if (strlen(strSelectedLayers) > static_cast<unsigned int>(iStringSize))
572  throw GarpException(82, "String size exceeded in getParameter::parametersToXML()");
573 
574  strResult = new char[strlen(strSelectedLayers) + 2];
575  // get rid of the first colon (;) if there is at least one gene active
576  if (iActiveGenes > 1)
577  strcpy(strResult, strSelectedLayers + 1);
578  else
579  strcpy(strResult, strSelectedLayers);
580 
581  return strResult;
582 }
583 
584 // ==========================================================================
585 void GarpAlgorithm::setSelectedLayers(char * strParamValue)
586 {
587  // set active genes, given a string with the gene numbers separeted by colons
588  char strAux[1024], * strToken;
589  int iCurrentGene;
590 
591  iActiveGenes = 1;
592  iGeneIndex[0] = 0;
593  bGeneIsActive[0] = true;
594  for (int i = 1; i < MAX_ENV_LAYERS; i++)
595  bGeneIsActive[i] = false;
596 
597  strcpy(strAux, strParamValue);
598  strToken = strtok(strAux, ";");
599  while (strToken != NULL)
600  {
601  // get the gene number
602  iCurrentGene = 1 + atoi(strToken);
603  bGeneIsActive[iCurrentGene] = true;
604  iGeneIndex[iActiveGenes] = iCurrentGene;
605  iActiveGenes++;
606 
607  strToken = strtok(NULL, ";");
608  }
609 }
610 
611 // ==========================================================================
613 {
614  if (done())
615  return 1;
616 
618 
619  return 1;
620 }
621 
622 // ==========================================================================
624 {
625  return Doneflag = ( Doneflag ||
626  (Gen >= Totalgens) ||
627  (Spin >= Maxspin) ||
628  (Convergence < Conv_limit) );
629 }
630 
631 // ==========================================================================
633 {
634  int i, iNewSize;
635 
636  // create a new population
637  Spin++;
638 
639  if (!Gen || Resample)
640  objTrainSet->resampleInPlace();
641 
642  // evaluate the newly formed population
643  evaluate(&objNew, objTrainSet);
644 
645  // put rule into archive
646  iNewSize = objNew.size();
647  for(i = 0; i < iNewSize; i++)
648  Conv += saveRule(i);
649 
651 
652  // gather performance statistics
653  measure();
654 
655  // check termination condition for this experiment
656  Doneflag = ( done() ) ? true : false;
657 
658  //objBest.gatherRuleSetStats(Gen);
659  //objNew.gatherRuleSetStats(-Gen);
660 
661  if (Doneflag)
662  {
663  // sort rules by usage
664  //objBest.sort(9);
665 
666  // discard rules which performance is worse than the specified significance
668  }
669  else
670  {
671  if (objBest.size())
672  select();
673 
674  // Fill the rest with new rules */
675  colonize(&objNew, objTrainSet, Popsize - objNew.size());
676 
677  // Kill off proportion of archive
678  objBest.trim((int) ((double) objBest.size() * Mortality));
679 
680  // apply heuristic operators
681  mutate();
682  crossover();
683  join();
684  }
685 
686  // one more generation has been computed
687  Gen++;
688 }
689 
690 // ==========================================================================
692 {
693  double performance, ch;
694  int i, iBestSize;
695 
696  iBestSize = objBest.size();
697 
698  // update current statistics
699  if (iBestSize > 0)
700  {
701  performance = objBest.get(0)->dblPerformance[0];
702  Best_current_perf = performance;
703  Worst_current_perf = performance;
704  Ave_current_perf = performance;
705  Best_guy = 0;
706  }
707 
708  for (i = 1; i < iBestSize; i++)
709  {
710  // update current statistics
711  performance = objBest.get(i)->dblPerformance[0];
712 
713  Ave_current_perf += performance;
714  if (BETTER(performance, Best_current_perf))
715  {
716  Best_current_perf = performance;
717  Best_guy = i;
718  }
719 
720  if (BETTER(Worst_current_perf, performance))
721  Worst_current_perf = performance;
722  }
723 
725 
726  // Adjuct heuristic operators
727  if (Adjustflag)
728  {
729  ch = (double) (Heuristic[1][0] - Heuristic[0][0]);
730 
731  if (ch < 1)
732  I_rate = Gapsize = M_rate = 0.1;
733  else
734  {
735  I_rate = ch / (double) objBest.size();
736  Gapsize = 0.5 * (Heuristic[1][1] - Heuristic[0][1]) / ch + 0.1;
737  M_rate = 0.5 * (Heuristic[1][2] - Heuristic[0][2]) / ch + 0.1;
738  C_rate = C_rate ? 0.5 * (Heuristic[1][3] - Heuristic[0][3]) / ch + 0.1 : 0;
739  J_rate = J_rate ? 0.5 * (Heuristic[1][4] - Heuristic[0][4]) / ch + 0.1 : 0;
740  }
741 
742  for (i = 0; i < 5; i++)
743  Heuristic[0][i] = Heuristic[1][i];
744 
745  //printf("gs=%4.2f ", Gapsize);
746  }
747 
748  // update overall performance measures
749  Convergence = converge();
750 
751  //DisplayStatus();
752 }
753 
754 // ==========================================================================
756 {
757  FILE * fp = fopen("status.txt", "a");
758 
759  double perf = objBest.getOveralPerformance(8, 5);
760  fprintf(fp, "%5d %f\n", Gen, perf);
761 
762  /*
763  int i;
764 
765  fprintf(fp,"Max generations %d", Totalgens);
766  fprintf(fp,"\nGens \tTrials \tConv \t\tData\tResamp\tBest \tAverage");
767  fprintf(fp,"\n%4d \t%6d \t%4f \t%4d \t%4d \t%5.2f \t%5.2f",
768  Gen, Trials, Convergence, objTrainSet->count(), objTrainSet->count(),
769  Best, Ave_current_perf);
770  fprintf(fp,"\nHeuristic: Rules Improve Random Mutate Cross Join");
771  fprintf(fp,"\nNo\t\t%d", objBest.size());
772  for (i=0; i<5; i++)
773  fprintf(fp,"\t%d", Heuristic[1][i]);
774 
775  fprintf(fp,"\nRate\t\t\t%4.2f\t%4.2f\t%4.2f\t%4.2f\t%4.2f", I_rate, Gapsize, M_rate, C_rate, J_rate);
776 
777  if (objBest.size() > 0)
778  {
779  //fprintf(fp,"\n<RuleSet>\n");
780  for (i = 0; i < objBest.size(); i++)
781  {
782  Rule * pRule = objBest.objRules[i];
783 
784  if (pRule->dblPerformance[8] > Significance)
785  fprintf(fp,"%f %5d %f %d %f %f %d\n", pRule->_pXYs, pRule->_no, pRule->_dA, -1, pRule->_dSig, pRule->dblPerformance[8], pRule->Gene[0] == pRule->intConclusion);
786  //fprintf(fp,"%s", objBest.objRules[i]->headerToXML());
787  }
788 
789  //fprintf(fp,"</RuleSet>");
790  }
791 
792  fprintf(fp,"\nPerformance: %5.3f",objBest.objRules[0]->dblPerformance[0]);
793 
794  double * Utility;
795 
796  Utility = objBest.objRules[0]->dblPerformance;
797  fprintf(fp,"\n\n\t\tStrength\tCertainty\tError\nAll");
798  for (i=1; i<4; i++)
799  fprintf(fp,"\t\t%5.3f",Utility[i]);
800  fprintf(fp,"\nSelect");
801  for (i=4; i<7; i++)
802  fprintf(fp,"\t\t%5.3f",Utility[i]);
803  fprintf(fp,"\n\n\t\tno/n\t\tSignificance\tError\n");
804  for (i=7; i<10; i++)
805  fprintf(fp,"\t\t%5.3f",Utility[i]);
806  */
807 
808  fclose(fp);
809 }
810 
811 // ==========================================================================
813 {
814  Improvements++;
815 
816  Heuristic[1][0] = Improvements;
817 
818  switch (chrType)
819  {
820  case 'r': Heuristic[1][1]++; break;
821  case 'm': Heuristic[1][2]++; break;
822  case 'c': Heuristic[1][3]++; break;
823  case 'j': Heuristic[1][4]++; break;
824  }
825 }
826 
827 // ==========================================================================
828 void GarpAlgorithm::colonize(RuleSet * objRules, EnvCellSet * objTrainSet, int intNewRules)
829 {
830  int i, p, t;
831  Rule * newRule=0;
832 
833  // number of available rule types
834  int iRuleIndex[4];
835  int iRuleTypes;
836 
837  iRuleTypes = 0;
838  if (RangeRuleFlag) iRuleIndex[iRuleTypes++] = 0;
839  if (NegatedRuleFlag) iRuleIndex[iRuleTypes++] = 1;
840  if (AtomicRuleFlag) iRuleIndex[iRuleTypes++] = 2;
841  if (LogitRuleFlag) iRuleIndex[iRuleTypes++] = 3;
842 
843  for (i = 0; i < intNewRules; i++)
844  {
845  // pick the next rule to be generate
846  p = i % iRuleTypes;
847  t = iRuleIndex[p];
848 
849  switch (t)
850  {
851  case 0:
852  newRule = new RangeRule();
853  break;
854 
855  case 1:
856  newRule = new NegatedRangeRule();
857  break;
858 
859  case 2:
860  newRule = new AtomicRule();
861  break;
862 
863  case 3:
864  newRule = new LogitRule();
865  break;
866  }
867 
868  //printf("Active Genes: %3d\n", iActiveGenes);
869 
870  newRule->initialize(objTrainSet, objRules, bGeneIsActive, iGeneIndex, iActiveGenes);
871  newRule->lId = Gen * 1000 + i;
872  newRule->iOrigGen = Gen;
873 
874  objRules->add(newRule);
875  }
876 }
877 
878 // ==========================================================================
879 void GarpAlgorithm::evaluate(RuleSet * objRules, EnvCellSet * objTrainSet)
880 {
881  Rule * pRule;
882  register double performance=0.0;
883  register int i, n;
884 
885  Conv = 0;
886 
887  n = objRules->size();
888  for (i = 0; i < n; i++)
889  {
890  pRule = objRules->get(i);
891 
892  if (pRule->needsEvaluation())
893  {
894  performance = pRule->testWithData(objTrainSet);
895 
896  /* Make not rule if significance is decreased */
897  /*if (Speciesflag && performance<0)
898  {
899  New[i]->Perf[0] = -New[i]->Perf[0];
900  New[i]->Perf[8] = -New[i]->Perf[8];
901  New[i]->Type = '!';
902  }*/
903 
904  pRule->intGens += 1;
905  pRule->intTrials += 1;
906  pRule->blnNeedsEvaluation = false;
907  Trials++;
908  }
909 
910  Spin = 0; /* we're making progress */
911 
912  if (Trials == 1)
913  Best = performance;
914 
915  if (BETTER(performance, Best))
916  Best = performance;
917  }
918 }
919 
920 // ==========================================================================
921 int GarpAlgorithm::saveRule(int iIndex)
922 {
923  // Save the ith structure in current population
924  // if it is one of the Popsize best seen so far
925 
926  int j, l; // loop control var
927  int found, ind, iBestSize;
928  char cNewType;
929  Rule * Temp, * oNewRule;
930 
931  // Set criteria for optimizing
932  ind = 0;
933 
934  // get best size
935  iBestSize = objBest.size();
936 
937  // get rule from objNew being inserted into objBest
938  oNewRule = objNew.get(iIndex);
939  cNewType = oNewRule->type();
940 
941  // get Gene length
942  oNewRule->intGenes * 2;
943 
944  // Check if an identical or more general structure is already there
945  for (j = 0, found = 0; j < iBestSize && (!found); j++)
946  {
947  if (cNewType == objBest.get(j)->type())
948  found = oNewRule->similar(objBest.get(j));
949  }
950 
951  if (found)
952  {
953  j--;
954 
955  //Rule * oRuleToBeReplaced = objBest.get(j);
956 
957  if (oNewRule->dblPerformance[ind] > objBest.objRules[j]->dblPerformance[ind])
958  // Replace if better
959  {
960  delete objBest.objRules[j];
961  objBest.objRules[j] = oNewRule->clone();
962 
963  /*
964  int k;
965  for (k = 0; k < iLength; k++)
966  oRuleToBeReplaced->Gene[k] = oNewRule->Gene[k];
967 
968  for (k = 0; k < 10; k++)
969  oRuleToBeReplaced->dblPerformance[k] = oNewRule->dblPerformance[k];
970 
971  oRuleToBeReplaced->intGens = oNewRule->intGens;
972  oRuleToBeReplaced->chrOrigin = oNewRule->chrOrigin;
973  oRuleToBeReplaced->intTrials++;
974  */
975 
976  objBest.objRules[j]->intTrials++;
978  }
979 
980  return 1;
981  }
982 
983 
984  // No similar structure found
985  // allocate new structure at the end if room
986 
987  Rule * oRuleBeingInserted;
988  if (iBestSize < Popsize )
989  {
990  // create dummy rule and insert it into the best rule set
991  oRuleBeingInserted = oNewRule->clone();
992  oRuleBeingInserted->dblPerformance[ind] = 0.0;
993  objBest.add(oRuleBeingInserted);
994  }
995 
996  // get best size again
997  iBestSize = objBest.size();
998 
999  // find insertion point and sort j
1000  for (j = 0; (iBestSize > 1 && j < iBestSize - 2) &&
1001  (objBest.get(j)->dblPerformance[ind] > oNewRule->dblPerformance[ind]); j++)
1002  {
1003  if (objBest.get(j)->dblPerformance[ind] < objBest.get(j + 1)->dblPerformance[ind])
1004  {
1005  Temp = objBest.objRules[j];
1006  objBest.objRules[j] = objBest.objRules[j + 1];
1007  objBest.objRules[j + 1] = Temp;
1008  }
1009  }
1010 
1011  if (j >= Popsize)
1012  return 0;
1013 
1014  // Inserting new rule in j
1015  // Shift rules down
1016  for (l = iBestSize - 1; (l >= j); l--)
1017  {
1018  objBest.objRules[l + 1] = objBest.objRules[l];
1019  }
1020 
1021  objBest.objRules[j] = oNewRule->clone();
1022  objBest.intRules++;
1023 
1024  updateHeuOpPerformance(oNewRule->chrOrigin);
1025 
1026  return 1;
1027 }
1028 
1029 // ==========================================================================
1030 void GarpAlgorithm::saveBestRules(RuleSet * toRuleSet, RuleSet * fromRuleSet)
1031 {
1032  // deprecated!!!
1033  throw GarpException(1, "GarpAlgorithm::saveBestRules() method is deprecated");
1034 
1035  /*
1036  // put marks on both sets so it is easy to tell where each rule came from
1037  toRuleSet->setPad(' ');
1038  fromRuleSet->setPad('n');
1039 
1040  // concatenate the rulesets, replacing the similar rules and adding the different ones
1041  concatenateRuleSets(toRuleSet, fromRuleSet);
1042 
1043  // sort the result set
1044  toRuleSet->sort(0);
1045 
1046  // trim it to the max number of rules
1047  toRuleSet->trim(Popsize);
1048 
1049  // count how many of the remaining rules came from the new set
1050  updateHeuOpPerformance(toRuleSet, 'n');
1051  */
1052 }
1053 
1054 // ==========================================================================
1055 void GarpAlgorithm::concatenateRuleSets(RuleSet * toRuleSet, RuleSet * fromRuleSet)
1056 {
1057  // deprecated!!!
1058  throw GarpException(1, "GarpAlgorithm::concatenateRuleSets() method is deprecated");
1059 
1060 
1061  Rule * fromRule;
1062  int i, j, nf;
1063  bool found;
1064 
1065  //nt = toRuleSet->size();
1066  nf = fromRuleSet->size();
1067 
1068  for (i = 0; i < nf; i++)
1069  {
1070  fromRule = fromRuleSet->get(i);
1071 
1072  // Check if an identical or more general structure is already there
1073  for (j = 0, found = false; j < toRuleSet->size() && (!found); j++)
1074  {
1075  if (fromRule->type() == toRuleSet->get(j)->type())
1076  found = fromRule->similar(toRuleSet->get(j));
1077  }
1078 
1079  j--;
1080 
1081  if (found)
1082  toRuleSet->get(j)->copy(fromRule);
1083  else
1084  toRuleSet->add(fromRule->clone());
1085  }
1086 }
1087 
1088 // ==========================================================================
1090 {
1091  double expected; // expected number of offspring
1092  double factor; // normalizer for expected value
1093  double ptr; // determines fractional selection
1094  double sum; // control for selection loop
1095 
1096  int i, j, k, n, temp;
1097 
1098  int Sample[MAX_RULES];
1099 
1100  n = objBest.size();
1101  for (i = 0; i < MAX_RULES; i++)
1102  Sample[i] = i % n;
1103 
1104  // normalizer for proportional selection probabilities
1105  if (Ave_current_perf - Worst)
1106  factor = Maxflag ? 1.0 / (Ave_current_perf - Worst) : 1.0/(Worst - Ave_current_perf);
1107  else
1108  factor=1.0;
1109 
1110 
1111  // Stochastic universal sampling algorithm by James E. Baker
1112  k=0; // index of next Selected structure
1113  ptr = GarpUtil::random(); // spin the wheel one time
1114 
1115  for (sum = i = 0; i < objBest.size(); i++)
1116  {
1117  if (Maxflag)
1118  {
1119  if (objBest.get(i)->dblPerformance[0] > Worst)
1120  expected = (objBest.get(i)->dblPerformance[0] - Worst) * factor;
1121  else
1122  expected = 0.0;
1123  }
1124 
1125  else
1126  {
1127  if (objBest.get(i)->dblPerformance[0] < Worst)
1128  expected = (Worst - objBest.get(i)->dblPerformance[0]) * factor;
1129  else
1130  expected = 0.0;
1131  }
1132 
1133  for (sum += expected; (sum > ptr) && (k<=Popsize); ptr++)
1134  Sample[k++] = i;
1135  }
1136 
1137  // randomly shuffle pointers to new structures
1138  for (i = 0; i < Popsize; i++)
1139  {
1140  j = GarpUtil::randint (i, Popsize - 1);
1141  temp = Sample[j];
1142  Sample[j] = Sample[i];
1143  Sample[i] = temp;
1144  }
1145 
1146  // finally, form the new population
1147  // Gapsize giving the proportion contribution
1148  // to the new population from the objBest archive set
1149  Rule * oRuleBeingInserted;
1150  double dSize;
1151 
1152  objNew.clear();
1153  dSize = ((double) Popsize) * Gapsize;
1154 
1155  for (i = 0; i < dSize; i++)
1156  {
1157  oRuleBeingInserted = objBest.objRules[Sample[i]]->clone();
1158  oRuleBeingInserted->blnNeedsEvaluation = true;
1159  objNew.add(oRuleBeingInserted);
1160  }
1161 }
1162 
1163 // ==========================================================================
1165 {
1166  if (Heuristic[1][0] != 0)
1167  Convergence = (Convergence + ((double)Conv)/Improvements)/2;
1168  else
1169  Convergence = 1.0;
1170 
1171  return Convergence;
1172 }
1173 
1174 // ==========================================================================
1176 {
1177  return;
1178 
1179  register int mom, dad; /* participants in the crossover */
1180  register int xpoint1; /* first crossover point w.r.t. structure */
1181  register int xpoint2; /* second crossover point w.r.t. structure */
1182  register int i; /* loop control variable */
1183  register BYTE temp; /* used for swapping alleles */
1184  static double last; /* last element to undergo Crossover */
1185  int diff; /* set if parents differ from offspring */
1186  BYTE *kid1; /* pointers to the offspring */
1187  BYTE *kid2;
1188 
1189  last = Popsize * J_rate;
1190 
1191  if (J_rate > 0.0)
1192  {
1193  for (mom = Popsize - 1; mom > Popsize-last ; mom -= 2)
1194  {
1195  /* diff wasn't beeing initialized in original Stockwell's code */
1196  diff = 0;
1197 
1198  dad = mom - 1;
1199 
1200  /* kids start as identical copies of parents */
1201  kid1 = objNew.get(mom)->Gene;
1202  kid2 = objNew.get(dad)->Gene;
1203 
1204  /* choose two Crossover points */
1205  xpoint1 = GarpUtil::randint(0, objNew.get(mom)->intLength);
1206  xpoint2 = GarpUtil::randint(0, objNew.get(mom)->intLength);
1207 
1208 
1209  /* perform crossover */
1210  for (i=xpoint1 ; i % objNew.get(mom)->intLength == xpoint2; i++)
1211  {
1212  temp = kid1[i];
1213  if (kid1[i] == MAX_BYTE) kid1[i] = kid2[i];
1214  if (temp != MAX_BYTE) kid2[i] = temp;
1215  diff += (kid1[i] != kid2[i]);
1216  }
1217 
1218  if (diff) /* kids differ from parents */
1219  {
1220  /* set evaluation flags */
1221  objNew.get(mom)->blnNeedsEvaluation = true;
1222  objNew.get(mom)->intGens = 0;
1223  objNew.get(mom)->chrOrigin = 'j';
1224  }
1225  }
1226  }
1227 }
1228 
1229 // ==========================================================================
1231 {
1232  int i;
1233  int Temperature = MAX_MUTATION_TEMPERATURE;
1234 
1235  if (Gen) Temperature = (int)(MAX_MUTATION_TEMPERATURE/(double)Gen);
1236 
1237  for (i = 0; i < Popsize; i++)
1238  objNew.get(i)->mutate(Temperature);
1239 
1240  return;
1241 }
1242 
1243 // ==========================================================================
1245 {
1246  return;
1247 
1248  register int mom, dad; /* participants in the crossover */
1249  register int xpoint1; /* first crossover point w.r.t. structure */
1250  register int xpoint2; /* second crossover point w.r.t. structure */
1251  register int i; /* loop control variable */
1252  register char temp; /* used for swapping alleles */
1253  static double last; /* last element to undergo Crossover */
1254  int diff; /* set if parents differ from offspring */
1255  BYTE *kid1; /* pointers to the offspring */
1256  BYTE *kid2;
1257 
1258  last = C_rate * Popsize;
1259 
1260  if (C_rate > 0.0)
1261  {
1262  for (mom=0; mom < last ; mom += 2)
1263  {
1264  /* diff wasn't beeing initialized in original Stockwell's code */
1265  diff = 0;
1266 
1267  dad = mom + 1;
1268 
1269  /* kids start as identical copies of parents */
1270  kid1 = objNew.get(mom)->Gene;
1271  kid2 = objNew.get(dad)->Gene;
1272 
1273  /* choose two Crossover points */
1274  xpoint1 = GarpUtil::randint(0,objNew.get(mom)->intLength);
1275  xpoint2 = GarpUtil::randint(0,objNew.get(mom)->intLength);
1276 
1277 
1278  /* perform crossover */
1279  for (i=xpoint1 ; i%(objNew.get(mom)->intLength)==xpoint2; i++)
1280  {
1281  temp = kid1[i];
1282  kid1[i] = kid2[i];
1283  kid2[i] = temp;
1284  diff += (kid1[i] != kid2[i]);
1285  }
1286 
1287  if (diff) /* kids differ from parents */
1288  {
1289  /* set evaluation flags */
1290  objNew.get(mom)->blnNeedsEvaluation = true;
1291  objNew.get(dad)->blnNeedsEvaluation = true;
1292  objNew.get(mom)->intGens = 0;
1293  objNew.get(dad)->intGens = 0;
1294  objNew.get(mom)->chrOrigin = 'c';
1295  objNew.get(dad)->chrOrigin = 'c';
1296  }
1297  }
1298  }
1299 }
1300 
1301 // ==========================================================================
1302 void GarpAlgorithm::getInitialModel(int intSize, EnvCellSet * objTrainSet)
1303 {
1304  Popsize = intSize;
1305  colonize(&objNew, objTrainSet, Popsize);
1306 }
1307 
1308 // ==========================================================================
1309 
1310 /****************************************************************/
1311 /****************** configuration *******************************/
1312 void
1314 {
1315 
1316  // Only get the Model portion. Since the parameter portion is
1317  // handled by the base class AlgorithmImpl.
1318  if ( !done() )
1319  return;
1320 
1321  // Const hack.
1322  // To accomodate other's const-incorrectness.
1323  RuleSet* rs = const_cast<RuleSet*>( & objBest );
1324 
1325  ConfigurationPtr model_config ( new ConfigurationImpl("Garp") );
1326  config->addSubsection( model_config );
1327 
1328  const int rule_count = rs->size();
1329 
1330  model_config->addNameValue( "ActiveGenes", iActiveGenes );
1331  model_config->addNameValue( "RuleCount", rule_count );
1332 
1333  for ( int i=0; i<rule_count; i++ ) {
1334 
1335  //Log::instance()->debug( "Gettting Rule %d\n",i );
1336 
1337  Rule *rule = rs->get(i);
1338 
1339  ConfigurationPtr rule_config ( new ConfigurationImpl("Rule") );
1340  model_config->addSubsection( rule_config );
1341 
1342  char type[2];
1343  sprintf(type, "%c", rule->type() );
1344  rule_config->addNameValue( "Type", type );
1345 
1346  rule_config->addNameValue( "Performance", rule->dblPerformance, 10 );
1347 
1348  rule_config->addNameValue( "Genes", rule->Gene, rule->intLength );
1349 
1350  }
1351 
1352 
1353 }
1354 
1355 void
1357 {
1358 
1359  ConstConfigurationPtr model_config = config->getSubsection( "Garp", false );
1360 
1361  if (!model_config)
1362  return;
1363 
1364  Doneflag = true;
1365  //
1366  // Clear out the rule set.
1367  objBest.clear();
1368 
1369  iActiveGenes = model_config->getAttributeAsInt( "ActiveGenes", 0 );
1370 
1372 
1373  Configuration::subsection_list::const_iterator ss;
1374  for( ss = model_config->getAllSubsections().begin();
1375  ss != model_config->getAllSubsections().end();
1376  ++ss ) {
1377 
1378  const ConstConfigurationPtr& c(*ss);
1379 
1380  std::string type = c->getAttribute( "Type" );
1381 
1382  double *perf;
1383  c->getAttributeAsDoubleArray( "Performance", &perf, 0 );
1384 
1385  int n_genes;
1386  unsigned char *p_genes;
1387  c->getAttributeAsByteArray( "Genes", &p_genes, &n_genes );
1388 
1389  Rule * newRule=0;
1390  switch( type [0] ) {
1391 
1392  case 'a':
1393  newRule = new AtomicRule();
1394  newRule->RestoreRule( perf, p_genes, n_genes, iGeneIndex );
1395  break;
1396 
1397  case 'd':
1398  newRule = new RangeRule();
1399  newRule->RestoreRule( perf, p_genes, n_genes, iGeneIndex );
1400  break;
1401 
1402  case 'r':
1403  newRule = new LogitRule();
1404  newRule->RestoreRule( perf, p_genes, n_genes, iGeneIndex );
1405  break;
1406 
1407  case '!':
1408  newRule = new NegatedRangeRule();
1409  newRule->RestoreRule( perf, p_genes, n_genes, iGeneIndex );
1410  break;
1411 
1412  }
1413 
1414  objBest.add(newRule);
1415  delete [] p_genes;
1416  delete [] perf;
1417  }
1418 
1419  return;
1420 
1421 }
int intGenes
Number of genes stored by the rule.
Definition: Rule.h:48
EnvCellSet * objTrainSet
Definition: GarpAlgorithm.h:43
virtual Rule * clone()
Definition: Rule.cpp:73
void initialize(int size)
Definition: EnvCellSet.cpp:116
static int randint(int low, int high)
Definition: Utilities.h:216
char chrOrigin
Definition: Rule.h:60
OM_ALG_DLL_EXPORT AlgorithmImpl * algorithmFactory()
static AlgParamMetadata parameters[NUM_PARAM]
double Scalar
Type of map values.
Definition: om_defs.hh:39
int size()
Definition: RuleSet.cpp:157
int saveRule(int iIndex)
Scalar getValue(const Sample &sample) const
Definition: RuleSet.cpp:411
int Heuristic[2][5]
double Convergence
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
void setSelectedLayers(char *strParamValue)
void getInitialModel(int intSize, EnvCellSet *objTrainSet)
bool bGeneIsActive[MAX_ENV_LAYERS]
#define NUM_PARAM
double testWithData(EnvCellSet *objTrainSet)
Definition: Rule.cpp:387
virtual void initialize(EnvCellSet *objEnvCellSet, const RuleSet *objRuleSet, bool *geneIsActivePtr, int *geneIndexPtr, int iActGenes)=0
Definition: Rule.cpp:180
unsigned char BYTE
Definition: Utilities.h:36
virtual void mutate(int intTemperature)
Definition: Rule.cpp:313
void setDimension(int dim)
Definition: RuleSet.h:65
static void randomize(unsigned long iOrigSeedProvided=0)
Definition: Utilities.h:144
RuleSet objNew
Definition: GarpAlgorithm.h:37
double Worst_current_perf
#define MAX_BYTE
Definition: Utilities.h:80
void error(const char *format,...)
'Error' level.
Definition: Log.cpp:290
virtual ~GarpAlgorithm()
int iOrigGen
Definition: Rule.h:63
double getOveralPerformance(int iPerfIndex, int iFirstRulesToBeIncluded)
Definition: RuleSet.cpp:382
double Best_current_perf
int getParameter(std::string const &name, std::string *value)
void evaluate(RuleSet *objRules, EnvCellSet *objTrainSet)
#define Maxflag
Definition: Utilities.h:42
void concatenateRuleSets(RuleSet *toRuleSet, RuleSet *fromRuleSet)
int done() const
static double random()
Definition: Utilities.h:206
const int MAX_RULES
Definition: Utilities.h:55
char * getParameter2(char *sParamName)
Rule * get(int index)
Definition: RuleSet.cpp:153
void _setConfiguration(const ConstConfigurationPtr &)
void add(EnvCell *cell)
Definition: EnvCellSet.cpp:154
int intGens
Definition: Rule.h:53
const int MAX_ENV_LAYERS
Definition: Utilities.h:53
double Accuracylimit
Definition: Rule.h:37
Scalar getValue(const Sample &x) const
void setParameter(char *sParamName, char *sParamValue)
BYTE * Gene
BYTE vector containing the genes (representation of the variables in a Genetic Algorithm.
Definition: Rule.h:46
double Ave_current_perf
void resampleInPlace()
Definition: EnvCellSet.cpp:80
virtual char type() const
Definition: Rule.h:88
void generate(EnvCellSet *objTestDataset)
OM_ALG_DLL_EXPORT AlgMetadata const * algorithmMetadata()
void RestoreRule(double *perf, unsigned char *genes, int arry_len, int *gene_index)
Restore Model.
Definition: Rule.cpp:156
static AlgMetadata metadata
char * getSelectedLayersAsString()
double dblPerformance[10]
Vector for storing the performance values for the rule.
Definition: Rule.h:51
RuleSet objBest
Definition: GarpAlgorithm.h:40
void clear()
Definition: RuleSet.cpp:55
int intLength
Definition: Rule.h:57
int lId
Definition: Rule.h:62
void discardRules(int iPerfIndex, double dValue)
Definition: RuleSet.cpp:194
bool blnNeedsEvaluation
Definition: Rule.h:52
float getProgress() const
int intRules
Definition: RuleSet.h:42
void colonize(RuleSet *objRules, EnvCellSet *objTrainSet, int intNewRules)
SamplerPtr _samp
Definition: Algorithm.hh:245
int getConvergence(Scalar *const val) const
int intTrials
Definition: Rule.h:54
bool needsEvaluation()
Definition: Rule.h:108
#define BETTER(X, Y)
Definition: Utilities.h:43
const int MAX_MUTATION_TEMPERATURE
Definition: Utilities.h:61
void saveBestRules(RuleSet *toRuleSet, RuleSet *fromRuleSet)
Rule * objRules[MAX_RULES]
Definition: RuleSet.h:41
void initializeProperties()
void add(Rule *objRule)
Definition: RuleSet.cpp:165
void updateHeuOpPerformance(char chrType)
double Resampling_f
void trim(int intMaxRules)
Definition: RuleSet.cpp:179
void _getConfiguration(ConfigurationPtr &) const
int iGeneIndex[MAX_ENV_LAYERS]
virtual void copy(Rule *fromRule)
Definition: Rule.cpp:115
Normalizer * _normalizerPtr
Definition: Algorithm.hh:247
virtual bool similar(Rule *objOtherRule)
Definition: Rule.cpp:349
double Significance
Definition: Sample.hh:25
void createBioclimHistogram()
Definition: EnvCellSet.cpp:167