Search in sources :

Example 16 with FitnessFunction

use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.

the class TestSuiteLocalSearch method applyAVM.

/**
 * Applies AVM on the test case in the suite
 *
 * @param suite
 * @param testIndex
 * @param test
 * @param localSearchObjective
 * @return
 */
private boolean applyAVM(TestSuiteChromosome suite, int testIndex, TestChromosome test, LocalSearchObjective<TestSuiteChromosome> objective) {
    logger.debug("Local search on test " + testIndex + ", current fitness: " + suite.getFitness());
    final List<FitnessFunction<? extends Chromosome>> fitnessFunctions = objective.getFitnessFunctions();
    TestSuiteLocalSearchObjective testCaseLocalSearchObjective = TestSuiteLocalSearchObjective.buildNewTestSuiteLocalSearchObjective(fitnessFunctions, suite, testIndex);
    AVMTestCaseLocalSearch testCaselocalSearch = new AVMTestCaseLocalSearch();
    boolean improved = testCaselocalSearch.doSearch(test, testCaseLocalSearchObjective);
    return improved;
}
Also used : Chromosome(org.evosuite.ga.Chromosome) TestChromosome(org.evosuite.testcase.TestChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) AVMTestCaseLocalSearch(org.evosuite.testcase.localsearch.AVMTestCaseLocalSearch) FitnessFunction(org.evosuite.ga.FitnessFunction) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction)

Example 17 with FitnessFunction

use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.

the class CompositionalFitnessSystemTest method testCompositionalTwoFunction.

@Test
public void testCompositionalTwoFunction() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = DefUseExample1.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.CRITERION = new Properties.Criterion[2];
    Properties.CRITERION[0] = Criterion.DEFUSE;
    Properties.CRITERION[1] = Criterion.METHODNOEXCEPTION;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    Map<FitnessFunction<?>, Double> fitnesses = best.getFitnessValues();
    double sum = 0.0;
    double cov = 0.0;
    for (FitnessFunction<?> fitness : fitnesses.keySet()) {
        sum += fitnesses.get(fitness);
        cov += best.getCoverage(fitness);
        assert (fitnesses.get(fitness) == best.getFitness(fitness));
    }
    cov = cov / best.getCoverageValues().size();
    Assert.assertEquals("Inconsistent fitness: ", sum, best.getFitness(), 0.001);
    Assert.assertEquals("Inconsistent coverage: ", cov, best.getCoverage(), 0.001);
    Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) FitnessFunction(org.evosuite.ga.FitnessFunction) Properties(org.evosuite.Properties) Test(org.junit.Test)

Example 18 with FitnessFunction

use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.

the class TestDominanceComparator method testDominanceComparatorSeveralFitnessesNoDomination.

@Test
public void testDominanceComparatorSeveralFitnessesNoDomination() {
    Problem p = new FON();
    List<FitnessFunction<NSGAChromosome>> fitnessFunctions = p.getFitnessFunctions();
    FitnessFunction<NSGAChromosome> ff_1 = fitnessFunctions.get(0);
    FitnessFunction<NSGAChromosome> ff_2 = fitnessFunctions.get(1);
    NSGAChromosome c1 = new NSGAChromosome();
    NSGAChromosome c2 = new NSGAChromosome();
    // Set Fitness
    c1.setFitness(ff_1, 0.7);
    c1.setFitness(ff_2, 0.2);
    c2.setFitness(ff_1, 0.3);
    c2.setFitness(ff_2, 0.5);
    List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
    population.add(c1);
    population.add(c2);
    DominanceComparator dc = new DominanceComparator();
    Collections.sort(population, dc);
    Assert.assertEquals(population.get(0).getFitness(ff_1), 0.7, 0.0);
    Assert.assertEquals(population.get(0).getFitness(ff_2), 0.2, 0.0);
    Assert.assertEquals(population.get(1).getFitness(ff_1), 0.3, 0.0);
    Assert.assertEquals(population.get(1).getFitness(ff_2), 0.5, 0.0);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) FON(org.evosuite.ga.problems.multiobjective.FON) ArrayList(java.util.ArrayList) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) FONIntTest(org.evosuite.ga.problems.multiobjective.FONIntTest) Test(org.junit.Test)

Example 19 with FitnessFunction

use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.

the class NSGAIISystemTest method testFastNonDominatedSort.

@Test
public void testFastNonDominatedSort() {
    NSGAII<NSGAChromosome> ga = new NSGAII<NSGAChromosome>(null);
    Problem p = new Booths<NSGAChromosome>();
    List<FitnessFunction<NSGAChromosome>> fitnessFunctions = p.getFitnessFunctions();
    ga.addFitnessFunctions(fitnessFunctions);
    NSGAChromosome c1 = new NSGAChromosome();
    NSGAChromosome c2 = new NSGAChromosome();
    NSGAChromosome c3 = new NSGAChromosome();
    NSGAChromosome c4 = new NSGAChromosome();
    NSGAChromosome c5 = new NSGAChromosome();
    NSGAChromosome c6 = new NSGAChromosome();
    NSGAChromosome c7 = new NSGAChromosome();
    NSGAChromosome c8 = new NSGAChromosome();
    NSGAChromosome c9 = new NSGAChromosome();
    NSGAChromosome c10 = new NSGAChromosome();
    // Set Fitness
    c1.setFitness(fitnessFunctions.get(0), 0.6);
    c2.setFitness(fitnessFunctions.get(0), 0.2);
    c3.setFitness(fitnessFunctions.get(0), 0.4);
    c4.setFitness(fitnessFunctions.get(0), 0.0);
    c5.setFitness(fitnessFunctions.get(0), 0.8);
    c6.setFitness(fitnessFunctions.get(0), 0.8);
    c7.setFitness(fitnessFunctions.get(0), 0.2);
    c8.setFitness(fitnessFunctions.get(0), 0.4);
    c9.setFitness(fitnessFunctions.get(0), 0.6);
    c10.setFitness(fitnessFunctions.get(0), 0.0);
    List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
    population.add(c1);
    population.add(c2);
    population.add(c3);
    population.add(c4);
    population.add(c5);
    population.add(c6);
    population.add(c7);
    population.add(c8);
    population.add(c9);
    population.add(c10);
    List<List<NSGAChromosome>> fronts = ga.fastNonDominatedSort(population);
    // Total number of Fronts
    Assert.assertEquals(fronts.size(), 5);
    // Front 0
    Assert.assertTrue(fronts.get(0).get(0).getFitness() == 0.0);
    Assert.assertTrue(fronts.get(0).get(1).getFitness() == 0.0);
    // Front 1
    Assert.assertTrue(fronts.get(1).get(0).getFitness() == 0.2);
    Assert.assertTrue(fronts.get(1).get(1).getFitness() == 0.2);
    // Front 2
    Assert.assertTrue(fronts.get(2).get(0).getFitness() == 0.4);
    Assert.assertTrue(fronts.get(2).get(1).getFitness() == 0.4);
    // Front 3
    Assert.assertTrue(fronts.get(3).get(0).getFitness() == 0.6);
    Assert.assertTrue(fronts.get(3).get(1).getFitness() == 0.6);
    // Front 4
    Assert.assertTrue(fronts.get(4).get(0).getFitness() == 0.8);
    Assert.assertTrue(fronts.get(4).get(1).getFitness() == 0.8);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) ArrayList(java.util.ArrayList) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) ArrayList(java.util.ArrayList) List(java.util.List) Booths(org.evosuite.ga.problems.singleobjective.Booths) Test(org.junit.Test)

Example 20 with FitnessFunction

use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.

the class NSGAIISystemTest method testCrowingDistanceAssignment_SeveralVariables.

@Test
public void testCrowingDistanceAssignment_SeveralVariables() {
    NSGAII<NSGAChromosome> ga = new NSGAII<NSGAChromosome>(null);
    Problem p = new SCH();
    List<FitnessFunction<NSGAChromosome>> fitnessFunctions = p.getFitnessFunctions();
    ga.addFitnessFunctions(fitnessFunctions);
    NSGAChromosome c1 = new NSGAChromosome();
    NSGAChromosome c2 = new NSGAChromosome();
    NSGAChromosome c3 = new NSGAChromosome();
    NSGAChromosome c4 = new NSGAChromosome();
    NSGAChromosome c5 = new NSGAChromosome();
    NSGAChromosome c6 = new NSGAChromosome();
    NSGAChromosome c7 = new NSGAChromosome();
    NSGAChromosome c8 = new NSGAChromosome();
    NSGAChromosome c9 = new NSGAChromosome();
    NSGAChromosome c10 = new NSGAChromosome();
    // Set Fitness 1
    c1.setFitness(fitnessFunctions.get(0), 0.0);
    c2.setFitness(fitnessFunctions.get(0), 0.2);
    c3.setFitness(fitnessFunctions.get(0), 0.4);
    c4.setFitness(fitnessFunctions.get(0), 0.6);
    c5.setFitness(fitnessFunctions.get(0), 0.8);
    c6.setFitness(fitnessFunctions.get(0), 0.0);
    c7.setFitness(fitnessFunctions.get(0), 0.2);
    c8.setFitness(fitnessFunctions.get(0), 0.4);
    c9.setFitness(fitnessFunctions.get(0), 0.6);
    c10.setFitness(fitnessFunctions.get(0), 0.8);
    // Set Fitness 2
    c1.setFitness(fitnessFunctions.get(1), 0.1);
    c2.setFitness(fitnessFunctions.get(1), 0.3);
    c3.setFitness(fitnessFunctions.get(1), 0.5);
    c4.setFitness(fitnessFunctions.get(1), 0.7);
    c5.setFitness(fitnessFunctions.get(1), 0.9);
    c6.setFitness(fitnessFunctions.get(1), 0.1);
    c7.setFitness(fitnessFunctions.get(1), 0.3);
    c8.setFitness(fitnessFunctions.get(1), 0.5);
    c9.setFitness(fitnessFunctions.get(1), 0.7);
    c10.setFitness(fitnessFunctions.get(1), 0.9);
    List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
    population.add(c1);
    population.add(c2);
    population.add(c3);
    population.add(c4);
    population.add(c5);
    population.add(c6);
    population.add(c7);
    population.add(c8);
    population.add(c9);
    population.add(c10);
    ga.crowingDistanceAssignment(population);
    Collections.sort(population, new CrowdingComparator(true));
    Assert.assertTrue(population.get(0).getDistance() == Double.POSITIVE_INFINITY);
    Assert.assertTrue(population.get(1).getDistance() == Double.POSITIVE_INFINITY);
    double epsilon = 1e-10;
    Assert.assertTrue(Math.abs(0.5 - population.get(2).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.5 - population.get(3).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.5 - population.get(4).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.5 - population.get(5).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.5 - population.get(6).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.5 - population.get(7).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.5 - population.get(8).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.5 - population.get(9).getDistance()) < epsilon);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) ArrayList(java.util.ArrayList) CrowdingComparator(org.evosuite.ga.comparators.CrowdingComparator) SCH(org.evosuite.ga.problems.multiobjective.SCH) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) Test(org.junit.Test)

Aggregations

FitnessFunction (org.evosuite.ga.FitnessFunction)47 Test (org.junit.Test)38 Problem (org.evosuite.ga.problems.Problem)33 NSGAChromosome (org.evosuite.ga.NSGAChromosome)32 List (java.util.List)15 Chromosome (org.evosuite.ga.Chromosome)15 ArrayList (java.util.ArrayList)13 SBXCrossover (org.evosuite.ga.operators.crossover.SBXCrossover)13 BinaryTournamentSelectionCrowdedComparison (org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison)13 NSGAII (org.evosuite.ga.metaheuristics.NSGAII)12 RandomFactory (org.evosuite.ga.metaheuristics.RandomFactory)12 GenerationalDistance (org.evosuite.ga.problems.metrics.GenerationalDistance)9 Spacing (org.evosuite.ga.problems.metrics.Spacing)9 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)9 Booths (org.evosuite.ga.problems.singleobjective.Booths)5 TestChromosome (org.evosuite.testcase.TestChromosome)5 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)5 EvoSuite (org.evosuite.EvoSuite)4 LinkedHashSet (java.util.LinkedHashSet)3 Properties (org.evosuite.Properties)3