Search in sources :

Example 21 with Chromosome

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

the class SinglePointRelativeCrossOver method crossOver.

/**
 * {@inheritDoc}
 *
 * The splitting point is not an absolute value but a relative value (eg, at
 * position 70% of n). For example, if n1=10 and n2=20 and splitting point
 * is 70%, we would have position 7 in the first and 14 in the second.
 * Therefore, the offspring d have n<=max(n1,n2)
 */
@Override
public void crossOver(Chromosome parent1, Chromosome parent2) throws ConstructionFailedException {
    if (parent1.size() < 2 || parent2.size() < 2) {
        return;
    }
    Chromosome t1 = parent1.clone();
    Chromosome t2 = parent2.clone();
    // Choose a position in the middle
    float splitPoint = Randomness.nextFloat();
    int pos1 = ((int) Math.floor((t1.size() - 1) * splitPoint)) + 1;
    int pos2 = ((int) Math.floor((t2.size() - 1) * splitPoint)) + 1;
    parent1.crossOver(t2, pos1, pos2);
    parent2.crossOver(t1, pos2, pos1);
}
Also used : Chromosome(org.evosuite.ga.Chromosome)

Example 22 with Chromosome

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

the class GeneticAlgorithm method applyLocalSearch.

/**
 * Apply local search, starting from the best individual and continue
 * applying it to all individuals until the local search budget is used up.
 *
 * The population list is re-ordered if needed.
 */
protected void applyLocalSearch() {
    if (!shouldApplyLocalSearch())
        return;
    logger.debug("Applying local search");
    LocalSearchBudget.getInstance().localSearchStarted();
    boolean improvement = false;
    for (Chromosome individual : population) {
        if (isFinished())
            break;
        if (LocalSearchBudget.getInstance().isFinished()) {
            logger.debug("Local search budget used up, exiting local search");
            break;
        }
        if (individual.localSearch(localObjective)) {
            improvement = true;
        }
    }
    if (improvement) {
        DSEStats.getInstance().reportNewIncrease();
        updateProbability(true);
        logger.debug("Increasing probability of applying LS to " + localSearchProbability);
    } else {
        DSEStats.getInstance().reportNewDecrease();
        updateProbability(false);
        logger.debug("Decreasing probability of applying LS to " + localSearchProbability);
    }
    if (improvement) {
        // list not sorted any more.
        if (!populationIsSorted()) {
            this.sortPopulation();
        }
    }
}
Also used : Chromosome(org.evosuite.ga.Chromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome)

Example 23 with Chromosome

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

the class GeneticAlgorithm method toString.

/**
 * @return
 */
@Override
public String toString() {
    StringBuilder str = new StringBuilder();
    int i = 0;
    for (Chromosome c : population) {
        str.append("\n  - test " + i);
        for (FitnessFunction<T> ff : this.fitnessFunctions) {
            DecimalFormat df = new DecimalFormat("#.#####");
            str.append(", " + ff.getClass().getSimpleName().replace("CoverageSuiteFitness", "") + " " + df.format(c.getFitness(ff)));
        }
        i++;
    }
    return str.toString();
}
Also used : DecimalFormat(java.text.DecimalFormat) Chromosome(org.evosuite.ga.Chromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome)

Example 24 with Chromosome

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

the class SinglePointFixedCrossOver method crossOver.

/**
 * {@inheritDoc}
 *
 * The splitting point for to individuals p1, p2 is selected within
 * min(length(p1),length(p2))
 */
@Override
public void crossOver(Chromosome parent1, Chromosome parent2) throws ConstructionFailedException {
    if (parent1.size() < 2 || parent2.size() < 2) {
        return;
    }
    int point = Randomness.nextInt(Math.min(parent1.size(), parent2.size()) - 1) + 1;
    Chromosome t1 = parent1.clone();
    Chromosome t2 = parent2.clone();
    parent1.crossOver(t2, point, point);
    parent2.crossOver(t1, point, point);
}
Also used : Chromosome(org.evosuite.ga.Chromosome)

Example 25 with Chromosome

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

the class TestSuiteLocalSearch method applyAVM.

/**
 * Applies AVM on the test case in the suite
 *
 * @param suite
 * @param testIndex
 * @param test
 * @param localSearchObjective
 * @return
 */
private boolean applyAVM(TestSuiteChromosome suite, int testIndex, TestChromosome test, LocalSearchObjective<TestSuiteChromosome> objective) {
    logger.debug("Local search on test " + testIndex + ", current fitness: " + suite.getFitness());
    final List<FitnessFunction<? extends Chromosome>> fitnessFunctions = objective.getFitnessFunctions();
    TestSuiteLocalSearchObjective testCaseLocalSearchObjective = TestSuiteLocalSearchObjective.buildNewTestSuiteLocalSearchObjective(fitnessFunctions, suite, testIndex);
    AVMTestCaseLocalSearch testCaselocalSearch = new AVMTestCaseLocalSearch();
    boolean improved = testCaselocalSearch.doSearch(test, testCaseLocalSearchObjective);
    return improved;
}
Also used : Chromosome(org.evosuite.ga.Chromosome) TestChromosome(org.evosuite.testcase.TestChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) AVMTestCaseLocalSearch(org.evosuite.testcase.localsearch.AVMTestCaseLocalSearch) FitnessFunction(org.evosuite.ga.FitnessFunction) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction)

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