/* * A redesigned Fitness Function calculator * It takes into account the accuracy of the decision tree while classifying both, training and test examples * The fitness score is a function of the weighted average of the two accuracies. */ private void calculateFitnessScore(double trainingWeight, double testingWeight) throws OptimalScoreException { DecisionTreeClassifier dtClassifier = getDecisionTree(); dtClassifier.TestAndFindAccuracy(); // Part 1 - Get training set accuracy double trainingSetAccuracy = dtClassifier.getAccuracy(); // Part 2 - Get test set accuracy SampleCollection test_samples = new SampleCollection( DataHolder.getTestingSamplesFileName(), DataHolder.getAttributesFileName()); // test_samples.discretizeSamples(Constants.DiscretizerAlgorithms.EQUAL_BINNING); test_samples.discretizeSamplesBasedOnOtherSampleCollection(dtClassifier.getTrainingSamples()); dtClassifier.setTestingSamples(test_samples); dtClassifier.TestAndFindAccuracy(); double testSetAccuracy = dtClassifier.getAccuracy(); fitnessScore = (trainingWeight * trainingSetAccuracy + testingWeight * testSetAccuracy) / (trainingWeight + testingWeight); // fitnessScore = trainingSetAccuracy; It was running very slowly that's why all this circus. // We'll find a solution. // fitnessScore = trainingSetAccuracy > testSetAccuracy ? trainingSetAccuracy : testSetAccuracy; }
/* * Used to reinitialize the static variables of this class, when DataHolder is updated, since static variables * aren't updated automatically. */ public static void reInitializeStaticVariables() { samples = new SampleCollection( DataHolder.getTrainingSamplesFileName(), DataHolder.getAttributesFileName()); FeatureSuperSet = samples.getfeatureList(); samples.discretizeSamples(Constants.DiscretizerAlgorithms.EQUAL_BINNING); }
/* * Initializes a decision tree that uses only the features present in the chromosome. * It also calculates the fitness score of this chromosome. */ private void initDTfromChromosome(String chromosome) throws OptimalScoreException { this.chromosome = chromosome; calculateFitnessScore(TRAINING_SET_WEIGHT, TEST_SET_WEIGHT); if (fitnessScore >= DataHolder.getFitnessScoreThreshold()) throw new OptimalScoreException(this); }