def test_forwardcheck_empty_domain(): constrainer = SomeNotInSetConstraint(set('abc')) v1, v2 = variables = [Variable('v1'), Variable('v2')] domains = {v1: Domain(['a']), v2: Domain(['b'])} assert constrainer(variables, domains, {v1: 'a'}) assert not constrainer(variables, domains, {v1: 'a'}, True)
def preProcess(self, variables, domains, constraints, vconstraints): if 'edge_tag_cache' not in self.graph.graph: return cache = self.graph.graph['edge_tag_cache'] if self.tag not in cache: edges = [] e_0 = set() e_1 = set() for i, j, t in self.graph.edges(data="tag"): if t == self.tag: e_0.add(i) e_1.add(j) edges.append((i, j)) cache[self.tag] = edges else: # FIXME: would it be better to switch to representing edges as # edge variables, instead of a pair of node variables? edges = cache[self.tag] e_0 = set(e[0] for e in edges) e_1 = set(e[1] for e in edges) vb = iter(variables) for i, j in zip(vb, vb): if self.graph.is_directed(): domains[i] = Domain(e_0) domains[j] = Domain(e_1) else: all_nodes = e_0.union(e_1) domains[i] = Domain(all_nodes) domains[j] = Domain(all_nodes)
def preProcess(self, variables, domains, constraints, vconstraints): # If only a single variable allowed, variables must be equal to that. # If domain already chopped, then what? if len(self.allowedSet) == 0: # Impossible for v in variables: domains[v] = Domain([]) vconstraints[v].remove((self, variables)) constraints.remove((self, variables)) return elif len(self.allowedSet) == 1: allowed = next(iter(self.allowedSet)) for (v, val) in zip(variables, allowed): if val in domains[v]: domains[v] = Domain([val]) else: domains[v] = Domain([]) vconstraints[v].remove((self, variables)) constraints.remove((self, variables)) return # If some variable is only allowed a subset of values in its domain, # then restrict to that subset. for (i, v) in enumerate(variables): allowed = set(domains[v]).intersection(self.nthSet[i]) domains[v] = Domain(list(allowed))
def test_forwardcheck_empty_domain(): constrainer = SomeNotInSetConstraint(set("abc")) v1, v2 = variables = [Variable("v1"), Variable("v2")] domains = {v1: Domain(["a"]), v2: Domain(["b"])} assert constrainer(variables, domains, {v1: "a"}) assert not constrainer(variables, domains, {v1: "a"}, True)
def test_forwardcheck(): constrainer = SomeNotInSetConstraint(set('abc'), n=2) v1, v2, v3 = variables = [Variable('v1'), Variable('v2'), Variable('v3')] domains = {v1: Domain(['a']), v2: Domain(['b', 'y']), v3: Domain(['c', 'z'])} assert constrainer(variables, domains, {v1: 'a'}) assert ['a'] == list(domains[v1]) assert ['b', 'y'] == list(domains[v2]) assert ['c', 'z'] == list(domains[v3]) assert constrainer(variables, domains, {v1: 'a'}, True) assert ['a'] == list(domains[v1]) assert ['y'] == list(domains[v2]) assert ['z'] == list(domains[v3])
def test_forwardcheck(): constrainer = SomeNotInSetConstraint(set("abc"), n=2) v1, v2, v3 = variables = [Variable("v1"), Variable("v2"), Variable("v3")] domains = { v1: Domain(["a"]), v2: Domain(["b", "y"]), v3: Domain(["c", "z"]) } assert constrainer(variables, domains, {v1: "a"}) assert ["a"] == list(domains[v1]) assert ["b", "y"] == list(domains[v2]) assert ["c", "z"] == list(domains[v3]) assert constrainer(variables, domains, {v1: "a"}, True) assert ["a"] == list(domains[v1]) assert ["y"] == list(domains[v2]) assert ["z"] == list(domains[v3])
def preProcess(self, variables, domains, constraints, vconstraints): if 'node_tag_cache' not in self.graph.graph: return cache = self.graph.graph['node_tag_cache'] if self.tag not in cache: nodes = [] for n, t in self.graph.nodes(data="tag"): if t == self.tag: nodes.append(n) cache[self.tag] = nodes else: nodes = cache[self.tag] for v in variables: domains[v] = Domain(nodes)
def test_forwardcheck_exact(): constrainer = SomeNotInSetConstraint(set('abc'), n=2, exact=True) v1, v2, v3 = variables = [Variable('v1'), Variable('v2'), Variable('v3')] assignments = {v1: 'a'} domains = {v1: Domain(['a', 'x']), v2: Domain(['b', 'y']), v3: Domain(['c', 'z'])} assert constrainer(variables, domains, assignments) assert constrainer(variables, domains, assignments, True) assert 'b' not in domains[v2] assert 'y' in domains[v2] assert 'c' not in domains[v3] assert 'z' in domains[v3] domains = {v1: Domain(['a', 'x']), v2: Domain(['b', 'y']), v3: Domain(['c'])} assert constrainer(variables, domains, assignments) assert not constrainer(variables, domains, assignments, True)
def test_forwardcheck_exact(): constrainer = SomeNotInSetConstraint(set("abc"), n=2, exact=True) v1, v2, v3 = variables = [Variable("v1"), Variable("v2"), Variable("v3")] assignments = {v1: "a"} domains = { v1: Domain(["a", "x"]), v2: Domain(["b", "y"]), v3: Domain(["c", "z"]) } assert constrainer(variables, domains, assignments) assert constrainer(variables, domains, assignments, True) assert "b" not in domains[v2] assert "y" in domains[v2] assert "c" not in domains[v3] assert "z" in domains[v3] domains = { v1: Domain(["a", "x"]), v2: Domain(["b", "y"]), v3: Domain(["c"]) } assert constrainer(variables, domains, assignments) assert not constrainer(variables, domains, assignments, True)