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 * @author  Tomáš Müller
025 * @version IFS 1.3 (Iterative Forward Search)<br>
026 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
027 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
028 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
029 * <br>
030 *          This library is free software; you can redistribute it and/or modify
031 *          it under the terms of the GNU Lesser General Public License as
032 *          published by the Free Software Foundation; either version 3 of the
033 *          License, or (at your option) any later version. <br>
034 * <br>
035 *          This library is distributed in the hope that it will be useful, but
036 *          WITHOUT ANY WARRANTY; without even the implied warranty of
037 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
038 *          Lesser General Public License for more details. <br>
039 * <br>
040 *          You should have received a copy of the GNU Lesser General Public
041 *          License along with this library; if not see
042 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
043 * 
044 * @param <V> Variable 
045 * @param <T> Value
046 */
047public class MPPSolutionComparator<V extends Variable<V, T>, T extends Value<V, T>> implements SolutionComparator<V, T> {
048
049    public MPPSolutionComparator() {
050    }
051
052    /** No parameters are used so far.
053     * @param properties solver configuration
054     **/
055    public MPPSolutionComparator(DataProperties properties) {
056    }
057
058    @Override
059    public boolean isBetterThanBestSolution(Solution<V, T> currentSolution) {
060        if (currentSolution.getBestInfo() == null)
061            return true;
062        int unassigned = currentSolution.getAssignment().nrUnassignedVariables(currentSolution.getModel());
063        if (currentSolution.getModel().getBestUnassignedVariables() != unassigned)
064            return currentSolution.getModel().getBestUnassignedVariables() > unassigned;
065        int pert = currentSolution.getModel().perturbVariables(currentSolution.getAssignment()).size();
066        if (currentSolution.getModel().getBestPerturbations() != pert)
067            return currentSolution.getModel().getBestPerturbations() > pert;
068        return currentSolution.getModel().getTotalValue(currentSolution.getAssignment()) < currentSolution.getModel().getBestValue();
069    }
070
071}