    001package org.cpsolver.ifs.dbt;
    002
    003import org.cpsolver.ifs.extension.Extension;
    004import org.cpsolver.ifs.heuristics.VariableSelection;
    005import org.cpsolver.ifs.model.Value;
    006import org.cpsolver.ifs.model.Variable;
    007import org.cpsolver.ifs.solution.Solution;
    008import org.cpsolver.ifs.solver.Solver;
    009import org.cpsolver.ifs.util.DataProperties;
    010import org.cpsolver.ifs.util.ToolBox;
    011
    012/**
    013 * Selection of a variable for dynamic backtracking. <br>
    014 * <br>
    015 * <ul>
    016 * <li>Returns null if all variables are assigned.
    017 * <li>Checks if there is a varaible with all values marked as nogood (and pick it if there is any).
    018 * <li> Returns the first unassigned variable.
    019 * </ul>
    020 * <br>
    021 * This IFS solver variable selection heuristics is to be used only in case of
    022 * dynamic backtracking and it has no parameters.
    023 *
    024 *
    025 * @author  Tomáš Müller
    026 * @version IFS 1.3 (Iterative Forward Search)<br>
    027 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
    028 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    029 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    030 * <br>
    031 *          This library is free software; you can redistribute it and/or modify
    032 *          it under the terms of the GNU Lesser General Public License as
    033 *          published by the Free Software Foundation; either version 3 of the
    034 *          License, or (at your option) any later version. <br>
    035 * <br>
    036 *          This library is distributed in the hope that it will be useful, but
    037 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    038 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    039 *          Lesser General Public License for more details. <br>
    040 * <br>
    041 *          You should have received a copy of the GNU Lesser General Public
    042 *          License along with this library; if not see
    043 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    044 * @param <V> Variable
    045 * @param <T> Value
    046 */
    047public class DbtVariableSelection<V extends Variable<V, T>, T extends Value<V, T>> implements VariableSelection<V, T> {
    048    private DbtPropagation<V, T> iProp = null;
    049
    050    public DbtVariableSelection(DataProperties properties) {
    051    }
    052
    053    /**
    054     * Heuristics initialization
    055     *
    056     * @see VariableSelection#init(Solver)
    057     */
    058    @Override
    059    public void init(Solver<V, T> solver) {
    060        for (Extension<V, T> extension : solver.getExtensions()) {
    061            if (extension instanceof DbtPropagation<?, ?>) {
    062                iProp = (DbtPropagation<V, T>) extension;
    063            }
    064        }
    065    }
    066
    067    /**
    068     * Variable selection
    069     *
    070     * @see VariableSelection#selectVariable(Solution)
    071     */
    072    @Override
    073    public V selectVariable(Solution<V, T> solution) {
    074        if (solution.getAssignment().nrAssignedVariables() == solution.getModel().variables().size()) {
    075            return null;
    076        }
    077        if (iProp != null) {
    078            for (V variable : solution.getAssignment().unassignedVariables(solution.getModel())) {
    079                if (iProp.goodValues(solution.getAssignment(), variable).isEmpty()) {
    080                    return variable;
    081                }
    082            }
    083        }
    084        return ToolBox.random(solution.getAssignment().unassignedVariables(solution.getModel()));
    085    }
    086
    087}