use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.
the class NSGAIISystemTest method testIntegration.
@Test
public void testIntegration() {
Properties.MUTATION_RATE = 1d / 1d;
Properties.CRITERION = new Criterion[2];
Properties.CRITERION[0] = Criterion.RHO;
Properties.CRITERION[1] = Criterion.AMBIGUITY;
Properties.ALGORITHM = Algorithm.NSGAII;
EvoSuite evosuite = new EvoSuite();
String targetClass = Calculator.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
String[] command = new String[] { "-Dselection_function=BINARY_TOURNAMENT", "-Dminimize_values=false", "-Dinline=false", "-Dminimize=true", "-Dstop_zero=false", "-Dcoverage=false", "-Djunit_tests=true", "-Dassertions=true", "-Dsandbox=true", "-Dnew_statistics=false", "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
Assert.assertNotNull(result);
GeneticAlgorithm<?> ga = getGAFromResult(result);
final FitnessFunction rho = ga.getFitnessFunctions().get(0);
final FitnessFunction ag = ga.getFitnessFunctions().get(1);
List<Chromosome> population = new ArrayList<Chromosome>(ga.getBestIndividuals());
Collections.sort(population, new SortByFitness(rho, false));
for (Chromosome p : population) {
System.out.println("Rho: " + p.getFitness(rho) + ", AG: " + p.getFitness(ag) + " | Rank: " + p.getRank());
Assert.assertEquals(0.0, p.getFitness(rho), 0.0);
Assert.assertEquals(0.0, p.getFitness(ag), 0.0);
Assert.assertEquals(0, p.getRank());
}
}
use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.
the class NSGAIISystemTest method testCrowingDistanceAssignment_OneVariable.
@Test
public void testCrowingDistanceAssignment_OneVariable() {
NSGAII<NSGAChromosome> ga = new NSGAII<NSGAChromosome>(null);
Problem p = new Booths();
List<FitnessFunction<NSGAChromosome>> fitnessFunctions = p.getFitnessFunctions();
ga.addFitnessFunctions(fitnessFunctions);
NSGAChromosome c1 = new NSGAChromosome();
NSGAChromosome c2 = new NSGAChromosome();
NSGAChromosome c3 = new NSGAChromosome();
NSGAChromosome c4 = new NSGAChromosome();
NSGAChromosome c5 = new NSGAChromosome();
NSGAChromosome c6 = new NSGAChromosome();
NSGAChromosome c7 = new NSGAChromosome();
NSGAChromosome c8 = new NSGAChromosome();
NSGAChromosome c9 = new NSGAChromosome();
NSGAChromosome c10 = new NSGAChromosome();
// Set Fitness
c1.setFitness(fitnessFunctions.get(0), 0.0);
c2.setFitness(fitnessFunctions.get(0), 0.2);
c3.setFitness(fitnessFunctions.get(0), 0.4);
c4.setFitness(fitnessFunctions.get(0), 0.6);
c5.setFitness(fitnessFunctions.get(0), 0.8);
c6.setFitness(fitnessFunctions.get(0), 0.0);
c7.setFitness(fitnessFunctions.get(0), 0.2);
c8.setFitness(fitnessFunctions.get(0), 0.4);
c9.setFitness(fitnessFunctions.get(0), 0.6);
c10.setFitness(fitnessFunctions.get(0), 0.8);
List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
population.add(c1);
population.add(c2);
population.add(c3);
population.add(c4);
population.add(c5);
population.add(c6);
population.add(c7);
population.add(c8);
population.add(c9);
population.add(c10);
ga.crowingDistanceAssignment(population);
Collections.sort(population, new CrowdingComparator(true));
Assert.assertTrue(population.get(0).getDistance() == Double.POSITIVE_INFINITY);
Assert.assertTrue(population.get(1).getDistance() == Double.POSITIVE_INFINITY);
double epsilon = 1e-10;
Assert.assertTrue(Math.abs(0.25 - population.get(2).getDistance()) < epsilon);
Assert.assertTrue(Math.abs(0.25 - population.get(3).getDistance()) < epsilon);
Assert.assertTrue(Math.abs(0.25 - population.get(4).getDistance()) < epsilon);
Assert.assertTrue(Math.abs(0.25 - population.get(5).getDistance()) < epsilon);
Assert.assertTrue(Math.abs(0.25 - population.get(6).getDistance()) < epsilon);
Assert.assertTrue(Math.abs(0.25 - population.get(7).getDistance()) < epsilon);
Assert.assertTrue(Math.abs(0.25 - population.get(8).getDistance()) < epsilon);
Assert.assertTrue(Math.abs(0.25 - population.get(9).getDistance()) < epsilon);
}
use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.
the class TestSuiteMinimizerSystemTest method testWithTwo.
@SuppressWarnings("rawtypes")
@Test
public void testWithTwo() {
Properties.CRITERION = new Criterion[2];
Properties.CRITERION[0] = Criterion.ONLYBRANCH;
Properties.CRITERION[1] = Criterion.LINE;
Properties.MINIMIZE_VALUES = true;
EvoSuite evosuite = new EvoSuite();
String targetClass = MethodReturnsPrimitive.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
String[] command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
Assert.assertNotNull(result);
GeneticAlgorithm<?> ga = getGAFromResult(result);
TestSuiteChromosome c = (TestSuiteChromosome) ga.getBestIndividual();
final FitnessFunction onlybranch = ga.getFitnessFunctions().get(0);
final FitnessFunction line = ga.getFitnessFunctions().get(1);
Assert.assertEquals(0.0, c.getFitness(onlybranch), 0.0);
Assert.assertEquals(0.0, c.getFitness(line), 0.0);
Assert.assertEquals(1.0, c.getCoverage(onlybranch), 0.0);
Assert.assertEquals(1.0, c.getCoverage(line), 0.0);
Assert.assertEquals(6.0, c.getNumOfCoveredGoals(onlybranch), 0.0);
Assert.assertEquals(10.0, c.getNumOfCoveredGoals(line), 0.0);
}
use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.
the class NoveltyFitnessEvaluationListener method iteration.
@Override
public void iteration(GeneticAlgorithm<?> algorithm) {
List<TestChromosome> population = ((GeneticAlgorithm<TestChromosome>) algorithm).getPopulation();
TestSuiteChromosome suite = createMergedSolution(population);
for (FitnessFunction fitnessFunction : fitnessFunctions) {
fitnessFunction.getFitness(suite);
}
// Update fitness functions based on goals just added to archive
algorithm.updateFitnessFunctionsAndValues();
}
use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.
the class SPEA2Test method testZDT4.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testZDT4() throws IOException {
Properties.MUTATION_RATE = 1d / 10d;
Properties.POPULATION = 100;
Properties.SEARCH_BUDGET = 250;
Properties.STOPPING_CONDITION = StoppingCondition.MAXGENERATIONS;
Properties.CROSSOVER_RATE = 0.9;
Properties.RANDOM_SEED = 1l;
ChromosomeFactory<?> factory = new RandomFactory(true, 10, -5.0, 5.0);
GeneticAlgorithm<?> ga = new SPEA2(factory);
BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison();
ts.setMaximize(false);
ga.setSelectionFunction(ts);
ga.setCrossOverFunction(new SBXCrossover());
Problem p = new ZDT4();
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];
for (int i = 0; i < chromosomes.size(); i++) {
Chromosome chromosome = chromosomes.get(i);
System.out.printf("%f,%f\n", chromosome.getFitness(f1), chromosome.getFitness(f2));
front[i][0] = Double.valueOf(chromosome.getFitness(f1));
front[i][1] = Double.valueOf(chromosome.getFitness(f2));
}
// load True Pareto Front
double[][] trueParetoFront = Metrics.readFront("ZDT4.pf");
GenerationalDistance gd = new GenerationalDistance();
double gdd = gd.evaluate(front, trueParetoFront);
System.out.println("GenerationalDistance: " + gdd);
assertEquals(0.00, gdd, 0.01);
Spacing sp = new Spacing();
double spd = sp.evaluate(front);
System.out.println("SpacingFront: " + spd);
assertEquals(0.71, spd, 0.01);
}
Aggregations