Search in sources :

Example 1 with SortByFitness

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

the class LIPS method evolve.

@SuppressWarnings("unchecked")
@Override
protected void evolve() {
    List<T> newGeneration = new ArrayList<T>();
    // Elitism. It is not specified in original paper [1].
    // However, we assume that LIPS uses elitism given the fact the
    // elitism has been shown to positively affect the convergence
    // speed of GAs in various optimisation problems
    Collections.sort(population, new SortByFitness(this.currentTarget, false));
    newGeneration.add((T) population.get(0).clone());
    newGeneration.add((T) population.get(1).clone());
    // new_generation.size() < population_size
    while (newGeneration.size() < Properties.POPULATION) {
        T parent1 = selectionFunction.select(population);
        T parent2 = selectionFunction.select(population);
        T offspring1 = (T) parent1.clone();
        T offspring2 = (T) parent2.clone();
        try {
            if (Randomness.nextDouble() <= Properties.CROSSOVER_RATE) {
                crossoverFunction.crossOver(offspring1, offspring2);
            }
            notifyMutation(offspring1);
            offspring1.mutate();
            newGeneration.add(offspring1);
            notifyMutation(offspring2);
            offspring2.mutate();
            newGeneration.add(offspring2);
            if (offspring1.isChanged()) {
                offspring1.updateAge(currentIteration);
            }
            if (offspring2.isChanged()) {
                offspring2.updateAge(currentIteration);
            }
        } catch (ConstructionFailedException e) {
            logger.info("CrossOver/Mutation failed.");
            continue;
        }
    }
    population.clear();
    population = newGeneration;
    // calculate fitness for all test cases in the current (new) population
    calculateFitness();
}
Also used : ArrayList(java.util.ArrayList) SortByFitness(org.evosuite.ga.comparators.SortByFitness) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 2 with SortByFitness

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

the class NSGAII method crowingDistanceAssignment.

protected void crowingDistanceAssignment(List<T> f) {
    int size = f.size();
    if (size == 0)
        return;
    if (size == 1) {
        f.get(0).setDistance(Double.POSITIVE_INFINITY);
        return;
    }
    if (size == 2) {
        f.get(0).setDistance(Double.POSITIVE_INFINITY);
        f.get(1).setDistance(Double.POSITIVE_INFINITY);
        return;
    }
    // use a new Population List to avoid altering the original Population
    List<T> front = new ArrayList<T>(size);
    front.addAll(f);
    for (int i = 0; i < size; i++) front.get(i).setDistance(0.0);
    double objetiveMaxn;
    double objetiveMinn;
    double distance;
    for (final FitnessFunction<?> ff : this.getFitnessFunctions()) {
        // Sort the population by Fit n
        Collections.sort(front, new SortByFitness(ff, true));
        objetiveMinn = front.get(0).getFitness(ff);
        objetiveMaxn = front.get(front.size() - 1).getFitness(ff);
        // set crowding distance
        front.get(0).setDistance(Double.POSITIVE_INFINITY);
        front.get(size - 1).setDistance(Double.POSITIVE_INFINITY);
        for (int j = 1; j < size - 1; j++) {
            distance = front.get(j + 1).getFitness(ff) - front.get(j - 1).getFitness(ff);
            distance = distance / (objetiveMaxn - objetiveMinn);
            distance += front.get(j).getDistance();
            front.get(j).setDistance(distance);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SortByFitness(org.evosuite.ga.comparators.SortByFitness)

Example 3 with SortByFitness

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

the class CrowdingDistance method crowdingDistanceAssignment.

/**
 * Method used to assign the 'traditional' Crowding Distance
 * @param front front of non-dominated solutions/tests
 * @param set set of goals/targets (e.g., branches) to consider
 */
protected void crowdingDistanceAssignment(List<T> front, Set<FitnessFunction<T>> set) {
    int size = front.size();
    if (size == 0)
        return;
    if (size == 1) {
        front.get(0).setDistance(Double.POSITIVE_INFINITY);
        return;
    }
    if (size == 2) {
        front.get(0).setDistance(Double.POSITIVE_INFINITY);
        front.get(1).setDistance(Double.POSITIVE_INFINITY);
        return;
    }
    for (int i = 0; i < size; i++) front.get(i).setDistance(0.0);
    double objetiveMaxn;
    double objetiveMinn;
    double distance;
    for (final FitnessFunction<?> ff : set) {
        // Sort the population by Fit n
        Collections.sort(front, new SortByFitness(ff, false));
        objetiveMinn = front.get(0).getFitness(ff);
        objetiveMaxn = front.get(front.size() - 1).getFitness(ff);
        // set crowding distance
        front.get(0).setDistance(Double.POSITIVE_INFINITY);
        front.get(size - 1).setDistance(Double.POSITIVE_INFINITY);
        for (int j = 1; j < size - 1; j++) {
            distance = front.get(j + 1).getFitness(ff) - front.get(j - 1).getFitness(ff);
            distance = distance / (objetiveMaxn - objetiveMinn);
            distance += front.get(j).getDistance();
            front.get(j).setDistance(distance);
        }
    }
}
Also used : SortByFitness(org.evosuite.ga.comparators.SortByFitness)

Example 4 with SortByFitness

use of org.evosuite.ga.comparators.SortByFitness 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());
    }
}
Also used : EvoSuite(org.evosuite.EvoSuite) ArrayList(java.util.ArrayList) Chromosome(org.evosuite.ga.Chromosome) NSGAChromosome(org.evosuite.ga.NSGAChromosome) SortByFitness(org.evosuite.ga.comparators.SortByFitness) FitnessFunction(org.evosuite.ga.FitnessFunction) Test(org.junit.Test)

Aggregations

SortByFitness (org.evosuite.ga.comparators.SortByFitness)4 ArrayList (java.util.ArrayList)3 EvoSuite (org.evosuite.EvoSuite)1 Chromosome (org.evosuite.ga.Chromosome)1 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)1 FitnessFunction (org.evosuite.ga.FitnessFunction)1 NSGAChromosome (org.evosuite.ga.NSGAChromosome)1 Test (org.junit.Test)1