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