    001    package ifs.solution;
    002
    003    import 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 ifs.model.Value#toInt()}
    012     * over all assigned variables.
    013     *
    014     * @see Solution
    015     * @see ifs.solver.Solver
    016     *
    017     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
    018     * @version 1.0
    019     */
    020    public class MPPSolutionComparator implements SolutionComparator {
    021
    022        public MPPSolutionComparator() {}
    023        /** No parameters are used so far. */
    024        public MPPSolutionComparator(DataProperties properties) {
    025        }
    026
    027        public boolean isBetterThanBestSolution(Solution currentSolution) {
    028            if (currentSolution.getBestInfo()==null) return true;
    029            int unassigned = currentSolution.getModel().unassignedVariables().size();
    030            if (currentSolution.getModel().getBestUnassignedVariables()!=unassigned)
    031                return currentSolution.getModel().getBestUnassignedVariables()>unassigned;
    032            int pert = currentSolution.getModel().perturbVariables().size();
    033            if (currentSolution.getModel().getBestPerturbations()!=pert)
    034                return currentSolution.getModel().getBestPerturbations()>pert;
    035            return currentSolution.getModel().getTotalValue()<currentSolution.getBestValue();
    036        }
    037
    038    }