Search in sources :

Example 31 with NSGAChromosome

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

Example 32 with NSGAChromosome

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

the class SCHIntTest method testSCHFitnesses.

@Test
public void testSCHFitnesses() {
    Problem p = new SCH();
    FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    FitnessFunction f2 = (FitnessFunction) p.getFitnessFunctions().get(1);
    double[] values_n = { -10 };
    NSGAChromosome c = new NSGAChromosome(Math.pow(-10.0, 3.0), Math.pow(10.0, 3.0), values_n);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(0)).getValue(), -10.0, 0.0);
    Assert.assertEquals(f1.getFitness(c), 100.0, 0.0);
    Assert.assertEquals(f2.getFitness(c), 144.0, 0.0);
    double[] values_z = { 0 };
    c = new NSGAChromosome(Math.pow(-10.0, 3.0), Math.pow(10.0, 3.0), values_z);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(0)).getValue(), 0.0, 0.0);
    Assert.assertEquals(f1.getFitness(c), 0.0, 0.0);
    Assert.assertEquals(f2.getFitness(c), 4.0, 0.0);
    double[] values_p = { 10 };
    c = new NSGAChromosome(Math.pow(-10.0, 3.0), Math.pow(10.0, 3.0), values_p);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(0)).getValue(), 10.0, 0.0);
    Assert.assertEquals(f1.getFitness(c), 100.0, 0.0);
    Assert.assertEquals(f2.getFitness(c), 64.0, 0.0);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) Test(org.junit.Test)

Example 33 with NSGAChromosome

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

the class ZDT4IntTest method testZDT4Fitnesses.

@Test
public void testZDT4Fitnesses() {
    Problem p = new ZDT4();
    FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    FitnessFunction f2 = (FitnessFunction) p.getFitnessFunctions().get(1);
    double[] values = { 0.5, -5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0 };
    NSGAChromosome c = new NSGAChromosome(-5, 5, values);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(0)).getValue(), 0.5, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(1)).getValue(), -5.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(2)).getValue(), -4.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(3)).getValue(), -3.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(4)).getValue(), -2.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(5)).getValue(), -1.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(6)).getValue(), 0.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(7)).getValue(), 1.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(8)).getValue(), 2.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(9)).getValue(), 3.0, 0.0);
    Assert.assertEquals(f1.getFitness(c), 0.5, 0.0);
    Assert.assertEquals(f2.getFitness(c), 65.68459221202592, 0.0);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) Test(org.junit.Test)

Example 34 with NSGAChromosome

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

the class TestBeales method testBealesFitness.

@Test
public void testBealesFitness() {
    Problem p = new Beales();
    FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    double[] values = { -2.0, 1.0 };
    NSGAChromosome c = new NSGAChromosome(-4.5, 4.5, values);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(0)).getValue(), -2.0, 0.0);
    Assert.assertEquals(((DoubleVariable) c.getVariables().get(1)).getValue(), 1.0, 0.0);
    Assert.assertEquals(f1.getFitness(c), 81.703125, 0.0);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) Test(org.junit.Test)

Example 35 with NSGAChromosome

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

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