22 #define _SCL_SECURE_NO_DEPRECATE
52 "Maximum number of iterations run by the Genetic Algorithm.",
55 "Maximum number of iterations (generations) run by the Genetic Algorithm.",
70 "Defines the convergence value that makes the algorithm stop (before reaching MaxGenerations).",
73 "Defines the convergence value that makes the algorithm stop (before reaching MaxGenerations).",
87 "Maximum number of rules to be kept in solution.",
88 "Maximum number of rules to be kept in solution.",
103 "Number of points sampled (with replacement) used to test rules.",
106 "Number of points sampled (with replacement) used to test rules.",
123 "GARP (single run) - DesktopGARP implementation",
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.",
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.",
142 "Stockwell, D. R. B., modified by Ricardo Scachetti Pereira",
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.\
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.\
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.",
160 "Ricardo Scachetti Pereira",
161 "rpereira [at] ku.edu",
174 #ifndef DONT_EXPORT_GARP_FACTORY
201 srand((
unsigned)time(NULL));
251 int dim =
_samp->numIndependent();
265 Sample sample = (*oc).environment();
266 Scalar dep = ( (*oc).abundance() > 0.0 ) ? 1.0 : 0.0;
271 values[0] = (
BYTE) dep;
273 for (
int j = 0; j < dim; j++)
275 values[j + 1] = (
BYTE) sample[j];
279 values[dim+1] = (
BYTE)0;
306 float progress = (byIterations > byConvergence) ? byIterations : byConvergence;
383 for (i = 0; i < 2; i++)
384 for (j = 0; j < 5; j++)
418 const int iStringLen = 512;
419 char * strResult =
new char[iStringLen];
421 strcpy(strResult,
"");
424 if (strcmp(strParamName,
"Gen" ) == 0) sprintf(strResult,
"%d",
Gen);
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);
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]);
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);
482 else if (strcmp(strParamName,
"SelectedLayers" ) == 0)
485 sprintf(strResult,
"%s", strAux);
489 if (strlen(strResult) > static_cast<unsigned int>(iStringLen))
490 throw GarpException(82,
"String size exceeded in getParameter::parametersToXML()");
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);
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);
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);
555 else if (strcmp(strParamName,
"SelectedLayers" ) == 0)
setSelectedLayers(strParamValue);
561 const int iStringSize = 1024;
562 char strSelectedLayers[iStringSize], strNextGene[16], * strResult;
564 strcpy(strSelectedLayers,
"");
567 sprintf(strNextGene,
";%d",
iGeneIndex[i] - 1);
568 strcat(strSelectedLayers, strNextGene);
571 if (strlen(strSelectedLayers) > static_cast<unsigned int>(iStringSize))
572 throw GarpException(82,
"String size exceeded in getParameter::parametersToXML()");
574 strResult =
new char[strlen(strSelectedLayers) + 2];
576 if (iActiveGenes > 1)
577 strcpy(strResult, strSelectedLayers + 1);
579 strcpy(strResult, strSelectedLayers);
588 char strAux[1024], * strToken;
597 strcpy(strAux, strParamValue);
598 strToken = strtok(strAux,
";");
599 while (strToken != NULL)
602 iCurrentGene = 1 + atoi(strToken);
607 strToken = strtok(NULL,
";");
647 for(i = 0; i < iNewSize; i++)
693 double performance, ch;
708 for (i = 1; i < iBestSize; i++)
742 for (i = 0; i < 5; i++)
757 FILE * fp = fopen(
"status.txt",
"a");
760 fprintf(fp,
"%5d %f\n",
Gen, perf);
843 for (i = 0; i < intNewRules; i++)
871 newRule->
lId =
Gen * 1000 + i;
874 objRules->
add(newRule);
882 register double performance=0.0;
887 n = objRules->
size();
888 for (i = 0; i < n; i++)
890 pRule = objRules->
get(i);
927 int found, ind, iBestSize;
929 Rule * Temp, * oNewRule;
939 cNewType = oNewRule->
type();
945 for (j = 0, found = 0; j < iBestSize && (!found); j++)
987 Rule * oRuleBeingInserted;
991 oRuleBeingInserted = oNewRule->
clone();
1000 for (j = 0; (iBestSize > 1 && j < iBestSize - 2) &&
1016 for (l = iBestSize - 1; (l >= j); l--)
1033 throw GarpException(1,
"GarpAlgorithm::saveBestRules() method is deprecated");
1058 throw GarpException(1,
"GarpAlgorithm::concatenateRuleSets() method is deprecated");
1066 nf = fromRuleSet->
size();
1068 for (i = 0; i < nf; i++)
1070 fromRule = fromRuleSet->
get(i);
1073 for (j = 0, found =
false; j < toRuleSet->
size() && (!found); j++)
1075 if (fromRule->
type() == toRuleSet->
get(j)->
type())
1076 found = fromRule->
similar(toRuleSet->
get(j));
1082 toRuleSet->
get(j)->
copy(fromRule);
1096 int i, j, k, n, temp;
1133 for (sum += expected; (sum > ptr) && (k<=
Popsize); ptr++)
1142 Sample[j] = Sample[i];
1149 Rule * oRuleBeingInserted;
1153 dSize = ((double) Popsize) *
Gapsize;
1155 for (i = 0; i < dSize; i++)
1179 register int mom, dad;
1180 register int xpoint1;
1181 register int xpoint2;
1213 if (kid1[i] ==
MAX_BYTE) kid1[i] = kid2[i];
1214 if (temp !=
MAX_BYTE) kid2[i] = temp;
1215 diff += (kid1[i] != kid2[i]);
1248 register int mom, dad;
1249 register int xpoint1;
1250 register int xpoint2;
1262 for (mom=0; mom < last ; mom += 2)
1284 diff += (kid1[i] != kid2[i]);
1326 config->addSubsection( model_config );
1328 const int rule_count = rs->
size();
1330 model_config->addNameValue(
"ActiveGenes",
iActiveGenes );
1331 model_config->addNameValue(
"RuleCount", rule_count );
1333 for (
int i=0; i<rule_count; i++ ) {
1340 model_config->addSubsection( rule_config );
1343 sprintf(type,
"%c", rule->
type() );
1344 rule_config->addNameValue(
"Type", type );
1346 rule_config->addNameValue(
"Performance", rule->
dblPerformance, 10 );
1348 rule_config->addNameValue(
"Genes", rule->
Gene, rule->
intLength );
1369 iActiveGenes = model_config->getAttributeAsInt(
"ActiveGenes", 0 );
1373 Configuration::subsection_list::const_iterator ss;
1374 for( ss = model_config->getAllSubsections().begin();
1375 ss != model_config->getAllSubsections().end();
1380 std::string type = c->getAttribute(
"Type" );
1383 c->getAttributeAsDoubleArray(
"Performance", &perf, 0 );
1386 unsigned char *p_genes;
1387 c->getAttributeAsByteArray(
"Genes", &p_genes, &n_genes );
1390 switch( type [0] ) {
int intGenes
Number of genes stored by the rule.
void initialize(int size)
static int randint(int low, int high)
OM_ALG_DLL_EXPORT AlgorithmImpl * algorithmFactory()
static AlgParamMetadata parameters[NUM_PARAM]
double Scalar
Type of map values.
Scalar getValue(const Sample &sample) const
static Log * instance()
Returns the instance pointer, creating the object on the first call.
void setSelectedLayers(char *strParamValue)
void getInitialModel(int intSize, EnvCellSet *objTrainSet)
bool bGeneIsActive[MAX_ENV_LAYERS]
double testWithData(EnvCellSet *objTrainSet)
virtual void initialize(EnvCellSet *objEnvCellSet, const RuleSet *objRuleSet, bool *geneIsActivePtr, int *geneIndexPtr, int iActGenes)=0
virtual void mutate(int intTemperature)
void setDimension(int dim)
static void randomize(unsigned long iOrigSeedProvided=0)
double Worst_current_perf
void error(const char *format,...)
'Error' level.
double getOveralPerformance(int iPerfIndex, int iFirstRulesToBeIncluded)
int getParameter(std::string const &name, std::string *value)
void evaluate(RuleSet *objRules, EnvCellSet *objTrainSet)
void concatenateRuleSets(RuleSet *toRuleSet, RuleSet *fromRuleSet)
char * getParameter2(char *sParamName)
void _setConfiguration(const ConstConfigurationPtr &)
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.
virtual char type() const
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.
static AlgMetadata metadata
char * getSelectedLayersAsString()
double dblPerformance[10]
Vector for storing the performance values for the rule.
void discardRules(int iPerfIndex, double dValue)
float getProgress() const
void colonize(RuleSet *objRules, EnvCellSet *objTrainSet, int intNewRules)
int getConvergence(Scalar *const val) const
const int MAX_MUTATION_TEMPERATURE
void saveBestRules(RuleSet *toRuleSet, RuleSet *fromRuleSet)
Rule * objRules[MAX_RULES]
void initializeProperties()
void updateHeuOpPerformance(char chrType)
void trim(int intMaxRules)
void _getConfiguration(ConfigurationPtr &) const
int iGeneIndex[MAX_ENV_LAYERS]
virtual void copy(Rule *fromRule)
Normalizer * _normalizerPtr
virtual bool similar(Rule *objOtherRule)
void createBioclimHistogram()