openModeller  Version 1.5.0
RuleSet.cpp
Go to the documentation of this file.
1 
2 /* **************************************
3  * GARP Modeling Package
4  *
5  * **************************************
6  *
7  * Copyright (c), The Center for Research, University of Kansas, 2385 Irving Hill Road, Lawrence, KS 66044-4755, USA.
8  * Copyright (C), David R.B. Stockwell of Symbiotik Pty. Ltd.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the license that is distributed with the software.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * license.txt file included with this software for more details.
17  */
18 
19 // RuleSet.cpp : Implementation of CRuleSet
20 
21 #ifdef WIN32
22 // avoid warnings caused by problems in VC headers
23 #define _SCL_SECURE_NO_DEPRECATE
24 #endif
25 
26 #include "Rule.h"
27 #include "RuleSet.h"
28 #include "EnvCell.h"
29 #include "EnvCellSet.h"
30 #include "Utilities.h"
31 
32 // ==========================================================================
33 // RuleSet implementation
34 // ==========================================================================
36 {
37  // rule set starts empty (without rules)
38  intRules = 0;
39 
40  // reset rule pointers
41  for (int i = 0; i < MAX_RULES; i++)
42  objRules[i] = NULL;
43 
44  // reset area counters
46 }
47 
48 // ==========================================================================
50 {
51  clear();
52 }
53 
54 // ==========================================================================
56 {
57  for (int i = 0; i < intRules; i++)
58  if (objRules[i])
59  {
60  delete objRules[i];
61  objRules[i] = NULL;
62  }
63 
64  // rule set become empty (without rules)
65  intRules = 0;
66 
67  // reset area counters
69 }
70 
71 // ==========================================================================
72 void RuleSet::setActiveGenes(bool * bGeneIsActivePtr, int * iGeneIndexPtr, int iActiveGenesAux)
73 {
74  for (int i = 0; i < intRules; i++)
75  {
76  objRules[i]->bGeneIsActive = bGeneIsActivePtr;
77  objRules[i]->iGeneIndex = iGeneIndexPtr;
78  objRules[i]->iActiveGenes = iActiveGenesAux;
79  }
80 }
81 
82 // ==========================================================================
83 char * RuleSet::toXML(char * id)
84 {
85  /* XML Sample
86 
87  <RuleSet Id="Best">
88  <Rule>...</Rule>
89  . .
90  . .
91  . .
92  <Rule>...</Rule>
93  </RuleSet>
94  */
95 
96  int i;
97  int size;
98  char * strXML, *strXMLRule;
99 
100  size = (1024 * intRules) + 2048;
101  strXML = new char[size];
102 
103 
104  // rules
105  if (!id)
106  strcpy(strXML, "<RuleSet>\n");
107  else
108  sprintf(strXML, "<RuleSet Id=\"%s\">\n", id);
109 
110  for (i = 0; i < intRules; i++)
111  {
112  strXMLRule = objRules[i]->toXML();
113  sprintf(strXML, "%s%s\n", strXML, strXMLRule);
114  delete[] strXMLRule;
115  }
116  strcat(strXML, "</RuleSet>");
117 
118  // check string sizes
119  if (strlen(strXML) > size_t(size))
120  throw GarpException(82, "String size exceeded in RuleSet::toXML()");
121 
122  // return result
123  return strXML;
124 }
125 
126 // ==========================================================================
127 void RuleSet::saveText(char * strSaveFilename)
128 {
129  FILE * outf;
130  int i;
131 
132  if (strcmp(strSaveFilename, "") == 0)
133  throw GarpException(3, "Cannot save rule set with an empty filename");
134 
135  outf = fopen(strSaveFilename, "w");
136 
137  if (outf)
138  {
139  for (i = 0; i < intRules; i++)
140  fprintf(outf, "%s\n", objRules[i]->toString());
141 
142  fclose(outf);
143  }
144  else
145  {
146  char msg[256];
147  sprintf(msg, "Rule set file <%s> could not be opened for writing", strSaveFilename);
148  throw GarpException(4, msg);
149  }
150 }
151 
152 // ==========================================================================
153 Rule * RuleSet::get(int index)
154 { return objRules[index]; }
155 
156 // ==========================================================================
158 { return intRules; }
159 
160 // ==========================================================================
161 void RuleSet::set(int index, Rule * objRule)
162 { objRules[index] = objRule; }
163 
164 // ==========================================================================
165 void RuleSet::add(Rule * objRule)
166 {
167  if (objRule)
168  {
169  if (intRules < MAX_RULES - 1)
170  objRules[intRules++] = objRule;
171  else
172  throw GarpException(16, "Cannot add rule. Ruleset is full");
173  }
174  else
175  throw GarpException(17, "Cannot add null rule");
176 }
177 
178 // ==========================================================================
179 void RuleSet::trim(int intMaxRules)
180 {
181  int intInitialRules = intRules;
182  for (int i = intMaxRules; i < intInitialRules; i++)
183  {
184  delete objRules[i];
185  objRules[i] = NULL;
186  intRules--;
187  }
188 }
189 
190 // ==========================================================================
191 // Discard rules which performance (determined by the iPerfIndex) is less
192 // than the specified dValue
193 // ==========================================================================
194 void RuleSet::discardRules(int iPerfIndex, double dValue)
195 {
196  int i, j;
197 
198  i = 0;
199  while (i < intRules)
200  {
201  if (!GarpUtil::equalEps(objRules[i]->dblPerformance[8], objRules[i]->_dSig, 0.0001))
202  throw GarpException(1, "The actual and calculated significance values of this rule do not match.");
203 
204  if (objRules[i]->dblPerformance[iPerfIndex] < dValue)
205  {
206  // this rule has not enough performance
207  // delete rule
208  delete objRules[i];
209 
210  // and shift rules up
211  for (j = i; j < intRules - 1; j++)
212  objRules[j] = objRules[j + 1];
213 
214  // remove the duplicated reference to the last rule
215  objRules[intRules - 1] = NULL;
216 
217  // update the number of rules in this set
218  // no need to increment <i> because the rules were shifted up one position
219  intRules--;
220  }
221  else
222  {
223  // this rule passed the test
224  // go to the next one
225  i++;
226  }
227  }
228 }
229 
230 // ==========================================================================
231 void RuleSet::setEvaluation(bool value)
232 {
233  for (int i = 0; i < intRules; i++)
234  objRules[i]->blnNeedsEvaluation = value;
235 }
236 
237 // ==========================================================================
238 void RuleSet::setPad(char pad)
239 {
240  for (int i = 0; i < intRules; i++)
241  objRules[i]->chrPad = pad;
242 }
243 
244 // ==========================================================================
245 int RuleSet::countPad(char pad)
246 {
247  int sum = 0;
248 
249  for (int i = 0; i < intRules; i++)
250  if (objRules[i]->chrPad == pad)
251  sum++;
252 
253  return sum;
254 }
255 
256 // ==========================================================================
257 void RuleSet::sort(int intPerfIndex)
258 {
259  // sort the entire ruleset using the provided performance index
260  // implemented using bubble sort (argh!!) to be improved
261  Rule * temp;
262  int i, j;
263 
264  double dSum;
265 
266  dSum = 0.0;
267  for (i = 0; i < this->intRules; i++)
268  dSum += objRules[i]->dblPerformance[9];
269 
270  for (i = 0; i < intRules - 1; i++)
271  for (j = intRules - 1; j > i; j--)
272  if (objRules[j]->dblPerformance[intPerfIndex] > objRules[j - 1]->dblPerformance[intPerfIndex])
273  {
274  temp = objRules[j];
275  objRules[j] = objRules[j - 1];
276  objRules[j - 1] = temp;
277  }
278 }
279 
280 // ==========================================================================
281 void RuleSet::verify(EnvCellSet * objTestDataset, double dAccLimit)
282 {
283  EnvCell * cell;
284  int iRuleIndex, iPredictedValue, iActualValue;
285  int i, n;
286 
287  double dSum;
288 
289  dSum = 0.0;
290  for (i = 0; i < this->intRules; i++)
291  dSum += objRules[i]->dblPerformance[9];
292 
293  // init temp structures
294  resetConfMatrix(objTestDataset);
295 
296  n = objTestDataset->count();
297  for (i = 0; i < n; i++)
298  {
299  // apply rules to each cell in the dataset
300  cell = objTestDataset->get(i);
301  iRuleIndex = applyRulesToCell(cell, dAccLimit);
302 
303  if (iRuleIndex >= 0)
304  {
305  // rule applies
306  iPredictedValue = objRules[iRuleIndex]->Gene[0];
307  iActualValue = cell->values[0];
308 
309  addConfMatrix(iPredictedValue, iActualValue);
310  }
311  }
312 
313  dSum = 0.0;
314  for (i = 0; i < this->intRules; i++)
315  dSum += objRules[i]->dblPerformance[9];
316 }
317 
318 // ==========================================================================
319 void RuleSet::resetConfMatrix(EnvCellSet * objTestDataset)
320 {
321  // init temp structures
322  iConfMatrix[0][0] = 0;
323  iConfMatrix[0][1] = 0;
324  iConfMatrix[1][0] = 0;
325  iConfMatrix[1][1] = 0;
326 
327  iTotalPoints = objTestDataset->count();
328 }
329 // ==========================================================================
330 int RuleSet::getConfMatrix(int iPredictedValue, int iActualValue)
331 {
332  if ((iActualValue < 0) || (iActualValue > 1) || (iPredictedValue < 0) || (iPredictedValue > 1))
333  throw GarpException(1, "Confusion matrix index out of range");
334 
335  return iConfMatrix[iActualValue][iPredictedValue];
336 }
337 
338 // ==========================================================================
339 void RuleSet::addConfMatrix(int iPredictedValue, int iActualValue)
340 {
341  if ((iActualValue < 0) || (iActualValue > 1) || (iPredictedValue < 0) || (iPredictedValue > 1))
342  throw GarpException(1, "Confusion matrix index out of range");
343 
344  iConfMatrix[iActualValue][iPredictedValue]++;
345 }
346 
347 // ==========================================================================
349 {
350  return iTotalPoints;
351 }
352 // ==========================================================================
354 {
355  return (iConfMatrix[0][0] + iConfMatrix[0][1] + iConfMatrix[1][0] + iConfMatrix[1][1]);
356 }
357 
358 // ==========================================================================
360 {
361  return getTotalPoints() - getPredictedPoints();
362 }
363 
364 // ==========================================================================
366 {
367  return (double) (iConfMatrix[0][0] + iConfMatrix[1][1]) / (double) getPredictedPoints();
368 }
369 // ==========================================================================
371 {
372  return (double) (iConfMatrix[0][0] + iConfMatrix[1][1]) / (double) getTotalPoints();
373 }
374 
375 // ==========================================================================
377 {
378  throw GarpException(131, "Method RuleSet::updateRuleUsage not implemented yet");
379 }
380 
381 // ==========================================================================
382 double RuleSet::getOveralPerformance(int iPerfIndex, int iFirstRulesToBeIncluded)
383 {
384  int i;
385  double dResult, dWeight, dWSum;
386 
387  dWSum = 0.0;
388  dResult = 0.0;
389  dWeight = (double) iFirstRulesToBeIncluded;
390 
391  for (i = 0; i < iFirstRulesToBeIncluded; i++)
392  {
393  if (i < intRules)
394  {
395  dResult += objRules[i]->dblPerformance[iPerfIndex] * dWeight;
396  dWSum += dWeight;
397  dWeight--;
398 
399  //dResult += objRules[i]->dblPerformance[iPerfIndex];
400  }
401  else
402  break;
403  }
404 
405  dResult /= dWSum;
406 
407  return dResult;
408 }
409 
410 // ==========================================================================
411 Scalar RuleSet::getValue(const Sample& sample) const
412 {
413  // convert values to EnvCell
414  BYTE bytes[256];
415  EnvCell cell(_dim + 2, bytes);
416 
417  // first element of bytes is reserved for presence/absence value
418  for (int i = 1; i < _dim; i++)
419  {
420  // Guard against values outside the normalization range
421  // due to reprojection to a non-native range
422  Scalar value = sample[i - 1];
423  if (value > 253.0) value = 253.0;
424  if (value < 1.0) value = 1.0;
425 
426  bytes[i] = (BYTE) value;
427  }
428 
429  int ruleIndex = applyRulesToCell(&cell, 0.0);
430  //printf(" Idx=%+2d Pred=%+4.1f\n", ruleIndex, (ruleIndex >= 0)? (Scalar) (objRules[ruleIndex]->Gene[0]) : -1.0);
431 
432  if (ruleIndex >= 0)
433  return (Scalar) (objRules[ruleIndex]->Gene[0]);
434 
435  return 0.0;
436 }
437 
438 // ==========================================================================
440 {
441  for ( int i = 0; i < intRules; i++ )
442  {
443  printf( "%2d] ", i );
444  objRules[i]->log();
445  }
446 }
447 
448 /*
449 // ==========================================================================
450 void RuleSet::predict(EnvLayer * objPredictionLayer, EnvLayerSet * objPredictionArea, double Accuracylimit)
451 {
452 int row, col, pos;
453 int intRows, intColumns, intSize;
454 int ruleIndex;
455 bool status;
456 BYTE bytPred;
457 EnvCell cell;
458 BYTE values[MAX_ENV_LAYERS];
459 
460 intRows = objPredictionArea->rows();
461 intColumns = objPredictionArea->columns();
462 
463 // number of values in each cell (including species and mask values (+2)
464 intSize = objPredictionArea->size() + 2;
465 
466 // reset position counter
467 pos = 0;
468 
469 // set cell parameters
470 cell.setSize(intSize);
471 cell.setValues(values);
472 
473 // reset area counters
474 iTotalArea = iPresenceArea = iAbsenceArea = iNonPredictedArea = 0;
475 
476 // new prediction algorithm
477 // for each cell do
478 // visit each row
479 for (row = 0; row < intRows; row++)
480 {
481 // visit each column within the current row
482 for (col = 0; col < intColumns; col++)
483 {
484 // get current position
485 //pos = (row * intColumns) + col;
486 
487 // get current cell value
488 status = objPredictionArea->getValue(pos, &cell);
489 
490 // check if cell is not masked
491 if (status)
492 {
493 // NOTE: the element Item[0] should never be used in prediction!!!
494 // we don't need the datapoints to project the model back to the env. layers
495 
496 // cell is not masked: add one cell to the total area counter
497 iTotalArea++;
498 
499 // check which (if any) rule apply to this cell
500 ruleIndex = applyRulesToCell(&cell, Accuracylimit);
501 
502 // if there is a rule that applies to this cell, write the rule value to file
503 if (ruleIndex >= 0)
504 {
505 // rule <ruleIndex> applies: write its value to output layer
506 bytPred = (BYTE) objRules[ruleIndex]->Gene[0];
507 objPredictionLayer->set(pos, bytPred);
508 
509 // increment area counter (depending on the prediction)
510 if (bytPred == PRESENCE)
511 iPresenceArea++;
512 else if (bytPred == ABSENCE)
513 iAbsenceArea++;
514 }
515 
516 else
517 {
518 // no rule applies: write NODATA
519 objPredictionLayer->set(pos, (BYTE) MISSING_VALUE);
520 iNonPredictedArea++;
521 }
522 
523 }
524 else
525 {
526  // if it is masked then write NODATA to cell
527  objPredictionLayer->set(pos, (BYTE) MASK_VALUE);
528 }
529 
530 // visit next cell
531 pos++;
532 }
533 
534 
535 //GarpProgressEvents::fireProcessEvent((int) (row * 100 / intRows), 0, 0);
536 }
537 
538 //GarpProgressEvents::fireProcessEvent(100, 0, 0);
539 }
540 
541 // ==========================================================================
542 void RuleSet::predictRuleCoverage(EnvLayer * objPredictionLayer, EnvLayerSet * objPredictionArea, double Accuracylimit)
543 {
544  int row, col, pos;
545  int intRows, intColumns, intSize;
546  int ruleIndex;
547  bool status;
548  EnvCell cell;
549  BYTE values[MAX_ENV_LAYERS];
550 
551  intRows = objPredictionArea->rows();
552  intColumns = objPredictionArea->columns();
553 
554  // number of values in each cell (including species and mask values (+2)
555  intSize = objPredictionArea->size() + 2;
556 
557  // reset position counter
558  pos = 0;
559 
560  // set cell parameters
561  cell.setSize(intSize);
562  cell.setValues(values);
563 
564  // new prediction algorithm
565  // for each cell do
566  // visit each row
567  for (row = 0; row < intRows; row++)
568  {
569  // visit each column within the current row
570  for (col = 0; col < intColumns; col++)
571  {
572  // get current position
573  //pos = (row * intColumns) + col;
574 
575  // get current cell value
576  status = objPredictionArea->getValue(pos, &cell);
577 
578  // check if cell is not masked
579  if (status)
580  {
581  // NOTE: the element Item[0] should never be used in prediction!!!
582  // we don't need the datapoints to project the model back to the env. layers
583 
584  // check which (if any) rule apply to this cell
585  ruleIndex = applyRulesToCell(&cell, Accuracylimit);
586 
587  // if there is a rule that applies to this cell, write the rule value to file
588  if (ruleIndex >= 0)
589  {
590  // rule <ruleIndex> applies: write its value to output layer
591  //objPredictionLayer->set(pos, (BYTE) objRules[ruleIndex]->Gene[0]);
592  objPredictionLayer->set(pos, (BYTE) ruleIndex + 1);
593  }
594 
595  else
596  {
597  // no rule applies: write NODATA
598  objPredictionLayer->set(pos, (BYTE) MISSING_VALUE);
599  }
600 
601  }
602  else
603  {
604  // if it is masked then write NODATA to cell
605  objPredictionLayer->set(pos, (BYTE) MASK_VALUE);
606  }
607 
608  // visit next cell
609  pos++;
610  }
611 
612 
613  //GarpProgressEvents::fireProcessEvent((int) (row * 100 / intRows), 0, 0);
614  }
615 
616  //GarpProgressEvents::fireProcessEvent(100, 0, 0);
617 }
618 
619 */
620 
621 // ==========================================================================
622 int RuleSet::applyRulesToCell(EnvCell * cell, double Accuracylimit) const
623 {
624  int i, maxi, ruleIndex;
625  double u, max;
626  int b_membership[256];
627 
628  for (i = 0; i < 256; i++)
629  b_membership[i] = 0;
630 
631  max = maxi = 0;
632 
633  ruleIndex = -1;
634 
635  // check every rule
636  i = 0;
637  while ((i < intRules) && (ruleIndex == -1))
638  {
639  // get the 5th performance value
640  u = (int)(255.0 * objRules[i]->dblPerformance[5]);
641 
642  if (objRules[i]->dblPerformance[5] >= Accuracylimit)
643  {
644  if (objRules[i]->applyToCell(cell))
645  {
646  ruleIndex = i;
647 
648  // update value for
649  if (objRules[i]->intScreen)
650  {
651  if (b_membership[objRules[i]->Gene[0]] < u)
652  b_membership[objRules[i]->Gene[0]] = (int) u;
653 
654  if (max < u)
655  {
656  max = u;
657  maxi = i;
658  }
659  }
660  }
661  }
662 
663  // check next rule
664  i++;
665  }
666 
667  if (max)
668  objRules[maxi]->dblPerformance[9]++;
669 
670  return ruleIndex;
671 }
672 
673 /*
674 // ==========================================================================
675 bool RuleSet::projectRuleSet(EnvLayerSet * oToLayerSet, bool bConservative, RuleSet * oToRuleSet)
676 {
677 // check if rule set is not null
678 if ((intRules <= 0) || (objModelLayerSet->size() == 0) || (oToLayerSet->size() == 0) ||
679 (objModelLayerSet->size() != oToLayerSet->size()))
680 return false;
681 
682 // this code does not work right, I guess. See the other implementation below it
683 int i, j, iLayers;
684 int iNatMin[MAX_ENV_LAYERS];
685 int iNatMax[MAX_ENV_LAYERS];
686 int iProjMin[MAX_ENV_LAYERS];
687 int iProjMax[MAX_ENV_LAYERS];
688 double dFactor[MAX_ENV_LAYERS];
689 
690 EnvLayer * oNat, *oProj;
691 
692 // find out how many genes a rule have
693 iLayers = objModelLayerSet->size();
694 
695 // now calculate the factor, min and max values for each gene
696 // those values will be used to modify the gene values in the
697 // result dataset
698 for (i = 0; i < iLayers; i++)
699 {
700 // get one layer from native range
701 // and other from projection area
702 oNat = objModelLayerSet->getLayerByIndex(i);
703 oProj = oToLayerSet->getLayerByIndex(i);
704 
705 // get projection values for each gene
706 // min values
707 if (oNat->dblMinValue > oProj->dblMinValue)
708 {
709 iNatMin[i] = MIN_SCALED_VALUE;
710 iProjMin[i] = oProj->scale(oNat->dblMinValue);
711 }
712 else
713 {
714 iNatMin[i] = oNat->scale(oProj->dblMinValue);
715 iProjMin[i] = MIN_SCALED_VALUE;
716 }
717 
718 // now max values
719 if (oNat->dblMaxValue > oProj->dblMaxValue)
720 {
721 iNatMax[i] = MAX_SCALED_VALUE;
722 iProjMax[i] = oProj->scale(oNat->dblMaxValue);
723 }
724 else
725 {
726 iNatMax[i] = oNat->scale(oProj->dblMaxValue);
727 iProjMax[i] = MAX_SCALED_VALUE;
728 }
729 
730 dFactor[i] = (((double) iProjMax[i] - (double) iProjMin[i]) /
731 ((double) iNatMax[i] - (double) iNatMin[i]));
732 }
733 
734 // first clear the rule set
735 oToRuleSet->trim(0);
736 
737 // copy the rules to the result rule set and modify the rules
738 for (j = 0; j < intRules; j++)
739 {
740 Rule * oAuxRule;
741 
742 // get a clone of the ith rule in the current
743 oAuxRule = objRules[j]->clone();
744 
745 // modify the gene values
746 // projecting the values from the Native Dataset to the projection area
747 // jump first gene (rule value for the species -
748 // - presence or absence - cannot project these values)
749 for (i = 0; i < iLayers; i++)
750 {
751 int iOldValue1, iOldValue2;
752 int iNewValue1, iNewValue2;
753 
754 iOldValue1 = (int) oAuxRule->Gene[(i + 1) * 2];
755 iOldValue2 = (int) oAuxRule->Gene[(i + 1) * 2 + 1];
756 
757 iNewValue1 = (int) ((iOldValue1 - iNatMin[i]) * dFactor[i]) + iProjMin[i];
758 iNewValue2 = (int) ((iOldValue2 - iNatMin[i]) * dFactor[i]) + iProjMin[i];
759 
760 // correct the values to avoid values out of range
761 if (iNewValue1 < 0)
762  iNewValue1 = 0;
763 else if (iNewValue1 > 255)
764  iNewValue1 = 255;
765 
766 if (iNewValue2 < 0)
767  iNewValue2 = 0;
768 else if (iNewValue2 > 255)
769  iNewValue2 = 255;
770 
771  // decide if it is an conservative or liberal projection
772  // In conservative projections, if the value of a gene
773  // is in one of the bounds of the native range layer value,
774  // this value is mapped to the correspondent value to the
775  // projection area. In liberal projections, a value in one
776  // of the bounds of the layer values means that the rule
777  // applies for all values above (or below) that gene value
778 
779 if (iOldValue1 == 0 && !bConservative)
780  iNewValue1 = 0;
781 
782 if (iOldValue2 == 0 && !bConservative)
783  iNewValue2 = 0;
784 
785  oAuxRule->Gene[(i + 1) * 2] = (BYTE) iNewValue1;
786  oAuxRule->Gene[(i + 1) * 2 + 1] = (BYTE) iNewValue2;
787  }
788 
789 // add rule to the rule set
790 oToRuleSet->add(oAuxRule);
791 }
792 
793 //
794 
795 int i, j, iLayers;
796 int iOldValue1, iOldValue2;
797 int iNewValue1, iNewValue2;
798 EnvLayer * oNat, *oProj;
799 Rule * oAuxRule;
800 
801 // find out how many genes a rule have
802 iLayers = objModelLayerSet->size();
803 
804 // first clear the rule set
805 oToRuleSet->trim(0);
806 
807 // copy the rules to the result rule set and modify the rules
808 for (j = 0; j < intRules; j++)
809 {
810  // get a clone of the ith rule in the current
811  oAuxRule = objRules[j]->clone();
812 
813  // logit rules cannot be projected using this approach
814  // so, for now, let's just copy them and hope for the best
815  // that means, for logit rules, projection will disconsider the difference in the range of values
816  if (oAuxRule->type() != 'r')
817  {
818  // modify the gene values
819  // projecting the values from the Native Dataset to the projection area
820  // jump first gene (rule value for the species -
821  // - presence or absence - cannot project these values)
822  for (i = 0; i < iLayers; i++)
823  {
824  oNat = objModelLayerSet->getLayerByIndex(i);
825  oProj = oToLayerSet->getLayerByIndex(i);
826 
827  // assertion
828  //if ((oAuxRule->Gene[(i + 1) * 2] != objRules[j]->Gene[(i + 1) * 2]) ||
829  // (oAuxRule->Gene[(i + 1) * 2 + 1] != objRules[j]->Gene[(i + 1) * 2 + 1]))
830  // return false;
831  //
832 
833  iOldValue1 = (int) oAuxRule->Gene[(i + 1) * 2];
834  iOldValue2 = (int) oAuxRule->Gene[(i + 1) * 2 + 1];
835 
836  iNewValue1 = (int) oProj->scale(oNat->unscale(iOldValue1) + 0.000000000001);
837  iNewValue2 = (int) oProj->scale(oNat->unscale(iOldValue2) + 0.000000000001);
838 
839  // correct the values to avoid values out of range
840  if (iNewValue1 < 0)
841  iNewValue1 = 1;
842  else if (iNewValue1 > 255)
843  iNewValue1 = 254;
844 
845  if (iNewValue2 < 0)
846  iNewValue2 = 1;
847  else if (iNewValue2 > 255)
848  iNewValue2 = 254;
849 
850  // decide if it is an conservative or liberal projection
851  // In conservative projections, if the value of a gene
852  // is in one of the bounds of the native range layer value,
853  // this value is mapped to the correspondent value to the
854  // projection area. In liberal projections, a value in one
855  // of the bounds of the layer values means that the rule
856  // applies for all values above (or below) that gene value
857 
858  if (iOldValue1 == 0 && !bConservative)
859  iNewValue1 = 0;
860 
861  if (iOldValue2 == 0 && !bConservative)
862  iNewValue2 = 0;
863 
864  if (iOldValue1 == 255 && !bConservative)
865  iNewValue1 = 255;
866 
867  if (iOldValue2 == 255 && !bConservative)
868  iNewValue2 = 255;
869 
870  oAuxRule->Gene[(i + 1) * 2] = (BYTE) iNewValue1;
871  oAuxRule->Gene[(i + 1) * 2 + 1] = (BYTE) iNewValue2;
872  }
873  } // end if oAuxRule->type() != 'r'
874 
875  // add rule to the rule set
876  oToRuleSet->add(oAuxRule);
877 }
878 
879 oToRuleSet->objModelLayerSet = oToLayerSet;
880 
881 return true;
882 }
883 */
884 // ==========================================================================
885 
886 // ==============================================================
887 
888 // gather rule set statistics
890 {
891  char type='0'; //initialise to something invalid
892 
893  printf("%4d]", gen);
894 
895  for (int i = 0; i < 3; i++)
896  {
897  switch (i)
898  {
899  case 0: type ='d'; break;
900  case 1: type ='!'; break;
901  case 2: type ='r'; break;
902  //case 3: type ='a'; break;
903  }
904 
905  double max = -10000;
906  double sum = 0;
907  int ct = 0;
908  int pres = 0;
909  for (int j = 0; j < intRules; j++)
910  {
911  Rule * rule = objRules[j];
912  if (rule->type() == type)
913  {
914  ct++;
915  sum += rule->dblPerformance[0];
916  pres += (int) rule->Gene[0];
917  if (max < rule->dblPerformance[0])
918  max = rule->dblPerformance[0];
919  }
920  }
921 
922  if (max == -10000)
923  max = 0;
924 
925  printf("%c %2d %+7.2f %+7.2f %2d|", type, ct, max, sum / ct, pres);
926  }
927 
928  printf("\n");
929 }
int countPad(char pad)
Definition: RuleSet.cpp:245
void log()
Definition: RuleSet.cpp:439
void sort(int intPerfIndex)
Definition: RuleSet.cpp:257
char * toXML(char *id)
Definition: RuleSet.cpp:83
BYTE * values
Definition: EnvCell.h:32
int iTotalPoints
Definition: RuleSet.h:48
bool * bGeneIsActive
Definition: Rule.h:71
double Scalar
Type of map values.
Definition: om_defs.hh:39
int size()
Definition: RuleSet.cpp:157
void saveText(char *strFilename)
Definition: RuleSet.cpp:127
void setPad(char pad)
Definition: RuleSet.cpp:238
Scalar getValue(const Sample &sample) const
Definition: RuleSet.cpp:411
static bool equalEps(double x, double y, double eps)
Definition: Utilities.h:200
int iAbsenceArea
Definition: RuleSet.h:53
void verify(EnvCellSet *objTestDataset, double dAccLimit)
Definition: RuleSet.cpp:281
int iActiveGenes
Definition: Rule.h:73
unsigned char BYTE
Definition: Utilities.h:36
virtual char * toXML()
Definition: Rule.cpp:254
void setEvaluation(bool value)
Definition: RuleSet.cpp:231
void resetConfMatrix(EnvCellSet *objTestDataset)
Definition: RuleSet.cpp:319
double getOveralPerformance(int iPerfIndex, int iFirstRulesToBeIncluded)
Definition: RuleSet.cpp:382
int iNonPredictedArea
Definition: RuleSet.h:54
int applyRulesToCell(EnvCell *cell, double Accuracylimit) const
Definition: RuleSet.cpp:622
void gatherRuleSetStats(int gen)
Definition: RuleSet.cpp:889
void setActiveGenes(bool *bGeneIsActivePtr, int *iGeneIndexPtr, int iActiveGenesAux)
Definition: RuleSet.cpp:72
int iTotalArea
Definition: RuleSet.h:51
int getPredictedPoints()
Definition: RuleSet.cpp:353
RuleSet()
Definition: RuleSet.cpp:35
const int MAX_RULES
Definition: Utilities.h:55
Rule * get(int index)
Definition: RuleSet.cpp:153
void addConfMatrix(int iPredictedValue, int iActualValue)
Definition: RuleSet.cpp:339
Definition: Rule.h:37
int * iGeneIndex
Definition: Rule.h:72
BYTE * Gene
BYTE vector containing the genes (representation of the variables in a Genetic Algorithm.
Definition: Rule.h:46
virtual char type() const
Definition: Rule.h:88
int iConfMatrix[2][2]
Definition: RuleSet.h:47
double getOverallAccuracy()
Definition: RuleSet.cpp:370
int getConfMatrix(int iPredictedValue, int iActualValue)
Definition: RuleSet.cpp:330
int iPresenceArea
Definition: RuleSet.h:52
double dblPerformance[10]
Vector for storing the performance values for the rule.
Definition: Rule.h:51
void clear()
Definition: RuleSet.cpp:55
void discardRules(int iPerfIndex, double dValue)
Definition: RuleSet.cpp:194
int getTotalPoints()
Definition: RuleSet.cpp:348
int intRules
Definition: RuleSet.h:42
int getUnpredictedPoints()
Definition: RuleSet.cpp:359
Rule * objRules[MAX_RULES]
Definition: RuleSet.h:41
void add(Rule *objRule)
Definition: RuleSet.cpp:165
int _dim
Definition: RuleSet.h:44
void trim(int intMaxRules)
Definition: RuleSet.cpp:179
void updateRuleUsage()
Definition: RuleSet.cpp:376
virtual void log()
Definition: Rule.cpp:904
virtual ~RuleSet()
Definition: RuleSet.cpp:49
void set(int index, Rule *objRule)
Definition: RuleSet.cpp:161
double getAccuracy()
Definition: RuleSet.cpp:365
EnvCell * get(int index)
Definition: EnvCellSet.cpp:145
Definition: Sample.hh:25