Search in sources :

Example 1 with CrowdingComparator

use of org.evosuite.ga.comparators.CrowdingComparator in project evosuite by EvoSuite.

the class NSGAII method evolve.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
protected void evolve() {
    // Create the offSpring population
    List<T> offspringPopulation = new ArrayList<T>(population.size());
    // create a offspring population Qt of size N
    for (int i = 0; i < (population.size() / 2); i++) {
        // Selection
        T parent1 = selectionFunction.select(population);
        T parent2 = selectionFunction.select(population);
        // Crossover
        T offspring1 = (T) parent1.clone();
        T offspring2 = (T) parent2.clone();
        try {
            if (Randomness.nextDouble() <= Properties.CROSSOVER_RATE)
                crossoverFunction.crossOver(offspring1, offspring2);
        } catch (Exception e) {
            logger.info("CrossOver failed");
        }
        // Mutation
        if (Randomness.nextDouble() <= Properties.MUTATION_RATE) {
            notifyMutation(offspring1);
            offspring1.mutate();
            notifyMutation(offspring2);
            offspring2.mutate();
        }
        // Evaluate
        for (final FitnessFunction<T> ff : this.getFitnessFunctions()) {
            ff.getFitness(offspring1);
            notifyEvaluation(offspring1);
            ff.getFitness(offspring2);
            notifyEvaluation(offspring2);
        }
        offspringPopulation.add(offspring1);
        offspringPopulation.add(offspring2);
    }
    // Create the population union of Population and offSpring
    List<T> union = union(population, offspringPopulation);
    // Ranking the union
    List<List<T>> ranking = fastNonDominatedSort(union);
    int remain = population.size();
    int index = 0;
    List<T> front = null;
    population.clear();
    // Obtain the next front
    front = ranking.get(index);
    while ((remain > 0) && (remain >= front.size())) {
        // Assign crowding distance to individuals
        crowingDistanceAssignment(front);
        // Add the individuals of this front
        for (int k = 0; k < front.size(); k++) population.add(front.get(k));
        // Decrement remain
        remain = remain - front.size();
        // Obtain the next front
        index++;
        if (remain > 0)
            front = ranking.get(index);
    }
    // Remain is less than front(index).size, insert only the best one
    if (remain > 0) {
        // front contains individuals to insert
        crowingDistanceAssignment(front);
        Collections.sort(front, new CrowdingComparator(true));
        for (int k = 0; k < remain; k++) population.add(front.get(k));
        remain = 0;
    }
    // archive // TODO does it make any sense to use an archive with NSGA-II?
    /*updateFitnessFunctionsAndValues();
		for (T t : population) {
			if(t.isToBeUpdated()){
			    for (FitnessFunction<T> fitnessFunction : fitnessFunctions) {
					fitnessFunction.getFitness(t);
				}
			    t.isToBeUpdated(false);
			}
		}*/
    // 
    currentIteration++;
}
Also used : ArrayList(java.util.ArrayList) CrowdingComparator(org.evosuite.ga.comparators.CrowdingComparator) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 2 with CrowdingComparator

use of org.evosuite.ga.comparators.CrowdingComparator in project evosuite by EvoSuite.

the class NSGAIISystemTest method testCrowingDistanceAssignment_SeveralVariables.

@Test
public void testCrowingDistanceAssignment_SeveralVariables() {
    NSGAII<NSGAChromosome> ga = new NSGAII<NSGAChromosome>(null);
    Problem p = new SCH();
    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 1
    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);
    // Set Fitness 2
    c1.setFitness(fitnessFunctions.get(1), 0.1);
    c2.setFitness(fitnessFunctions.get(1), 0.3);
    c3.setFitness(fitnessFunctions.get(1), 0.5);
    c4.setFitness(fitnessFunctions.get(1), 0.7);
    c5.setFitness(fitnessFunctions.get(1), 0.9);
    c6.setFitness(fitnessFunctions.get(1), 0.1);
    c7.setFitness(fitnessFunctions.get(1), 0.3);
    c8.setFitness(fitnessFunctions.get(1), 0.5);
    c9.setFitness(fitnessFunctions.get(1), 0.7);
    c10.setFitness(fitnessFunctions.get(1), 0.9);
    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.5 - population.get(2).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.5 - population.get(3).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.5 - population.get(4).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.5 - population.get(5).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.5 - population.get(6).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.5 - population.get(7).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.5 - population.get(8).getDistance()) < epsilon);
    Assert.assertTrue(Math.abs(0.5 - population.get(9).getDistance()) < epsilon);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) ArrayList(java.util.ArrayList) CrowdingComparator(org.evosuite.ga.comparators.CrowdingComparator) SCH(org.evosuite.ga.problems.multiobjective.SCH) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) Test(org.junit.Test)

Example 3 with CrowdingComparator

use of org.evosuite.ga.comparators.CrowdingComparator 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);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) ArrayList(java.util.ArrayList) CrowdingComparator(org.evosuite.ga.comparators.CrowdingComparator) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) Booths(org.evosuite.ga.problems.singleobjective.Booths) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)3 CrowdingComparator (org.evosuite.ga.comparators.CrowdingComparator)3 FitnessFunction (org.evosuite.ga.FitnessFunction)2 NSGAChromosome (org.evosuite.ga.NSGAChromosome)2 Problem (org.evosuite.ga.problems.Problem)2 Test (org.junit.Test)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 SCH (org.evosuite.ga.problems.multiobjective.SCH)1 Booths (org.evosuite.ga.problems.singleobjective.Booths)1