    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 * @author  Tomáš Müller
    020 * @version ExamTT 1.3 (Examination Timetabling)<br>
    021 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
    022 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    023 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    024 * <br>
    025 *          This library is free software; you can redistribute it and/or modify
    026 *          it under the terms of the GNU Lesser General Public License as
    027 *          published by the Free Software Foundation; either version 3 of the
    028 *          License, or (at your option) any later version. <br>
    029 * <br>
    030 *          This library is distributed in the hope that it will be useful, but
    031 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    032 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    033 *          Lesser General Public License for more details. <br>
    034 * <br>
    035 *          You should have received a copy of the GNU Lesser General Public
    036 *          License along with this library; if not see
    037 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    038 */
    039public class ExamUnassignedVariableSelection implements VariableSelection<Exam, ExamPlacement> {
    040    private boolean iRandomSelection = true;
    041
    042    /** Constructor
    043     * @param properties solver configuration
    044     **/
    045    public ExamUnassignedVariableSelection(DataProperties properties) {
    046        iRandomSelection = properties.getPropertyBoolean("ExamUnassignedVariableSelection.random", iRandomSelection);
    047    }
    048
    049    /** Initialization */
    050    @Override
    051    public void init(Solver<Exam, ExamPlacement> solver) {
    052    }
    053
    054    /** Variable selection */
    055    @Override
    056    public Exam selectVariable(Solution<Exam, ExamPlacement> solution) {
    057        ExamModel model = (ExamModel) solution.getModel();
    058        Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
    059        if (model.variables().size() == assignment.nrAssignedVariables())
    060            return null;
    061        if (iRandomSelection) {
    062            int idx = ToolBox.random(model.variables().size() - assignment.nrAssignedVariables());
    063            for (Exam v: model.variables()) {
    064                if (assignment.getValue(v) != null) continue;
    065                if (idx == 0) return v;
    066                idx --;
    067            }
    068        }
    069        Exam variable = null;
    070        for (Exam v : model.variables()) {
    071            if (assignment.getValue(v) != null) continue;
    072            if (variable == null || v.compareTo(variable) < 0)
    073                variable = v;
    074        }
    075        return variable;
    076    }
    077}