openModeller  Version 1.4.0
dg_garp_bs.cpp
Go to the documentation of this file.
00001 
00030 #include "dg_garp_bs.hh"
00031 #include "GarpAlgorithm.h"
00032 
00033 #include <openmodeller/om.hh>
00034 #include <openmodeller/Random.hh>
00035 #include <openmodeller/Sampler.hh>
00036 
00037 #include <stdlib.h>
00038 #include <stdio.h>
00039 #include <math.h>
00040 
00041 #ifdef WIN32
00042 #include <windows.h>
00043 #define SLEEP(secs) Sleep(secs * 1000)
00044 #else
00045 #include <unistd.h>
00046 #define SLEEP(secs) sleep(secs);
00047 #endif
00048 
00049 #define NUM_PARAM 11
00050 
00051 /************************************/
00052 /*** Algorithm parameter metadata ***/
00053 
00054 AlgParamMetadata parameters_bs[NUM_PARAM] = 
00055 {
00056   // Best Subsets Parameters
00057   {
00058     "TrainingProportion",          // Id.
00059     "Training Proportion",          // Name.
00060     "Double",                      // Type.
00061 
00062     // Overview.
00063     "Proportion of the occurrence data to be used to train the models.",
00064 
00065     // Description.
00066     "",
00067 
00068     1,      // Not zero if the parameter has lower limit.
00069     0,      // Parameter's lower limit.
00070     1,      // Not zero if the parameter has upper limit.
00071     1,      // Parameter's upper limit.
00072     "0.5"   // Parameter's typical (default) value.
00073   },
00074 
00075   {
00076     "TotalRuns",            // Id.
00077     "Total Runs",           // Name.
00078     "Integer",              // Type.
00079 
00080     // Overview.
00081     "Maximum number of GARP runs to be performed.",
00082 
00083     // Description.
00084     "",
00085 
00086     1,      // Not zero if the parameter has lower limit.
00087     0,      // Parameter's lower limit.
00088     1,      // Not zero if the parameter has upper limit.
00089     10000,  // Parameter's upper limit.
00090     "10"   // Parameter's typical (default) value.
00091   },
00092 
00093   {
00094     "HardOmissionThreshold",          // Id.
00095     "Hard Omission Threshold",        // Name.
00096     "Double",                         // Type.
00097 
00098     // Overview.
00099     "Maximum acceptable omission error. Set to 100% to use only soft omission",
00100 
00101     // Description.
00102     "",
00103 
00104     1,      // Not zero if the parameter has lower limit.
00105     0,      // Parameter's lower limit.
00106     1,      // Not zero if the parameter has upper limit.
00107     100,    // Parameter's upper limit.
00108     "100"    // Parameter's typical (default) value.
00109   },
00110 
00111   {
00112     "ModelsUnderOmissionThreshold",                   // Id.
00113     "Models Under Omission Threshold",                // Name.
00114     "Integer",                                        // Type.
00115 
00116     // Overview.
00117     "Minimum number of models below omission threshold.",
00118 
00119     // Description.
00120     "",
00121 
00122     1,      // Not zero if the parameter has lower limit.
00123     0,      // Parameter's lower limit.
00124     1,      // Not zero if the parameter has upper limit.
00125     10000,  // Parameter's upper limit.
00126     "20"    // Parameter's typical (default) value.
00127   },
00128 
00129   {
00130     "CommissionThreshold",          // Id.
00131     "Commission Threshold",         // Name.
00132     "Double",                       // Type.
00133 
00134     // Overview.
00135     "Percentage of distribution of models to be taken regarding commission error.",
00136 
00137     // Description.
00138     "",
00139 
00140     1,      // Not zero if the parameter has lower limit.
00141     0,      // Parameter's lower limit.
00142     1,      // Not zero if the parameter has upper limit.
00143     100,    // Parameter's upper limit.
00144     "50"    // Parameter's typical (default) value.
00145   },
00146 
00147   {
00148     "CommissionSampleSize",          // Id.
00149     "Commission Sample Size",        // Name.
00150     "Integer",                       // Type.
00151 
00152     // Overview.
00153     "Number of samples used to calculate commission error.",
00154 
00155     // Description.
00156     "",
00157 
00158     1,        // Not zero if the parameter has lower limit.
00159     1,        // Parameter's lower limit.
00160     0,        // Not zero if the parameter has upper limit.
00161     0,        // Parameter's upper limit.
00162     "10000"   // Parameter's typical (default) value.
00163   },
00164 
00165   {
00166     "MaxThreads",                       // Id.
00167     "Maximum Number of Threads",        // Name.
00168     "Integer",                          // Type.
00169 
00170     // Overview.
00171     "Maximum number of threads of executions to run simultaneously.",
00172 
00173     // Description.
00174     "",
00175 
00176     1,        // Not zero if the parameter has lower limit.
00177     1,        // Parameter's lower limit.
00178     1,        // Not zero if the parameter has upper limit.
00179     1024,     // Parameter's upper limit.
00180     "1"       // Parameter's typical (default) value.
00181   },
00182 
00183   // GARP parameters
00184   {
00185     "MaxGenerations",              // Id.
00186     "Max generations",             // Name.
00187     "Integer",                     // Type.
00188 
00189     // Overview.
00190     "Maximum number of iterations run by the Genetic Algorithm.",
00191 
00192     // Description.
00193     "Maximum number of iterations (generations) run by the Genetic\
00194  Algorithm.",
00195 
00196     1,      // Not zero if the parameter has lower limit.
00197     1,      // Parameter's lower limit.
00198     0,      // Not zero if the parameter has upper limit.
00199     0,      // Parameter's upper limit.
00200     "400"   // Parameter's typical (default) value.
00201   },
00202 
00203   {
00204     "ConvergenceLimit",        // Id.
00205     "Convergence limit",       // Name.
00206     "Real",                    // Type.
00207 
00208     // Overview.
00209     "Defines the convergence value that makes the algorithm stop\
00210  (before reaching MaxGenerations).",
00211 
00212     // Description.
00213     "",
00214 
00215     1,     // Not zero if the parameter has lower limit.
00216     0.0,   // Parameter's lower limit.
00217     1,     // Not zero if the parameter has upper limit.
00218     1.0,   // Parameter's upper limit.
00219     "0.01"  // Parameter's typical (default) value.
00220   },
00221 
00222   {
00223     "PopulationSize",        // Id.
00224     "Population size",       // Name.
00225     "Integer",               // Type.
00226 
00227     "Maximum number of rules to be kept in solution.", // Overview.
00228     "", // Description
00229 
00230     1,     // Not zero if the parameter has lower limit.
00231     1,   // Parameter's lower limit.
00232     1,     // Not zero if the parameter has upper limit.
00233     500,   // Parameter's upper limit.
00234     "50"  // Parameter's typical (default) value.
00235   },
00236 
00237   {
00238     "Resamples",      // Id.
00239     "Resamples",      // Name.
00240     "Integer",        // Type.
00241 
00242     // Overview.
00243     "Number of points sampled (with replacement) used to test rules.",
00244 
00245     // Description.
00246     "",
00247 
00248     1,     // Not zero if the parameter has lower limit.
00249     1,   // Parameter's lower limit.
00250     1,     // Not zero if the parameter has upper limit.
00251     100000,   // Parameter's upper limit.
00252     "2500"  // Parameter's typical (default) value.
00253     }
00254 };
00255 
00256 /************************************/
00257 /*** Algorithm's general metadata ***/
00258 
00259 AlgMetadata metadata_bs = {
00260   
00261   "DG_GARP_BS",                        // Id.
00262   "GARP with best subsets - DesktopGARP implementation",  // Name.
00263   "3.0.1 alpha",                       // Version.
00264 
00265   // Overview.
00266   "GARP is a genetic algorithm that creates ecological niche \
00267 models for species. The models describe environmental conditions \
00268 under which the species should be able to maintain populations. \
00269 For input, GARP uses a set of point localities where the species \
00270 is known to occur and a set of geographic layers representing \
00271 the environmental parameters that might limit the species' \
00272 capabilities to survive.",
00273 
00274   // Description.
00275   "GARP is a genetic algorithm that creates ecological niche \
00276 models for species. The models describe environmental conditions \
00277 under which the species should be able to maintain populations. \
00278 For input, GARP uses a set of point localities where the species \
00279 is known to occur and a set of geographic layers representing \
00280 the environmental parameters that might limit the species' \
00281 capabilities to survive.",
00282 
00283   // Author
00284   "Anderson, R. P., D. Lew, D. and A. T. Peterson.",  
00285 
00286   // Bibliography.
00287   "Anderson, R. P., D. Lew, and A. T. Peterson. 2003. \
00288 Evaluating predictive models of species' distributions: criteria \
00289 for selecting optimal models.Ecological Modelling, v. 162, p. 211 232.",
00290 
00291   "Ricardo Scachetti Pereira",  // Code author.
00292   "rpereira [at] ku.edu",       // Code author's contact.
00293 
00294   0,  // Does not accept categorical data.
00295   1,  // Does not need (pseudo)absence points.
00296 
00297   NUM_PARAM,   // Algorithm's parameters.
00298   parameters_bs
00299 };
00300 
00301 
00302 /****************************************************************/
00303 /****************** Algorithm's factory function ****************/
00304 
00305 OM_ALG_DLL_EXPORT 
00306 AlgorithmImpl * 
00307 algorithmFactory()
00308 {
00309   return new DgGarpBestSubsets();
00310 }
00311 
00312 OM_ALG_DLL_EXPORT
00313 AlgMetadata const *
00314 algorithmMetadata()
00315 {
00316   return &metadata_bs;
00317 }
00318 
00319 /****************************************************************/
00320 /****************** Garp class **********************************/
00321 
00322 DgGarpBestSubsets::DgGarpBestSubsets()
00323   : BestSubsets(& metadata_bs)
00324 {
00325   _normalizerPtr = new ScaleNormalizer( 1.0, 253.0, true );
00326 }
00327 
00328 // ****************************************************************
00329 DgGarpBestSubsets::~DgGarpBestSubsets()
00330 {
00331 }
00332 
00333 // ****************************************************************
00334 AlgorithmImpl * DgGarpBestSubsets::getBSAlgorithm()
00335 {
00336   return new GarpAlgorithm();
00337 }
00338 
00339 // ****************************************************************
00340 // ************* initialize ***************************************
00341 
00342 int DgGarpBestSubsets::transferParametersToAlgorithm()
00343 {
00344   // GARP parameters
00345   if (!getParameter("MaxGenerations",   &_max_gen))        
00346       Log::instance()->error(1, "Parameter MaxGenerations not set properly.\n");
00347 
00348   if (!getParameter("ConvergenceLimit", &_conv_limit))     
00349       Log::instance()->error(1, "Parameter ConvergenceLimit not set properly.\n");
00350 
00351   if (!getParameter("PopulationSize",   &_popsize))        
00352       Log::instance()->error(1, "Parameter PopulationSize not set properly.\n");
00353 
00354   if (!getParameter("Resamples",        &_resamples))      
00355       Log::instance()->error(1, "Parameter Resamples not set properly.\n");
00356 
00357   if (_alg_params)
00358     delete[] _alg_params;
00359 
00360   _nparam = 4;
00361   _alg_params = new AlgParameter[_nparam];
00362 
00363   char buffer[1024];
00364   sprintf(buffer, "%d", _max_gen); 
00365   _alg_params[0].setId("MaxGenerations");
00366   _alg_params[0].setValue(buffer);
00367   
00368   sprintf(buffer, "%f", _conv_limit); 
00369   _alg_params[1].setId("ConvergenceLimit");
00370   _alg_params[1].setValue(buffer);
00371 
00372   sprintf(buffer, "%d", _popsize); 
00373   _alg_params[2].setId("PopulationSize");
00374   _alg_params[2].setValue(buffer);
00375 
00376   sprintf(buffer, "%d", _resamples); 
00377   _alg_params[3].setId("Resamples");
00378   _alg_params[3].setValue(buffer);
00379 
00380   return 1;
00381 }
00382