Search in sources :

Example 21 with FitnessFunction

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

the class PrivateReflectionSystemTest method testPrivateConstructor.

private TestSuiteChromosome testPrivateConstructor() {
    Properties.P_REFLECTION_ON_PRIVATE = 0.9;
    Properties.REFLECTION_START_PERCENT = 0.0;
    GeneticAlgorithm ga = do100percentLineTestOnStandardCriteriaWithMethodTrace(PrivateConstructor.class);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    assertTrue(!best.getTests().isEmpty());
    double cov = best.getCoverageInstanceOf(MethodCoverageSuiteFitness.class);
    Assert.assertEquals("Non-optimal method coverage: ", 1d, cov, 0.001);
    Optional<FitnessFunction<?>> ff = ga.getFitnessFunctions().stream().filter(m -> m instanceof MethodCoverageSuiteFitness).findAny();
    assertEquals(1, best.getNumOfCoveredGoals(ff.get()));
    cov = best.getCoverageInstanceOf(MethodTraceCoverageSuiteFitness.class);
    Assert.assertEquals("Non-optimal method trace coverage: ", 1d, cov, 0.001);
    ff = ga.getFitnessFunctions().stream().filter(m -> m instanceof MethodTraceCoverageSuiteFitness).findAny();
    assertEquals(1, best.getNumOfCoveredGoals(ff.get()));
    return best;
}
Also used : Arrays(java.util.Arrays) GeneticAlgorithm(org.evosuite.ga.metaheuristics.GeneticAlgorithm) RuntimeVariable(org.evosuite.statistics.RuntimeVariable) MethodCoverageFitnessFunctionSystemTest(org.evosuite.coverage.method.MethodCoverageFitnessFunctionSystemTest) OutputVariable(org.evosuite.statistics.OutputVariable) ArrayList(java.util.ArrayList) MethodTraceCoverageSuiteFitness(org.evosuite.coverage.method.MethodTraceCoverageSuiteFitness) FitnessFunction(org.evosuite.ga.FitnessFunction) SystemTestBase(org.evosuite.SystemTestBase) Properties(org.evosuite.Properties) com.examples.with.different.packagename.reflection(com.examples.with.different.packagename.reflection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) TestCaseExecutor(org.evosuite.testcase.execution.TestCaseExecutor) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) EvoSuite(org.evosuite.EvoSuite) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) MethodCoverageSuiteFitness(org.evosuite.coverage.method.MethodCoverageSuiteFitness) LineCoverageSuiteFitness(org.evosuite.coverage.line.LineCoverageSuiteFitness) Optional(java.util.Optional) Assert(org.junit.Assert) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction) Assert.assertEquals(org.junit.Assert.assertEquals) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) MethodTraceCoverageSuiteFitness(org.evosuite.coverage.method.MethodTraceCoverageSuiteFitness) FitnessFunction(org.evosuite.ga.FitnessFunction) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction) MethodCoverageSuiteFitness(org.evosuite.coverage.method.MethodCoverageSuiteFitness) GeneticAlgorithm(org.evosuite.ga.metaheuristics.GeneticAlgorithm)

Example 22 with FitnessFunction

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

the class SCH2IntTest method testSCH2Fitnesses.

@Test
public void testSCH2Fitnesses() {
    Problem p = new SCH2();
    FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    FitnessFunction f2 = (FitnessFunction) p.getFitnessFunctions().get(1);
    double[] values_n = { -3.0 };
    NSGAChromosome c = new NSGAChromosome(-5.0, 10.0, values_n);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(0)).getValue(), -3.0, 0.0);
    Assert.assertEquals(f1.getFitness(c), 3.0, 0.0);
    Assert.assertEquals(f2.getFitness(c), 64.0, 0.0);
    double[] values_z = { 0.0 };
    c = new NSGAChromosome(-5.0, 10.0, values_z);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(0)).getValue(), 0.0, 0.0);
    Assert.assertEquals(f1.getFitness(c), 0.0, 0.0);
    Assert.assertEquals(f2.getFitness(c), 25.0, 0.0);
    double[] values_p = { 9.0 };
    c = new NSGAChromosome(-5.0, 10.0, values_p);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(0)).getValue(), 9.0, 0.0);
    Assert.assertEquals(f1.getFitness(c), 5.0, 0.0);
    Assert.assertEquals(f2.getFitness(c), 16.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 23 with FitnessFunction

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

the class TestBooths method testBoothsFitness.

@Test
public void testBoothsFitness() {
    Problem p = new Booths();
    FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    double[] values = { -2.0, 1.0 };
    NSGAChromosome c = new NSGAChromosome(-10.0, 10.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(f1.getFitness(c), 113.0, 0.0);
    double[] values_m = { 1.0, 3.0 };
    c = new NSGAChromosome(-10.0, 10.0, values_m);
    Assert.assertEquals(f1.getFitness(c), 0.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 24 with FitnessFunction

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

the class TestBooths method testBooths.

/**
 * Testing NSGA-II with Booths Problem
 *
 * @throws IOException
 * @throws NumberFormatException
 */
@Test
public void testBooths() throws NumberFormatException, IOException {
    Properties.MUTATION_RATE = 1d / 2d;
    ChromosomeFactory<?> factory = new RandomFactory(false, 2, -10.0, 10.0);
    // GeneticAlgorithm<?> ga = new NSGAII(factory);
    GeneticAlgorithm<?> ga = new NSGAII(factory);
    BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison();
    // BinaryTournament ts = new BinaryTournament();
    ga.setSelectionFunction(ts);
    ga.setCrossOverFunction(new SBXCrossover());
    Problem p = new Booths();
    final FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    ga.addFitnessFunction(f1);
    // 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));
        }
    });
    for (Chromosome chromosome : chromosomes) Assert.assertEquals(chromosome.getFitness(f1), 0.000, 0.001);
    for (Chromosome chromosome : chromosomes) {
        NSGAChromosome nsga_c = (NSGAChromosome) chromosome;
        DoubleVariable x = (DoubleVariable) nsga_c.getVariables().get(0);
        DoubleVariable y = (DoubleVariable) nsga_c.getVariables().get(1);
        System.out.printf("%f,%f : %f\n", x.getValue(), y.getValue(), chromosome.getFitness(f1));
    }
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) Chromosome(org.evosuite.ga.Chromosome) NSGAChromosome(org.evosuite.ga.NSGAChromosome) FitnessFunction(org.evosuite.ga.FitnessFunction) RandomFactory(org.evosuite.ga.metaheuristics.RandomFactory) NSGAII(org.evosuite.ga.metaheuristics.NSGAII) BinaryTournamentSelectionCrowdedComparison(org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison) Problem(org.evosuite.ga.problems.Problem) List(java.util.List) DoubleVariable(org.evosuite.ga.variables.DoubleVariable) SBXCrossover(org.evosuite.ga.operators.crossover.SBXCrossover) Test(org.junit.Test)

Example 25 with FitnessFunction

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

the class TestThreeHump method testThreeHumpFitness.

@Test
public void testThreeHumpFitness() {
    Problem p = new ThreeHump();
    FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    double[] values = { -2.0, 3.0 };
    NSGAChromosome c = new NSGAChromosome(-5.0, 5.0, values);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(0)).getValue(), -2.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(1)).getValue(), 3.0, 0.0);
    Assert.assertEquals(f1.getFitness(c), 4.87, 0.01);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) 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