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 * @version IFS 1.3 (Iterative Forward Search)<br> 026 * Copyright (C) 2006 - 2014 Tomáš Müller<br> 027 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 028 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 029 * <br> 030 * This library is free software; you can redistribute it and/or modify 031 * it under the terms of the GNU Lesser General Public License as 032 * published by the Free Software Foundation; either version 3 of the 033 * License, or (at your option) any later version. <br> 034 * <br> 035 * This library is distributed in the hope that it will be useful, but 036 * WITHOUT ANY WARRANTY; without even the implied warranty of 037 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 038 * Lesser General Public License for more details. <br> 039 * <br> 040 * You should have received a copy of the GNU Lesser General Public 041 * License along with this library; if not see 042 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 043 * @param <V> Variable 044 * @param <T> Value 045 */ 046public class DbtVariableSelection<V extends Variable<V, T>, T extends Value<V, T>> implements VariableSelection<V, T> { 047 private DbtPropagation<V, T> iProp = null; 048 049 public DbtVariableSelection(DataProperties properties) { 050 } 051 052 /** 053 * Heuristics initialization 054 * 055 * @see VariableSelection#init(Solver) 056 */ 057 @Override 058 public void init(Solver<V, T> solver) { 059 for (Extension<V, T> extension : solver.getExtensions()) { 060 if (extension instanceof DbtPropagation<?, ?>) { 061 iProp = (DbtPropagation<V, T>) extension; 062 } 063 } 064 } 065 066 /** 067 * Variable selection 068 * 069 * @see VariableSelection#selectVariable(Solution) 070 */ 071 @Override 072 public V selectVariable(Solution<V, T> solution) { 073 if (solution.getAssignment().nrAssignedVariables() == solution.getModel().variables().size()) { 074 return null; 075 } 076 if (iProp != null) { 077 for (V variable : solution.getAssignment().unassignedVariables(solution.getModel())) { 078 if (iProp.goodValues(solution.getAssignment(), variable).isEmpty()) { 079 return variable; 080 } 081 } 082 } 083 return ToolBox.random(solution.getAssignment().unassignedVariables(solution.getModel())); 084 } 085 086}