def test_UncontainedVariableError(self):
     var1 = csp.Variable([i for i in range(5)], 4)
     var2 = csp.Variable([i for i in range(5, 10)], 6)
     var3 = csp.Variable([i for i in range(11, 20)])
     variables = [var1, var2]
     const = csp.Constraint(variables, csp.MaxSum(28))
     self.assertRaises(csp.UncontainedVariableError,
                       const.get_consistent_domain_values, var3)
 def test_unique_domain_elements(self):
     var1 = csp.Variable(
         (1, 1, "1", "1", 1.0, 1.0, "1.0", "1.0", "one", "one"))
     self.assertEqual(set(var1.domain), {1, 1.0, "1", "1.0", "one"})
     var2 = csp.Variable("abcd")
     self.assertEqual(var2.domain.sort(), list("abcd").sort())
     var3 = csp.Variable("abcd blah xyz")
     self.assertEqual(var3.domain.sort(), list("abcd").sort())
 def test_get_consistent_domain(self):
     var1 = csp.Variable([i for i in range(5)], 4)
     var2 = csp.Variable([i for i in range(5, 10)], 6)
     var3 = csp.Variable([i for i in range(15, 26)])
     variables = [var1, var2, var3]
     const = csp.Constraint(variables, csp.MaxSum(28))
     consistent_domain = const.get_consistent_domain_values(var3)
     const_domain = frozenset({15, 16, 17})
     self.assertTrue(consistent_domain, const_domain)
 def test_is_consistent(self):
     var1 = csp.Variable([i for i in range(5)], 4)
     var2 = csp.Variable([i for i in range(5, 10)], 6)
     var3 = csp.Variable([i for i in range(11, 20)])
     variables = [var1, var2, var3]
     const = csp.Constraint(variables, csp.MaxSum(28))
     self.assertTrue(const.is_consistent())
     self.assertFalse(const)
     var3.assign(17)
     self.assertTrue(const)
 def test_iterable_constructor(self):
     gen_domain = (i for i in range(10))
     var1 = csp.Variable(gen_domain)
     self.assertEqual(list(i for i in range(10)).sort(), var1.domain.sort())
     set_domain = {i for i in range(17)}
     var2 = csp.Variable(set_domain)
     self.assertEqual(list(set_domain).sort(), var2.domain.sort())
     dict_domain = {i: i + 1 for i in range(5)}
     var3 = csp.Variable(dict_domain)
     self.assertEqual(list(dict_domain).sort(), var3.domain.sort())
     arr_domain = array.array('h', (i for i in range(3)))
     var4 = csp.Variable(arr_domain)
     self.assertEqual(list(arr_domain).sort(), var4.domain.sort())
def construct_sudoku_problem(file_name: str) -> csp.ConstraintProblem:
    grid = _parse_sudoku(file_name)
    name_to_var_map = dict()
    grid_len = len(grid)
    for i in range(grid_len):
        for j in range(grid_len):
            if grid[i][j] != 0:
                name_to_var_map[(i, j)] = csp.Variable((grid[i][j], ),
                                                       grid[i][j])
            else:
                name_to_var_map[(i, j)] = csp.Variable(range(1, grid_len + 1))

    return _constraint_sudoku_problem_from_name_to_var_map(name_to_var_map)
 def test_bool_operator(self):
     var1 = csp.Variable([i for i in range(10)])
     var2 = csp.Variable([i for i in range(10, 20)])
     variables = {var1, var2}
     const = csp.Constraint(variables, csp.ExactSum(28))
     self.assertFalse(const)
     var1.assign(7)
     var2.assign(13)
     self.assertFalse(const)
     var1.unassign()
     var1.assign(9)
     var2.unassign()
     var2.assign(19)
     self.assertTrue(const)
 def test_string_domain(self):
     var1 = csp.Variable("abcd efg")
     var1.assign("a")
     self.assertTrue(var1)
     var1.unassign()
     self.assertNotIn(" ", var1.domain)
     self.assertNotIn("e", var1.domain)
     self.assertNotIn("f", var1.domain)
     self.assertNotIn("g", var1.domain)
     var1.assign("b")
     self.assertTrue(var1)
     self.assertEqual(set(var1.domain), {"a", "b", "c", "d"})
예제 #9
0
import time
import copy
import csp
from examples.performance_testing import measure_performance

# SOLVING N-QUEENS PROBLEM:
# Each variable represent a column in the n x n sized board.
# Each variable's domain is (1, ..., n), and it represent a possible queen row-coordinate location in the column i.
# For the queens not to attack each other, the constraints ara:
#   1. No single row hold two queens: all variables are (pair-wise) all-different.
#   2. The queens don't attack each other horizontally.
#   3. The queens don't attack each other diagonally.

n = 8

name_to_variable_map = {col: csp.Variable(range(n)) for col in range(n)}


def get_not_attacking_constraint(
        columns_difference: int) -> csp.ConstraintEvaluator:
    def not_attacking_constraint(values: tuple) -> bool:
        if len(values) < 2:
            return True
        row1, row2 = values
        return row1 != row2 and abs(row1 - row2) != columns_difference

    return not_attacking_constraint


not_attacking_constraints = dict()
for i in range(1, n):
 def test_assigned_variable_construction(self):
     var1 = csp.Variable((i for i in range(10)), 7)
     self.assertEqual(7, var1.value)
     self.assertRaises(csp.UncontainedValueError, csp.Variable,
                       (i for i in range(10)), 12)
 def setUp(self):
     self.var = csp.Variable([i for i in range(10)])
예제 #12
0
    def setUp(self):
        colors = {"red", "green"}
        names = {"wa", "nt", "q", "nsw", "v", "sa", "t"}
        self.name_to_variable_map = csp.Variable.from_names_to_equal_domain(
            names, colors)
        const1 = csp.Constraint(
            [self.name_to_variable_map["sa"], self.name_to_variable_map["wa"]],
            csp.all_diff_constraint_evaluator)
        const2 = csp.Constraint(
            [self.name_to_variable_map["sa"], self.name_to_variable_map["nt"]],
            csp.all_diff_constraint_evaluator)
        const3 = csp.Constraint(
            [self.name_to_variable_map["sa"], self.name_to_variable_map["q"]],
            csp.all_diff_constraint_evaluator)
        const4 = csp.Constraint([
            self.name_to_variable_map["sa"], self.name_to_variable_map["nsw"]
        ], csp.all_diff_constraint_evaluator)
        const5 = csp.Constraint(
            [self.name_to_variable_map["sa"], self.name_to_variable_map["v"]],
            csp.all_diff_constraint_evaluator)
        const6 = csp.Constraint(
            [self.name_to_variable_map["wa"], self.name_to_variable_map["nt"]],
            csp.all_diff_constraint_evaluator)
        const7 = csp.Constraint(
            [self.name_to_variable_map["nt"], self.name_to_variable_map["q"]],
            csp.all_diff_constraint_evaluator)
        const8 = csp.Constraint(
            [self.name_to_variable_map["q"], self.name_to_variable_map["nsw"]],
            csp.all_diff_constraint_evaluator)
        const9 = csp.Constraint(
            [self.name_to_variable_map["nsw"], self.name_to_variable_map["v"]],
            csp.all_diff_constraint_evaluator)
        const10 = csp.Constraint([self.name_to_variable_map["t"]],
                                 csp.always_satisfied)
        constraints = [
            const1, const2, const3, const4, const5, const6, const7, const8,
            const9, const10
        ]
        self.const_problem1 = csp.ConstraintProblem(constraints)

        x = csp.Variable((2, 5))
        y = csp.Variable((2, 4))
        z = csp.Variable((2, 5))

        def is_divisor(values: tuple) -> bool:
            if len(values) < 2:
                return True
            result = values[0] / values[1]
            return result == int(result)

        const1 = csp.Constraint((x, z), is_divisor)
        const2 = csp.Constraint((y, z), is_divisor)
        self.const_problem2 = csp.ConstraintProblem((const1, const2))

        x = csp.Variable((1, 2, 3))
        y = csp.Variable((1, 2, 3))

        def less_than(values: tuple) -> bool:
            if len(values) < 2:
                return True
            return values[0] < values[1]

        const = csp.Constraint((x, y), less_than)
        self.const_problem3 = csp.ConstraintProblem((const, ))
# //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

colors = ("red", "white", "green", "yellow", "blue")
nationalities = ("brit", "swede", "dane", "norwegian", "german")
drinks = ("tea", "coffee", "milk", "beer", "water")
smokes = ("pallmall", "dunhill", "blends", "bluemaster", "prince")
pets = ("dogs", "birds", "cats", "horses", "fish")

color_vars = dict()
nationality_vars = dict()
drink_vars = dict()
smoke_vars = dict()
pets_vars = dict()

for i in range(1, 6):
    color_vars[i] = csp.Variable(colors)
    nationality_vars[i] = csp.Variable(nationalities)
    drink_vars[i] = csp.Variable(drinks)
    smoke_vars[i] = csp.Variable(smokes)
    pets_vars[i] = csp.Variable(pets)

constraints = set()
constraints.add(
    csp.Constraint(color_vars.values(), csp.all_diff_constraint_evaluator))
constraints.add(
    csp.Constraint(nationality_vars.values(),
                   csp.all_diff_constraint_evaluator))
constraints.add(
    csp.Constraint(drink_vars.values(), csp.all_diff_constraint_evaluator))
constraints.add(
    csp.Constraint(smoke_vars.values(), csp.all_diff_constraint_evaluator))
 def test_get_variables(self):
     var1 = csp.Variable([i for i in range(10)])
     var2 = csp.Variable([i for i in range(10, 20)])
     variables = (var1, var2)
     const = csp.Constraint(variables, csp.ExactSum(28))
     self.assertEqual(const.variables, variables)
 def test_unary_constraint(self):
     var = csp.Variable([i for i in range(10)])
     const = csp.Constraint({var}, csp.MinSum(5))
     var_from_const, *_ = const.variables
     self.assertEqual(frozenset(var_from_const.domain),
                      frozenset(i for i in range(6, 10)))
예제 #16
0

# number of variables: n x n
# domain of each variable: 1, ..., n x n
# constraints:
#   1. global all different.
#   2. the values of all rows sum up to magic sum.
#   3. the values of all columns sum up to magic sum.
#   4. the values of both diagonals sum up to magic sum.

n = 3
order = n**2
magic_sum = n * int((order + 1) / 2)


name_to_variable_map = {square: csp.Variable(range(1, order + 1)) for square in range(1, order + 1)}

constraints = set()
constraints.add(csp.Constraint(name_to_variable_map.values(), csp.all_diff_constraint_evaluator))

exact_magic_sum = csp.ExactLengthExactSum(n, magic_sum)

for row in range(1, order + 1, n):
    constraints.add(csp.Constraint((name_to_variable_map[i] for i in range(row, row + n)),
                                   exact_magic_sum))

for column in range(1, n + 1):
    constraints.add(csp.Constraint((name_to_variable_map[i] for i in range(column, order + 1, n)),
                                   exact_magic_sum))

constraints.add(csp.Constraint((name_to_variable_map[diag] for diag in range(1, order + 1, n + 1)),