/** * Fisher distribution * * @param f Fisher value * @param n1 N1 value * @param n2 N2 value * @return P-value associated */ private static double FishF(double f, int n1, int n2) { double x = n2 / (n1 * f + n2); if ((n1 % 2) == 0) { return StatCom(1 - x, n2, n1 + n2 - 4, n2 - 2) * Math.pow(x, n2 / 2.0); } if ((n2 % 2) == 0) { return 1 - StatCom(x, n1, n1 + n2 - 4, n1 - 2) * Math.pow(1 - x, n1 / 2.0); } double th = Math.atan(Math.sqrt(n1 * f / (1.0 * n2))); double a = th / (Math.PI / 2.0); double sth = Math.sin(th); double cth = Math.cos(th); if (n2 > 1) { a = a + sth * cth * StatCom(cth * cth, 2, n2 - 3, -1) / (Math.PI / 2.0); } if (n1 == 1) { return 1 - a; } double c = 4 * StatCom(sth * sth, n2 + 1, n1 + n2 - 4, n2 - 2) * sth * Math.pow(cth, n2) / Math.PI; if (n2 == 1) { return 1 - a + c / 2.0; } int k = 2; while (k <= (n2 - 1) / 2.0) { c = c * k / (k - .5); k = k + 1; } return 1 - a + c; }
/** * Claculates the upper bound for the Chi-Squared value of a rule. * * @param suppAnte the support for the antecedent of a rule. * @param suppCons the support for the consequent of a rule. * @return the Chi-Squared upper bound. */ private double calcChiSquaredUpperBound(double suppAnte, double suppCons) { double term; // Test support for antecedent and confidence and choose minimum if (suppAnte < suppCons) term = Math.pow(suppAnte - ((suppAnte * suppCons) / numRecords), 2.0); else term = Math.pow(suppCons - ((suppAnte * suppCons) / numRecords), 2.0); // Determine e double eVlaue = calcWCSeValue(suppAnte, suppCons); // Rerturn upper bound return (term * eVlaue * numRecords); }
/** * It converts double to char by the gray's code * * @param ds the vector which is goint to change * @param length the size of the vector * @return the changed vector */ private char[] StringRep(double[] ds, int length) { int i; double n; int pos; double INCREMENTO; char[] Cad_sal; Cad_sal = new char[Genes * BITS_GEN + 1]; if (flag == 1) { tmpstring = new char[Genes * BITS_GEN]; flag = 0; } pos = 0; for (i = 0; i < length; i++) { INCREMENTO = (Gene[i].max() - Gene[i].min()) / (Math.pow(2.0, (double) BITS_GEN) - 1.0); n = (((ds[i] - Gene[i].min()) / INCREMENTO) + 0.5); tmpstring = F.Itoc((int) n, BITS_GEN); F.Gray(tmpstring, Cad_sal, BITS_GEN, pos); pos += BITS_GEN; } return Cad_sal; }
/** * Calculates the Chi squared values and returns their sum. * * @return the sum of the Chi Squared values. */ private double calcChiSquaredValue() { double sumChiSquaredValues = 0.0; for (int index = 0; index < obsValues.length; index++) { double chiValue = Math.pow((obsValues[index] - expValues[index]), 2.0) / expValues[index]; sumChiSquaredValues = sumChiSquaredValues + chiValue; } // Return return (sumChiSquaredValues); }
/** * 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 Evaluates the performance of the fuzzy system. The Mean Square Error (MSE) by training is * used */ public double Evaluate_fuzzy_system() { int i; double result, suma, fuerza; suma = 0.0; for (i = 0; i < train.getnData(); i++) { fuerza = Output_fuzzy_system(train.getExample(i)); suma += Math.pow(train.getOutputAsReal(i) - fuerza, 2.0); } result = suma / train.getnData(); /* We want to have a maximization problem so, we invert the error */ if (result != 0.0) { result = 1.0 / result; } return (result); }
/** * It Evaluates the performance of the best evolved fuzzy system on test data. The Mean Square * Error (MSE) is used * * @return double The MSE error in test data */ public double Evaluate_best_fuzzy_system_in_test() { int i; double result, suma, fuerza; SistemaDifuso.clear(); for (i = 0; i < Nr; i++) { Individual indi = new Individual(BestSistemaDifuso.get(i)); SistemaDifuso.add(indi); } suma = 0.0; for (i = 0; i < test.getnData(); i++) { fuerza = Output_fuzzy_system(test.getExample(i)); suma += Math.pow(test.getOutputAsReal(i) - fuerza, 2.0); } result = suma / test.getnData(); return (result); }
/** * Obtain all exhaustive comparisons possible from an array of indexes * * @param indices A verctos of indexes. * @return A vector with vectors containing all the possible relations between the indexes */ @SuppressWarnings("unchecked") public static Vector<Vector<Relation>> obtainExhaustive(Vector<Integer> indices) { Vector<Vector<Relation>> result = new Vector<Vector<Relation>>(); int i, j, k; String binario; boolean[] number = new boolean[indices.size()]; Vector<Integer> ind1, ind2; Vector<Relation> set = new Vector<Relation>(); Vector<Vector<Relation>> res1, res2; Vector<Relation> temp; Vector<Relation> temp2; Vector<Relation> temp3; ind1 = new Vector<Integer>(); ind2 = new Vector<Integer>(); temp = new Vector<Relation>(); temp2 = new Vector<Relation>(); temp3 = new Vector<Relation>(); for (i = 0; i < indices.size(); i++) { for (j = i + 1; j < indices.size(); j++) { set.addElement( new Relation( ((Integer) indices.elementAt(i)).intValue(), ((Integer) indices.elementAt(j)).intValue())); } } if (set.size() > 0) result.addElement(set); for (i = 1; i < (int) (Math.pow(2, indices.size() - 1)); i++) { Arrays.fill(number, false); ind1.removeAllElements(); ind2.removeAllElements(); temp.removeAllElements(); temp2.removeAllElements(); temp3.removeAllElements(); binario = Integer.toString(i, 2); for (k = 0; k < number.length - binario.length(); k++) { number[k] = false; } for (j = 0; j < binario.length(); j++, k++) { if (binario.charAt(j) == '1') number[k] = true; } for (j = 0; j < number.length; j++) { if (number[j] == true) { ind1.addElement(new Integer(((Integer) indices.elementAt(j)).intValue())); } else { ind2.addElement(new Integer(((Integer) indices.elementAt(j)).intValue())); } } res1 = obtainExhaustive(ind1); res2 = obtainExhaustive(ind2); for (j = 0; j < res1.size(); j++) { result.addElement(new Vector<Relation>((Vector<Relation>) res1.elementAt(j))); } for (j = 0; j < res2.size(); j++) { result.addElement(new Vector<Relation>((Vector<Relation>) res2.elementAt(j))); } for (j = 0; j < res1.size(); j++) { temp = (Vector<Relation>) ((Vector<Relation>) res1.elementAt(j)).clone(); for (k = 0; k < res2.size(); k++) { temp2 = (Vector<Relation>) temp.clone(); temp3 = (Vector<Relation>) ((Vector<Relation>) res2.elementAt(k)).clone(); if (((Relation) temp2.elementAt(0)).i < ((Relation) temp3.elementAt(0)).i) { temp2.addAll((Vector<Relation>) temp3); result.addElement(new Vector<Relation>(temp2)); } else { temp3.addAll((Vector<Relation>) temp2); result.addElement(new Vector<Relation>(temp3)); } } } } for (i = 0; i < result.size(); i++) { if (((Vector<Relation>) result.elementAt(i)).toString().equalsIgnoreCase("[]")) { result.removeElementAt(i); i--; } } for (i = 0; i < result.size(); i++) { for (j = i + 1; j < result.size(); j++) { if (((Vector<Relation>) result.elementAt(i)) .toString() .equalsIgnoreCase(((Vector<Relation>) result.elementAt(j)).toString())) { result.removeElementAt(j); j--; } } } return result; } // end-method