Search in sources :

Example 31 with FitnessFunction

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

the class NSGAIISystemTest method testIntegration.

@Test
public void testIntegration() {
    Properties.MUTATION_RATE = 1d / 1d;
    Properties.CRITERION = new Criterion[2];
    Properties.CRITERION[0] = Criterion.RHO;
    Properties.CRITERION[1] = Criterion.AMBIGUITY;
    Properties.ALGORITHM = Algorithm.NSGAII;
    EvoSuite evosuite = new EvoSuite();
    String targetClass = Calculator.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    String[] command = new String[] { "-Dselection_function=BINARY_TOURNAMENT", "-Dminimize_values=false", "-Dinline=false", "-Dminimize=true", "-Dstop_zero=false", "-Dcoverage=false", "-Djunit_tests=true", "-Dassertions=true", "-Dsandbox=true", "-Dnew_statistics=false", "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    Assert.assertNotNull(result);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    final FitnessFunction rho = ga.getFitnessFunctions().get(0);
    final FitnessFunction ag = ga.getFitnessFunctions().get(1);
    List<Chromosome> population = new ArrayList<Chromosome>(ga.getBestIndividuals());
    Collections.sort(population, new SortByFitness(rho, false));
    for (Chromosome p : population) {
        System.out.println("Rho: " + p.getFitness(rho) + ", AG: " + p.getFitness(ag) + " | Rank: " + p.getRank());
        Assert.assertEquals(0.0, p.getFitness(rho), 0.0);
        Assert.assertEquals(0.0, p.getFitness(ag), 0.0);
        Assert.assertEquals(0, p.getRank());
    }
}
Also used : EvoSuite(org.evosuite.EvoSuite) ArrayList(java.util.ArrayList) Chromosome(org.evosuite.ga.Chromosome) NSGAChromosome(org.evosuite.ga.NSGAChromosome) SortByFitness(org.evosuite.ga.comparators.SortByFitness) FitnessFunction(org.evosuite.ga.FitnessFunction) Test(org.junit.Test)

Example 32 with FitnessFunction

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

the class NSGAIISystemTest method testCrowingDistanceAssignment_OneVariable.

@Test
public void testCrowingDistanceAssignment_OneVariable() {
    NSGAII<NSGAChromosome> ga = new NSGAII<NSGAChromosome>(null);
    Problem p = new Booths();
    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.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);
    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.25 - population.get(2).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.25 - population.get(3).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.25 - population.get(4).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.25 - population.get(5).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.25 - population.get(6).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.25 - population.get(7).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.25 - population.get(8).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.25 - population.get(9).getDistance()) < epsilon);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) ArrayList(java.util.ArrayList) CrowdingComparator(org.evosuite.ga.comparators.CrowdingComparator) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) Booths(org.evosuite.ga.problems.singleobjective.Booths) Test(org.junit.Test)

Example 33 with FitnessFunction

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

the class TestSuiteMinimizerSystemTest method testWithTwo.

@SuppressWarnings("rawtypes")
@Test
public void testWithTwo() {
    Properties.CRITERION = new Criterion[2];
    Properties.CRITERION[0] = Criterion.ONLYBRANCH;
    Properties.CRITERION[1] = Criterion.LINE;
    Properties.MINIMIZE_VALUES = true;
    EvoSuite evosuite = new EvoSuite();
    String targetClass = MethodReturnsPrimitive.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    Assert.assertNotNull(result);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome c = (TestSuiteChromosome) ga.getBestIndividual();
    final FitnessFunction onlybranch = ga.getFitnessFunctions().get(0);
    final FitnessFunction line = ga.getFitnessFunctions().get(1);
    Assert.assertEquals(0.0, c.getFitness(onlybranch), 0.0);
    Assert.assertEquals(0.0, c.getFitness(line), 0.0);
    Assert.assertEquals(1.0, c.getCoverage(onlybranch), 0.0);
    Assert.assertEquals(1.0, c.getCoverage(line), 0.0);
    Assert.assertEquals(6.0, c.getNumOfCoveredGoals(onlybranch), 0.0);
    Assert.assertEquals(10.0, c.getNumOfCoveredGoals(line), 0.0);
}
Also used : EvoSuite(org.evosuite.EvoSuite) FitnessFunction(org.evosuite.ga.FitnessFunction) Test(org.junit.Test)

Example 34 with FitnessFunction

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

the class NoveltyFitnessEvaluationListener method iteration.

@Override
public void iteration(GeneticAlgorithm<?> algorithm) {
    List<TestChromosome> population = ((GeneticAlgorithm<TestChromosome>) algorithm).getPopulation();
    TestSuiteChromosome suite = createMergedSolution(population);
    for (FitnessFunction fitnessFunction : fitnessFunctions) {
        fitnessFunction.getFitness(suite);
    }
    // Update fitness functions based on goals just added to archive
    algorithm.updateFitnessFunctionsAndValues();
}
Also used : TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) FitnessFunction(org.evosuite.ga.FitnessFunction) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction) TestChromosome(org.evosuite.testcase.TestChromosome) GeneticAlgorithm(org.evosuite.ga.metaheuristics.GeneticAlgorithm)

Example 35 with FitnessFunction

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

the class SPEA2Test method testZDT4.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testZDT4() throws IOException {
    Properties.MUTATION_RATE = 1d / 10d;
    Properties.POPULATION = 100;
    Properties.SEARCH_BUDGET = 250;
    Properties.STOPPING_CONDITION = StoppingCondition.MAXGENERATIONS;
    Properties.CROSSOVER_RATE = 0.9;
    Properties.RANDOM_SEED = 1l;
    ChromosomeFactory<?> factory = new RandomFactory(true, 10, -5.0, 5.0);
    GeneticAlgorithm<?> ga = new SPEA2(factory);
    BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison();
    ts.setMaximize(false);
    ga.setSelectionFunction(ts);
    ga.setCrossOverFunction(new SBXCrossover());
    Problem p = new ZDT4();
    final FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    final FitnessFunction f2 = (FitnessFunction) p.getFitnessFunctions().get(1);
    ga.addFitnessFunction(f1);
    ga.addFitnessFunction(f2);
    // execute
    ga.generateSolution();
    List<Chromosome> chromosomes = (List<Chromosome>) ga.getPopulation();
    Collections.sort(chromosomes, new Comparator<Chromosome>() {

        @Override
        public int compare(Chromosome arg0, Chromosome arg1) {
            return Double.compare(arg0.getFitness(f1), arg1.getFitness(f1));
        }
    });
    double[][] front = new double[Properties.POPULATION][2];
    for (int i = 0; i < chromosomes.size(); i++) {
        Chromosome chromosome = chromosomes.get(i);
        System.out.printf("%f,%f\n", chromosome.getFitness(f1), chromosome.getFitness(f2));
        front[i][0] = Double.valueOf(chromosome.getFitness(f1));
        front[i][1] = Double.valueOf(chromosome.getFitness(f2));
    }
    // load True Pareto Front
    double[][] trueParetoFront = Metrics.readFront("ZDT4.pf");
    GenerationalDistance gd = new GenerationalDistance();
    double gdd = gd.evaluate(front, trueParetoFront);
    System.out.println("GenerationalDistance: " + gdd);
    assertEquals(0.00, gdd, 0.01);
    Spacing sp = new Spacing();
    double spd = sp.evaluate(front);
    System.out.println("SpacingFront: " + spd);
    assertEquals(0.71, spd, 0.01);
}
Also used : ZDT4(org.evosuite.ga.problems.multiobjective.ZDT4) Chromosome(org.evosuite.ga.Chromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) FitnessFunction(org.evosuite.ga.FitnessFunction) Spacing(org.evosuite.ga.problems.metrics.Spacing) GenerationalDistance(org.evosuite.ga.problems.metrics.GenerationalDistance) BinaryTournamentSelectionCrowdedComparison(org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison) Problem(org.evosuite.ga.problems.Problem) ArrayList(java.util.ArrayList) List(java.util.List) SBXCrossover(org.evosuite.ga.operators.crossover.SBXCrossover) 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