001 package net.sf.cpsolver.ifs.termination;
002
003 import net.sf.cpsolver.ifs.solution.Solution;
004 import net.sf.cpsolver.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 net.sf.cpsolver.ifs.solver.Solver
021 *
022 * @version
023 * IFS 1.1 (Iterative Forward Search)<br>
024 * Copyright (C) 2006 Tomáš Müller<br>
025 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
026 * Lazenska 391, 76314 Zlin, Czech Republic<br>
027 * <br>
028 * This library is free software; you can redistribute it and/or
029 * modify it under the terms of the GNU Lesser General Public
030 * License as published by the Free Software Foundation; either
031 * version 2.1 of the License, or (at your option) any later version.
032 * <br><br>
033 * This library is distributed in the hope that it will be useful,
034 * but WITHOUT ANY WARRANTY; without even the implied warranty of
035 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
036 * Lesser General Public License for more details.
037 * <br><br>
038 * You should have received a copy of the GNU Lesser General Public
039 * License along with this library; if not, write to the Free Software
040 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
041 **/
042 public class GeneralTerminationCondition implements TerminationCondition {
043 protected static org.apache.log4j.Logger sLogger = org.apache.log4j.Logger.getLogger(GeneralTerminationCondition.class);
044 private int iMaxIter;
045 private double iTimeOut;
046 private boolean iStopWhenComplete;
047
048 public GeneralTerminationCondition(DataProperties properties) {
049 iMaxIter = properties.getPropertyInt("Termination.MaxIters",-1);
050 iTimeOut = properties.getPropertyDouble("Termination.TimeOut", -1.0);
051 iStopWhenComplete = properties.getPropertyBoolean("Termination.StopWhenComplete", false);
052 }
053
054 public boolean canContinue(Solution currentSolution) {
055 if (iMaxIter>=0 && currentSolution.getIteration()>=iMaxIter) {
056 sLogger.info("Maximum number of iteration reached.");
057 return false;
058 }
059 if (iTimeOut>=0 && currentSolution.getTime()>iTimeOut) {
060 sLogger.info("Timeout reached.");
061 return false;
062 }
063 if (iStopWhenComplete || (iMaxIter<0 && iTimeOut<0)) {
064 boolean ret = (currentSolution.getModel().nrUnassignedVariables()!=0);
065 if (!ret) sLogger.info("Complete solution found.");
066 return ret;
067 }
068 return true;
069 }
070 }