Search in sources :

Example 1 with FitnessFunction

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

the class TestGenerationResultBuilder method fillInformationFromTestData.

private void fillInformationFromTestData(TestGenerationResultImpl result) {
    Set<MutationInfo> exceptionMutants = new LinkedHashSet<MutationInfo>();
    for (Mutation m : MutationPool.getMutants()) {
        if (MutationTimeoutStoppingCondition.isDisabled(m)) {
            MutationInfo info = new MutationInfo(m);
            exceptionMutants.add(info);
            uncoveredMutants.remove(info);
        }
    }
    for (String test : testCode.keySet()) {
        result.setTestCode(test, testCode.get(test));
        result.setTestCase(test, testCases.get(test));
        result.setContractViolations(test, contractViolations.get(test));
        result.setCoveredLines(test, testLineCoverage.get(test));
        result.setCoveredBranches(test, testBranchCoverage.get(test));
        result.setCoveredMutants(test, testMutantCoverage.get(test));
        result.setComment(test, testComments.get(test));
    }
    result.setUncoveredLines(uncoveredLines);
    result.setUncoveredBranches(uncoveredBranches);
    result.setUncoveredMutants(uncoveredMutants);
    result.setExceptionMutants(exceptionMutants);
    result.setTestSuiteCode(code);
    result.setGeneticAlgorithm(ga);
    for (Map.Entry<FitnessFunction<?>, Double> e : targetCoverages.entrySet()) {
        result.setTargetCoverage(e.getKey(), e.getValue());
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FitnessFunction(org.evosuite.ga.FitnessFunction) Mutation(org.evosuite.coverage.mutation.Mutation) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with FitnessFunction

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

the class InputCoverageFitnessFunctionSystemTest method testInputCoverageClassWithField.

@Test
public void testInputCoverageClassWithField() throws NoSuchFieldException, NoSuchMethodException {
    Class<?> sut = ClassWithField.class;
    DefaultTestCase tc = new DefaultTestCase();
    // ClassWithField classWithField0 = new ClassWithField();
    GenericConstructor constructor = new GenericConstructor(sut.getConstructors()[0], sut);
    ConstructorStatement constructorStatement = new ConstructorStatement(tc, constructor, Arrays.asList(new VariableReference[] {}));
    VariableReference obj = tc.addStatement(constructorStatement);
    // classWithField0.testFoo(classWithField0.BOOLEAN_FIELD);
    FieldReference field = new FieldReference(tc, new GenericField(sut.getDeclaredField("BOOLEAN_FIELD"), sut), obj);
    Method m = sut.getMethod("testFoo", new Class<?>[] { Boolean.TYPE });
    GenericMethod gm = new GenericMethod(m, sut);
    tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
    // classWithField0.BOOLEAN_FIELD = false;
    VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc, false));
    tc.addStatement(new AssignmentStatement(tc, field, boolRef));
    tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
    Properties.TARGET_CLASS = sut.getCanonicalName();
    Properties.JUNIT_TESTS = true;
    TestSuiteChromosome testSuite = new TestSuiteChromosome();
    testSuite.addTest(tc);
    FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(Properties.Criterion.INPUT);
    assertEquals("Should be 0.0", 0.0, ffunction.getFitness(testSuite), 0.0);
    assertEquals("Should be 1.0", 1.0, testSuite.getCoverage(ffunction), 0.0);
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) ClassWithField(com.examples.with.different.packagename.coverage.ClassWithField) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) GenericField(org.evosuite.utils.generic.GenericField) Test(org.junit.Test)

Example 3 with FitnessFunction

use of org.evosuite.ga.FitnessFunction 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 FitnessFunction

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

the class FONIntTest method testFONFitnesses.

@Test
public void testFONFitnesses() {
    Problem p = new FON();
    FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    FitnessFunction f2 = (FitnessFunction) p.getFitnessFunctions().get(1);
    double[] values = { -2.0, 1.0, 3.0 };
    NSGAChromosome c = new NSGAChromosome(-4.0, 4.0, values);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(0)).getValue(), -2.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(1)).getValue(), 1.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(2)).getValue(), 3.0, 0.0);
    Assert.assertEquals(f1.getFitness(c), 0.9999969200553233, 0.0);
    Assert.assertEquals(f2.getFitness(c), 0.9999999696175615, 0.0);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) Test(org.junit.Test)

Example 5 with FitnessFunction

use of org.evosuite.ga.FitnessFunction 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)

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