001    package ifs.dbt;
002    
003    import ifs.extension.*;
004    import ifs.heuristics.*;
005    import ifs.model.*;
006    import ifs.solution.*;
007    import ifs.solver.*;
008    import ifs.util.*;
009    import java.util.*;
010    
011    /**
012     * Selection of a variable for dynamic backtracking.
013     * <br><br>
014     * <li> Returns null if all variables are assigned.
015     * <li> Checks if there is a varaible with all values marked as nogood (and pick it if there is any).
016     * <li> Returns the first unassigned variable.
017     * <br><br>
018     * This IFS solver variable selection heuristics is to be used only in case of dynamic backtracking and it has no parameters.
019     *
020     * 
021     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
022     * @version 1.0
023     */
024    public class DbtVariableSelection implements VariableSelection {
025        private DbtPropagation iProp = null;
026    
027        public DbtVariableSelection(DataProperties properties) {}
028    
029        /** 
030         * Heuristics initialization
031         *
032         * @see VariableSelection#init(Solver)
033         */
034        public void init(Solver solver) {
035            for (Enumeration i = solver.getExtensions().elements(); i.hasMoreElements();) {
036                Extension extension = (Extension) i.nextElement();
037    
038                if (extension instanceof DbtPropagation) {
039                    iProp = (DbtPropagation) extension;
040                }
041            }
042        }
043        
044        /** 
045         * Variable selection 
046         *
047         * @see VariableSelection#selectVariable(Solution)
048         */
049        public Variable selectVariable(Solution solution) {
050            if (solution.getModel().unassignedVariables().isEmpty()) {
051                return null;
052            }
053            if (iProp != null) { 
054                for (Enumeration i1 = solution.getModel().unassignedVariables().elements(); i1.hasMoreElements();) {
055                    Variable variable = (Variable) i1.nextElement();
056    
057                    if (iProp.goodValues(variable).isEmpty()) {
058                        return variable;
059                    }
060                }
061            }
062            return (Variable) solution.getModel().unassignedVariables().firstElement();
063        }
064        
065    }