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