Search in sources :

Example 16 with Problem

use of org.evosuite.ga.problems.Problem 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 17 with Problem

use of org.evosuite.ga.problems.Problem 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 18 with Problem

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

Example 19 with Problem

use of org.evosuite.ga.problems.Problem in project evosuite by EvoSuite.

the class TestDominanceComparator method testDominanceComparatorOneFitness.

@Test
public void testDominanceComparatorOneFitness() {
    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);
    DominanceComparator dc = new DominanceComparator();
    Collections.sort(population, dc);
    Assert.assertEquals(population.get(0).getFitness(ff), 0.3, 0.0);
    Assert.assertEquals(population.get(1).getFitness(ff), 0.7, 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) FONIntTest(org.evosuite.ga.problems.multiobjective.FONIntTest) Test(org.junit.Test)

Example 20 with Problem

use of org.evosuite.ga.problems.Problem in project evosuite by EvoSuite.

the class TestDominanceComparator method testDominanceComparatorSeveralFitnessesDomination.

@Test
public void testDominanceComparatorSeveralFitnessesDomination() {
    Problem p = new FON();
    List<FitnessFunction<NSGAChromosome>> fitnessFunctions = p.getFitnessFunctions();
    FitnessFunction<NSGAChromosome> ff_1 = fitnessFunctions.get(0);
    FitnessFunction<NSGAChromosome> ff_2 = fitnessFunctions.get(1);
    NSGAChromosome c1 = new NSGAChromosome();
    NSGAChromosome c2 = new NSGAChromosome();
    // Set Fitness
    c1.setFitness(ff_1, 0.7);
    c1.setFitness(ff_2, 0.6);
    c2.setFitness(ff_1, 0.3);
    c2.setFitness(ff_2, 0.5);
    List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
    population.add(c1);
    population.add(c2);
    DominanceComparator dc = new DominanceComparator();
    Collections.sort(population, dc);
    Assert.assertEquals(population.get(0).getFitness(ff_1), 0.3, 0.0);
    Assert.assertEquals(population.get(0).getFitness(ff_2), 0.5, 0.0);
    Assert.assertEquals(population.get(1).getFitness(ff_1), 0.7, 0.0);
    Assert.assertEquals(population.get(1).getFitness(ff_2), 0.6, 0.0);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) FON(org.evosuite.ga.problems.multiobjective.FON) ArrayList(java.util.ArrayList) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) FONIntTest(org.evosuite.ga.problems.multiobjective.FONIntTest) Test(org.junit.Test)

Aggregations

FitnessFunction (org.evosuite.ga.FitnessFunction)33 Problem (org.evosuite.ga.problems.Problem)33 Test (org.junit.Test)33 NSGAChromosome (org.evosuite.ga.NSGAChromosome)31 List (java.util.List)14 Chromosome (org.evosuite.ga.Chromosome)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 ArrayList (java.util.ArrayList)10 GenerationalDistance (org.evosuite.ga.problems.metrics.GenerationalDistance)9 Spacing (org.evosuite.ga.problems.metrics.Spacing)9 Booths (org.evosuite.ga.problems.singleobjective.Booths)6 DoubleVariable (org.evosuite.ga.variables.DoubleVariable)4 FONIntTest (org.evosuite.ga.problems.multiobjective.FONIntTest)3 CrowdingComparator (org.evosuite.ga.comparators.CrowdingComparator)2 FON (org.evosuite.ga.problems.multiobjective.FON)2 SCH (org.evosuite.ga.problems.multiobjective.SCH)1 ZDT4 (org.evosuite.ga.problems.multiobjective.ZDT4)1