/** * Copyright (C) 2010-2017 Gordon Fraser, Andrea Arcuri and EvoSuite * contributors * * This file is part of EvoSuite. * * EvoSuite is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation, either version 3.0 of the License, or * (at your option) any later version. * * EvoSuite is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with EvoSuite. If not, see <http://www.gnu.org/licenses/>. */ package org.evosuite.ga.problems.multiobjective; import java.io.IOException; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.evosuite.Properties; import org.evosuite.ga.Chromosome; import org.evosuite.ga.ChromosomeFactory; import org.evosuite.ga.FitnessFunction; import org.evosuite.ga.NSGAChromosome; import org.evosuite.ga.metaheuristics.GeneticAlgorithm; import org.evosuite.ga.metaheuristics.NSGAII; import org.evosuite.ga.metaheuristics.RandomFactory; import org.evosuite.ga.operators.crossover.SBXCrossover; import org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison; import org.evosuite.ga.problems.Problem; import org.evosuite.ga.problems.metrics.GenerationalDistance; import org.evosuite.ga.problems.metrics.Metrics; import org.evosuite.ga.problems.metrics.Spacing; import org.junit.Assert; import org.junit.Before; import org.junit.Test; /** * * @author José Campos */ @SuppressWarnings({ "rawtypes", "unchecked" }) public class POLIntTest { @Before public void setUp() { Properties.POPULATION = 100; Properties.SEARCH_BUDGET = 10000; Properties.CROSSOVER_RATE = 0.9; Properties.RANDOM_SEED = 1l; } @Test public void testPOLFitnesses() { Problem p = new POL(); FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0); FitnessFunction f2 = (FitnessFunction) p.getFitnessFunctions().get(1); double[] values = {-2.9272124303, 2.7365080818}; NSGAChromosome c = new NSGAChromosome(-Math.PI, Math.PI, values); Assert.assertEquals(f1.getFitness(c), 9.25584063461892, 0.0); Assert.assertEquals(f2.getFitness(c), 13.966790675659546, 0.0); } /** * Testing NSGA-II with POL Problem * * @throws IOException * @throws NumberFormatException */ @Test public void testPOL() throws NumberFormatException, IOException { Properties.MUTATION_RATE = 1d / 2d; ChromosomeFactory<?> factory = new RandomFactory(false, 2, -Math.PI, Math.PI); GeneticAlgorithm<?> ga = new NSGAII(factory); BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison(); ga.setSelectionFunction(ts); ga.setCrossOverFunction(new SBXCrossover()); Problem p = new POL(); 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("Poloni.pf"); GenerationalDistance gd = new GenerationalDistance(); double gdd = gd.evaluate(front, trueParetoFront); System.out.println("GenerationalDistance: " + gdd); Assert.assertEquals(gdd, 0.0005, 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.10, 0.05); } }