001 package net.sf.cpsolver.ifs.solution;
002
003 import net.sf.cpsolver.ifs.model.Value;
004 import net.sf.cpsolver.ifs.model.Variable;
005 import net.sf.cpsolver.ifs.util.DataProperties;
006
007 /**
008 * General implementation of solution comparator for minimal perturbation
009 * problem. <br>
010 * <br>
011 * The solution is better than the best ever found solution when it has more
012 * variables assigned. In the case, when both solutions have the same number of
013 * assigned variables, the one with smaller number of perturbations (i.e.,
014 * variables assigned to non-initial values) is selected. When all solution have
015 * the same number of assigned variables and number of perturbations, better
016 * solution is the one with smaller total value, i.e., the sum of
017 * {@link net.sf.cpsolver.ifs.model.Value#toDouble()} over all assigned
018 * variables.
019 *
020 * @see Solution
021 * @see net.sf.cpsolver.ifs.solver.Solver
022 *
023 * @version IFS 1.2 (Iterative Forward Search)<br>
024 * Copyright (C) 2006 - 2010 Tomas Muller<br>
025 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
026 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
027 * <br>
028 * This library is free software; you can redistribute it and/or modify
029 * it under the terms of the GNU Lesser General Public License as
030 * published by the Free Software Foundation; either version 3 of the
031 * License, or (at your option) any later version. <br>
032 * <br>
033 * This library is distributed in the hope that it will be useful, but
034 * 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. <br>
037 * <br>
038 * You should have received a copy of the GNU Lesser General Public
039 * License along with this library; if not see
040 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
041 */
042 public class MPPSolutionComparator<V extends Variable<V, T>, T extends Value<V, T>> implements SolutionComparator<V, T> {
043
044 public MPPSolutionComparator() {
045 }
046
047 /** No parameters are used so far. */
048 public MPPSolutionComparator(DataProperties properties) {
049 }
050
051 @Override
052 public boolean isBetterThanBestSolution(Solution<V, T> currentSolution) {
053 if (currentSolution.getBestInfo() == null)
054 return true;
055 int unassigned = currentSolution.getModel().nrUnassignedVariables();
056 if (currentSolution.getModel().getBestUnassignedVariables() != unassigned)
057 return currentSolution.getModel().getBestUnassignedVariables() > unassigned;
058 int pert = currentSolution.getModel().perturbVariables().size();
059 if (currentSolution.getModel().getBestPerturbations() != pert)
060 return currentSolution.getModel().getBestPerturbations() > pert;
061 return currentSolution.getModel().getTotalValue() < currentSolution.getBestValue();
062 }
063
064 }