Search in sources :

Example 1 with DoubleVariable

use of org.evosuite.ga.variables.DoubleVariable 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)

Example 2 with DoubleVariable

use of org.evosuite.ga.variables.DoubleVariable 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 3 with DoubleVariable

use of org.evosuite.ga.variables.DoubleVariable 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)

Example 4 with DoubleVariable

use of org.evosuite.ga.variables.DoubleVariable in project evosuite by EvoSuite.

the class TestSBXCrossOver method crossoverDifferentChromosomes.

@Test
public void crossoverDifferentChromosomes() throws Exception {
    NSGAChromosome c1 = new NSGAChromosome();
    c1.addVariable(new DoubleVariable(0.9, 0.0, 1.0));
    NSGAChromosome c2 = new NSGAChromosome();
    c2.addVariable(new DoubleVariable(0.1, 0.0, 1.0));
    SBXCrossover sbx = new SBXCrossover();
    sbx.crossOver(c1, c2);
    double v_c1 = ((DoubleVariable) c1.getVariable(0)).getValue();
    Assert.assertEquals(v_c1, 0.1, 0.01);
    double v_c2 = ((DoubleVariable) c2.getVariable(0)).getValue();
    Assert.assertEquals(v_c2, 0.9, 0.01);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) DoubleVariable(org.evosuite.ga.variables.DoubleVariable) Test(org.junit.Test)

Example 5 with DoubleVariable

use of org.evosuite.ga.variables.DoubleVariable in project evosuite by EvoSuite.

the class TestSBXCrossOver method crossoverEqualChromosomes.

@Test
public void crossoverEqualChromosomes() throws Exception {
    NSGAChromosome c1 = new NSGAChromosome();
    c1.addVariable(new DoubleVariable(0.5, 0.0, 1.0));
    NSGAChromosome c2 = new NSGAChromosome();
    c2.addVariable(new DoubleVariable(0.5, 0.0, 1.0));
    SBXCrossover sbx = new SBXCrossover();
    sbx.crossOver(c1, c2);
    double v_c1 = ((DoubleVariable) c1.getVariable(0)).getValue();
    Assert.assertEquals(v_c1, 0.5, 0.0);
    double v_c2 = ((DoubleVariable) c2.getVariable(0)).getValue();
    Assert.assertEquals(v_c2, 0.5, 0.0);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) DoubleVariable(org.evosuite.ga.variables.DoubleVariable) Test(org.junit.Test)

Aggregations

NSGAChromosome (org.evosuite.ga.NSGAChromosome)7 DoubleVariable (org.evosuite.ga.variables.DoubleVariable)7 Test (org.junit.Test)6 List (java.util.List)4 Chromosome (org.evosuite.ga.Chromosome)4 FitnessFunction (org.evosuite.ga.FitnessFunction)4 NSGAII (org.evosuite.ga.metaheuristics.NSGAII)4 RandomFactory (org.evosuite.ga.metaheuristics.RandomFactory)4 SBXCrossover (org.evosuite.ga.operators.crossover.SBXCrossover)4 BinaryTournamentSelectionCrowdedComparison (org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison)4 Problem (org.evosuite.ga.problems.Problem)4 Variable (org.evosuite.ga.variables.Variable)1