001    package net.sf.cpsolver.exam.heuristics;
002    
003    import java.util.Enumeration;
004    
005    import net.sf.cpsolver.ifs.heuristics.VariableSelection;
006    import net.sf.cpsolver.ifs.model.Model;
007    import net.sf.cpsolver.ifs.model.Variable;
008    import net.sf.cpsolver.ifs.solution.Solution;
009    import net.sf.cpsolver.ifs.solver.Solver;
010    import net.sf.cpsolver.ifs.util.DataProperties;
011    import net.sf.cpsolver.ifs.util.ToolBox;
012    
013    /**
014     * Unassigned variable selection. The "biggest" variable (using {@link Variable#compareTo(Object)})
015     * unassigned variable is selected. One is selected randomly if there are more than one of
016     * such variables.
017     *  
018     * @version
019     * ExamTT 1.1 (Examination Timetabling)<br>
020     * Copyright (C) 2008 Tomáš Müller<br>
021     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
022     * Lazenska 391, 76314 Zlin, Czech Republic<br>
023     * <br>
024     * This library is free software; you can redistribute it and/or
025     * modify it under the terms of the GNU Lesser General Public
026     * License as published by the Free Software Foundation; either
027     * version 2.1 of the License, or (at your option) any later version.
028     * <br><br>
029     * This library is distributed in the hope that it will be useful,
030     * but 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.
033     * <br><br>
034     * You should have received a copy of the GNU Lesser General Public
035     * License along with this library; if not, write to the Free Software
036     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
037     */
038    public class ExamUnassignedVariableSelection implements VariableSelection {
039        private boolean iRandomSelection = true;
040        
041        /** Constructor */
042        public ExamUnassignedVariableSelection(DataProperties properties) {
043            iRandomSelection = properties.getPropertyBoolean("ExamUnassignedVariableSelection.random", iRandomSelection);
044        }
045        
046        /** Initialization */
047        public void init(Solver solver) {}
048        
049        /** Variable selection */
050        public Variable selectVariable(Solution solution) {
051            Model model = solution.getModel();
052            if (model.nrUnassignedVariables()==0) return null;
053            if (iRandomSelection) return (Variable)ToolBox.random(model.unassignedVariables());
054            Variable variable = null;
055            for (Enumeration e=model.unassignedVariables().elements();e.hasMoreElements();) {
056                Variable v = (Variable)e.nextElement();
057                if (variable==null || v.compareTo(variable)<0) variable = v;
058            }
059            return variable;
060        }
061    }