001 package net.sf.cpsolver.ifs.solution;
002
003 import net.sf.cpsolver.ifs.util.DataProperties;
004
005 /**
006 * General implementation of solution comparator.
007 * <br><br>
008 * The solution is better than the best ever found solution when it has more variables assigned. In the case, when both
009 * solutions have the same number of assigned variables, the better solution is the one with smaller total value, i.e.,
010 * the sum of {@link net.sf.cpsolver.ifs.model.Value#toDouble()} over all assigned variables.
011 *
012 * @see Solution
013 * @see net.sf.cpsolver.ifs.solver.Solver
014 *
015 * @version
016 * IFS 1.1 (Iterative Forward Search)<br>
017 * Copyright (C) 2006 Tomáš Müller<br>
018 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
019 * Lazenska 391, 76314 Zlin, Czech Republic<br>
020 * <br>
021 * This library is free software; you can redistribute it and/or
022 * modify it under the terms of the GNU Lesser General Public
023 * License as published by the Free Software Foundation; either
024 * version 2.1 of the License, or (at your option) any later version.
025 * <br><br>
026 * This library is distributed in the hope that it will be useful,
027 * but WITHOUT ANY WARRANTY; without even the implied warranty of
028 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
029 * Lesser General Public License for more details.
030 * <br><br>
031 * You should have received a copy of the GNU Lesser General Public
032 * License along with this library; if not, write to the Free Software
033 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
034 */
035 public class GeneralSolutionComparator implements SolutionComparator {
036
037 public GeneralSolutionComparator() {}
038 /** No parameters are used so far. */
039 public GeneralSolutionComparator(DataProperties properties) {
040 }
041
042 public boolean isBetterThanBestSolution(Solution currentSolution) {
043 if (currentSolution.getBestInfo()==null) return true;
044 int unassigned = currentSolution.getModel().nrUnassignedVariables();
045 if (currentSolution.getModel().getBestUnassignedVariables()!=unassigned)
046 return currentSolution.getModel().getBestUnassignedVariables()>unassigned;
047 return currentSolution.getModel().getTotalValue()<currentSolution.getBestValue();
048 }
049
050 }