use of org.evosuite.ga.variables.DoubleVariable in project evosuite by EvoSuite.
the class TestBooths method testBooths.
/**
* Testing NSGA-II with Booths Problem
*
* @throws IOException
* @throws NumberFormatException
*/
@Test
public void testBooths() throws NumberFormatException, IOException {
Properties.MUTATION_RATE = 1d / 2d;
ChromosomeFactory<?> factory = new RandomFactory(false, 2, -10.0, 10.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 Booths();
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.000, 0.001);
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));
}
}
use of org.evosuite.ga.variables.DoubleVariable in project evosuite by EvoSuite.
the class SBXCrossover method crossOver.
@Override
public void crossOver(Chromosome p1, Chromosome p2) throws ConstructionFailedException {
NSGAChromosome n1 = (NSGAChromosome) p1;
NSGAChromosome n2 = (NSGAChromosome) p2;
for (int i = 0; i < n1.getNumberOfVariables(); i++) {
Variable v1 = n1.getVariable(i);
Variable v2 = n2.getVariable(i);
if ((v1 instanceof DoubleVariable) && (v2 instanceof DoubleVariable))
this.doCrossover((DoubleVariable) v1, (DoubleVariable) v2);
}
}
use of org.evosuite.ga.variables.DoubleVariable in project evosuite by EvoSuite.
the class TestThreeHump method testThreeHump.
/**
* Testing NSGA-II with ThreeHump Problem
*
* @throws IOException
* @throws NumberFormatException
*/
@Test
public void testThreeHump() throws NumberFormatException, IOException {
Properties.MUTATION_RATE = 1d / 2d;
ChromosomeFactory<?> factory = new RandomFactory(false, 2, -5.0, 5.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 ThreeHump();
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.000, 0.001);
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));
}
}
use of org.evosuite.ga.variables.DoubleVariable 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);
}
use of org.evosuite.ga.variables.DoubleVariable 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);
}
Aggregations