Search in sources :

Example 6 with DoubleVariable

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

the class TestBeales method testBeales.

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

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

the class TestShere method testSphere.

/**
 * Testing NSGA-II with Sphere Problem
 *
 * @throws IOException
 * @throws NumberFormatException
 */
@Test
public void testSphere() throws NumberFormatException, IOException {
    Properties.MUTATION_RATE = 1d / 1d;
    ChromosomeFactory<?> factory = new RandomFactory(false, 1, Math.pow(-10.0, 3.0), Math.pow(10.0, 3.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 Sphere();
    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.00, 0.01);
    for (Chromosome chromosome : chromosomes) {
        NSGAChromosome nsga_c = (NSGAChromosome) chromosome;
        DoubleVariable x = (DoubleVariable) nsga_c.getVariables().get(0);
        System.out.printf("%f : %f\n", x.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)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