def test_consistent_for_empty_assignment(self):
        # Arrange
        variables = [Variable('1', Domain([1, 2, 3]))]
        constraints = [EqualConstraint(variables[0], 2)]
        csp = ConstraintSatisfactionProblem(variables, constraints)
        assignment = Assignment()

        # Act
        consistent = csp.consistent(assignment)

        # Assert
        self.assertTrue(consistent)
    def test_order_domain_values_returns_all_domain_values(self):
        expected_values = set([1, 2, 3])
        variables = [Variable('1', Domain(list(expected_values)))]
        constraints = []
        csp = ConstraintSatisfactionProblem(variables, constraints)
        assignment = Assignment()
        values = set()

        # Act
        for value in csp.order_domain_values(variables[0], assignment):
            values.add(value)

        # Assert
        self.assertEqual(expected_values, values)
Пример #3
0
    def test_solver_success_many_variables_many_constraints(self):
        # Arrange
        variables = []
        domain = set([1, 2, 3, 4, 5])
        for i in range(5):
            variable = Variable(str(i), Domain(list(domain)))
            variables.append(variable)
        constraints = [
            EqualConstraint(variables[2], 3),
            AllDiffConstraint(variables)
        ]
        csp = ConstraintSatisfactionProblem(variables, constraints)
        solver = Solver()

        # Act
        result = solver.solve(csp)

        # Assert
        self.assertTrue(result.success)
        assignment = result.assignment
        self.assertIsNotNone(assignment)
        self.assertEqual(3, assignment.get(variables[2].name))
        encountered_values = set()
        for variable in variables:
            value = assignment.get(variable.name)
            self.assertTrue(value in domain)
            self.assertFalse(value in encountered_values)
            encountered_values.add(value)
    def test_not_consistent_if_assignment_violates_constraint(self):
        # Arrange
        variables = [
            Variable('1', Domain([1, 2, 3])),
            Variable('2', Domain([1, 2]))
        ]
        constraints = [EqualConstraint(variables[0], 2)]
        csp = ConstraintSatisfactionProblem(variables, constraints)
        assignment = Assignment()
        assignment.set(variables[0].name, 3)

        # Act
        consistent = csp.consistent(assignment)

        # Assert
        self.assertFalse(consistent)
    def test_select_unassigned_variable_returns_valid_variable(self):
        # Arrange
        variables = [
            Variable('1', Domain([1, 2, 3])),
            Variable('2', Domain([1, 2]))
        ]
        constraints = [EqualConstraint(variables[0], 2)]
        csp = ConstraintSatisfactionProblem(variables, constraints)
        assignment = Assignment()
        assignment.set(variables[0].name, 2)

        # Act
        variable = csp.select_unassigned_variable(assignment)

        # Assert
        self.assertIsNotNone(variable)
        self.assertEqual(variables[1].name, variable.name)
    def test_select_unassigned_variable_raises_exception_if_variable_not_available(
            self):
        # Arrange
        exception_thrown = False
        variables = [Variable('1', Domain([1]))]
        constraints = []
        csp = ConstraintSatisfactionProblem(variables, constraints)
        assignment = Assignment()
        assignment.set(variables[0].name, 1)

        # Act
        try:
            csp.select_unassigned_variable(assignment)
        except:
            exception_thrown = True

        # Assert
        self.assertTrue(exception_thrown)
    def test_consistent_and_complete_for_full_assignment(self):
        # Arrange
        variables = [
            Variable('1', Domain([1, 2, 3])),
            Variable('2', Domain([1, 2]))
        ]
        constraints = [EqualConstraint(variables[0], 2)]
        csp = ConstraintSatisfactionProblem(variables, constraints)
        assignment = Assignment()
        assignment.set(variables[0].name, 2)
        assignment.set(variables[1].name, 1)

        # Act
        consistent = csp.consistent(assignment)
        complete = csp.complete(assignment)

        # Assert
        self.assertTrue(consistent)
        self.assertTrue(complete)
Пример #8
0
    def test_solver_failure_one_variable_one_constraint(self):
        # Arrange
        variable = Variable('1', Domain([1, 2, 3, 4, 5]))
        constraint = EqualConstraint(variable, 10)
        csp = ConstraintSatisfactionProblem([variable], [constraint])
        solver = Solver()

        # Act
        result = solver.solve(csp)

        # Assert
        self.assertFalse(result.success)
        self.assertIsNone(result.assignment)
Пример #9
0
    def test_solver_success_one_variable_one_constraint(self):
        # Arrange
        expected_value = 3
        variable = Variable('1', Domain([1, 2, 3, 4, 5]))
        constraint = EqualConstraint(variable, expected_value)
        csp = ConstraintSatisfactionProblem([variable], [constraint])
        solver = Solver()

        # Act
        result = solver.solve(csp)

        # Assert
        self.assertTrue(result.success)
        self.assertIsNotNone(result.assignment)
        self.assertEqual(expected_value, result.assignment.get(variable.name))
Пример #10
0
 def prepare(self, board):
     variables = self._build_variables(board)
     constraints = self._build_constraints(board, variables)
     self._csp = ConstraintSatisfactionProblem(variables, constraints)