Search in sources :

Example 1 with StoppingCondition

use of org.evosuite.ga.stoppingconditions.StoppingCondition in project evosuite by EvoSuite.

the class PropertiesTestGAFactory method getSearchAlgorithm.

@Override
public GeneticAlgorithm<TestChromosome> getSearchAlgorithm() {
    ChromosomeFactory<TestChromosome> factory = getChromosomeFactory();
    // FIXXME
    GeneticAlgorithm<TestChromosome> ga = getGeneticAlgorithm(factory);
    if (Properties.NEW_STATISTICS)
        ga.addListener(new org.evosuite.statistics.StatisticsListener());
    // How to select candidates for reproduction
    SelectionFunction<TestChromosome> selection_function = getSelectionFunction();
    selection_function.setMaximize(false);
    ga.setSelectionFunction(selection_function);
    // When to stop the search
    StoppingCondition stopping_condition = getStoppingCondition();
    ga.setStoppingCondition(stopping_condition);
    // ga.addListener(stopping_condition);
    if (Properties.STOP_ZERO) {
        ga.addStoppingCondition(new ZeroFitnessStoppingCondition());
    }
    if (!(stopping_condition instanceof MaxTimeStoppingCondition)) {
        ga.addStoppingCondition(new GlobalTimeStoppingCondition());
    }
    if (ArrayUtil.contains(Properties.CRITERION, Criterion.MUTATION) || ArrayUtil.contains(Properties.CRITERION, Criterion.STRONGMUTATION)) {
        ga.addStoppingCondition(new MutationTimeoutStoppingCondition());
    }
    ga.resetStoppingConditions();
    ga.setPopulationLimit(getPopulationLimit());
    // How to cross over
    CrossOverFunction crossover_function = getCrossoverFunction();
    ga.setCrossOverFunction(crossover_function);
    if (Properties.CHECK_BEST_LENGTH) {
        org.evosuite.testcase.RelativeTestLengthBloatControl bloat_control = new org.evosuite.testcase.RelativeTestLengthBloatControl();
        ga.addBloatControl(bloat_control);
        ga.addListener(bloat_control);
    }
    TestCaseSecondaryObjective.setSecondaryObjectives();
    if (Properties.DYNAMIC_LIMIT) {
        // max_s = GAProperties.generations * getBranches().size();
        // TODO: might want to make this dependent on the selected coverage
        // criterion
        // TODO also, question: is branchMap.size() really intended here?
        // I think BranchPool.getBranchCount() was intended
        Properties.SEARCH_BUDGET = Properties.SEARCH_BUDGET * (BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getNumBranchlessMethods(Properties.TARGET_CLASS) + BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranchCountForClass(Properties.TARGET_CLASS) * 2);
        stopping_condition.setLimit(Properties.SEARCH_BUDGET);
        logger.info("Setting dynamic length limit to " + Properties.SEARCH_BUDGET);
    }
    if (Properties.RECYCLE_CHROMOSOMES) {
        if (Properties.STRATEGY == Strategy.ONEBRANCH)
            ga.addListener(TestCaseRecycler.getInstance());
    }
    // ga.addListener(new ResourceController<TestChromosome>());
    return ga;
}
Also used : ZeroFitnessStoppingCondition(org.evosuite.ga.stoppingconditions.ZeroFitnessStoppingCondition) MaxTimeStoppingCondition(org.evosuite.ga.stoppingconditions.MaxTimeStoppingCondition) CrossOverFunction(org.evosuite.ga.operators.crossover.CrossOverFunction) MutationTimeoutStoppingCondition(org.evosuite.coverage.mutation.MutationTimeoutStoppingCondition) ZeroFitnessStoppingCondition(org.evosuite.ga.stoppingconditions.ZeroFitnessStoppingCondition) StoppingCondition(org.evosuite.ga.stoppingconditions.StoppingCondition) MaxTimeStoppingCondition(org.evosuite.ga.stoppingconditions.MaxTimeStoppingCondition) GlobalTimeStoppingCondition(org.evosuite.ga.stoppingconditions.GlobalTimeStoppingCondition) MutationTimeoutStoppingCondition(org.evosuite.coverage.mutation.MutationTimeoutStoppingCondition) TestChromosome(org.evosuite.testcase.TestChromosome) GlobalTimeStoppingCondition(org.evosuite.ga.stoppingconditions.GlobalTimeStoppingCondition)

Example 2 with StoppingCondition

use of org.evosuite.ga.stoppingconditions.StoppingCondition in project evosuite by EvoSuite.

the class GeneticAlgorithm method progress.

/**
 * Returns the progress of the search.
 *
 * @return a value [0.0, 1.0]
 */
protected double progress() {
    long totalbudget = 0;
    long currentbudget = 0;
    for (StoppingCondition sc : this.stoppingConditions) {
        if (sc.getLimit() != 0) {
            totalbudget += sc.getLimit();
            currentbudget += sc.getCurrentValue();
        }
    }
    return (double) currentbudget / (double) totalbudget;
}
Also used : StoppingCondition(org.evosuite.ga.stoppingconditions.StoppingCondition) MaxGenerationStoppingCondition(org.evosuite.ga.stoppingconditions.MaxGenerationStoppingCondition)

Example 3 with StoppingCondition

use of org.evosuite.ga.stoppingconditions.StoppingCondition in project evosuite by EvoSuite.

the class ProgressMonitor method searchStarted.

/* (non-Javadoc)
	 * @see org.evosuite.ga.SearchListener#searchStarted(org.evosuite.ga.GeneticAlgorithm)
	 */
/**
 * {@inheritDoc}
 */
@Override
public void searchStarted(GeneticAlgorithm<?> algorithm) {
    for (StoppingCondition cond : algorithm.getStoppingConditions()) {
        if (// No ZeroStoppingCondition
        cond.getLimit() == 0)
            continue;
        stoppingCondition = cond;
        max = stoppingCondition.getLimit();
        break;
    }
}
Also used : StoppingCondition(org.evosuite.ga.stoppingconditions.StoppingCondition)

Example 4 with StoppingCondition

use of org.evosuite.ga.stoppingconditions.StoppingCondition in project evosuite by EvoSuite.

the class RegressionSuiteStrategy method generateRandomRegressionTests.

private TestSuiteChromosome generateRandomRegressionTests() {
    LoggingUtils.getEvoLogger().info("* Using RANDOM regression test generation");
    if (Properties.KEEP_REGRESSION_ARCHIVE) {
        Properties.TEST_ARCHIVE = true;
    }
    RegressionTestSuiteChromosome suite = new RegressionTestSuiteChromosome();
    PropertiesSuiteGAFactory algorithmFactory = new PropertiesSuiteGAFactory();
    GeneticAlgorithm<?> suiteGA = algorithmFactory.getSearchAlgorithm();
    // statistics.searchStarted(suiteGA);
    BranchCoverageSuiteFitness branchCoverageSuiteFitness = new BranchCoverageSuiteFitness(TestGenerationContext.getInstance().getClassLoaderForSUT());
    // regressionMonitor.searchStarted(suiteGA);
    RegressionTestChromosomeFactory factory = new RegressionTestChromosomeFactory();
    LoggingUtils.getEvoLogger().warn("*** generating RANDOM regression tests");
    // TODO: Shutdown hook?
    List<TestFitnessFunction> goals = getGoals(true);
    track(RuntimeVariable.Total_Goals, goals.size());
    StoppingCondition stoppingCondition = getStoppingCondition();
    // fitnessFunction.getFitness(suite);
    int totalTestCount = 0;
    int usefulTestCount = 0;
    int simulatedAge = 0;
    int numAssertions = 0;
    int executedStatemets = 0;
    boolean firstTry = true;
    // Properties.REGRESSION_RANDOM_STRATEGY:
    // 0: skip evaluation after first find, dont keep tests
    // 1: dont skip evaluation after first find, dont keep tests
    // 2: dont skip evaluation after first find, keep tests
    // 3: skip evaluation after first find, keep tests [default]
    long startTime = System.currentTimeMillis();
    while (!stoppingCondition.isFinished() || (numAssertions != 0)) {
        if (numAssertions == 0 || Properties.REGRESSION_RANDOM_STRATEGY == 1 || Properties.REGRESSION_RANDOM_STRATEGY == 2) {
            RegressionTestChromosome test = factory.getChromosome();
            RegressionTestSuiteChromosome clone = new RegressionTestSuiteChromosome();
            clone.addTest(test);
            List<TestCase> testCases = clone.getTests();
            // fitnessFunction.getFitness(clone);
            /*
         * logger.debug("Old fitness: {}, new fitness: {}",
         * suite.getFitness(), clone.getFitness());
         */
            executedStatemets += test.size();
            numAssertions = RegressionAssertionCounter.getNumAssertions(clone);
            if (Properties.KEEP_REGRESSION_ARCHIVE) {
                branchCoverageSuiteFitness.getFitness(clone.getTestSuite());
            }
            if (numAssertions > 0) {
                LoggingUtils.getEvoLogger().warn("Generated test with {} assertions.", numAssertions);
            }
            totalTestCount++;
            if (numAssertions > 0) {
                numAssertions = 0;
                // boolean compilable = JUnitAnalyzer.verifyCompilationAndExecution(testCases);
                JUnitAnalyzer.removeTestsThatDoNotCompile(testCases);
                JUnitAnalyzer.handleTestsThatAreUnstable(testCases);
                if (testCases.size() > 0) {
                    clone = new RegressionTestSuiteChromosome();
                    for (TestCase t : testCases) {
                        RegressionTestChromosome rtc = new RegressionTestChromosome();
                        if (t.isUnstable()) {
                            continue;
                        }
                        TestChromosome tc = new TestChromosome();
                        tc.setTestCase(t);
                        rtc.setTest(tc);
                        clone.addTest(rtc);
                    }
                    // test.set
                    // clone.addTest(testCases);
                    numAssertions = RegressionAssertionCounter.getNumAssertions(clone, false, false);
                    LoggingUtils.getEvoLogger().warn("Keeping {} assertions.", numAssertions);
                    if (numAssertions > 0) {
                        usefulTestCount++;
                        suite.addTest(test);
                    }
                } else {
                    LoggingUtils.getEvoLogger().warn("ignored assertions. tests were removed.");
                }
            }
        } else {
            if (numAssertions > 0) {
                break;
            }
        /*
        try {
                Thread.sleep(1000);
        } catch (InterruptedException e) {
                e.printStackTrace();
        }
        */
        }
        // regressionMonitor.iteration(suiteGA);
        if (firstTry || (System.currentTimeMillis() - startTime) >= 4000) {
            startTime = System.currentTimeMillis();
            simulatedAge++;
            firstTry = false;
        }
    }
    // regressionMonitor.searchFinished(suiteGA);
    LoggingUtils.getEvoLogger().warn("*** Random test generation finished.");
    LoggingUtils.getEvoLogger().warn("*=*=*=* Total tests: {} | Tests with assertion: {}", totalTestCount, usefulTestCount);
    // statistics.searchFinished(suiteGA);
    zero_fitness.setFinished();
    LoggingUtils.getEvoLogger().info("* Generated " + suite.size() + " tests with total length " + suite.totalLengthOfTestCases());
    goals = getGoals(false);
    track(RuntimeVariable.Total_Goals, goals.size());
    suiteGA.printBudget();
    if (!(Properties.REGRESSION_RANDOM_STRATEGY == 2 || Properties.REGRESSION_RANDOM_STRATEGY == 3)) {
        suite = new RegressionTestSuiteChromosome();
    }
    TestSuiteChromosome bestSuites = new TestSuiteChromosome();
    for (TestCase t : suite.getTests()) {
        bestSuites.addTest(t);
    }
    return bestSuites;
}
Also used : RegressionTestChromosomeFactory(org.evosuite.regression.RegressionTestChromosomeFactory) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) RegressionTestSuiteChromosome(org.evosuite.regression.RegressionTestSuiteChromosome) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) MaxStatementsStoppingCondition(org.evosuite.ga.stoppingconditions.MaxStatementsStoppingCondition) ZeroFitnessStoppingCondition(org.evosuite.ga.stoppingconditions.ZeroFitnessStoppingCondition) StoppingCondition(org.evosuite.ga.stoppingconditions.StoppingCondition) TestCase(org.evosuite.testcase.TestCase) RegressionTestChromosome(org.evosuite.regression.RegressionTestChromosome) RegressionTestSuiteChromosome(org.evosuite.regression.RegressionTestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome) RegressionTestChromosome(org.evosuite.regression.RegressionTestChromosome)

Example 5 with StoppingCondition

use of org.evosuite.ga.stoppingconditions.StoppingCondition in project evosuite by EvoSuite.

the class PropertiesNoveltySearchFactory method getSearchAlgorithm.

@Override
public // public GeneticAlgorithm<TestChromosome> getSearchAlgorithm() {
NoveltySearch<TestChromosome> getSearchAlgorithm() {
    ChromosomeFactory<TestChromosome> factory = getChromosomeFactory();
    NoveltySearch<TestChromosome> ga = new NoveltySearch<>(factory);
    if (Properties.NEW_STATISTICS)
        ga.addListener(new StatisticsListener());
    // How to select candidates for reproduction
    SelectionFunction<TestChromosome> selectionFunction = getSelectionFunction();
    selectionFunction.setMaximize(false);
    ga.setSelectionFunction(selectionFunction);
    // When to stop the search
    StoppingCondition stopping_condition = getStoppingCondition();
    ga.setStoppingCondition(stopping_condition);
    // ga.addListener(stopping_condition);
    if (Properties.STOP_ZERO) {
        ga.addStoppingCondition(new ZeroFitnessStoppingCondition());
    }
    if (!(stopping_condition instanceof MaxTimeStoppingCondition)) {
        ga.addStoppingCondition(new GlobalTimeStoppingCondition());
    }
    if (ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.MUTATION) || ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.STRONGMUTATION)) {
        if (Properties.STRATEGY == Properties.Strategy.ONEBRANCH)
            ga.addStoppingCondition(new MutationTimeoutStoppingCondition());
        else
            ga.addListener(new MutationTestPool());
    // } else if (Properties.CRITERION == Criterion.DEFUSE) {
    // if (Properties.STRATEGY == Strategy.EVOSUITE)
    // ga.addListener(new DefUseTestPool());
    }
    ga.resetStoppingConditions();
    ga.setPopulationLimit(getPopulationLimit());
    // How to cross over
    CrossOverFunction crossover_function = getCrossoverFunction();
    ga.setCrossOverFunction(crossover_function);
    if (Properties.CHECK_BEST_LENGTH) {
        RelativeSuiteLengthBloatControl bloat_control = new org.evosuite.testsuite.RelativeSuiteLengthBloatControl();
        ga.addBloatControl(bloat_control);
        ga.addListener(bloat_control);
    }
    // ga.addBloatControl(new MaxLengthBloatControl());
    TestCaseSecondaryObjective.setSecondaryObjectives();
    if (Properties.DYNAMIC_LIMIT) {
        // max_s = GAProperties.generations * getBranches().size();
        // TODO: might want to make this dependent on the selected coverage
        // criterion
        // TODO also, question: is branchMap.size() really intended here?
        // I think BranchPool.getBranchCount() was intended
        Properties.SEARCH_BUDGET = Properties.SEARCH_BUDGET * (BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getNumBranchlessMethods(Properties.TARGET_CLASS) + BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranchCountForClass(Properties.TARGET_CLASS) * 2);
        stopping_condition.setLimit(Properties.SEARCH_BUDGET);
        logger.info("Setting dynamic length limit to " + Properties.SEARCH_BUDGET);
    }
    if (Properties.LOCAL_SEARCH_RESTORE_COVERAGE) {
        org.evosuite.ga.metaheuristics.SearchListener map = BranchCoverageMap.getInstance();
        ga.addListener(map);
    }
    if (Properties.SHUTDOWN_HOOK) {
        // ShutdownTestWriter writer = new
        // ShutdownTestWriter(Thread.currentThread());
        ShutdownTestWriter writer = new ShutdownTestWriter();
        ga.addStoppingCondition(writer);
        RMIStoppingCondition rmi = RMIStoppingCondition.getInstance();
        ga.addStoppingCondition(rmi);
        if (Properties.STOPPING_PORT != -1) {
            SocketStoppingCondition ss = new SocketStoppingCondition();
            ss.accept();
            ga.addStoppingCondition(ss);
        }
        // Runtime.getRuntime().addShutdownHook(writer);
        Signal.handle(new Signal("INT"), writer);
    }
    ga.addListener(new ResourceController());
    return ga;
}
Also used : ZeroFitnessStoppingCondition(org.evosuite.ga.stoppingconditions.ZeroFitnessStoppingCondition) MutationTestPool(org.evosuite.coverage.mutation.MutationTestPool) NoveltySearch(org.evosuite.ga.metaheuristics.NoveltySearch) StatisticsListener(org.evosuite.statistics.StatisticsListener) MaxTimeStoppingCondition(org.evosuite.ga.stoppingconditions.MaxTimeStoppingCondition) RelativeSuiteLengthBloatControl(org.evosuite.testsuite.RelativeSuiteLengthBloatControl) ShutdownTestWriter(org.evosuite.ShutdownTestWriter) CrossOverFunction(org.evosuite.ga.operators.crossover.CrossOverFunction) Signal(sun.misc.Signal) MutationTimeoutStoppingCondition(org.evosuite.coverage.mutation.MutationTimeoutStoppingCondition) SocketStoppingCondition(org.evosuite.ga.stoppingconditions.SocketStoppingCondition) RMIStoppingCondition(org.evosuite.ga.stoppingconditions.RMIStoppingCondition) ZeroFitnessStoppingCondition(org.evosuite.ga.stoppingconditions.ZeroFitnessStoppingCondition) StoppingCondition(org.evosuite.ga.stoppingconditions.StoppingCondition) MaxTimeStoppingCondition(org.evosuite.ga.stoppingconditions.MaxTimeStoppingCondition) GlobalTimeStoppingCondition(org.evosuite.ga.stoppingconditions.GlobalTimeStoppingCondition) MutationTimeoutStoppingCondition(org.evosuite.coverage.mutation.MutationTimeoutStoppingCondition) SocketStoppingCondition(org.evosuite.ga.stoppingconditions.SocketStoppingCondition) ResourceController(org.evosuite.utils.ResourceController) RMIStoppingCondition(org.evosuite.ga.stoppingconditions.RMIStoppingCondition) TestChromosome(org.evosuite.testcase.TestChromosome) GlobalTimeStoppingCondition(org.evosuite.ga.stoppingconditions.GlobalTimeStoppingCondition)

Aggregations

StoppingCondition (org.evosuite.ga.stoppingconditions.StoppingCondition)8 TestChromosome (org.evosuite.testcase.TestChromosome)5 ZeroFitnessStoppingCondition (org.evosuite.ga.stoppingconditions.ZeroFitnessStoppingCondition)4 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)4 MutationTimeoutStoppingCondition (org.evosuite.coverage.mutation.MutationTimeoutStoppingCondition)3 CrossOverFunction (org.evosuite.ga.operators.crossover.CrossOverFunction)3 GlobalTimeStoppingCondition (org.evosuite.ga.stoppingconditions.GlobalTimeStoppingCondition)3 MaxTimeStoppingCondition (org.evosuite.ga.stoppingconditions.MaxTimeStoppingCondition)3 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)3 ArrayList (java.util.ArrayList)2 ShutdownTestWriter (org.evosuite.ShutdownTestWriter)2 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)2 MutationTestPool (org.evosuite.coverage.mutation.MutationTestPool)2 MaxStatementsStoppingCondition (org.evosuite.ga.stoppingconditions.MaxStatementsStoppingCondition)2 RMIStoppingCondition (org.evosuite.ga.stoppingconditions.RMIStoppingCondition)2 SocketStoppingCondition (org.evosuite.ga.stoppingconditions.SocketStoppingCondition)2 StatisticsListener (org.evosuite.statistics.StatisticsListener)2 RelativeSuiteLengthBloatControl (org.evosuite.testsuite.RelativeSuiteLengthBloatControl)2 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)2 ResourceController (org.evosuite.utils.ResourceController)2