openModeller  Version 1.5.0
rules_logit.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_logit.hh"
44 #include "regression.hh"
45 
46 const double coeficientThreshold = 0.05;
47 
48 // ==========================================================================
49 // LogitRule implelentation
50 // ==========================================================================
52  GarpRule()
53 { }
54 
55 LogitRule::LogitRule(int numGenes) :
56  GarpRule(numGenes)
57 { }
58 
59 LogitRule::LogitRule(Scalar prediction, int numGenes,
60  const Sample& chrom1, const Sample& chrom2,
61  const double * performances) :
62  GarpRule(prediction, numGenes, chrom1, chrom2, performances)
63 {}
64 
65 // ==========================================================================
67 
68 // ==========================================================================
70 {
71  int i, j;
72  Random rnd;
73 
74  for (i = 0; i < _numGenes; i++)
75  {
76  j = rnd.get(_numGenes);
77 
78  // decide where the constant (a) will go;
79  _chrom1[j] = reg.getB()[j];
80  _chrom2[j] = reg.getC()[j];
81  }
82 }
83 
84 // ==========================================================================
85 bool LogitRule::applies(const Sample& sample) const
86 {
87  return (getStrength(sample) == 1);
88 }
89 
90 // ==========================================================================
91 int LogitRule::getStrength(const Sample& sample) const
92 {
93  Scalar sum = 0.0;
94  Scalar prob = 0.0;
95 
96  //static SExprType< PLUS<  
97  //sum = expr(sample)(_chrom1)(_chrom2);
98  //sum = (sample * _chrom1) + (sample * sqr(_chrom2));
99 
100  Sample::const_iterator si = sample.begin();
101  Sample::const_iterator end = sample.end();
104 
105  while (si != end)
106  {
107  if (!equalEps( (*c1i), -1.0 ) )
108  {
109  Scalar c2i2 = (*c2i); c2i2 *= c2i2;
110  sum += ( (*si) * (*c1i) ) + ( (*si) * c2i2 );
111  }
112 
113  ++si; ++c1i; ++c2i;
114  }
115 
116  prob = 1.0 / (1.0 + (double) exp(-sum));
117 
118  return (prob >= 0.5);
119 }
120 
121 // ==========================================================================
122 bool LogitRule::similar(const GarpRule * rule) const
123 {
124  Scalar thisGene, otherGene;
125 
126  if (type() != rule->type())
127  { return false; }
128 
129  // cast to LogitRule to gain access to private data members
130  LogitRule * otherRule = (LogitRule *) rule;
131 
132  // check rule value (presence/absence)
133  if (_prediction != otherRule->_prediction)
134  { return false; }
135 
136  // rules are similar if they share the same relevant coeficients,
137  // i.e., abs(gene) > 0.05.
138  for (int k = 0; k < _numGenes; k++)
139  {
140  thisGene = fabs(_chrom1[k]);
141  otherGene = fabs(otherRule->_chrom1[k]);
142 
143  if ( ( (thisGene < coeficientThreshold) && (otherGene > coeficientThreshold) ) ||
144  ( (thisGene > coeficientThreshold) && (otherGene < coeficientThreshold) ) )
145  {
146  return false;
147  }
148  }
149 
150  return true;
151 }
152 
153 // ==========================================================================
155 {
156  Log::instance()->info( "Logit: " );
157 
158  for (int i = 0; i < _numGenes; ++i)
159  {
160  if (fabs(_chrom1[i]) + fabs(_chrom2[i]) <= coeficientThreshold)
161  Log::instance()->info( "****** ****** ");
162  else
163  Log::instance()->info( "%+6.2f %+6.2f ", _chrom1[i], _chrom2[i] );
164  }
165 
166  Log::instance()->info( "- (%.2f) : %f\n", _prediction, getPerformance(PerfSig));
167 }
168 
169 // ==========================================================================
double get(double min, double max)
Definition: Random.cpp:54
const double coeficientThreshold
Definition: rules_logit.cpp:46
iterator end()
Definition: Sample.hh:88
double Scalar
Type of map values.
Definition: om_defs.hh:39
virtual bool applies(const Sample &sample) const
Definition: rules_logit.cpp:85
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
const Sample getB() const
Definition: regression.hh:49
virtual char type() const
Definition: Rule.h:187
Scalar _prediction
Definition: rules_base.hh:182
iterator begin()
Definition: Sample.hh:87
Sample _chrom1
BYTE vector containing the genes (representation of the variables in a Genetic Algorithm.
Definition: rules_base.hh:180
Definition: Random.hh:44
LogitRule()
Definition: Rule.cpp:728
virtual double getStrength(EnvCell *cell)
Definition: Rule.cpp:814
virtual ~LogitRule()
Definition: Rule.cpp:733
Scalar const * const_iterator
Definition: Sample.hh:90
int _numGenes
Number of genes stored by the rule.
Definition: rules_base.hh:185
virtual void initialize(EnvCellSet *objEnvCellSet, const RuleSet *objRuleSet, bool *geneIsActivePtr, int *geneIndexPtr, int iActGenes)
Definition: Rule.cpp:743
const Sample getC() const
Definition: regression.hh:50
bool equalEps(double v1, double v2)
Definition: rules_base.cpp:62
Sample _chrom2
Definition: rules_base.hh:181
virtual char type() const
Definition: Rule.h:122
Definition: Rule.h:116
void info(const char *format,...)
'Info' level.
Definition: Log.cpp:256
double getPerformance(PerfIndex perfIndex) const
Definition: rules_base.cpp:177
virtual bool similar(Rule *objOtherRule)
Definition: Rule.cpp:873
void log()
Definition: Sample.hh:25