use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.
the class SCH2IntTest method testSCH2.
/**
* Testing NSGA-II with SCH2 Problem
*
* @throws IOException
* @throws NumberFormatException
*/
@Test
public void testSCH2() throws NumberFormatException, IOException {
Properties.MUTATION_RATE = 1d / 1d;
ChromosomeFactory<?> factory = new RandomFactory(false, 1, -5.0, 10.0);
GeneticAlgorithm<?> ga = new NSGAII(factory);
BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison();
ts.setMaximize(false);
ga.setSelectionFunction(ts);
ga.setCrossOverFunction(new SBXCrossover());
Problem p = new SCH2();
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("Schaffer2.pf");
GenerationalDistance gd = new GenerationalDistance();
double gdd = gd.evaluate(front, trueParetoFront);
System.out.println("GenerationalDistance: " + gdd);
Assert.assertEquals(gdd, 0.0004, 0.0001);
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.05, 0.05);
}
use of org.evosuite.ga.FitnessFunction 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.FitnessFunction in project evosuite by EvoSuite.
the class NullReferenceSearch method doSearch.
/* (non-Javadoc)
* @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
*/
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
NullStatement nullStatement = (NullStatement) test.getTestCase().getStatement(statement);
TestCase newTest = test.getTestCase();
TestCase oldTest = newTest.clone();
ExecutionResult oldResult = test.getLastExecutionResult();
// double oldFitness = test.getFitness();
Map<FitnessFunction<?>, Double> oldFitnesses = test.getFitnessValues();
Map<FitnessFunction<?>, Double> oldLastFitnesses = test.getPreviousFitnessValues();
try {
TestFactory.getInstance().attemptGeneration(newTest, nullStatement.getReturnType(), statement);
if (!objective.hasImproved(test)) {
test.setTestCase(oldTest);
test.setLastExecutionResult(oldResult);
// test.setFitness(oldFitness);
test.setFitnessValues(oldFitnesses);
test.setPreviousFitnessValues(oldLastFitnesses);
} else {
return true;
}
} catch (ConstructionFailedException e) {
// If we can't construct it, then ignore
}
return false;
}
use of org.evosuite.ga.FitnessFunction 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.FitnessFunction 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);
}
Aggregations