use of org.evosuite.ga.problems.Problem 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.problems.Problem 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.problems.Problem 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);
}
use of org.evosuite.ga.problems.Problem in project evosuite by EvoSuite.
the class ZDT1IntTest method testZDT1.
/**
* Testing NSGA-II with ZDT1 Problem
*
* @throws IOException
* @throws NumberFormatException
*/
@Test
public void testZDT1() throws NumberFormatException, IOException {
Properties.MUTATION_RATE = 1d / 30d;
ChromosomeFactory<?> factory = new RandomFactory(false, 30, 0.0, 1.0);
GeneticAlgorithm<?> ga = new NSGAII(factory);
BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison();
ts.setMaximize(false);
ga.setSelectionFunction(ts);
ga.setCrossOverFunction(new SBXCrossover());
Problem p = new ZDT1();
final FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
final FitnessFunction f2 = (FitnessFunction) p.getFitnessFunctions().get(1);
ga.addFitnessFunction(f1);
ga.addFitnessFunction(f2);
// 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));
}
});
double[][] front = new double[Properties.POPULATION][2];
int index = 0;
for (Chromosome chromosome : chromosomes) {
System.out.printf("%f,%f\n", chromosome.getFitness(f1), chromosome.getFitness(f2));
front[index][0] = Double.valueOf(chromosome.getFitness(f1));
front[index][1] = Double.valueOf(chromosome.getFitness(f2));
index++;
}
// load True Pareto Front
double[][] trueParetoFront = Metrics.readFront("ZDT1.pf");
GenerationalDistance gd = new GenerationalDistance();
double gdd = gd.evaluate(front, trueParetoFront);
System.out.println("GenerationalDistance: " + gdd);
Assert.assertEquals(gdd, 0.001, 0.001);
Spacing sp = new Spacing();
double spd = sp.evaluate(front);
double spdt = sp.evaluate(trueParetoFront);
System.out.println("SpacingFront (" + spd + ") - SpacingTrueFront (" + spdt + ") = " + Math.abs(spd - spdt));
Assert.assertEquals(Math.abs(spd - spdt), 0.10, 0.10);
}
use of org.evosuite.ga.problems.Problem 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);
}
Aggregations