Search in sources :

Example 6 with Spacing

use of org.evosuite.ga.problems.metrics.Spacing 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 Spacing

use of org.evosuite.ga.problems.metrics.Spacing in project evosuite by EvoSuite.

the class SPEA2SystemTest method nonMinimalSpacing.

@Test
public void nonMinimalSpacing() {
    String targetClass = BMICalculator.class.getCanonicalName();
    Properties.POPULATION = 50;
    double[][] front = test(targetClass);
    for (int i = 0; i < front.length; i++) {
        assertNotEquals(front[i][0], front[i][1], 0.0);
    }
    Spacing sp = new Spacing();
    double[] max = sp.getMaximumValues(front);
    double[] min = sp.getMinimumValues(front);
    double[][] frontNormalized = sp.getNormalizedFront(front, max, min);
    assertNotEquals(0.0, sp.evaluate(frontNormalized), 0.0);
}
Also used : Spacing(org.evosuite.ga.problems.metrics.Spacing) Test(org.junit.Test)

Example 8 with Spacing

use of org.evosuite.ga.problems.metrics.Spacing in project evosuite by EvoSuite.

the class SPEA2Test method testZDT4.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testZDT4() throws IOException {
    Properties.MUTATION_RATE = 1d / 10d;
    Properties.POPULATION = 100;
    Properties.SEARCH_BUDGET = 250;
    Properties.STOPPING_CONDITION = StoppingCondition.MAXGENERATIONS;
    Properties.CROSSOVER_RATE = 0.9;
    Properties.RANDOM_SEED = 1l;
    ChromosomeFactory<?> factory = new RandomFactory(true, 10, -5.0, 5.0);
    GeneticAlgorithm<?> ga = new SPEA2(factory);
    BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison();
    ts.setMaximize(false);
    ga.setSelectionFunction(ts);
    ga.setCrossOverFunction(new SBXCrossover());
    Problem p = new ZDT4();
    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];
    for (int i = 0; i < chromosomes.size(); i++) {
        Chromosome chromosome = chromosomes.get(i);
        System.out.printf("%f,%f\n", chromosome.getFitness(f1), chromosome.getFitness(f2));
        front[i][0] = Double.valueOf(chromosome.getFitness(f1));
        front[i][1] = Double.valueOf(chromosome.getFitness(f2));
    }
    // load True Pareto Front
    double[][] trueParetoFront = Metrics.readFront("ZDT4.pf");
    GenerationalDistance gd = new GenerationalDistance();
    double gdd = gd.evaluate(front, trueParetoFront);
    System.out.println("GenerationalDistance: " + gdd);
    assertEquals(0.00, gdd, 0.01);
    Spacing sp = new Spacing();
    double spd = sp.evaluate(front);
    System.out.println("SpacingFront: " + spd);
    assertEquals(0.71, spd, 0.01);
}
Also used : ZDT4(org.evosuite.ga.problems.multiobjective.ZDT4) Chromosome(org.evosuite.ga.Chromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) FitnessFunction(org.evosuite.ga.FitnessFunction) Spacing(org.evosuite.ga.problems.metrics.Spacing) GenerationalDistance(org.evosuite.ga.problems.metrics.GenerationalDistance) BinaryTournamentSelectionCrowdedComparison(org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison) Problem(org.evosuite.ga.problems.Problem) ArrayList(java.util.ArrayList) List(java.util.List) SBXCrossover(org.evosuite.ga.operators.crossover.SBXCrossover) Test(org.junit.Test)

Example 9 with Spacing

use of org.evosuite.ga.problems.metrics.Spacing in project evosuite by EvoSuite.

the class SCH2IntTest method testSCH2.

/**
 * Testing NSGA-II with SCH2 Problem
 *
 * @throws IOException
 * @throws NumberFormatException
 */
@Test
public void testSCH2() throws NumberFormatException, IOException {
    Properties.MUTATION_RATE = 1d / 1d;
    ChromosomeFactory<?> factory = new RandomFactory(false, 1, -5.0, 10.0);
    GeneticAlgorithm<?> ga = new NSGAII(factory);
    BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison();
    ts.setMaximize(false);
    ga.setSelectionFunction(ts);
    ga.setCrossOverFunction(new SBXCrossover());
    Problem p = new SCH2();
    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("Schaffer2.pf");
    GenerationalDistance gd = new GenerationalDistance();
    double gdd = gd.evaluate(front, trueParetoFront);
    System.out.println("GenerationalDistance: " + gdd);
    Assert.assertEquals(gdd, 0.0004, 0.0001);
    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.05, 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 10 with Spacing

use of org.evosuite.ga.problems.metrics.Spacing in project evosuite by EvoSuite.

the class SPEA2SystemTest method minimalSpacing.

@Test
public void minimalSpacing() {
    String targetClass = BMICalculator.class.getCanonicalName();
    Properties.POPULATION = 10;
    double[][] front = test(targetClass);
    Spacing sp = new Spacing();
    double[] max = sp.getMaximumValues(front);
    double[] min = sp.getMinimumValues(front);
    double[][] frontNormalized = sp.getNormalizedFront(front, max, min);
    assertEquals(0.0, sp.evaluate(frontNormalized), 0.0);
}
Also used : Spacing(org.evosuite.ga.problems.metrics.Spacing) Test(org.junit.Test)

Aggregations

Spacing (org.evosuite.ga.problems.metrics.Spacing)11 Test (org.junit.Test)11 List (java.util.List)9 Chromosome (org.evosuite.ga.Chromosome)9 FitnessFunction (org.evosuite.ga.FitnessFunction)9 SBXCrossover (org.evosuite.ga.operators.crossover.SBXCrossover)9 BinaryTournamentSelectionCrowdedComparison (org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison)9 Problem (org.evosuite.ga.problems.Problem)9 GenerationalDistance (org.evosuite.ga.problems.metrics.GenerationalDistance)9 NSGAII (org.evosuite.ga.metaheuristics.NSGAII)8 RandomFactory (org.evosuite.ga.metaheuristics.RandomFactory)8 NSGAChromosome (org.evosuite.ga.NSGAChromosome)7 ArrayList (java.util.ArrayList)1 ZDT4 (org.evosuite.ga.problems.multiobjective.ZDT4)1 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)1