Search in sources :

Example 41 with FitnessFunction

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

the class TestSortByFitness method testSortByFitnessC1win.

@Test
public void testSortByFitnessC1win() {
    Problem p = new Booths<NSGAChromosome>();
    List<FitnessFunction<NSGAChromosome>> fitnessFunctions = p.getFitnessFunctions();
    FitnessFunction<NSGAChromosome> ff = fitnessFunctions.get(0);
    NSGAChromosome c1 = new NSGAChromosome();
    NSGAChromosome c2 = new NSGAChromosome();
    // Set Fitness
    c1.setFitness(ff, 0.7);
    c2.setFitness(ff, 0.3);
    List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
    population.add(c1);
    population.add(c2);
    SortByFitness sf = new SortByFitness(ff, true);
    Collections.sort(population, sf);
    Assert.assertTrue(population.get(0) == c1);
    Assert.assertEquals(population.get(0).getFitness(ff), 0.7, 0.0);
    Assert.assertTrue(population.get(1) == c2);
    Assert.assertEquals(population.get(1).getFitness(ff), 0.3, 0.0);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) ArrayList(java.util.ArrayList) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) Booths(org.evosuite.ga.problems.singleobjective.Booths) Test(org.junit.Test)

Example 42 with FitnessFunction

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

the class SCHIntTest method testSCHFitnesses.

@Test
public void testSCHFitnesses() {
    Problem p = new SCH();
    FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    FitnessFunction f2 = (FitnessFunction) p.getFitnessFunctions().get(1);
    double[] values_n = { -10 };
    NSGAChromosome c = new NSGAChromosome(Math.pow(-10.0, 3.0), Math.pow(10.0, 3.0), values_n);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(0)).getValue(), -10.0, 0.0);
    Assert.assertEquals(f1.getFitness(c), 100.0, 0.0);
    Assert.assertEquals(f2.getFitness(c), 144.0, 0.0);
    double[] values_z = { 0 };
    c = new NSGAChromosome(Math.pow(-10.0, 3.0), Math.pow(10.0, 3.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), 4.0, 0.0);
    double[] values_p = { 10 };
    c = new NSGAChromosome(Math.pow(-10.0, 3.0), Math.pow(10.0, 3.0), values_p);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(0)).getValue(), 10.0, 0.0);
    Assert.assertEquals(f1.getFitness(c), 100.0, 0.0);
    Assert.assertEquals(f2.getFitness(c), 64.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 43 with FitnessFunction

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

the class ZDT1IntTest method testZDT1.

/**
 * Testing NSGA-II with ZDT1 Problem
 *
 * @throws IOException
 * @throws NumberFormatException
 */
@Test
public void testZDT1() throws NumberFormatException, IOException {
    Properties.MUTATION_RATE = 1d / 30d;
    ChromosomeFactory<?> factory = new RandomFactory(false, 30, 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 ZDT1();
    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("ZDT1.pf");
    GenerationalDistance gd = new GenerationalDistance();
    double gdd = gd.evaluate(front, trueParetoFront);
    System.out.println("GenerationalDistance: " + gdd);
    Assert.assertEquals(gdd, 0.001, 0.001);
    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.10);
}
Also used : Chromosome(org.evosuite.ga.Chromosome) 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 44 with FitnessFunction

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

the class ZDT4IntTest method testZDT4Fitnesses.

@Test
public void testZDT4Fitnesses() {
    Problem p = new ZDT4();
    FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    FitnessFunction f2 = (FitnessFunction) p.getFitnessFunctions().get(1);
    double[] values = { 0.5, -5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0 };
    NSGAChromosome c = new NSGAChromosome(-5, 5, values);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(0)).getValue(), 0.5, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(1)).getValue(), -5.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(2)).getValue(), -4.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(3)).getValue(), -3.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(4)).getValue(), -2.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(5)).getValue(), -1.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(6)).getValue(), 0.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(7)).getValue(), 1.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(8)).getValue(), 2.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(9)).getValue(), 3.0, 0.0);
    Assert.assertEquals(f1.getFitness(c), 0.5, 0.0);
    Assert.assertEquals(f2.getFitness(c), 65.68459221202592, 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 45 with FitnessFunction

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

the class TestBeales method testBealesFitness.

@Test
public void testBealesFitness() {
    Problem p = new Beales();
    FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    double[] values = { -2.0, 1.0 };
    NSGAChromosome c = new NSGAChromosome(-4.5, 4.5, 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), 81.703125, 0.0);
}
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