Search in sources :

Example 31 with Chromosome

use of org.evosuite.ga.Chromosome 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));
    }
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) Chromosome(org.evosuite.ga.Chromosome) NSGAChromosome(org.evosuite.ga.NSGAChromosome) FitnessFunction(org.evosuite.ga.FitnessFunction) RandomFactory(org.evosuite.ga.metaheuristics.RandomFactory) NSGAII(org.evosuite.ga.metaheuristics.NSGAII) BinaryTournamentSelectionCrowdedComparison(org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison) Problem(org.evosuite.ga.problems.Problem) List(java.util.List) DoubleVariable(org.evosuite.ga.variables.DoubleVariable) SBXCrossover(org.evosuite.ga.operators.crossover.SBXCrossover) Test(org.junit.Test)

Example 32 with Chromosome

use of org.evosuite.ga.Chromosome in project evosuite by EvoSuite.

the class StatementsPopulationLimit method isPopulationFull.

/* (non-Javadoc)
	 * @see org.evosuite.ga.PopulationLimit#isPopulationFull(java.util.List)
	 */
/**
 * {@inheritDoc}
 */
@Override
public boolean isPopulationFull(List<? extends Chromosome> population) {
    int numStatements = 0;
    for (Chromosome chromosome : population) {
        TestSuiteChromosome suite = (TestSuiteChromosome) chromosome;
        numStatements += suite.totalLengthOfTestCases();
    }
    return numStatements >= Properties.POPULATION;
}
Also used : Chromosome(org.evosuite.ga.Chromosome)

Example 33 with Chromosome

use of org.evosuite.ga.Chromosome in project evosuite by EvoSuite.

the class RelativeSuiteLengthBloatControl method iteration.

/**
 * {@inheritDoc}
 *
 * Set current max length to max of best chromosome
 */
@Override
public void iteration(GeneticAlgorithm<?> algorithm) {
    Chromosome best = algorithm.getBestIndividual();
    if (best instanceof TestSuiteChromosome)
        current_max = ((TestSuiteChromosome) best).totalLengthOfTestCases();
    if (best instanceof TestChromosome)
        current_max = ((TestChromosome) best).size();
    best_fitness = best.getFitness();
}
Also used : TestChromosome(org.evosuite.testcase.TestChromosome) Chromosome(org.evosuite.ga.Chromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 34 with Chromosome

use of org.evosuite.ga.Chromosome in project evosuite by EvoSuite.

the class UniformCrossOver method crossOver.

/**
 * {@inheritDoc}
 */
@Override
public void crossOver(Chromosome parent1, Chromosome parent2) throws ConstructionFailedException {
    if (parent1.size() < 2 || parent2.size() < 2) {
        return;
    }
    int maxNumGenes = Math.min(parent1.size(), parent2.size());
    Chromosome t1 = parent1.clone();
    Chromosome t2 = parent2.clone();
    for (int i = 0; i < maxNumGenes; i++) {
        if (Randomness.nextDouble() <= Properties.CROSSOVER_RATE) {
            parent1.crossOver(t2, i);
            parent2.crossOver(t1, i);
        }
    }
}
Also used : Chromosome(org.evosuite.ga.Chromosome)

Example 35 with Chromosome

use of org.evosuite.ga.Chromosome in project evosuite by EvoSuite.

the class TournamentSelection method getIndex.

/**
 * {@inheritDoc}
 *
 * Perform the tournament on the population, return one index
 */
@Override
public int getIndex(List<T> population) {
    int new_num = Randomness.nextInt(population.size());
    int winner = new_num;
    int round = 0;
    while (round < Properties.TOURNAMENT_SIZE - 1) {
        new_num = Randomness.nextInt(population.size());
        Chromosome selected = population.get(new_num);
        if (maximize) {
            if (selected.getFitness() > population.get(winner).getFitness()) {
                winner = new_num;
            }
        } else {
            if (selected.getFitness() < population.get(winner).getFitness()) {
                winner = new_num;
            }
        }
        round++;
    }
    return winner;
}
Also used : Chromosome(org.evosuite.ga.Chromosome)

Aggregations

Chromosome (org.evosuite.ga.Chromosome)60 Test (org.junit.Test)41 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)32 ArrayList (java.util.ArrayList)29 Neighbourhood (org.evosuite.ga.Neighbourhood)20 FitnessFunction (org.evosuite.ga.FitnessFunction)15 List (java.util.List)13 SBXCrossover (org.evosuite.ga.operators.crossover.SBXCrossover)13 BinaryTournamentSelectionCrowdedComparison (org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison)13 Problem (org.evosuite.ga.problems.Problem)13 NSGAChromosome (org.evosuite.ga.NSGAChromosome)12 NSGAII (org.evosuite.ga.metaheuristics.NSGAII)12 RandomFactory (org.evosuite.ga.metaheuristics.RandomFactory)12 GenerationalDistance (org.evosuite.ga.problems.metrics.GenerationalDistance)9 Spacing (org.evosuite.ga.problems.metrics.Spacing)9 TestChromosome (org.evosuite.testcase.TestChromosome)6 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)5 LineCoverageSuiteFitness (org.evosuite.coverage.line.LineCoverageSuiteFitness)5 DoubleVariable (org.evosuite.ga.variables.DoubleVariable)4 EvoSuite (org.evosuite.EvoSuite)3