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