/** * Creates a Local Search objective for a TestCase that will be optimized * using a containing TestSuite to measure the changes in fitness values. * * @param fitness * the list of fitness functions to use to compute the fitness of * the TestSuiteChromosome * @param suite * a TestSuite chromosome that will be subjected to local search * @param index * a test index (between 0 and the test suite length) that will * be used to modify the testchromosome each time a changed has * been applied. */ private TestSuiteLocalSearchObjective(List<TestSuiteFitnessFunction> fitness, TestSuiteChromosome suite, int index) { this.fitnessFunctions.addAll(fitness); this.suite = suite; this.testIndex = index; for (TestSuiteFitnessFunction ff : fitness) { if (ff.isMaximizationFunction()) isMaximization = true; else isMaximization = false; break; } updateLastFitness(); updateLastCoverage(); }
/** * Overrides the test case at position <code>testIndex</code> with the * individual. It returns <code>-1</code> if the new fitness has improved, * <code>1</code> if the fitness has worsened or <code>0</code> if the * fitness has not changed. */ @Override public int hasChanged(TestChromosome testCase) { testCase.setChanged(true); suite.setTestChromosome(testIndex, testCase); LocalSearchBudget.getInstance().countFitnessEvaluation(); for (TestSuiteFitnessFunction fitnessFunction : fitnessFunctions) fitnessFunction.getFitness(suite); double newFitness = suite.getFitness(); if (isFitnessBetter(newFitness, lastFitnessSum)) { logger.info("Local search improved fitness from " + lastFitnessSum + " to " + newFitness); updateLastFitness(); updateLastCoverage(); return -1; } else if (isFitnessWorse(newFitness, lastFitnessSum)) { logger.info("Local search worsened fitness from " + lastFitnessSum + " to " + newFitness); suite.setFitnessValues(lastFitness); suite.setCoverageValues(lastCoverage); return 1; } else { logger.info("Local search did not change fitness of " + lastFitnessSum); updateLastCoverage(); return 0; } }