openModeller  Version 1.5.0
rules_range.cpp
Go to the documentation of this file.
1 
36 #include <openmodeller/Random.hh>
37 #include <math.h>
38 #include <string.h>
39 
40 #include <openmodeller/Log.hh>
41 #include <openmodeller/Sample.hh>
42 
43 #include "rules_range.hh"
44 #include "bioclim_histogram.hh"
45 
46 
47 // ==========================================================================
48 // RangeRule implelentation
49 // ==========================================================================
51  GarpRule()
52 {}
53 
54 RangeRule::RangeRule(int numGenes) :
55  GarpRule(numGenes)
56 { }
57 
58 RangeRule::RangeRule(Scalar prediction, int numGenes,
59  const Sample& chrom1, const Sample& chrom2,
60  const double * performances) :
61  GarpRule(prediction, numGenes, chrom1, chrom2, performances)
62 {}
63 
64 // ==========================================================================
66 
67 // ==========================================================================
69 {
70  int i, j;
71  Random rnd;
72 
73  // loop iterates through variables
74  for(i = 0; i < _numGenes; i++)
75  {
76  j = rnd.get(_numGenes);
77 
78  Scalar a = 0 , b = 0;
79  histogram.getBioclimRange(_prediction, j, a, b);
80  _chrom1[j] = a;
81  _chrom2[j] = b;
82  }
83 }
84 
85 // ==========================================================================
86 bool RangeRule::applies(const Sample& sample) const
87 {
88  // visit each of the genes
89  for (int i = 0; i < _numGenes; i++)
90  {
91  if (!(equalEps(_chrom1[i], -1.0) && equalEps(_chrom2[i], +1.0)))
92  if (!between(sample[i], _chrom1[i], _chrom2[i]))
93  return false;
94  }
95 
96  return true;
97 }
98 
99 // ==========================================================================
100 int RangeRule::getStrength(const Sample& sample) const
101 {
102  //printf("GetStrength(%+7.3f)\n", _prediction);
103  for (int i = 0; i < _numGenes; i++)
104  {
105  if (!membership(_chrom1[i], _chrom2[i], sample[i]))
106  {
107  //printf("Strength = 0\n");
108  return 0;
109  }
110  }
111 
112  //printf("Strength = 1\n");
113  return 1;
114 }
115 
116 // ==========================================================================
118 {
119  Log::instance()->info( "Range: " );
120  GarpRule::log();
121 }
122 
123 // ==========================================================================
double get(double min, double max)
Definition: Random.cpp:54
RangeRule()
Definition: Rule.cpp:500
virtual void log()
Definition: rules_base.cpp:469
double Scalar
Type of map values.
Definition: om_defs.hh:39
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
void getBioclimRange(Scalar prediction, int layerIndex, Scalar &minCutLevel, Scalar &maxCutLevel) const
virtual bool applies(const Sample &sample) const
Definition: rules_range.cpp:86
Scalar _prediction
Definition: rules_base.hh:182
virtual void initialize(EnvCellSet *objEnvCellSet, const RuleSet *objRuleSet, bool *geneIsActivePtr, int *geneIndexPtr, int iActGenes)
Definition: Rule.cpp:511
Sample _chrom1
BYTE vector containing the genes (representation of the variables in a Genetic Algorithm.
Definition: rules_base.hh:180
virtual double getStrength(EnvCell *cell)
Definition: Rule.cpp:588
Definition: Random.hh:44
bool between(double value, double min, double max)
Definition: rules_base.cpp:67
int membership(double value1, double value2, double value)
Definition: rules_base.cpp:73
int _numGenes
Number of genes stored by the rule.
Definition: rules_base.hh:185
bool equalEps(double v1, double v2)
Definition: rules_base.cpp:62
Sample _chrom2
Definition: rules_base.hh:181
Definition: Rule.h:116
void info(const char *format,...)
'Info' level.
Definition: Log.cpp:256
void log()
virtual ~RangeRule()
Definition: Rule.cpp:503
Definition: Sample.hh:25