Search in sources :

Example 1 with Chromosome

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

the class SearchStatistics method searchFinished.

/**
 * {@inheritDoc}
 */
@Override
public void searchFinished(GeneticAlgorithm<?> algorithm) {
    Chromosome result = algorithm.getBestIndividual();
    StatisticEntry entry = statistics.get(statistics.size() - 1);
    entry.end_time = System.currentTimeMillis();
    entry.result_tests_executed = MaxTestsStoppingCondition.getNumExecutedTests();
    entry.result_statements_executed = MaxStatementsStoppingCondition.getNumExecutedStatements();
    entry.testExecutionTime = TestCaseExecutor.timeExecuted;
    entry.goalComputationTime = AbstractFitnessFactory.goalComputationTime;
    entry.covered_goals = result.getNumOfCoveredGoals();
    entry.timedOut = TestSuiteGenerator.global_time.isFinished();
    entry.stoppingCondition = TestSuiteGenerator.stopping_condition.getCurrentValue();
    entry.globalTimeStoppingCondition = TestSuiteGenerator.global_time.getCurrentValue();
    if (result instanceof TestSuiteChromosome) {
        TestSuiteChromosome best = (TestSuiteChromosome) result;
        entry.size_final = best.size();
        entry.length_final = best.totalLengthOfTestCases();
    }
}
Also used : Chromosome(org.evosuite.ga.Chromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 2 with Chromosome

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

the class TestCaseRecycler method searchFinished.

@Override
public void searchFinished(GeneticAlgorithm<?> algorithm) {
    Chromosome individual = algorithm.getBestIndividual();
    if (individual instanceof TestChromosome) {
        TestChromosome testChromosome = (TestChromosome) individual;
        testPool.add(testChromosome.getTestCase());
    } else if (individual instanceof TestSuiteChromosome) {
        TestSuiteChromosome testSuiteChromosome = (TestSuiteChromosome) individual;
        testPool.addAll(testSuiteChromosome.getTests());
    }
}
Also used : TestChromosome(org.evosuite.testcase.TestChromosome) Chromosome(org.evosuite.ga.Chromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 3 with Chromosome

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

the class FONIntTest method testFON.

/**
 * Testing NSGA-II with FON Problem
 *
 * @throws IOException
 * @throws NumberFormatException
 */
@Test
public void testFON() throws NumberFormatException, IOException {
    Properties.MUTATION_RATE = 1d / 3d;
    ChromosomeFactory<?> factory = new RandomFactory(false, 3, -4.0, 4.0);
    GeneticAlgorithm<?> ga = new NSGAII(factory);
    BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison();
    ga.setSelectionFunction(ts);
    ga.setCrossOverFunction(new SBXCrossover());
    Problem p = new FON();
    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];
    int index = 0;
    for (Chromosome chromosome : chromosomes) {
        System.out.printf("%f,%f\n", chromosome.getFitness(f1), chromosome.getFitness(f2));
        front[index][0] = Double.valueOf(chromosome.getFitness(f1));
        front[index][1] = Double.valueOf(chromosome.getFitness(f2));
        index++;
    }
    // load True Pareto Front
    double[][] trueParetoFront = Metrics.readFront("Fonseca.pf");
    GenerationalDistance gd = new GenerationalDistance();
    double gdd = gd.evaluate(front, trueParetoFront);
    System.out.println("GenerationalDistance: " + gdd);
    Assert.assertEquals(gdd, 0.0006, 0.0001);
    Spacing sp = new Spacing();
    double spd = sp.evaluate(front);
    double spdt = sp.evaluate(trueParetoFront);
    System.out.println("SpacingFront (" + spd + ") - SpacingTrueFront (" + spdt + ") = " + Math.abs(spd - spdt));
    Assert.assertEquals(Math.abs(spd - spdt), 0.01, 0.01);
}
Also used : Chromosome(org.evosuite.ga.Chromosome) NSGAChromosome(org.evosuite.ga.NSGAChromosome) FitnessFunction(org.evosuite.ga.FitnessFunction) Spacing(org.evosuite.ga.problems.metrics.Spacing) RandomFactory(org.evosuite.ga.metaheuristics.RandomFactory) NSGAII(org.evosuite.ga.metaheuristics.NSGAII) GenerationalDistance(org.evosuite.ga.problems.metrics.GenerationalDistance) BinaryTournamentSelectionCrowdedComparison(org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison) Problem(org.evosuite.ga.problems.Problem) List(java.util.List) SBXCrossover(org.evosuite.ga.operators.crossover.SBXCrossover) Test(org.junit.Test)

Example 4 with Chromosome

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

the class KURIntTest method testKUR.

/**
 * Testing NSGA-II with KUR Problem
 *
 * @throws IOException
 * @throws NumberFormatException
 */
@Test
public void testKUR() throws NumberFormatException, IOException {
    Properties.MUTATION_RATE = 1d / 3d;
    ChromosomeFactory<?> factory = new RandomFactory(false, 3, -5.0, 5.0);
    GeneticAlgorithm<?> ga = new NSGAII(factory);
    BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison();
    ga.setSelectionFunction(ts);
    ga.setCrossOverFunction(new SBXCrossover());
    Problem p = new KUR();
    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];
    int index = 0;
    for (Chromosome chromosome : chromosomes) {
        System.out.printf("%f,%f\n", chromosome.getFitness(f1), chromosome.getFitness(f2));
        front[index][0] = Double.valueOf(chromosome.getFitness(f1));
        front[index][1] = Double.valueOf(chromosome.getFitness(f2));
        index++;
    }
    // load True Pareto Front
    double[][] trueParetoFront = Metrics.readFront("Kursawe.pf");
    GenerationalDistance gd = new GenerationalDistance();
    double gdd = gd.evaluate(front, trueParetoFront);
    System.out.println("GenerationalDistance: " + gdd);
    Assert.assertEquals(gdd, 0.0004, 0.0001);
    Spacing sp = new Spacing();
    double spd = sp.evaluate(front);
    double spdt = sp.evaluate(trueParetoFront);
    System.out.println("SpacingFront (" + spd + ") - SpacingTrueFront (" + spdt + ") = " + Math.abs(spd - spdt));
    Assert.assertEquals(Math.abs(spd - spdt), 0.30, 0.05);
}
Also used : Chromosome(org.evosuite.ga.Chromosome) NSGAChromosome(org.evosuite.ga.NSGAChromosome) FitnessFunction(org.evosuite.ga.FitnessFunction) Spacing(org.evosuite.ga.problems.metrics.Spacing) RandomFactory(org.evosuite.ga.metaheuristics.RandomFactory) NSGAII(org.evosuite.ga.metaheuristics.NSGAII) GenerationalDistance(org.evosuite.ga.problems.metrics.GenerationalDistance) BinaryTournamentSelectionCrowdedComparison(org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison) Problem(org.evosuite.ga.problems.Problem) List(java.util.List) SBXCrossover(org.evosuite.ga.operators.crossover.SBXCrossover) Test(org.junit.Test)

Example 5 with Chromosome

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

the class POLIntTest method testPOL.

/**
 * Testing NSGA-II with POL Problem
 *
 * @throws IOException
 * @throws NumberFormatException
 */
@Test
public void testPOL() throws NumberFormatException, IOException {
    Properties.MUTATION_RATE = 1d / 2d;
    ChromosomeFactory<?> factory = new RandomFactory(false, 2, -Math.PI, Math.PI);
    GeneticAlgorithm<?> ga = new NSGAII(factory);
    BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison();
    ga.setSelectionFunction(ts);
    ga.setCrossOverFunction(new SBXCrossover());
    Problem p = new POL();
    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];
    int index = 0;
    for (Chromosome chromosome : chromosomes) {
        System.out.printf("%f,%f\n", chromosome.getFitness(f1), chromosome.getFitness(f2));
        front[index][0] = Double.valueOf(chromosome.getFitness(f1));
        front[index][1] = Double.valueOf(chromosome.getFitness(f2));
        index++;
    }
    // load True Pareto Front
    double[][] trueParetoFront = Metrics.readFront("Poloni.pf");
    GenerationalDistance gd = new GenerationalDistance();
    double gdd = gd.evaluate(front, trueParetoFront);
    System.out.println("GenerationalDistance: " + gdd);
    Assert.assertEquals(gdd, 0.0005, 0.0001);
    Spacing sp = new Spacing();
    double spd = sp.evaluate(front);
    double spdt = sp.evaluate(trueParetoFront);
    System.out.println("SpacingFront (" + spd + ") - SpacingTrueFront (" + spdt + ") = " + Math.abs(spd - spdt));
    Assert.assertEquals(Math.abs(spd - spdt), 0.10, 0.05);
}
Also used : Chromosome(org.evosuite.ga.Chromosome) NSGAChromosome(org.evosuite.ga.NSGAChromosome) FitnessFunction(org.evosuite.ga.FitnessFunction) Spacing(org.evosuite.ga.problems.metrics.Spacing) RandomFactory(org.evosuite.ga.metaheuristics.RandomFactory) NSGAII(org.evosuite.ga.metaheuristics.NSGAII) GenerationalDistance(org.evosuite.ga.problems.metrics.GenerationalDistance) BinaryTournamentSelectionCrowdedComparison(org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison) Problem(org.evosuite.ga.problems.Problem) List(java.util.List) SBXCrossover(org.evosuite.ga.operators.crossover.SBXCrossover) Test(org.junit.Test)

Aggregations

Chromosome (org.evosuite.ga.Chromosome)60 Test (org.junit.Test)41 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)32 ArrayList (java.util.ArrayList)29 Neighbourhood (org.evosuite.ga.Neighbourhood)20 FitnessFunction (org.evosuite.ga.FitnessFunction)15 List (java.util.List)13 SBXCrossover (org.evosuite.ga.operators.crossover.SBXCrossover)13 BinaryTournamentSelectionCrowdedComparison (org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison)13 Problem (org.evosuite.ga.problems.Problem)13 NSGAChromosome (org.evosuite.ga.NSGAChromosome)12 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 TestChromosome (org.evosuite.testcase.TestChromosome)6 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)5 LineCoverageSuiteFitness (org.evosuite.coverage.line.LineCoverageSuiteFitness)5 DoubleVariable (org.evosuite.ga.variables.DoubleVariable)4 EvoSuite (org.evosuite.EvoSuite)3