    001    package ifs.termination;
    002
    003    import ifs.solution.Solution;
    004    import ifs.util.DataProperties;
    005
    006    /**
    007     * General implementation of termination condition.
    008     * <br><br>
    009     * Solver stops when the solution is complete (all varaibles are assigned) or when a timeout
    010     * is reached (expressed either by the number of iterations or by a time).
    011     * <br><br>
    012     * Parameters:
    013     * <br>
    014     * <table border='1'><tr><th>Parameter</th><th>Type</th><th>Comment</th></tr>
    015     * <tr><td>Termination.StopWhenComplete</td><td>{@link Double}</td><td>if true, solver stops when a complete solution is found</td></tr>
    016     * <tr><td>Termination.MaxIters</td><td>{@link Integer}</td><td>if zero or positive, solver stops when the given number of iteration is reached</td></tr>
    017     * <tr><td>Termination.TimeOut</td><td>{@link Double}</td><td>if zero or positive, solver stops when the given timeout (given in seconds) is reached</td></tr>
    018     * </table>
    019     *
    020     * @see ifs.solver.Solver
    021     *
    022     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
    023     * @version 1.0
    024     **/
    025    public class GeneralTerminationCondition implements TerminationCondition {
    026        protected static org.apache.log4j.Logger sLogger = org.apache.log4j.Logger.getLogger(GeneralTerminationCondition.class);
    027        private int iMaxIter;
    028        private double iTimeOut;
    029        private boolean iStopWhenComplete;
    030
    031        public GeneralTerminationCondition(DataProperties properties) {
    032            iMaxIter = properties.getPropertyInt("Termination.MaxIters",-1);
    033            iTimeOut = properties.getPropertyDouble("Termination.TimeOut",  -1.0);
    034            iStopWhenComplete = properties.getPropertyBoolean("Termination.StopWhenComplete", false);
    035        }
    036
    037        public boolean canContinue(Solution currentSolution) {
    038            if (iMaxIter>=0 && currentSolution.getIteration()>=iMaxIter) {
    039                sLogger.info("Maximum number of iteration reached.");
    040                return false;
    041            }
    042            if (iTimeOut>=0 && currentSolution.getTime()>iTimeOut) {
    043                sLogger.info("Timeout reached.");
    044                return false;
    045            }
    046            if (iStopWhenComplete || (iMaxIter<0 && iTimeOut<0)) {
    047                boolean ret = (!currentSolution.getModel().unassignedVariables().isEmpty());
    048                if (!ret) sLogger.info("Complete solution found.");
    049                return ret;
    050            }
    051            return true;
    052        }
    053    }