All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.evosuite.ga.stoppingconditions.MaxLengthStoppingCondition Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2010-2018 Gordon Fraser, Andrea Arcuri and EvoSuite
 * contributors
 *
 * This file is part of EvoSuite.
 *
 * EvoSuite is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3.0 of the License, or
 * (at your option) any later version.
 *
 * EvoSuite is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with EvoSuite. If not, see .
 */
package org.evosuite.ga.stoppingconditions;

import org.evosuite.Properties;
import org.evosuite.ga.Chromosome;
import org.evosuite.ga.metaheuristics.GeneticAlgorithm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Stop search when a maximum (average) length has been reached. Used for
 * experiments on length bloat.
 * 
 * @author Gordon Fraser
 */
public class MaxLengthStoppingCondition extends StoppingConditionImpl {

	private static final Logger logger = LoggerFactory.getLogger(MaxLengthStoppingCondition.class);

	private static final long serialVersionUID = 8537667219135128366L;

	private double averageLength = 0.0;
	private int maxLength = Properties.MAX_LENGTH;

	/* (non-Javadoc)
	 * @see org.ga.StoppingCondition#isFinished()
	 */
	/** {@inheritDoc} */
	@Override
	public boolean isFinished() {
		if (averageLength >= maxLength)
			logger.info("Maximum average length reached, stopping");
		return averageLength >= maxLength;
	}

	/* (non-Javadoc)
	 * @see org.ga.StoppingCondition#reset()
	 */
	/** {@inheritDoc} */
	@Override
	public void reset() {
		averageLength = 0.0;
		maxLength = Properties.MAX_LENGTH;
	}

	/** {@inheritDoc} */
	@Override
	public void iteration(GeneticAlgorithm algorithm) {
		double avg = 0.0;
		for (Chromosome c : algorithm.getPopulation()) {
			avg += c.size();
		}
		averageLength = avg / algorithm.getPopulation().size();
	}

	/* (non-Javadoc)
	 * @see org.evosuite.ga.StoppingCondition#getCurrentValue()
	 */
	/** {@inheritDoc} */
	@Override
	public long getCurrentValue() {
		return (long) averageLength;
	}

	/* (non-Javadoc)
	 * @see org.evosuite.ga.StoppingCondition#setLimit(int)
	 */
	/** {@inheritDoc} */
	@Override
	public void setLimit(long limit) {
		maxLength = (int) limit;
	}

	/** {@inheritDoc} */
	@Override
	public long getLimit() {
		return (long) (maxLength + 0.5);
	}

	/** {@inheritDoc} */
	@Override
	public void forceCurrentValue(long value) {
		// TODO Auto-generated method stub
		// TODO ?
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy