    001package org.cpsolver.ifs.termination;
    002
    003import org.cpsolver.ifs.model.Value;
    004import org.cpsolver.ifs.model.Variable;
    005import org.cpsolver.ifs.solution.Solution;
    006import org.cpsolver.ifs.util.DataProperties;
    007
    008/**
    009 * General implementation of termination condition for minimal perturbation
    010 * problem. <br>
    011 * <br>
    012 * Solver stops when a timeout is reached (expressed either by the number of
    013 * iterations or by a time) or when an acceptable complete (all variables are
    014 * assigned) solution is found. The acceptance of a solution is expressed either
    015 * by the minimal number of variables assigned to not-initial values or by the
    016 * perturbations penalty. <br>
    017 * <br>
    018 * Parameters: <br>
    019 * <table border='1'><caption>Related Solver Parameters</caption>
    020 * <tr>
    021 * <th>Parameter</th>
    022 * <th>Type</th>
    023 * <th>Comment</th>
    024 * </tr>
    025 * <tr>
    026 * <td>Termination.StopWhenComplete</td>
    027 * <td>{@link Double}</td>
    028 * <td>if true, solver stops when a complete solution is found</td>
    029 * </tr>
    030 * <tr>
    031 * <td>Termination.MaxIters</td>
    032 * <td>{@link Integer}</td>
    033 * <td>if zero or positive, solver stops when the given number of iteration is
    034 * reached</td>
    035 * </tr>
    036 * <tr>
    037 * <td>Termination.TimeOut</td>
    038 * <td>{@link Double}</td>
    039 * <td>if zero or positive, solver stops when the given timeout (given in
    040 * seconds) is reached</td>
    041 * </tr>
    042 * <tr>
    043 * <td>Termination.MinPerturbances</td>
    044 * <td>{@link Integer}</td>
    045 * <td>if zero or positive, solver stops when the solution is complete and the
    046 * number of variables with non-initial values is below or equal to this limit</td>
    047 * </tr>
    048 * <tr>
    049 * <td>Termination.MinPerturbationPenalty</td>
    050 * <td>{@link Double}</td>
    051 * <td>if zero or positive, solver stops when the solution is complete and when
    052 * the perturbation penaly of the solution is below or equal to this limit</td>
    053 * </tr>
    054 * </table>
    055 *
    056 * @see org.cpsolver.ifs.solver.Solver
    057 * @see org.cpsolver.ifs.perturbations.PerturbationsCounter
    058 *
    059 * @author  Tomáš Müller
    060 * @version IFS 1.3 (Iterative Forward Search)<br>
    061 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
    062 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    063 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    064 * <br>
    065 *          This library is free software; you can redistribute it and/or modify
    066 *          it under the terms of the GNU Lesser General Public License as
    067 *          published by the Free Software Foundation; either version 3 of the
    068 *          License, or (at your option) any later version. <br>
    069 * <br>
    070 *          This library is distributed in the hope that it will be useful, but
    071 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    072 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    073 *          Lesser General Public License for more details. <br>
    074 * <br>
    075 *          You should have received a copy of the GNU Lesser General Public
    076 *          License along with this library; if not see <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>.
    077 *
    078 * @param <V> Variable
    079 * @param <T> Value
    080 **/
    081public class MPPTerminationCondition<V extends Variable<V, T>, T extends Value<V, T>> implements
    082        TerminationCondition<V, T> {
    083    protected static org.apache.logging.log4j.Logger sLogger = org.apache.logging.log4j.LogManager.getLogger(MPPTerminationCondition.class);
    084    private int iMinPerturbances;
    085    private int iMaxIter;
    086    private double iTimeOut;
    087    private double iMinPertPenalty;
    088    private boolean iStopWhenComplete;
    089
    090    public MPPTerminationCondition(DataProperties properties) {
    091        iMaxIter = properties.getPropertyInt("Termination.MaxIters", -1);
    092        iTimeOut = properties.getPropertyDouble("Termination.TimeOut", -1.0);
    093        iMinPerturbances = properties.getPropertyInt("Termination.MinPerturbances", -1);
    094        iStopWhenComplete = properties.getPropertyBoolean("Termination.StopWhenComplete", false);
    095        iMinPertPenalty = properties.getPropertyDouble("Termination.MinPerturbationPenalty", -1.0);
    096    }
    097
    098    public MPPTerminationCondition(int maxIter, double timeout, int minPerturbances) {
    099        iMaxIter = maxIter;
    100        iMinPerturbances = minPerturbances;
    101        iTimeOut = timeout;
    102    }
    103
    104    @Override
    105    public boolean canContinue(Solution<V, T> currentSolution) {
    106        if (iMinPerturbances >= 0 && currentSolution.getAssignment().nrUnassignedVariables(currentSolution.getModel()) == 0
    107                && currentSolution.getModel().perturbVariables(currentSolution.getAssignment()).size() <= iMinPerturbances) {
    108            sLogger.info("A complete solution with allowed number of perturbances found.");
    109            return false;
    110        }
    111        if (iMinPertPenalty >= 0.0
    112                && currentSolution.getAssignment().nrUnassignedVariables(currentSolution.getModel()) == 0
    113                && currentSolution.getPerturbationsCounter().getPerturbationPenalty(currentSolution.getAssignment(), currentSolution.getModel()) <= iMinPertPenalty) {
    114            sLogger.info("A complete solution with allowed perturbation penalty found.");
    115            return false;
    116        }
    117        if (iMaxIter >= 0 && currentSolution.getIteration() >= iMaxIter) {
    118            sLogger.info("Maximum number of iteration reached.");
    119            return false;
    120        }
    121        if (iTimeOut >= 0 && currentSolution.getTime() > iTimeOut) {
    122            sLogger.info("Timeout reached.");
    123            return false;
    124        }
    125        if (iStopWhenComplete || (iMaxIter < 0 && iTimeOut < 0)) {
    126            boolean ret = (currentSolution.getAssignment().nrUnassignedVariables(currentSolution.getModel()) != 0);
    127            if (!ret)
    128                sLogger.info("Complete solution found.");
    129            return ret;
    130        }
    131        return true;
    132    }
    133
    134}