openModeller  Version 1.5.0
Rule.cpp
Go to the documentation of this file.
1 /* **************************************
2  * GARP Modeling Package
3  *
4  * **************************************
5  *
6  * Copyright (c), The Center for Research, University of Kansas, 2385 Irving Hill Road, Lawrence, KS 66044-4755, USA.
7  * Copyright (C), David R.B. Stockwell of Symbiotik Pty. Ltd.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the license that is distributed with the software.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * license.txt file included with this software for more details.
16  */
17 
18 // Rule.cpp: implementation of the Rule class.
19 //
21 
22 #include "Rule.h"
23 #include "EnvCellSet.h"
24 #include "Utilities.h"
25 
26 #include <openmodeller/om.hh>
27 
28 #include <math.h>
29 
30 // ==========================================================================
31 // Rule implelentation
32 // ==========================================================================
34 {
35  Gene = NULL;
36  intGenes = 0;
37 
38  for (int i = 0; i < 10; i++)
39  dblPerformance[i] = 0.0;
40 
41  bGeneIsActive = NULL;
42  iGeneIndex = NULL;
43  iActiveGenes = 0;
44 
45  blnNeedsEvaluation = true;
46  intGens = 0;
47  intTrials = 0;
48  intScreener = 0;
49  intScreen = 0;
50  intLength = 0;
51  intNumber = 0;
52  intConclusion = 0;
53  chrOrigin = (char) 0;
54  chrPad = (char) 0;
55 
56  lId = 0;
57  iOrigGen = 0;
58 
59  _pXYs = 0.0;
60  _no = 0;
61  _dA = 0.0;
62  _dSig = 0.0;
63 }
64 
65 // ==========================================================================
67 {
68  if (Gene)
69  delete[] Gene;
70 }
71 
72 // ==========================================================================
74 {
75  int i;
76 
77  Rule * newRule = objFactory();
78 
79  for (i = 0; i < 10; i++)
80  newRule->dblPerformance[i] = dblPerformance[i];
81 
82  newRule->intGenes = intGenes;
83  newRule->intGens = intGens;
85  newRule->intTrials = intTrials;
86  newRule->intScreener = intScreener;
87  newRule->intScreen = intScreen;
88  newRule->intLength = intLength;
89  newRule->chrOrigin = chrOrigin;
90  newRule->chrPad = chrPad;
91 
92  if (newRule->Gene) delete[] newRule->Gene;
93  newRule->Gene = new BYTE[newRule->intLength];
94 
95  for (i = 0;i < intLength; i++)
96  { newRule->Gene[i] = Gene[i]; }
97 
98  // parameters related to the algorithm
99  newRule->bGeneIsActive = bGeneIsActive;
100  newRule->iGeneIndex = iGeneIndex;
101  newRule->iActiveGenes = iActiveGenes;
102 
103  newRule->lId = lId;
104  newRule->iOrigGen = iOrigGen;
105 
106  newRule->_pXYs = _pXYs;
107  newRule->_no = _no;
108  newRule->_dA = _dA;
109  newRule->_dSig = _dSig;
110 
111  return newRule;
112 }
113 
114 // ==========================================================================
115 void Rule::copy(Rule * fromRule)
116 {
117  int i;
118 
119  if (type() != fromRule->type())
120  throw GarpException(1, "Cannot copy rules with different types");
121 
122  for (i = 0; i < 10; i++)
123  dblPerformance[i] = fromRule->dblPerformance[i];
124 
125  intGenes = fromRule->intGenes;
126  intGens = fromRule->intGens;
128  intTrials = fromRule->intTrials;
129  intScreener = fromRule->intScreener;
130  intScreen = fromRule->intScreen;
131  intLength = fromRule->intLength;
132  chrOrigin = fromRule->chrOrigin;
133  chrPad = fromRule->chrPad;
134 
135  if (Gene) delete[] Gene;
136  Gene = new BYTE[intLength];
137 
138  for (i = 0; i < intLength; i++)
139  { Gene[i] = fromRule->Gene[i]; }
140 
141  // parameters related to the algorithm
142  bGeneIsActive = fromRule->bGeneIsActive;
143  iGeneIndex = fromRule->iGeneIndex;
144  iActiveGenes = fromRule->iActiveGenes;
145 
146  lId = fromRule->lId;
147  iOrigGen = fromRule->iOrigGen;
148 
149  _pXYs = fromRule->_pXYs;
150  _no = fromRule->_no;
151  _dA = fromRule->_dA;
152  _dSig = fromRule->_dSig;
153 }
154 // ==========================================================================
155 void
156 Rule::RestoreRule( double *perf, unsigned char *genes, int arry_len, int *gene_index )
157 {
158  for( int i=0; i<10; i++ ) {
159 
160  dblPerformance[i] = perf[i];
161  }
162 
163  iGeneIndex = gene_index;
164 
165  if (Gene)
166  delete [] Gene;
167 
168  Gene = new BYTE[arry_len];
169 
170  intLength = arry_len;
171  iActiveGenes = arry_len/2;
172  for( int i=0; i<intLength; i++ ) {
173  Gene[i] = (BYTE) genes[i];
174  }
175 
176 
177 }
178 
179 // ==========================================================================
180 void Rule::initialize(EnvCellSet * objEnvCellSet, const RuleSet * objRuleSet,
181  bool * bGeneIsActivePtr, int * iGeneIndexPtr, int iActGenes)
182 {
183  int i, p;
184 
185  // parameters related to the algorithm
186  bGeneIsActive = bGeneIsActivePtr;
187  iGeneIndex = iGeneIndexPtr;
188  iActiveGenes = iActGenes;
189 
190  if (!iGeneIndex)
191  i = 0;
192 
193  intTrials = 0;
194  blnNeedsEvaluation = true;
195  intGens = 0;
196  chrOrigin = type();
197  intScreen = 1;
198  chrPad = ' ';
199 
200  // number of genes is equal to the number of values in a cell
201  // dont take the mask into account
202  intGenes = objEnvCellSet->get(0)->size() - 1;
203  intLength = intGenes * 2;
204 
205  //printf("intGenes=%3d intLength=%3d ActGenes=%3d\n", intGenes, intLength, iActGenes);
206 
207  if (Gene) delete[] Gene;
208  Gene = new BYTE[intLength];
209 
210  for (i = 1; i < intGenes; i++)
211  {
212  Gene[i * 2] = 0;
213  Gene[i * 2 + 1] = 255;
214  }
215 
216  p = GarpUtil::randint(0, objEnvCellSet->count() - 1);
217  Gene[0] = objEnvCellSet->get(p)->values[0];
218  Gene[1] = 0;
219 
220  intConclusion = Gene[0];
221 }
222 
223 // ==========================================================================
225 {
226  int i;
227  char typ = (char) 0;
228  char * strText = new char[1024];
229 
230  // rule type
231  typ = this->type();
232  strcpy(strText, "");
233  sprintf(strText, "%c ", typ);
234 
235  // genes
236  for (i = 0; i < intLength; i++)
237  sprintf(strText, "%s %3d ", strText, Gene[i]);
238 
239  // performance
240  for (i = 0; i < 10; i++)
241  sprintf(strText, "%s %-5.3f ", strText, dblPerformance[i]);
242 
243  // other values
244 
245  // check string sizes
246  if (strlen(strText) > 1024)
247  throw GarpException(82, "String size exceeded in Rule::toString()");
248 
249  // return result
250  return strText;
251 }
252 
253 // ==========================================================================
254 char * Rule::toXML()
255 {
256  /* XML Sample
257 
258  <Rule Type="r">
259  <Genes> 1 0 64 128 12 15 0 255 </Genes>
260  <Performance> 0.0000 1.2500 ... </Performance>
261  </Rule>
262  */
263 
264  int i;
265  char strAux[64];
266  char * strXML;
267 
268  strXML = new char[1024];
269 
270  // rule type
271  sprintf(strXML, "<Rule Type=\"%c\" Id=\"%d\" OrigGen=\"%d\">\n", this->type(), lId, iOrigGen);
272 
273  // genes
274  strcat(strXML, " <Genes>");
275  for (i = 0; i < intLength; i++)
276  {
277  sprintf(strAux, " %3d", Gene[i]);
278  strcat(strXML, strAux);
279  }
280  strcat(strXML, "</Genes>\n");
281 
282  // performance
283  strcat(strXML, " <Performance>");
284  for (i = 0; i < 10; i++)
285  {
286  sprintf(strAux, " %-5.3f", dblPerformance[i]);
287  strcat(strXML, strAux);
288  }
289 
290  strcat(strXML, "</Performance>");
291 
292  strcat(strXML, "</Rule>");
293 
294  // other values
295 
296  // check string sizes
297  if (strlen(strXML) > 1024)
298  throw GarpException(82, "String size exceeded in Rule::toXML()");
299 
300  // return result
301  return strXML;
302 }
303 
304 // ==========================================================================
306 { return (Gene[0] == cell->values[0]); }
307 
308 // ==========================================================================
309 double Rule::getError(BYTE pred, EnvCell * cell)
310 { return (double) abs( pred - cell->values[0] ); }
311 
312 // ==========================================================================
313 void Rule::mutate(int intTemperature)
314 {
315  BYTE aux1, aux2, aux_g1, aux_g2;
316  int rnd1, rnd2;
317  int j, k;
318 
320  k = 2 * iGeneIndex[j];
321 
322  rnd1 = GarpUtil::randint(-intTemperature, intTemperature);
323  rnd2 = GarpUtil::randint(-intTemperature, intTemperature);
324 
325  if ((k + 1 > intLength) || (k < 2))
326  throw GarpException(100, "Array read out of bounds (Rule::mutate)");
327 
328  aux1 = Gene[k];
329  aux2 = Gene[k + 1];
330 
331  Gene[k] = (BYTE) (aux1 - rnd1);
332  Gene[k + 1] = (BYTE) (aux2 + rnd2);
333 
334  aux_g1 = MIN(Gene[k], Gene[k + 1]);
335  aux_g2 = MAX(Gene[k], Gene[k + 1]);
336 
337  Gene[k] = aux_g1;
338  Gene[k + 1] = aux_g2;
339 
340  if (type() == 'a')
341  Gene[k + 1] = Gene[k];
342 
343  blnNeedsEvaluation = true;
344  intGens = 0;
345  chrOrigin = 'm';
346 }
347 
348 // ==========================================================================
349 bool Rule::similar(Rule * objOtherRule)
350 {
351  bool found;
352  int j, k;
353  int ng_2k, ng_2k_1, bg_2k, bg_2k_1;
354 
355  if (type() == objOtherRule->type())
356  {
357  // check rule value (presence/absence)
358  if (Gene[0] != objOtherRule->Gene[0])
359  return 0;
360 
361  for (j = 1, found = true; (j < iActiveGenes) && (found); j += 1)
362  {
363  k = iGeneIndex[j];
364 
365  if ((k * 2 + 1 > intLength) || (k * 2 < 2))
366  throw GarpException(100, "Array read out of bounds (Rule::similar)");
367 
368  ng_2k_1 = Gene[k * 2 + 1];
369  ng_2k = Gene[k * 2];
370 
371  bg_2k_1 = objOtherRule->Gene[k * 2 + 1];
372  bg_2k = objOtherRule->Gene[k * 2];
373 
374  found = !( ((ng_2k_1 - ng_2k) == 255 &&
375  (bg_2k_1 - bg_2k) != 255 ) ||
376  ((ng_2k_1 - ng_2k) != 255 &&
377  (bg_2k_1 - bg_2k) == 255 ) );
378  }
379 
380  return found;
381  }
382 
383  return false;
384 }
385 
386 // ==========================================================================
387 double Rule::testWithData(EnvCellSet * objTrainSet)
388 {
389  const int MAX_UTILS = 10;
390  double Utility[MAX_UTILS];
391 
392  EnvCell * cell;
393  int n, i, rnd, no=0;
394  double prediction, certainty, strength;
395  double pXs=0,pYs=0,pXYs=0,pYcXs=0,pXSs=0,pYcs=0;
396 
397  for (i = 1; i < MAX_UTILS; i++)
398  Utility[i] = 0.0;
399 
400  Utility[0] = 1.0;
401 
402  n = objTrainSet->size();
403  for(i=0; i < n; i++)
404  {
405  // Get an in range data point
406  rnd = GarpUtil::randint(0, n - 1);
407  cell = objTrainSet->get(rnd);
408 
409  strength = getStrength(cell);
410  certainty = getCertainty(cell);
411  prediction = getError(128, cell);
412 
413  pXs += strength;
414  pYs += certainty;
415  pYcs += prediction;
416 
417  if (strength > 0)
418  {
419  pXSs += strength;
420  pYcXs += getError((BYTE)prediction, cell);
421  pXYs += (MIN(certainty, strength)) / strength;
422  no++;
423  }
424  }
425 
426  // Priors
427  Utility[1] = pXs/n; // proportion
428  Utility[2] = pYs/n; // Prior probability
429  Utility[3] = pYcs/n;
430 
431  // Posteriors
432  if (no > 0)
433  Utility[4] = pXSs/no;
434  else
435  Utility[4] = 0;
436 
437  if (no > 0) // Posterior probability
438  Utility[5] = pXYs/no;
439  else
440  Utility[5] = 0;
441 
442  if (no>0)
443  Utility[6] = (pYcXs/no);
444  else
445  Utility[6] = 0;
446 
447  Utility[7] = ((double)no)/n;
448 
449  // Crisp Significance
450  if (no >= MIN_SIG_NO && Utility[2] > 0 && Utility[2] < 1.0)
451  Utility[8]= (pXYs-Utility[2] * no) / sqrt(no * Utility[2] * (1 - Utility[2]));
452  else Utility[8] = 0;
453 
454  Utility[9] = 0.0;
455 
456  //flags!!! not implemented yet
457  //if (Postflag)
458  Utility[0] *= Utility[5];
459 
460  //if (Compflag)
461  // Utility[0] *= Utility[7];
462 
463  //if (Sigflag)
464  Utility[0] *= Utility[8];
465 
466  //printf("%c] u0=%+7.3f u1=%+7.3f u2=%+7.3f u8=%+7.3f pXYs=%+7.3f no=%4d n=%4d\n",
467  //type(), Utility[0], Utility[1], Utility[2], Utility[8], pXYs, no, n);
468 
469  //if (Ecoflag)
470  // Utility[0] *= ecoSpace();
471 
472  //if (Lengthflag)
473  // Utility[0] /= length();
474  //*/
475 
476  // Record performance in rule
477 
478  for (i = 0; i < 10; i++)
479  dblPerformance[i] = Utility[i];
480 
481  _pXYs = pXYs;
482  _no = no;
483  _dA = pYs / n;
484  _dSig = Utility[8];
485 
486  return (Utility[0]);
487 }
488 
489 // ==========================================================================
490 // GarpRule implelentation
491 // ==========================================================================
493 
494 // ==========================================================================
496 
497 // ==========================================================================
498 // RangeRule implelentation
499 // ==========================================================================
501 
502 // ==========================================================================
504 
505 // ==========================================================================
506 void RangeRule::fromString(char * strRule)
507 {
508 }
509 
510 // ==========================================================================
511 void RangeRule::initialize(EnvCellSet * objEnvCellSet, const RuleSet * objRuleSet,
512  bool * geneIsActivePtr, int * geneIndexPtr, int iActGenes)
513 {
514  int i, j, k;
515 
516  // call parent initialize
517  GarpRule::initialize(objEnvCellSet, objRuleSet, geneIsActivePtr, geneIndexPtr, iActGenes);
518 
519  // loop iterates through variables
520  for(k = 1; k < iActiveGenes; k++)
521  {
522  i = GarpUtil::randint(1, iActiveGenes);
523 
524  j = iGeneIndex[i];
525 
526  if ((j * 2 + 1 > intLength) || (j * 2 < 2))
527  throw GarpException(100, "Array out of bounds (RangeRule::initialize)");
528 
529  bioclimRange(objEnvCellSet, Gene[0], GarpUtil::random() * 0.1, j);
530  }
531 }
532 
533 // ==========================================================================
534 void RangeRule::bioclimRange(EnvCellSet * objEnvCellSet, BYTE pred, double level, int var)
535 {
536  BioclimHistogram * histogram;
537  int sum, n;
538  int UL = 0;
539  int LL = 0;
540 
541  histogram = objEnvCellSet->getBioclimHistogram();
542 
543  sum = 0;
544 
545  for (n = 0; n < 256; n++)
546  {
547  sum += histogram->matrix[pred][var][n];
548  if (sum>(level * histogram->matrix[pred][0][pred]))
549  {
550  // printf("%d %d %f",var,sum,level);
551  LL = n;
552  break;
553  }
554  }
555 
556  sum = 0;
557 
558  for (n = 255; n >= 0; n--)
559  {
560  sum += histogram->matrix[pred][var][n];
561  if (sum > (level * histogram->matrix[pred][0][pred]))
562  {
563  // printf("%d %d %f",var,sum,level);
564  UL = n;
565  break;
566  }
567  }
568 
569  Gene[var * 2 + 1] = (BYTE) UL;
570  Gene[var * 2] = (BYTE) LL;
571 }
572 
573 // ==========================================================================
575 {
576  // visit each of the genes
577  for (int i = 1; i < iActiveGenes; i++)
578  {
579  if (!((Gene[iGeneIndex[i] * 2] == 0) && (Gene[iGeneIndex[i] * 2 + 1] == 255)))
580  if (GarpUtil::notBetween(cell->values[iGeneIndex[i]], Gene[iGeneIndex[i] * 2], Gene[iGeneIndex[i] * 2 + 1]))
581  return false;
582  }
583 
584  return true;
585 }
586 
587 // ==========================================================================
589 {
590  int a, b, c;
591  int i, k;
592 
593  //printf("GetStrength(%2d)\n", Gene[0]);
594  for (k = 1; k < iActiveGenes; k++)
595  {
596  // get the index of the kth active gene
597  i = iGeneIndex[k];
598 
599  if ((i * 2 + 1 > intLength) || (i * 2 < 2))
600  throw GarpException(100, "Array out of bounds (RangeRule::getStrength)");
601 
602  a = Gene[i * 2];
603  b = Gene[i * 2 + 1];
604  c = cell->values[i];
605 
606  if (!GarpUtil::membership(a, b, c))
607  {
608  //printf("Strength = 0\n");
609  return 0;
610  }
611  }
612 
613  //printf("Strength = 1\n");
614  return 1.0;
615 }
616 
617 // ==========================================================================
618 // NegatedRangeRule implelentation
619 // ==========================================================================
621 // ==========================================================================
623 // ==========================================================================
624 void NegatedRangeRule::fromString(char * strRule)
625 {
626 }
627 
628 // ==========================================================================
630 {
631  int i, j;
632 
633  // visit each of the genes
634  for (i = 1; i < iActiveGenes; i++)
635  {
636  j = iGeneIndex[i];
637 
638  if ((j * 2 + 1 > intLength) || (j * 2 < 2))
639  throw GarpException(100, "Array out of bounds (NegatedRangeRule::applyToCell)");
640 
641  if (!((Gene[j * 2] == 0) && (Gene[j * 2 + 1] == 255)))
642  if (GarpUtil::notBetween(cell->values[j], Gene[j * 2], Gene[j * 2 + 1]))
643  return true;
644  }
645 
646  return false;
647 }
648 
649 // ==========================================================================
651 {
652  double strength, neg_strength;
653 
654  strength = RangeRule::getStrength(cell);
655  neg_strength = 1 - strength;
656 
657  return neg_strength;
658 }
659 
660 // ==========================================================================
661 // AtomicRule implelentation
662 // ==========================================================================
664 
665 // ==========================================================================
667 
668 // ==========================================================================
669 void AtomicRule::initialize(EnvCellSet * objEnvCellSet, const RuleSet * objRuleSet,
670  bool * geneIsActivePtr, int * geneIndexPtr, int iActGenes)
671 {
672  int i, j, k, p;
673 
674  // call inherited initialize
675  GarpRule::initialize(objEnvCellSet, objRuleSet, geneIsActivePtr, geneIndexPtr, iActGenes);
676 
677  p = GarpUtil::randint(0, objEnvCellSet->size());
678  EnvCell * objEnvCell = objEnvCellSet->get(p);
679 
680  for (i = 1; i < iActiveGenes; i++)
681  {
682  j = GarpUtil::randint(1, iActiveGenes);
683  k = iGeneIndex[j];
684 
685  if ((k * 2 + 1 > intLength) || (k * 2 < 2))
686  throw GarpException(100, "Array out of bounds (AtomicRule::initialize)");
687 
688  Gene[2 * k] = Gene[2 * k + 1] = objEnvCell->values[k];
689  }
690 }
691 
692 // ==========================================================================
693 void AtomicRule::fromString(char * strRule)
694 {
695 }
696 
697 // ==========================================================================
699 {
700  int i;
701 
702  // visit each of the genes
703  for (i = 1; i < iActiveGenes; i++)
704  {
705  if (!((Gene[iGeneIndex[i] * 2] == 0) && (Gene[iGeneIndex[i] * 2 + 1] == 255)))
706  if (!(cell->values[iGeneIndex[i]] == Gene[iGeneIndex[i] * 2]))
707  return false;
708  }
709 
710  return true;
711 }
712 
713 // ==========================================================================
715 {
716  for (int i = 1; i < iActiveGenes; i++)
717  {
718  if (!GarpUtil::membership(Gene[iGeneIndex[i] * 2], Gene[iGeneIndex[i] * 2 + 1], cell->values[iGeneIndex[i]]))
719  return 0;
720  }
721 
722  return 1;
723 }
724 
725 // ==========================================================================
726 // LogitRule implelentation
727 // ==========================================================================
729 {
730 }
731 
732 // ==========================================================================
734 {
735 }
736 
737 // ==========================================================================
738 void LogitRule::fromString(char * strRule)
739 {
740 }
741 
742 // ==========================================================================
743 void LogitRule::initialize(EnvCellSet * objEnvCellSet, const RuleSet * objRuleSet,
744  bool * geneIsActivePtr, int * geneIndexPtr, int iActGenes)
745 {
746  int i, j, k;
747  double constant, coef[2];
748 
749  // call inherited initialize
750  GarpRule::initialize(objEnvCellSet, objRuleSet, geneIsActivePtr, geneIndexPtr, iActGenes);
751 
752  // TO DO: must take into account the variables in use (may use some, or all)
753  for (i = 0; i < iActiveGenes; i++)
754  {
755  k = GarpUtil::randint(1, iActiveGenes);
756  j = iGeneIndex[k];
757 
758  regression(objEnvCellSet, j, constant, coef[0], coef[1]);
759 
760  Gene[1] = (int) constant;
761  Gene[j * 2] = (int) coef[0];
762  Gene[j * 2 + 1] = (int) coef[1];
763  }
764 }
765 
766 // ==========================================================================
767 int LogitRule::regression(EnvCellSet * objEnvCellSet, int dep, double& constant, double& coef1, double& coef2)
768 {
769  double a, b, x, y, xi, yi, xiyi, xi2, xb, xx, xxi, xxiyi, xxi2;
770  int i, n, pred;
771  BYTE * values;
772 
773  n = objEnvCellSet->count();
774  pred = 0;
775 
776  a = b = x = y = xi = yi = xiyi = xi2 = xb = xx = xxi = xxiyi = xxi2 = 0.0;
777 
778  for (i = 0; i < n; i++)
779  {
780  values = objEnvCellSet->get(i)->values;
781 
782  y = (double) values[pred];
783  x = (double) values[dep];
784 
785  xx = x * x;
786  xi += x;
787  yi += y;
788  xiyi += x * y;
789  xi2 += x * x;
790 
791  xxi += xx;
792  xxiyi += xx * y;
793  xxi2 += xx * xx;
794  }
795 
796  b = (n * xiyi - xi * yi) / (n * xi2 - (xi * xi));
797  coef1 = ((b * 255) + 128);
798 
799  a = yi / n - b * xi / n;
800  constant = a;
801  xb = (n * xxiyi - xxi * yi) / (n * xxi2 - (xxi * xxi));
802  coef2 = ((xb * 255) + 128);
803 
804  //printf("\nb= %f %f %f %f %f %f %d",b,a,xb,coef[0],coef[1],coef[2],n);
805 
806  return n;
807 }
808 
809 // ==========================================================================
811 { return (getStrength(cell) == 1.0); }
812 
813 // ==========================================================================
815 {
816  BYTE * Data;
817  int i, k;
818  double Sum, prob, r;
819 
820  Sum = 0.0;
821  prob = 0.0;
822 
823  Data = cell->values;
824 
825  for (k = 1; k < iActiveGenes; k++)
826  {
827  i = iGeneIndex[k];
828 
829  if ((i * 2 + 1 > intLength) || (i * 2 < 2))
830  throw GarpException(100, "Array read out of bounds (LogitRule::getStrength)");
831 
832  if (GarpUtil::membership(Gene[i * 2], Gene[i * 2 + 1], 1) != 255)
833  {
834  r = (double) (Data[i] / 254.0);
835 
836  Sum += ((double) (Gene[i * 2] - 128)) * r;
837  Sum += ((double) (Gene[i * 2 + 1] - 128)) * r * r;
838 
839  //printf("\ni:%d %d C:%f %d %f", i,Rule->Gene[i*2],v,Data[i],Sum);
840  }
841 
842  prob = 1.0 / (1.0 + (double) exp(-Sum));
843 
844  //printf("\nprob %f",prob);
845  }
846 
847  if (prob > 0.5)
848  return 1.0;
849  else
850  return 0.0;
851 }
852 
853 // ==========================================================================
854 void LogitRule::mutate(int intTemperature)
855 {
856  int j, k;
857 
859  j = 2 * iGeneIndex[k];
860 
861  if ((j > intLength) || (j < 0))
862  throw GarpException(100, "Array read out of bounds (LogitRule::mutate)");
863 
864  Gene[j] = Gene[j] + GarpUtil::randint(-intTemperature, intTemperature);
865  Gene[j + 1] = Gene[j + 1] + GarpUtil::randint(-intTemperature, intTemperature);
866 
867  blnNeedsEvaluation = true;
868  intGens = 0;
869  chrOrigin = 'm';
870 }
871 
872 // ==========================================================================
873 bool LogitRule::similar(Rule * objRule)
874 {
875  bool found;
876  int k;
877 
878  LogitRule * objOtherRule = (LogitRule *) objRule;
879 
880  if (type() == objOtherRule->type())
881  {
882  // check rule value (presence/absence)
883  if (Gene[0] != objOtherRule->Gene[0])
884  return 0;
885 
886  for (k = 2, found = true; (k < intLength) && (found); k ++)
887  {
888  if ((k > intLength) || (k < 2))
889  throw GarpException(100, "Array read out of bounds (LogitRule::similar)");
890 
891  found = !( ((abs(Gene[k] - 128) < 10) &&
892  (abs(objOtherRule->Gene[k] - 128) > 10)) ||
893  ((abs(Gene[k] - 128)>10) &&
894  (abs(objOtherRule->Gene[k] - 128)<10)) );
895  }
896 
897  return found;
898  }
899 
900  return false;
901 }
902 
903 // ==========================================================================
904 void Rule::log()
905 {
906  printf("<%c> ", type());
907  for (int i = 0; i < intGenes * 2; i += 2)
908  {
909  //if (fabs(Genes[i] - Gene[i + 1]) >= 2.0)
910  //Log::instance()->info( "******** ******** ");
911  //else
912  printf( "%3d %3d ", Gene[i], Gene[i + 1] );
913  }
914 
915  printf( "- (%2d) : %f\n", Gene[0], dblPerformance[0]);
916 }
917 
918 // ==========================================================================
919 
Rule()
Default constructor.
Definition: Rule.cpp:33
int intScreen
Definition: Rule.h:56
virtual ~NegatedRangeRule()
Definition: Rule.cpp:622
virtual void fromString(char *strRule)
Definition: Rule.cpp:624
int intGenes
Number of genes stored by the rule.
Definition: Rule.h:48
RangeRule()
Definition: Rule.cpp:500
virtual Rule * clone()
Definition: Rule.cpp:73
double _pXYs
Definition: Rule.h:65
BYTE matrix[2][MAX_ENV_LAYERS][256]
Definition: Utilities.h:264
virtual void fromString(char *strRule)
Definition: Rule.cpp:506
static int randint(int low, int high)
Definition: Utilities.h:216
char chrOrigin
Definition: Rule.h:60
BYTE * values
Definition: EnvCell.h:32
bool * bGeneIsActive
Definition: Rule.h:71
virtual bool applyToCell(EnvCell *cell)
Definition: Rule.cpp:810
static int notBetween(double x, double y, double z)
Definition: Utilities.h:187
int intScreener
Definition: Rule.h:55
virtual double getStrength(EnvCell *cell)=0
static int membership(BYTE rval1, BYTE rval2, BYTE value)
Definition: Utilities.h:246
virtual ~AtomicRule()
Definition: Rule.cpp:666
double testWithData(EnvCellSet *objTrainSet)
Definition: Rule.cpp:387
BioclimHistogram * getBioclimHistogram()
Definition: EnvCellSet.cpp:163
virtual char type() const
Definition: Rule.h:187
virtual double getStrength(EnvCell *cell)
Definition: Rule.cpp:650
int iActiveGenes
Definition: Rule.h:73
virtual void initialize(EnvCellSet *objEnvCellSet, const RuleSet *objRuleSet, bool *geneIsActivePtr, int *geneIndexPtr, int iActGenes)=0
Definition: Rule.cpp:180
virtual char * toString()
Definition: Rule.cpp:224
virtual void initialize(EnvCellSet *objEnvCellSet, const RuleSet *objRuleSet, bool *geneIsActivePtr, int *geneIndexPtr, int iActGenes)
Definition: Rule.cpp:669
unsigned char BYTE
Definition: Utilities.h:36
virtual void mutate(int intTemperature)
Definition: Rule.cpp:313
virtual char * toXML()
Definition: Rule.cpp:254
AtomicRule()
Definition: Rule.cpp:663
virtual void initialize(EnvCellSet *objEnvCellSet, const RuleSet *objRuleSet, bool *geneIsActivePtr, int *geneIndexPtr, int iActGenes)
Definition: Rule.cpp:511
double _dSig
Definition: Rule.h:68
int iOrigGen
Definition: Rule.h:63
virtual double getStrength(EnvCell *cell)
Definition: Rule.cpp:588
int regression(EnvCellSet *objEnvCellSet, int dep, double &constant, double &coef1, double &coef2)
Definition: Rule.cpp:767
LogitRule()
Definition: Rule.cpp:728
static double random()
Definition: Utilities.h:206
virtual double getStrength(EnvCell *cell)
Definition: Rule.cpp:714
GarpRule()
Definition: Rule.cpp:492
int intGens
Definition: Rule.h:53
virtual double getStrength(EnvCell *cell)
Definition: Rule.cpp:814
Definition: Rule.h:37
int * iGeneIndex
Definition: Rule.h:72
int intNumber
Definition: Rule.h:58
virtual ~LogitRule()
Definition: Rule.cpp:733
BYTE * Gene
BYTE vector containing the genes (representation of the variables in a Genetic Algorithm.
Definition: Rule.h:46
virtual void mutate(int intTemperature)
Definition: Rule.cpp:854
virtual void initialize(EnvCellSet *objEnvCellSet, const RuleSet *objRuleSet, bool *geneIsActivePtr, int *geneIndexPtr, int iActGenes)
Definition: Rule.cpp:743
virtual char type() const
Definition: Rule.h:88
void RestoreRule(double *perf, unsigned char *genes, int arry_len, int *gene_index)
Restore Model.
Definition: Rule.cpp:156
int _no
Definition: Rule.h:66
virtual ~GarpRule()
Definition: Rule.cpp:495
double dblPerformance[10]
Vector for storing the performance values for the rule.
Definition: Rule.h:51
virtual ~Rule()
Default destructor.
Definition: Rule.cpp:66
virtual Rule * objFactory()=0
int intLength
Definition: Rule.h:57
int lId
Definition: Rule.h:62
bool blnNeedsEvaluation
Definition: Rule.h:52
virtual void fromString(char *strRule)
Definition: Rule.cpp:693
void bioclimRange(EnvCellSet *objEnvCellSet, BYTE pred, double level, int var)
Definition: Rule.cpp:534
virtual double getCertainty(EnvCell *cell)
Definition: Rule.cpp:305
virtual bool applyToCell(EnvCell *cell)
Definition: Rule.cpp:629
virtual bool applyToCell(EnvCell *cell)
Definition: Rule.cpp:698
int intConclusion
Definition: Rule.h:59
virtual void fromString(char *strRule)
Definition: Rule.cpp:738
int intTrials
Definition: Rule.h:54
virtual bool applyToCell(EnvCell *cell)
Definition: Rule.cpp:574
const int MIN_SIG_NO
Definition: Utilities.h:63
virtual bool similar(Rule *objOtherRule)
Definition: Rule.cpp:873
char chrPad
Definition: Rule.h:61
virtual ~RangeRule()
Definition: Rule.cpp:503
virtual double getError(BYTE pred, EnvCell *cell)
Definition: Rule.cpp:309
virtual void log()
Definition: Rule.cpp:904
virtual void copy(Rule *fromRule)
Definition: Rule.cpp:115
virtual bool similar(Rule *objOtherRule)
Definition: Rule.cpp:349
int size()
Definition: EnvCell.cpp:70
double _dA
Definition: Rule.h:67
EnvCell * get(int index)
Definition: EnvCellSet.cpp:145