Search in sources :

Example 1 with ZDT4

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

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Chromosome (org.evosuite.ga.Chromosome)1 FitnessFunction (org.evosuite.ga.FitnessFunction)1 SBXCrossover (org.evosuite.ga.operators.crossover.SBXCrossover)1 BinaryTournamentSelectionCrowdedComparison (org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison)1 Problem (org.evosuite.ga.problems.Problem)1 GenerationalDistance (org.evosuite.ga.problems.metrics.GenerationalDistance)1 Spacing (org.evosuite.ga.problems.metrics.Spacing)1 ZDT4 (org.evosuite.ga.problems.multiobjective.ZDT4)1 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)1 Test (org.junit.Test)1