Search in sources :

Example 21 with NSGAChromosome

use of org.evosuite.ga.NSGAChromosome in project evosuite by EvoSuite.

the class NSGAIISystemTest method testUnion.

@Test
public void testUnion() {
    NSGAII<NSGAChromosome> ga = new NSGAII<NSGAChromosome>(null);
    NSGAChromosome c1 = new NSGAChromosome();
    NSGAChromosome c2 = new NSGAChromosome();
    NSGAChromosome c3 = new NSGAChromosome();
    List<NSGAChromosome> pop = new ArrayList<NSGAChromosome>();
    pop.add(c1);
    pop.add(c2);
    List<NSGAChromosome> off = new ArrayList<NSGAChromosome>();
    off.add(c3);
    List<NSGAChromosome> union = ga.union(pop, off);
    Assert.assertEquals(union.size(), 3);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 22 with NSGAChromosome

use of org.evosuite.ga.NSGAChromosome in project evosuite by EvoSuite.

the class NSGAIISystemTest method testCrowingDistanceAssignment_OneVariable.

@Test
public void testCrowingDistanceAssignment_OneVariable() {
    NSGAII<NSGAChromosome> ga = new NSGAII<NSGAChromosome>(null);
    Problem p = new Booths();
    List<FitnessFunction<NSGAChromosome>> fitnessFunctions = p.getFitnessFunctions();
    ga.addFitnessFunctions(fitnessFunctions);
    NSGAChromosome c1 = new NSGAChromosome();
    NSGAChromosome c2 = new NSGAChromosome();
    NSGAChromosome c3 = new NSGAChromosome();
    NSGAChromosome c4 = new NSGAChromosome();
    NSGAChromosome c5 = new NSGAChromosome();
    NSGAChromosome c6 = new NSGAChromosome();
    NSGAChromosome c7 = new NSGAChromosome();
    NSGAChromosome c8 = new NSGAChromosome();
    NSGAChromosome c9 = new NSGAChromosome();
    NSGAChromosome c10 = new NSGAChromosome();
    // Set Fitness
    c1.setFitness(fitnessFunctions.get(0), 0.0);
    c2.setFitness(fitnessFunctions.get(0), 0.2);
    c3.setFitness(fitnessFunctions.get(0), 0.4);
    c4.setFitness(fitnessFunctions.get(0), 0.6);
    c5.setFitness(fitnessFunctions.get(0), 0.8);
    c6.setFitness(fitnessFunctions.get(0), 0.0);
    c7.setFitness(fitnessFunctions.get(0), 0.2);
    c8.setFitness(fitnessFunctions.get(0), 0.4);
    c9.setFitness(fitnessFunctions.get(0), 0.6);
    c10.setFitness(fitnessFunctions.get(0), 0.8);
    List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
    population.add(c1);
    population.add(c2);
    population.add(c3);
    population.add(c4);
    population.add(c5);
    population.add(c6);
    population.add(c7);
    population.add(c8);
    population.add(c9);
    population.add(c10);
    ga.crowingDistanceAssignment(population);
    Collections.sort(population, new CrowdingComparator(true));
    Assert.assertTrue(population.get(0).getDistance() == Double.POSITIVE_INFINITY);
    Assert.assertTrue(population.get(1).getDistance() == Double.POSITIVE_INFINITY);
    double epsilon = 1e-10;
    Assert.assertTrue(Math.abs(0.25 - population.get(2).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.25 - population.get(3).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.25 - population.get(4).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.25 - population.get(5).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.25 - population.get(6).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.25 - population.get(7).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.25 - population.get(8).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.25 - population.get(9).getDistance()) < epsilon);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) ArrayList(java.util.ArrayList) CrowdingComparator(org.evosuite.ga.comparators.CrowdingComparator) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) Booths(org.evosuite.ga.problems.singleobjective.Booths) Test(org.junit.Test)

Example 23 with NSGAChromosome

use of org.evosuite.ga.NSGAChromosome in project evosuite by EvoSuite.

the class SBXCrossover method crossOver.

@Override
public void crossOver(Chromosome p1, Chromosome p2) throws ConstructionFailedException {
    NSGAChromosome n1 = (NSGAChromosome) p1;
    NSGAChromosome n2 = (NSGAChromosome) p2;
    for (int i = 0; i < n1.getNumberOfVariables(); i++) {
        Variable v1 = n1.getVariable(i);
        Variable v2 = n2.getVariable(i);
        if ((v1 instanceof DoubleVariable) && (v2 instanceof DoubleVariable))
            this.doCrossover((DoubleVariable) v1, (DoubleVariable) v2);
    }
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) Variable(org.evosuite.ga.variables.Variable) DoubleVariable(org.evosuite.ga.variables.DoubleVariable) DoubleVariable(org.evosuite.ga.variables.DoubleVariable)

Example 24 with NSGAChromosome

use of org.evosuite.ga.NSGAChromosome in project evosuite by EvoSuite.

the class TestBinaryTournamentSelectionCrowdedComparison method testNonDominationRankMinimize.

@Test
public void testNonDominationRankMinimize() {
    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(1);
    c2.setRank(0);
    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 25 with NSGAChromosome

use of org.evosuite.ga.NSGAChromosome in project evosuite by EvoSuite.

the class TestThreeHump method testThreeHump.

/**
 * Testing NSGA-II with ThreeHump Problem
 *
 * @throws IOException
 * @throws NumberFormatException
 */
@Test
public void testThreeHump() throws NumberFormatException, IOException {
    Properties.MUTATION_RATE = 1d / 2d;
    ChromosomeFactory<?> factory = new RandomFactory(false, 2, -5.0, 5.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 ThreeHump();
    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

NSGAChromosome (org.evosuite.ga.NSGAChromosome)36 Test (org.junit.Test)35 FitnessFunction (org.evosuite.ga.FitnessFunction)24 Problem (org.evosuite.ga.problems.Problem)24 ArrayList (java.util.ArrayList)16 NSGAII (org.evosuite.ga.metaheuristics.NSGAII)8 DoubleVariable (org.evosuite.ga.variables.DoubleVariable)7 Booths (org.evosuite.ga.problems.singleobjective.Booths)6 List (java.util.List)5 Chromosome (org.evosuite.ga.Chromosome)4 RandomFactory (org.evosuite.ga.metaheuristics.RandomFactory)4 SBXCrossover (org.evosuite.ga.operators.crossover.SBXCrossover)4 BinaryTournamentSelectionCrowdedComparison (org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison)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 Variable (org.evosuite.ga.variables.Variable)1