001package net.sf.cpsolver.exam.heuristics;
002
003import net.sf.cpsolver.exam.model.Exam;
004import net.sf.cpsolver.exam.model.ExamModel;
005import net.sf.cpsolver.exam.model.ExamPlacement;
006import net.sf.cpsolver.ifs.heuristics.VariableSelection;
007import net.sf.cpsolver.ifs.model.Variable;
008import net.sf.cpsolver.ifs.solution.Solution;
009import net.sf.cpsolver.ifs.solver.Solver;
010import net.sf.cpsolver.ifs.util.DataProperties;
011import net.sf.cpsolver.ifs.util.ToolBox;
012
013/**
014 * Unassigned variable selection. The "biggest" variable (using
015 * {@link Variable#compareTo(Object)}) unassigned variable is selected. One is
016 * selected randomly if there are more than one of such variables.
017 * 
018 * @version ExamTT 1.2 (Examination Timetabling)<br>
019 *          Copyright (C) 2008 - 2010 Tomáš Müller<br>
020 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
021 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
022 * <br>
023 *          This library is free software; you can redistribute it and/or modify
024 *          it under the terms of the GNU Lesser General Public License as
025 *          published by the Free Software Foundation; either version 3 of the
026 *          License, or (at your option) any later version. <br>
027 * <br>
028 *          This library is distributed in the hope that it will be useful, but
029 *          WITHOUT ANY WARRANTY; without even the implied warranty of
030 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
031 *          Lesser General Public License for more details. <br>
032 * <br>
033 *          You should have received a copy of the GNU Lesser General Public
034 *          License along with this library; if not see
035 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
036 */
037public class ExamUnassignedVariableSelection implements VariableSelection<Exam, ExamPlacement> {
038    private boolean iRandomSelection = true;
039
040    /** Constructor */
041    public ExamUnassignedVariableSelection(DataProperties properties) {
042        iRandomSelection = properties.getPropertyBoolean("ExamUnassignedVariableSelection.random", iRandomSelection);
043    }
044
045    /** Initialization */
046    @Override
047    public void init(Solver<Exam, ExamPlacement> solver) {
048    }
049
050    /** Variable selection */
051    @Override
052    public Exam selectVariable(Solution<Exam, ExamPlacement> solution) {
053        ExamModel model = (ExamModel) solution.getModel();
054        if (model.nrUnassignedVariables() == 0)
055            return null;
056        if (iRandomSelection)
057            return ToolBox.random(model.unassignedVariables());
058        Exam variable = null;
059        for (Exam v : model.unassignedVariables()) {
060            if (variable == null || v.compareTo(variable) < 0)
061                variable = v;
062        }
063        return variable;
064    }
065}