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