/** * Populates test and training datasets. * * <p>Note: (1) assumes a 50:50 split, (2) training data set is stored in the dataArray structure * in which the input data is stored, (3) method called from application class as same training * and test sets may be required if using (say) "hill climbing" approach to maximise accuracy, (4) * method is not called from constructor partly for same reason as 3 but also because the input * data set may (given a particular application) first require ordering and possibly also pruning * and recasting (see recastClassifiers method). * * @param test myDataset Class where examples are stored to build the classifier * @param dataBase DataBase Class to store the examples to work with the algorithm and some other * useful information */ public void testDataSet(myDataset test, DataBase dataBase) { int i, j, k; int[] example; short value; // Determine size of training and test sets. setNumRowsInTrainingSet(); numRowsInTestSet = test.getnData(); // Dimension and populate test set testDataArray = new short[numRowsInTestSet][]; for (i = 0; i < numRowsInTestSet; i++) { example = test.getExample(i); testDataArray[i] = new short[dataBase.numVariablesUsed() + 1]; value = 1; for (j = 0, k = 0; j < example.length; j++) { if (dataBase.numLabels(j) > 1) { testDataArray[i][k] = (short) example[j]; testDataArray[i][k] += value; value += dataBase.numLabels(j); // System.out.print (testDataArray[i][k] + " "); k++; } } testDataArray[i][k] = (short) test.getOutputAsInteger(i); testDataArray[i][k] += value; // System.out.print (testDataArray[i][k] + " "); // System.out.println (""); } }
/** * Constructor with parameters. It creates a specific rule from a given sample * * @param database Data Base associated to the general rule base that includes this rule * @param n_classes Number of classes in the training set * @param tnorm T-norm used to compute the compatibility degree * @param tconorm T-conorm used to compute the compatibility degree * @param rule_weight Way of computing the rule weight * @param sample Data sample used to generate this rule * @param sample_class Output class associated to the data sample used to generate this rule */ public Rule( DataBase database, int n_classes, int tnorm, int tconorm, int rule_weight, double[] sample, int sample_class) { antecedent = new ArrayList<FuzzyAntecedent>(database.numVariables()); clas = sample_class; t_norm = tnorm; t_conorm = tconorm; ruleWeight = rule_weight; level = 2; n_e = true; for (int i = 0; i < database.numVariables(); i++) { double max = 0.0; int etq = -1; double per; for (int j = 0; j < database.numLabels(); j++) { per = database.membershipFunction(i, j, sample[i]); if (per > max) { max = per; etq = j; } } if (max == 0.0) { System.err.println("There was an error while searching for the antecedent of the rule"); System.err.println("Example: "); for (int j = 0; j < database.numVariables(); j++) { System.err.print(sample[j] + "\t"); } System.err.println("Variable " + i); System.exit(1); } FuzzyAntecedent new_antecedent = new FuzzyAntecedent(database.clone(i, etq), i, database.numLabels()); antecedent.add((FuzzyAntecedent) new_antecedent); } }
/** * Adds a label to the fuzzy antecedent of the given variable * * @param variable_mutated position of the variable that is going to have a label added * @param data Data Base associated to the general rule base that includes this rule */ public void addLabel(int variable_mutated, DataBase data) { if (variable_mutated >= antecedent.size()) { System.err.println("We cannot select a variable outside the antecedent"); System.exit(-1); } if ((((FuzzyAntecedent) antecedent.get(variable_mutated)).getnLabels() + 1) == data.numLabels()) { System.err.println("We cannot add a label to create an any condition"); System.exit(-1); } // Add label to the selected fuzzy antecedent ((FuzzyAntecedent) antecedent.get(variable_mutated)).addLabel(data); weight = 0.0; raw_fitness = 0.0; penalized_fitness = -1.0; n_e = true; ideal = 0; level = 1; }