use of org.evosuite.ga.NSGAChromosome in project evosuite by EvoSuite.
the class TestCrowdingComparator method testCrowdingComparisonOperatorMinimize.
@Test
public void testCrowdingComparisonOperatorMinimize() {
NSGAChromosome c1 = new NSGAChromosome();
NSGAChromosome c2 = new NSGAChromosome();
NSGAChromosome c3 = new NSGAChromosome();
// Set Rank
c1.setRank(1);
c2.setRank(0);
c3.setRank(0);
// Set Distance
c1.setDistance(0.1);
c2.setDistance(0.5);
c3.setDistance(0.4);
List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
population.add(c1);
population.add(c2);
population.add(c3);
CrowdingComparator cc = new CrowdingComparator(false);
Collections.sort(population, cc);
// assert by Rank
Assert.assertTrue(population.get(0).getRank() == 0);
Assert.assertTrue(population.get(1).getRank() == 0);
Assert.assertTrue(population.get(2).getRank() == 1);
// assert by Distance
Assert.assertTrue(population.get(0).getDistance() == 0.5);
Assert.assertTrue(population.get(1).getDistance() == 0.4);
Assert.assertTrue(population.get(2).getDistance() == 0.1);
}
use of org.evosuite.ga.NSGAChromosome in project evosuite by EvoSuite.
the class TestSortByFitness method testSortByFitnessC2win.
@Test
public void testSortByFitnessC2win() {
Problem p = new Booths<NSGAChromosome>();
List<FitnessFunction<NSGAChromosome>> fitnessFunctions = p.getFitnessFunctions();
FitnessFunction<NSGAChromosome> ff = fitnessFunctions.get(0);
NSGAChromosome c1 = new NSGAChromosome();
NSGAChromosome c2 = new NSGAChromosome();
// Set Fitness
c1.setFitness(ff, 0.3);
c2.setFitness(ff, 0.7);
List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
population.add(c1);
population.add(c2);
SortByFitness sf = new SortByFitness(ff, true);
Collections.sort(population, sf);
Assert.assertTrue(population.get(0) == c2);
Assert.assertEquals(population.get(0).getFitness(ff), 0.7, 0.0);
Assert.assertEquals(population.get(1).getFitness(ff), 0.3, 0.0);
Assert.assertTrue(population.get(1) == c1);
}
use of org.evosuite.ga.NSGAChromosome in project evosuite by EvoSuite.
the class TestSortByFitness method testSortByFitnessEqual.
@Test
public void testSortByFitnessEqual() {
Problem p = new Booths<NSGAChromosome>();
List<FitnessFunction<NSGAChromosome>> fitnessFunctions = p.getFitnessFunctions();
FitnessFunction<NSGAChromosome> ff = fitnessFunctions.get(0);
NSGAChromosome c1 = new NSGAChromosome();
NSGAChromosome c2 = new NSGAChromosome();
// Set Fitness
c1.setFitness(ff, 0.5);
c2.setFitness(ff, 0.5);
List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
population.add(c1);
population.add(c2);
SortByFitness sf = new SortByFitness(ff, true);
Collections.sort(population, sf);
Assert.assertTrue(population.get(0) == c1);
Assert.assertEquals(population.get(0).getFitness(ff), 0.5, 0.0);
Assert.assertEquals(population.get(1).getFitness(ff), 0.5, 0.0);
Assert.assertTrue(population.get(1) == c2);
}
use of org.evosuite.ga.NSGAChromosome in project evosuite by EvoSuite.
the class TestSortByFitness method testSortByFitnessC1win.
@Test
public void testSortByFitnessC1win() {
Problem p = new Booths<NSGAChromosome>();
List<FitnessFunction<NSGAChromosome>> fitnessFunctions = p.getFitnessFunctions();
FitnessFunction<NSGAChromosome> ff = fitnessFunctions.get(0);
NSGAChromosome c1 = new NSGAChromosome();
NSGAChromosome c2 = new NSGAChromosome();
// Set Fitness
c1.setFitness(ff, 0.7);
c2.setFitness(ff, 0.3);
List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
population.add(c1);
population.add(c2);
SortByFitness sf = new SortByFitness(ff, true);
Collections.sort(population, sf);
Assert.assertTrue(population.get(0) == c1);
Assert.assertEquals(population.get(0).getFitness(ff), 0.7, 0.0);
Assert.assertTrue(population.get(1) == c2);
Assert.assertEquals(population.get(1).getFitness(ff), 0.3, 0.0);
}
use of org.evosuite.ga.NSGAChromosome 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);
}
Aggregations