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);
}
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);
}
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);
}
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);
}
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));
}
}
Aggregations