def test_not_violated_if_variable_equal(self):
        # Arrange
        expected_value = 1
        variable = Variable('1', Domain([expected_value]))
        constraint = EqualConstraint(variable, expected_value)
        assignment = Assignment()
        assignment.set(variable.name, expected_value)

        # Act
        result = constraint.is_violated(assignment)

        # Assert
        self.assertFalse(result)
示例#2
0
    def test_unset_removes_assignment(self):
        # Arrange
        variable_name = 'A'
        assignment = Assignment()
        assignment.set(variable_name, 10)

        # Act
        assignment.unset(variable_name)

        # Assert
        self.assertEqual(0, assignment.count())
        self.assertFalse(assignment.is_set(variable_name))
        self.assertIsNone(assignment.get(variable_name))
示例#3
0
    def test_set_adds_assignment(self):
        # Arrange
        assignment = Assignment()
        variable_name = 'A'
        variable_value = 10

        # Act
        assignment.set(variable_name, variable_value)

        # Assert
        self.assertEqual(1, assignment.count())
        self.assertTrue(assignment.is_set(variable_name))
        self.assertEqual(variable_value, assignment.get(variable_name))
    def test_not_violated_if_all_variables_different(self):
        # Arrange
        assignment = Assignment()
        variables = []
        for i in range(9):
            variable = Variable(str(i), Domain(list(range(1, 10))))
            variables.append(variable)
            assignment.set(variable.name, i + 1)
        constraint = AllDiffConstraint(variables)

        # Act
        result = constraint.is_violated(assignment)

        # Assert
        self.assertFalse(result)
    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_consistent_for_partial_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)

        # Act
        consistent = csp.consistent(assignment)

        # Assert
        self.assertTrue(consistent)
    def test_not_violated_if_some_variables_not_set(self):
        # Arrange
        assignment = Assignment()
        variables = []
        for i in range(9):
            variable = Variable(str(i), Domain(list(range(1, 10))))
            variables.append(variable)
            if i < 4:
                # Only assign some of the values
                assignment.set(variable.name, i + 1)
        constraint = AllDiffConstraint(variables)

        # Act
        result = constraint.is_violated(assignment)

        # Assert
        self.assertFalse(result)
    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_violated_if_at_least_one_variable_not_different(self):
        # Arrange
        assignment = Assignment()
        variables = []
        for i in range(8):
            variable = Variable(str(i), Domain(list(range(1, 10))))
            variables.append(variable)
            assignment.set(variable.name, i + 1)
        non_unique_variable = Variable('8', Domain(list(range(1, 10))))
        variables.append(non_unique_variable)
        assignment.set(non_unique_variable.name, 1)
        constraint = AllDiffConstraint(variables)

        # Act
        result = constraint.is_violated(assignment)

        # Assert
        self.assertTrue(result)
    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)