    001package org.cpsolver.exam.heuristics;
    002
    003import org.cpsolver.exam.model.Exam;
    004import org.cpsolver.exam.model.ExamModel;
    005import org.cpsolver.exam.model.ExamPlacement;
    006import org.cpsolver.ifs.assignment.Assignment;
    007import org.cpsolver.ifs.heuristics.VariableSelection;
    008import org.cpsolver.ifs.model.Variable;
    009import org.cpsolver.ifs.solution.Solution;
    010import org.cpsolver.ifs.solver.Solver;
    011import org.cpsolver.ifs.util.DataProperties;
    012import org.cpsolver.ifs.util.ToolBox;
    013
    014/**
    015 * Unassigned variable selection. The "biggest" variable (using
    016 * {@link Variable#compareTo(Variable)}) unassigned variable is selected. One is
    017 * selected randomly if there are more than one of such variables.
    018 *
    019 * @version ExamTT 1.3 (Examination Timetabling)<br>
    020 *          Copyright (C) 2008 - 2014 Tomáš Müller<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 */
    038public class ExamUnassignedVariableSelection implements VariableSelection<Exam, ExamPlacement> {
    039    private boolean iRandomSelection = true;
    040
    041    /** Constructor
    042     * @param properties solver configuration
    043     **/
    044    public ExamUnassignedVariableSelection(DataProperties properties) {
    045        iRandomSelection = properties.getPropertyBoolean("ExamUnassignedVariableSelection.random", iRandomSelection);
    046    }
    047
    048    /** Initialization */
    049    @Override
    050    public void init(Solver<Exam, ExamPlacement> solver) {
    051    }
    052
    053    /** Variable selection */
    054    @Override
    055    public Exam selectVariable(Solution<Exam, ExamPlacement> solution) {
    056        ExamModel model = (ExamModel) solution.getModel();
    057        Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
    058        if (model.variables().size() == assignment.nrAssignedVariables())
    059            return null;
    060        if (iRandomSelection) {
    061            int idx = ToolBox.random(model.variables().size() - assignment.nrAssignedVariables());
    062            for (Exam v: model.variables()) {
    063                if (assignment.getValue(v) != null) continue;
    064                if (idx == 0) return v;
    065                idx --;
    066            }
    067        }
    068        Exam variable = null;
    069        for (Exam v : model.variables()) {
    070            if (assignment.getValue(v) != null) continue;
    071            if (variable == null || v.compareTo(variable) < 0)
    072                variable = v;
    073        }
    074        return variable;
    075    }
    076}