Search in sources :

Example 6 with NSGAII

use of org.evosuite.ga.metaheuristics.NSGAII 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 7 with NSGAII

use of org.evosuite.ga.metaheuristics.NSGAII in project evosuite by EvoSuite.

the class TestBinaryTournamentSelectionCrowdedComparison method testNonDominationRankMaximize.

@Test
public void testNonDominationRankMaximize() {
    NSGAII<NSGAChromosome> ga = new NSGAII<NSGAChromosome>(null);
    BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison(true);
    ts.setMaximize(true);
    ga.setSelectionFunction(ts);
    NSGAChromosome c1 = new NSGAChromosome();
    NSGAChromosome c2 = new NSGAChromosome();
    // Set Rank
    c1.setRank(1);
    c2.setRank(0);
    List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
    population.add(c1);
    population.add(c2);
    Assert.assertTrue(ts.getIndex(population) == 0);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) NSGAII(org.evosuite.ga.metaheuristics.NSGAII) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with NSGAII

use of org.evosuite.ga.metaheuristics.NSGAII in project evosuite by EvoSuite.

the class TestBinaryTournamentSelectionCrowdedComparison method testCrowdingDistanceMinimize.

@Test
public void testCrowdingDistanceMinimize() {
    NSGAII<NSGAChromosome> ga = new NSGAII<NSGAChromosome>(null);
    BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison(false);
    ts.setMaximize(false);
    ga.setSelectionFunction(ts);
    NSGAChromosome c1 = new NSGAChromosome();
    NSGAChromosome c2 = new NSGAChromosome();
    // Set Rank
    c1.setRank(0);
    c2.setRank(0);
    // Set Distance
    c1.setDistance(0.1);
    c2.setDistance(0.5);
    List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
    population.add(c1);
    population.add(c2);
    Assert.assertTrue(ts.getIndex(population) == 1);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) NSGAII(org.evosuite.ga.metaheuristics.NSGAII) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 9 with NSGAII

use of org.evosuite.ga.metaheuristics.NSGAII in project evosuite by EvoSuite.

the class TestBinaryTournamentSelectionCrowdedComparison method testCrowdingDistanceMaximize.

@Test
public void testCrowdingDistanceMaximize() {
    NSGAII<NSGAChromosome> ga = new NSGAII<NSGAChromosome>(null);
    BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison(true);
    ts.setMaximize(true);
    ga.setSelectionFunction(ts);
    NSGAChromosome c1 = new NSGAChromosome();
    NSGAChromosome c2 = new NSGAChromosome();
    // Set Rank
    c1.setRank(0);
    c2.setRank(0);
    // Set Distance
    c1.setDistance(0.1);
    c2.setDistance(0.5);
    List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
    population.add(c1);
    population.add(c2);
    Assert.assertTrue(ts.getIndex(population) == 1);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) NSGAII(org.evosuite.ga.metaheuristics.NSGAII) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 10 with NSGAII

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

Aggregations

NSGAII (org.evosuite.ga.metaheuristics.NSGAII)16 Test (org.junit.Test)16 NSGAChromosome (org.evosuite.ga.NSGAChromosome)15 List (java.util.List)12 Chromosome (org.evosuite.ga.Chromosome)12 FitnessFunction (org.evosuite.ga.FitnessFunction)12 RandomFactory (org.evosuite.ga.metaheuristics.RandomFactory)12 SBXCrossover (org.evosuite.ga.operators.crossover.SBXCrossover)12 BinaryTournamentSelectionCrowdedComparison (org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison)12 Problem (org.evosuite.ga.problems.Problem)12 GenerationalDistance (org.evosuite.ga.problems.metrics.GenerationalDistance)8 Spacing (org.evosuite.ga.problems.metrics.Spacing)8 ArrayList (java.util.ArrayList)4 DoubleVariable (org.evosuite.ga.variables.DoubleVariable)4