38 for (
int i = 0; i < 10; i++)
79 for (i = 0; i < 10; i++)
92 if (newRule->
Gene)
delete[] newRule->
Gene;
120 throw GarpException(1,
"Cannot copy rules with different types");
122 for (i = 0; i < 10; i++)
158 for(
int i=0; i<10; i++ ) {
181 bool * bGeneIsActivePtr,
int * iGeneIndexPtr,
int iActGenes)
213 Gene[i * 2 + 1] = 255;
228 char * strText =
new char[1024];
233 sprintf(strText,
"%c ", typ);
237 sprintf(strText,
"%s %3d ", strText,
Gene[i]);
240 for (i = 0; i < 10; i++)
246 if (strlen(strText) > 1024)
247 throw GarpException(82,
"String size exceeded in Rule::toString()");
268 strXML =
new char[1024];
271 sprintf(strXML,
"<Rule Type=\"%c\" Id=\"%d\" OrigGen=\"%d\">\n", this->
type(),
lId,
iOrigGen);
274 strcat(strXML,
" <Genes>");
277 sprintf(strAux,
" %3d",
Gene[i]);
278 strcat(strXML, strAux);
280 strcat(strXML,
"</Genes>\n");
283 strcat(strXML,
" <Performance>");
284 for (i = 0; i < 10; i++)
287 strcat(strXML, strAux);
290 strcat(strXML,
"</Performance>");
292 strcat(strXML,
"</Rule>");
297 if (strlen(strXML) > 1024)
298 throw GarpException(82,
"String size exceeded in Rule::toXML()");
310 {
return (
double) abs( pred - cell->
values[0] ); }
315 BYTE aux1, aux2, aux_g1, aux_g2;
326 throw GarpException(100,
"Array read out of bounds (Rule::mutate)");
338 Gene[k + 1] = aux_g2;
353 int ng_2k, ng_2k_1, bg_2k, bg_2k_1;
358 if (
Gene[0] != objOtherRule->
Gene[0])
361 for (j = 1, found =
true; (j <
iActiveGenes) && (found); j += 1)
365 if ((k * 2 + 1 >
intLength) || (k * 2 < 2))
366 throw GarpException(100,
"Array read out of bounds (Rule::similar)");
368 ng_2k_1 =
Gene[k * 2 + 1];
371 bg_2k_1 = objOtherRule->
Gene[k * 2 + 1];
372 bg_2k = objOtherRule->
Gene[k * 2];
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 ) );
389 const int MAX_UTILS = 10;
390 double Utility[MAX_UTILS];
394 double prediction, certainty, strength;
395 double pXs=0,pYs=0,pXYs=0,pYcXs=0,pXSs=0,pYcs=0;
397 for (i = 1; i < MAX_UTILS; i++)
402 n = objTrainSet->
size();
407 cell = objTrainSet->
get(rnd);
421 pXYs += (MIN(certainty, strength)) / strength;
433 Utility[4] = pXSs/no;
438 Utility[5] = pXYs/no;
443 Utility[6] = (pYcXs/no);
447 Utility[7] = ((double)no)/n;
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]));
458 Utility[0] *= Utility[5];
464 Utility[0] *= Utility[8];
478 for (i = 0; i < 10; i++)
512 bool * geneIsActivePtr,
int * geneIndexPtr,
int iActGenes)
526 if ((j * 2 + 1 >
intLength) || (j * 2 < 2))
527 throw GarpException(100,
"Array out of bounds (RangeRule::initialize)");
545 for (n = 0; n < 256; n++)
547 sum += histogram->
matrix[pred][var][n];
548 if (sum>(level * histogram->
matrix[pred][0][pred]))
558 for (n = 255; n >= 0; n--)
560 sum += histogram->
matrix[pred][var][n];
561 if (sum > (level * histogram->
matrix[pred][0][pred]))
599 if ((i * 2 + 1 >
intLength) || (i * 2 < 2))
600 throw GarpException(100,
"Array out of bounds (RangeRule::getStrength)");
638 if ((j * 2 + 1 >
intLength) || (j * 2 < 2))
639 throw GarpException(100,
"Array out of bounds (NegatedRangeRule::applyToCell)");
641 if (!((
Gene[j * 2] == 0) && (
Gene[j * 2 + 1] == 255)))
652 double strength, neg_strength;
655 neg_strength = 1 - strength;
670 bool * geneIsActivePtr,
int * geneIndexPtr,
int iActGenes)
685 if ((k * 2 + 1 >
intLength) || (k * 2 < 2))
686 throw GarpException(100,
"Array out of bounds (AtomicRule::initialize)");
688 Gene[2 * k] =
Gene[2 * k + 1] = objEnvCell->values[k];
744 bool * geneIsActivePtr,
int * geneIndexPtr,
int iActGenes)
747 double constant, coef[2];
758 regression(objEnvCellSet, j, constant, coef[0], coef[1]);
760 Gene[1] = (int) constant;
761 Gene[j * 2] = (int) coef[0];
762 Gene[j * 2 + 1] = (int) coef[1];
769 double a, b, x, y, xi, yi, xiyi, xi2, xb, xx, xxi, xxiyi, xxi2;
773 n = objEnvCellSet->
count();
776 a = b = x = y = xi = yi = xiyi = xi2 = xb = xx = xxi = xxiyi = xxi2 = 0.0;
778 for (i = 0; i < n; i++)
782 y = (double) values[pred];
783 x = (double) values[dep];
796 b = (n * xiyi - xi * yi) / (n * xi2 - (xi * xi));
797 coef1 = ((b * 255) + 128);
799 a = yi / n - b * xi / n;
801 xb = (n * xxiyi - xxi * yi) / (n * xxi2 - (xxi * xxi));
802 coef2 = ((xb * 255) + 128);
829 if ((i * 2 + 1 >
intLength) || (i * 2 < 2))
830 throw GarpException(100,
"Array read out of bounds (LogitRule::getStrength)");
834 r = (double) (Data[i] / 254.0);
836 Sum += ((double) (
Gene[i * 2] - 128)) * r;
837 Sum += ((double) (
Gene[i * 2 + 1] - 128)) * r * r;
842 prob = 1.0 / (1.0 + (double) exp(-Sum));
862 throw GarpException(100,
"Array read out of bounds (LogitRule::mutate)");
883 if (
Gene[0] != objOtherRule->
Gene[0])
886 for (k = 2, found =
true; (k <
intLength) && (found); k ++)
889 throw GarpException(100,
"Array read out of bounds (LogitRule::similar)");
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)) );
906 printf(
"<%c> ",
type());
907 for (
int i = 0; i <
intGenes * 2; i += 2)
912 printf(
"%3d %3d ",
Gene[i],
Gene[i + 1] );
Rule()
Default constructor.
virtual ~NegatedRangeRule()
virtual void fromString(char *strRule)
int intGenes
Number of genes stored by the rule.
BYTE matrix[2][MAX_ENV_LAYERS][256]
virtual void fromString(char *strRule)
static int randint(int low, int high)
virtual bool applyToCell(EnvCell *cell)
static int notBetween(double x, double y, double z)
virtual double getStrength(EnvCell *cell)=0
static int membership(BYTE rval1, BYTE rval2, BYTE value)
double testWithData(EnvCellSet *objTrainSet)
BioclimHistogram * getBioclimHistogram()
virtual char type() const
virtual double getStrength(EnvCell *cell)
virtual void initialize(EnvCellSet *objEnvCellSet, const RuleSet *objRuleSet, bool *geneIsActivePtr, int *geneIndexPtr, int iActGenes)=0
virtual char * toString()
virtual void initialize(EnvCellSet *objEnvCellSet, const RuleSet *objRuleSet, bool *geneIsActivePtr, int *geneIndexPtr, int iActGenes)
virtual void mutate(int intTemperature)
virtual void initialize(EnvCellSet *objEnvCellSet, const RuleSet *objRuleSet, bool *geneIsActivePtr, int *geneIndexPtr, int iActGenes)
virtual double getStrength(EnvCell *cell)
int regression(EnvCellSet *objEnvCellSet, int dep, double &constant, double &coef1, double &coef2)
virtual double getStrength(EnvCell *cell)
virtual double getStrength(EnvCell *cell)
BYTE * Gene
BYTE vector containing the genes (representation of the variables in a Genetic Algorithm.
virtual void mutate(int intTemperature)
virtual void initialize(EnvCellSet *objEnvCellSet, const RuleSet *objRuleSet, bool *geneIsActivePtr, int *geneIndexPtr, int iActGenes)
virtual char type() const
void RestoreRule(double *perf, unsigned char *genes, int arry_len, int *gene_index)
Restore Model.
double dblPerformance[10]
Vector for storing the performance values for the rule.
virtual ~Rule()
Default destructor.
virtual Rule * objFactory()=0
virtual void fromString(char *strRule)
void bioclimRange(EnvCellSet *objEnvCellSet, BYTE pred, double level, int var)
virtual double getCertainty(EnvCell *cell)
virtual bool applyToCell(EnvCell *cell)
virtual bool applyToCell(EnvCell *cell)
virtual void fromString(char *strRule)
virtual bool applyToCell(EnvCell *cell)
virtual bool similar(Rule *objOtherRule)
virtual double getError(BYTE pred, EnvCell *cell)
virtual void copy(Rule *fromRule)
virtual bool similar(Rule *objOtherRule)