/** It applies the migration stage */ void Migration() { int i, j, num; double u; /* First, the individual in the population are ordered according to their fitness */ Collections.sort(Poblacion); /* The second half (the worst one) is radomly initialized again */ for (j = (tamPoblacion / 2); j < tamPoblacion; j++) { /* First, the antecedent */ for (i = 0; i < entradas; i++) { Poblacion.get(j).antecedente[i].m = Randomize.RanddoubleClosed(train.getMin(i), train.getMax(i)); Poblacion.get(j).antecedente[i].sigma = Randomize.RandintClosed(1, 4); } /* Secondly, the consequent */ do { num = 0; for (i = 0; i < entradas; i++) { u = Randomize.RandClosed(); /* The term is used in the consequent */ if (u < 0.5) { Poblacion.get(j).consecuente[i] = Randomize.RanddoubleClosed(-1.0, 1.0); // Poblacion.get(j).consecuente[entradas] = // Randomize.RanddoubleClosed(-45.0, 45.0); if (Poblacion.get(j).consecuente[i] != 0.0) { num++; } } /* The term is NOT used in the consequent */ else { Poblacion.get(j).consecuente[i] = 0.0; } } u = Randomize.RandClosed(); /* The term is used in the consequent */ if (u < 0.5) { Poblacion.get(j).consecuente[entradas] = Randomize.RanddoubleClosed( -1.0 * ((train.getMax(entradas) - train.getMin(entradas)) / 2.0), ((train.getMax(entradas) - train.getMin(entradas)) / 2.0)); // Poblacion.get(j).consecuente[entradas] = // Randomize.RanddoubleClosed(-45.0, 45.0); // Poblacion.get(j).consecuente[entradas] = // Randomize.RanddoubleClosed(train.getMin(entradas), train.getMax(entradas)); if (Poblacion.get(j).consecuente[entradas] != 0.0) { num++; } } /* The term is NOT used in the consequent */ else { Poblacion.get(j).consecuente[entradas] = 0.0; } } while (num == 0); } }
/** It initializes each individual in the population */ void initializePopulation() { int i, j, num; double u; for (j = 0; j < tamPoblacion; j++) { /* First, the antecedent */ for (i = 0; i < entradas; i++) { Poblacion.get(j).antecedente[i].m = Randomize.RanddoubleClosed(train.getMin(i), train.getMax(i)); Poblacion.get(j).antecedente[i].sigma = Randomize.RandintClosed(1, 4); } /* Secondly, the consequent */ do { num = 0; for (i = 0; i < entradas; i++) { u = Randomize.RandClosed(); /* The term is used in the consequent */ if (u < 0.5) { Poblacion.get(j).consecuente[i] = Randomize.RanddoubleClosed(-1.0, 1.0); // Poblacion.get(j).consecuente[i] = // Randomize.RanddoubleClosed(train.getMin(i) - 45.0, train.getMax(i) + 45.0); // Poblacion.get(j).consecuente[i] = // Randomize.RanddoubleClosed(-45.0, 45.0); if (Poblacion.get(j).consecuente[i] != 0.0) { num++; } } /* The term is NOT used in the consequent */ else { Poblacion.get(j).consecuente[i] = 0.0; } } u = Randomize.RandClosed(); /* The term is used in the consequent */ if (u < 0.5) { // Poblacion.get(j).consecuente[entradas] = Randomize.RanddoubleClosed( // - 45.0, 45.0); Poblacion.get(j).consecuente[entradas] = Randomize.RanddoubleClosed( -1.0 * ((train.getMax(entradas) - train.getMin(entradas)) / 2.0), ((train.getMax(entradas) - train.getMin(entradas)) / 2.0)); if (Poblacion.get(j).consecuente[entradas] != 0.0) { num++; } } /* The term is NOT used in the consequent */ else { Poblacion.get(j).consecuente[entradas] = 0.0; } } while (num == 0); } }
/** * It calculate the output of the fuzzy system for a given example * * @param ejemplo double [] A given example * @return double The output value obtained as output of the fuzzy system for a given example */ double Output_fuzzy_system(double[] ejemplo) { int i; double result, suma1, suma2, omega, y; suma1 = suma2 = 0.0; for (i = 0; i < Nr; i++) { omega = Matching_degree(SistemaDifuso.get(i), ejemplo); y = Output_value(SistemaDifuso.get(i), ejemplo); suma1 += (omega * y); suma2 += omega; } if (suma2 != 0.0) { result = suma1 / suma2; } else { // result = 0.0; result = ((train.getMax(entradas) - train.getMin(entradas)) / 2.0); } return (result); }
/** * It calculate the matching degree between the antecedent of the rule and a given example * * @param indiv Individual The individual representing a fuzzy rule * @param ejemplo double [] A given example * @return double The matching degree between the example and the antecedent of the rule */ double Matching_degree(Individual indiv, double[] ejemplo) { int i, sig; double result, suma, numerador, denominador, sigma, ancho_intervalo; suma = 0.0; for (i = 0; i < entradas; i++) { ancho_intervalo = train.getMax(i) - train.getMin(i); sigma = -1.0; sig = indiv.antecedente[i].sigma; switch (sig) { case 1: sigma = 0.3; break; case 2: sigma = 0.4; break; case 3: sigma = 0.5; break; case 4: sigma = 0.6; break; } sigma *= ancho_intervalo; numerador = Math.pow((ejemplo[i] - indiv.antecedente[i].m), 2.0); denominador = Math.pow(sigma, 2.0); suma += (numerador / denominador); } suma *= -1.0; result = Math.exp(suma); return (result); }
/** * It prints the current population as a String * * @return String The current population as a String */ public String Print_Population() { int i, j, sig; double sigma, ancho_intervalo; boolean anterior_nulo; String output = new String(""); output += "Rule Base with " + Nr + " rules\n\n"; for (i = 0; i < Nr; i++) { output += "Rule " + (i + 1) + ": IF "; for (j = 0; j < entradas; j++) { ancho_intervalo = train.getMax(j) - train.getMin(j); sigma = -1.0; sig = BestSistemaDifuso.get(i).antecedente[j].sigma; switch (sig) { case 1: sigma = 0.3; break; case 2: sigma = 0.4; break; case 3: sigma = 0.5; break; case 4: sigma = 0.6; break; } sigma *= ancho_intervalo; output += "X(" + (j + 1) + ") is Gaussian(" + BestSistemaDifuso.get(i).antecedente[j].m + ", " + sigma + ")"; if (j != (entradas - 1)) { output += " and "; } } output += " THEN Y = "; anterior_nulo = true; if (BestSistemaDifuso.get(i).consecuente[entradas] != 0.0) { anterior_nulo = false; output += "(" + BestSistemaDifuso.get(i).consecuente[entradas] + ")"; } for (j = 0; j < entradas; j++) { if (BestSistemaDifuso.get(i).consecuente[j] != 0.0) { if (anterior_nulo == false) { output += " + "; } anterior_nulo = false; output += "(" + BestSistemaDifuso.get(i).consecuente[j] + " * X(" + (j + 1) + "))"; } } output += "\n\n"; } return (output); }
/** It applies mutation genetic operator */ void Mutation() { int i, j, aux1, num; double u, u2; for (j = 0; j < tamPoblacion; j++) { /* First, the antecedent */ for (i = 0; i < entradas; i++) { u = Randomize.RandClosed(); if (u < probMut) { Poblacion.get(j).antecedente[i].m = Randomize.RanddoubleClosed(train.getMin(i), train.getMax(i)); } u = Randomize.RandClosed(); if (u < probMut) { aux1 = Poblacion.get(j).antecedente[i].sigma; do { Poblacion.get(j).antecedente[i].sigma = Randomize.RandintClosed(1, 4); } while (aux1 == Poblacion.get(j).antecedente[i].sigma); } } /* Secondly, the consequent */ num = 0; for (i = 0; i <= entradas; i++) { if (Poblacion.get(j).consecuente[i] != 0.0) { num++; } } for (i = 0; i < entradas; i++) { u = Randomize.RandClosed(); if (u < probMut) { u2 = Randomize.RandClosed(); if (u2 < 0.5) { Poblacion.get(j).consecuente[i] = Randomize.RanddoubleClosed(-1.0, 1.0); } /* The term is NOT used in the consequent */ else { if (num != 1) { Poblacion.get(j).consecuente[i] = 0.0; num--; } } } } u = Randomize.RandClosed(); if (u < probMut) { u2 = Randomize.RandClosed(); if (u2 < 0.5) { Poblacion.get(j).consecuente[entradas] = Randomize.RanddoubleClosed( -1.0 * ((train.getMax(entradas) - train.getMin(entradas)) / 2.0), ((train.getMax(entradas) - train.getMin(entradas)) / 2.0)); // Poblacion.get(j).consecuente[entradas] = // Randomize.RanddoubleClosed(-45.0, 45.0); } /* The term is NOT used in the consequent */ else { if (num != 1) { Poblacion.get(j).consecuente[entradas] = 0.0; num--; } } } } }