openModeller  Version 1.5.0
test_rules_base.cpp
Go to the documentation of this file.
1 
31 #include <TestHarness.h>
32 #include <openmodeller/Sample.hh>
33 #include <rules_base.hh>
34 
35 class ExtGarpRule : public GarpRule
36 {
37 public:
39 
41  Sample& ch1, Sample& ch2, double * perf) :
42  GarpRule(pred, numGenes, ch1, ch2, perf) {};
43 
44  GarpRule * objFactory() const { return new ExtGarpRule(); }
45  int getStrength(const Sample&) const {}
46  bool applies (const Sample&) const {}
47 
48  void setPrediction(Scalar newPrediction) { _prediction = newPrediction; }
49  void setGenes(Scalar * genes, int numGenes);
50 };
51 
52 
53 void ExtGarpRule::setGenes(Scalar * genes, int numGenes)
54 {
55  int i;
56 
58 
59  if (genes)
60  {
61  Scalar values1[_numGenes];
62  Scalar values2[_numGenes];
63 
64  for (i = 0; i < _numGenes; i++)
65  {
66  values1[i] = genes[i * 2];
67  values2[i] = genes[i * 2 + 1];
68  }
69 
70  _chrom1 = Sample(_numGenes, values1);
71  _chrom2 = Sample(_numGenes, values2);
72  }
73 }
74 
75 
76 #define eps 10e-6
77 
78 TEST( initialize, GarpRule )
79 {
80  int i, n = 5;
81 
82  ExtGarpRule * rule = new ExtGarpRule(n);
83 
84  Sample chrom1 = rule->getChrom1();
85  Sample chrom2 = rule->getChrom2();
86 
87  for (i = 0; i < n; ++i)
88  {
89  DOUBLES_EQUAL((double) chrom1[i], -1.0, eps);
90  DOUBLES_EQUAL((double) chrom2[i], +1.0, eps);
91  }
92 
93  delete rule;
94 }
95 
96 
97 // helper function
98 bool checkEqualArray(Scalar * array1, Scalar * array2, int size, double veps)
99 {
100  int i;
101 
102  if (!array1 || !array2)
103  return false;
104 
105  for (i = 0; i < size; i++)
106  if (fabs(array1[i] - array2[i]) > veps)
107  { return false; }
108 
109  return true;
110 }
111 
112 
113 TEST( copy , GarpRule )
114 {
115  double perfs[10];
116  Sample c1(4, -0.5);
117  Sample c2(4, +0.5);
118  Sample blank(4);
119 
120  ExtGarpRule * src = new ExtGarpRule(1.0, 4, c1, c2, perfs);
121  ExtGarpRule * dst = new ExtGarpRule(1.0, 4, blank, blank, perfs);
122 
123  LONGS_EQUAL(1, dst->copy(src));
124 
125  for (int i = 0; i < 4; ++i)
126  {
127  DOUBLES_EQUAL(-0.5, dst->getChrom1()[i], eps);
128  DOUBLES_EQUAL(+0.5, dst->getChrom2()[i], eps);
129  }
130 
131  delete src;
132  delete dst;
133 }
134 
135 
136 TEST( clone , GarpRule )
137 {
138  double perfs[10];
139  Sample c1(4, -0.5);
140  Sample c2(4, +0.5);
141 
142  ExtGarpRule * src = new ExtGarpRule(1.0, 4, c1, c2, perfs);
143  ExtGarpRule * dst = (ExtGarpRule *) src->clone();
144 
145  for (int i = 0; i < 4; ++i)
146  {
147  DOUBLES_EQUAL(-0.5, dst->getChrom1()[i], eps);
148  DOUBLES_EQUAL(+0.5, dst->getChrom2()[i], eps);
149  }
150 
151  delete src;
152  delete dst;
153 }
154 
155 
156 TEST( getCertainty1 , GarpRule )
157 {
158  ExtGarpRule * rule = new ExtGarpRule;
159 
160  rule->setPrediction(0.0);
161 
162  DOUBLES_EQUAL(rule->getCertainty(0.0), 1.0, eps);
163  DOUBLES_EQUAL(rule->getCertainty(1.0), 0.0, eps);
164  DOUBLES_EQUAL(rule->getCertainty(0.5), 0.0, eps);
165 
166  delete rule;
167 }
168 
169 
170 TEST( getCertainty2 , GarpRule )
171 {
172  ExtGarpRule * rule = new ExtGarpRule;
173 
174  rule->setPrediction(1.0);
175 
176  DOUBLES_EQUAL(rule->getCertainty(0.0), 0.0, eps);
177  DOUBLES_EQUAL(rule->getCertainty(1.0), 1.0, eps);
178  DOUBLES_EQUAL(rule->getCertainty(0.5), 0.0, eps);
179 
180  delete rule;
181 }
182 
183 
184 TEST( getError , GarpRule )
185 {
186  ExtGarpRule * rule = new ExtGarpRule;
187 
188  DOUBLES_EQUAL(rule->getError(0.0, 0.0), 0.0, eps);
189  DOUBLES_EQUAL(rule->getError(1.0, 0.0), 1.0, eps);
190  DOUBLES_EQUAL(rule->getError(1.0, -1.0), 2.0, eps);
191  DOUBLES_EQUAL(rule->getError(-1.0, 1.0), 2.0, eps);
192  DOUBLES_EQUAL(rule->getError(0.0, 0.5), 0.5, eps);
193 
194  delete rule;
195 }
196 
197 
198 
199 Scalar genes1[8] = {-0.8, +0.8, -0.8, +0.8, -0.8, +0.8, -0.8, +0.8};
200 Scalar genes2[8] = {-0.8, +0.8, -0.8, +0.8, -0.8, +0.8, -0.8, +0.8};
201 
202 TEST( Similar1, GarpRule )
203 {
204  ExtGarpRule * rule1 = new ExtGarpRule;
205  ExtGarpRule * rule2 = new ExtGarpRule;
206  rule1->setGenes(genes1, 4);
207  rule2->setGenes(genes2, 4);
208 
209  LONGS_EQUAL(rule1->similar(rule2), true);
210 
211  delete rule1;
212  delete rule2;
213 }
214 
215 Scalar genes3[8] = {-0.2, +0.8, -0.8, +0.8, -0.8, +0.8, -0.8, +0.8};
216 Scalar genes4[8] = {+0.7, +0.8, -0.8, +0.8, -0.8, +0.8, -0.8, +0.8};
217 
218 TEST( Similar2, GarpRule )
219 {
220  ExtGarpRule * rule1 = new ExtGarpRule;
221  ExtGarpRule * rule2 = new ExtGarpRule;
222  rule1->setGenes(genes3, 4);
223  rule2->setGenes(genes4, 4);
224 
225  LONGS_EQUAL(rule1->similar(rule2), true);
226 
227  delete rule1;
228  delete rule2;
229 }
230 
231 Scalar genes5[8] = {-0.8, +0.8, -1.0, +1.0, -0.8, +0.8, -0.8, +0.8};
232 Scalar genes6[8] = {-0.8, +0.8, -0.8, +0.8, -0.8, +0.8, -0.8, +0.8};
233 
234 TEST( Similar3, GarpRule )
235 {
236  ExtGarpRule * rule1 = new ExtGarpRule;
237  ExtGarpRule * rule2 = new ExtGarpRule;
238  rule1->setGenes(genes5, 4);
239  rule2->setGenes(genes6, 4);
240 
241  LONGS_EQUAL(rule1->similar(rule2), false);
242 
243  delete rule1;
244  delete rule2;
245 }
246 
247 Scalar genes7[8] = {-0.8, +0.8, -1.0, +0.9, -0.8, +0.8, -0.8, +0.8};
248 Scalar genes8[8] = {-0.8, +0.8, -0.8, +0.8, -0.8, +0.8, -0.8, +0.8};
249 
250 TEST( Similar4, GarpRule )
251 {
252  ExtGarpRule * rule1 = new ExtGarpRule;
253  ExtGarpRule * rule2 = new ExtGarpRule;
254  rule1->setGenes(genes7, 4);
255  rule2->setGenes(genes8, 4);
256 
257  LONGS_EQUAL(rule1->similar(rule2), true);
258 
259  delete rule1;
260  delete rule2;
261 }
262 
#define eps
Scalar genes8[8]
double Scalar
Type of map values.
Definition: om_defs.hh:39
Scalar genes4[8]
GarpRule * objFactory() const
Scalar _prediction
Definition: rules_base.hh:182
Scalar genes2[8]
TEST(initialize, GarpRule)
int numGenes() const
Definition: rules_base.hh:150
Scalar genes5[8]
Sample _chrom1
BYTE vector containing the genes (representation of the variables in a Genetic Algorithm.
Definition: rules_base.hh:180
const Sample & getChrom1() const
Definition: rules_base.hh:159
ExtGarpRule(Scalar pred, int numGenes, Sample &ch1, Sample &ch2, double *perf)
Scalar genes7[8]
int _numGenes
Number of genes stored by the rule.
Definition: rules_base.hh:185
virtual GarpRule * clone() const
Definition: rules_base.cpp:134
virtual int copy(const GarpRule *fromRule)
Definition: rules_base.cpp:157
Sample _chrom2
Definition: rules_base.hh:181
void setGenes(Scalar *genes, int numGenes)
int getStrength(const Sample &) const
Definition: Rule.h:116
virtual int getCertainty(const Scalar pred) const
Definition: rules_base.cpp:183
ExtGarpRule(int numGenes=0)
bool applies(const Sample &) const
bool checkEqualArray(Scalar *array1, Scalar *array2, int size, double veps)
virtual double getError(const Scalar predefinedValue, const Scalar prediction) const
Definition: rules_base.cpp:189
void setPrediction(Scalar newPrediction)
const Sample & getChrom2() const
Definition: rules_base.hh:160
Scalar genes3[8]
Scalar genes6[8]
virtual bool similar(const GarpRule *compareToRule) const
Definition: rules_base.cpp:271
Definition: Sample.hh:25
Scalar genes1[8]