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