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