Esempio n. 1
0
    def test_constraint(self):
        reference_block = Block(self.reference_polygon, 1, self.domain)
        reference_value = Block.Value(0, 0, 0)
        block_with_same_color = Block(self.reference_polygon, 1, self.domain)
        block_with_different_color = Block(self.reference_polygon, 2, self.domain)

        self.assertTrue(Block.check_constraint(
            reference_block, reference_value,
            block_with_different_color, Block.Value(1, 0, 0)
        ))

        self.assertFalse(Block.check_constraint(
            reference_block, reference_value,
            block_with_same_color, Block.Value(1, 0, 0)
        ))

        self.assertFalse(Block.check_constraint(
            reference_block, reference_value,
            block_with_different_color, Block.Value(0, 0, 0)
        ))

        self.assertFalse(Block.check_constraint(
            reference_block, reference_value,
            block_with_same_color, Block.Value(0, 0, 0)
        ))

        self.assertTrue(Block.check_constraint(
            reference_block, reference_value,
            block_with_different_color, Block.Value(1, 1, 0)
        ))

        self.assertTrue(Block.check_constraint(
            reference_block, reference_value,
            block_with_same_color, Block.Value(1, 1, 0)
        ))
Esempio n. 2
0
 def is_solution_sound(solved_problem):
     for source_variable in solved_problem.variables:
         for destination_variable in solved_problem.variables:
             if source_variable is destination_variable:
                 continue
             if source_variable.domain.__len__() !=  1:
                 return False
             if destination_variable.domain.__len__() != 1:
                 return False
             constraint_is_complied = Block.check_constraint(
                 source_variable, source_variable.domain[0],
                 destination_variable, destination_variable.domain[0]
             )
             if not constraint_is_complied:
                 return False
     return True
Esempio n. 3
0
    def get_propositional_logic_cnf(self):

        logic_variables = []
        clauses = []
        clauses_to_ensure_at_least_one_assignment = []
        for variable in self.variables:
            clause_to_ensure_at_least_one_assignment = []
            for value in variable.domain:
                logic_variable = (variable, value)
                logic_variables.append(
                    logic_variable
                )
                clause_to_ensure_at_least_one_assignment.append((logic_variable, True))
                for other_value in variable.domain:
                    other_logic_variable = (variable, other_value)
                    if value is other_value:
                        continue
                    clause_to_ensure_at_most_one_assignment = [
                        (logic_variable, False), (other_logic_variable, False)
                    ]
                    clauses.append(clause_to_ensure_at_most_one_assignment)
            clauses_to_ensure_at_least_one_assignment.append(clause_to_ensure_at_least_one_assignment)

        for first_logic_variable in logic_variables:
            for second_logic_variable in logic_variables:
                if first_logic_variable is second_logic_variable:
                    continue
                there_is_conflict = not Block.check_constraint(
                    first_logic_variable[0], first_logic_variable[1],
                    second_logic_variable[0], second_logic_variable[1]
                )
                if there_is_conflict:
                    clause_to_ensure_constraint = [
                        (first_logic_variable, False), (second_logic_variable, False)
                    ]

                    clauses.append(clause_to_ensure_constraint)

        clauses.extend(clauses_to_ensure_at_least_one_assignment)

        return clauses