Search in sources :

Example 11 with FitnessFunction

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

the class ZDT6IntTest method testZDT6Fitnesses.

@Test
public void testZDT6Fitnesses() {
    Problem p = new ZDT6();
    FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    FitnessFunction f2 = (FitnessFunction) p.getFitnessFunctions().get(1);
    double[] values = { 0.541, 0.585, 0.915, 0.624, 0.493, 0.142, 0.971, 0.836, 0.763, 0.323 };
    NSGAChromosome c = new NSGAChromosome(0.0, 1.0, values);
    Assert.assertEquals(f1.getFitness(c), 0.9866973935066625, 0.0);
    Assert.assertEquals(f2.getFitness(c), 8.903810335418541, 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 12 with FitnessFunction

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

the class ZDT6IntTest method testZDT6.

/**
 * Testing NSGA-II with ZDT6 Problem
 *
 * @throws IOException
 * @throws NumberFormatException
 */
@Test
public void testZDT6() throws NumberFormatException, IOException {
    Properties.MUTATION_RATE = 1d / 10d;
    ChromosomeFactory<?> factory = new RandomFactory(false, 10, 0.0, 1.0);
    GeneticAlgorithm<?> ga = new NSGAII(factory);
    BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison();
    ts.setMaximize(false);
    ga.setSelectionFunction(ts);
    ga.setCrossOverFunction(new SBXCrossover());
    Problem p = new ZDT6();
    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("ZDT6.pf");
    GenerationalDistance gd = new GenerationalDistance();
    double gdd = gd.evaluate(front, trueParetoFront);
    System.out.println("GenerationalDistance: " + gdd);
    Assert.assertEquals(gdd, 0.0005, 0.0005);
    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.15, 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 13 with FitnessFunction

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

the class TestShere method testSphereFitness.

@Test
public void testSphereFitness() {
    Problem p = new Sphere();
    FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    double[] values = { -2.0 };
    NSGAChromosome c = new NSGAChromosome(Math.pow(-10.0, 3.0), Math.pow(10.0, 3.0), values);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(0)).getValue(), -2.0, 0.0);
    Assert.assertEquals(f1.getFitness(c), 4.0, 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 14 with FitnessFunction

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

the class BranchesManager method calculateFitness.

public void calculateFitness(T c) {
    // run the test
    TestCase test = ((TestChromosome) c).getTestCase();
    ExecutionResult result = TestCaseExecutor.runTest(test);
    ((TestChromosome) c).setLastExecutionResult(result);
    c.setChanged(false);
    if (result.hasTimeout() || result.hasTestException()) {
        for (FitnessFunction<T> f : currentGoals) c.setFitness(f, Double.MAX_VALUE);
        return;
    }
    // 1) we update the set of currents goals
    Set<FitnessFunction<T>> visitedStatements = new HashSet<FitnessFunction<T>>(uncoveredGoals.size() * 2);
    LinkedList<FitnessFunction<T>> targets = new LinkedList<FitnessFunction<T>>();
    targets.addAll(this.currentGoals);
    while (targets.size() > 0) {
        FitnessFunction<T> fitnessFunction = targets.poll();
        int past_size = visitedStatements.size();
        visitedStatements.add(fitnessFunction);
        if (past_size == visitedStatements.size())
            continue;
        double value = fitnessFunction.getFitness(c);
        if (value == 0.0) {
            updateCoveredGoals(fitnessFunction, c);
            for (FitnessFunction<T> child : graph.getStructuralChildren(fitnessFunction)) {
                targets.addLast(child);
            }
        } else {
            currentGoals.add(fitnessFunction);
        }
    }
    currentGoals.removeAll(coveredGoals.keySet());
    // 2) we update the archive
    for (Integer branchid : result.getTrace().getCoveredFalseBranches()) {
        FitnessFunction<T> branch = this.branchCoverageFalseMap.get(branchid);
        if (branch == null)
            continue;
        updateCoveredGoals((FitnessFunction<T>) branch, c);
    }
    for (Integer branchid : result.getTrace().getCoveredTrueBranches()) {
        FitnessFunction<T> branch = this.branchCoverageTrueMap.get(branchid);
        if (branch == null)
            continue;
        updateCoveredGoals((FitnessFunction<T>) branch, c);
    }
    for (String method : result.getTrace().getCoveredBranchlessMethods()) {
        FitnessFunction<T> branch = this.branchlessMethodCoverageMap.get(method);
        if (branch == null)
            continue;
        updateCoveredGoals((FitnessFunction<T>) branch, c);
    }
// debugStructuralDependencies(c);
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) FitnessFunction(org.evosuite.ga.FitnessFunction) LinkedList(java.util.LinkedList) TestCase(org.evosuite.testcase.TestCase) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet)

Example 15 with FitnessFunction

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

the class CoverageArchive method mergeArchiveAndSolution.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public TestSuiteChromosome mergeArchiveAndSolution(Chromosome solution) {
    // Deactivate in case a test is executed and would access the archive as this might cause a
    // concurrent access
    Properties.TEST_ARCHIVE = false;
    TestSuiteChromosome mergedSolution = (TestSuiteChromosome) solution.clone();
    // skip solutions that have been modified as those might not have been evaluated yet, or have
    // timeout or throw some exception and therefore they may slow down future analysis on the final
    // test suite
    mergedSolution.getTestChromosomes().removeIf(t -> t.isChanged() || (t.getLastExecutionResult() != null && (t.getLastExecutionResult().hasTimeout() || t.getLastExecutionResult().hasTestException())));
    // to avoid adding the same solution to 'mergedSolution' suite
    Set<T> solutionsSampledFromArchive = new LinkedHashSet<T>();
    for (F target : this.getTargets()) {
        // has target been covered? to answer it, we perform a local check rather than calling method
        // {@link TestFitnessFunction.isCoveredBy} as it may perform a fitness evaluation to access
        // whether that 'target' is covered or not (and therefore, it could be more expensive)
        boolean isGoalCovered = false;
        for (TestChromosome test : mergedSolution.getTestChromosomes()) {
            if (test.getTestCase().isGoalCovered(target)) {
                isGoalCovered = true;
                break;
            }
        }
        if (!isGoalCovered) {
            T chromosome = this.covered.get(target);
            // considered yet?
            if (chromosome != null && !solutionsSampledFromArchive.contains(chromosome)) {
                solutionsSampledFromArchive.add(chromosome);
                mergedSolution.addTest(chromosome);
            }
        }
    }
    // re-evaluate merged solution
    for (FitnessFunction fitnessFunction : solution.getFitnessValues().keySet()) {
        fitnessFunction.getFitness(mergedSolution);
    }
    // re-active it
    Properties.TEST_ARCHIVE = true;
    return mergedSolution;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) TestChromosome(org.evosuite.testcase.TestChromosome)

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