def setUp(self): super().setUp() self.reference_polygon = Polygon([ (0, 0), (1, 0), (1, 1), (0, 1), ]) self.side_tangent_polygon = Polygon([ (1, 0), (2, 0), (2, 1), (1, 1), ]) self.point_tangent_polygon = Polygon([ (1, 1), (2, 1), (2, 2), (1, 2), ]) self.crossing_polygon = Polygon([ (0, 0.5), (1, 0.5), (1, 1.5), (0, 1.5), ]) domain = [] for x in range(3): for y in range(3): for rotation in range(0, 360, 90): domain.append( Block.Value(x, y, rotation) ) self.domain = domain
def test_arc_consistency_checking_algorithm(self): domain = [] for x in range(3): for y in range(3): for rotation in range(0, 360, 90): domain.append( Block.Value(x, y, rotation) ) original_problem = BlockCSPProblem([ Block(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]), 1, domain), Block(Polygon([(0, 0), (3, 0), (3, 3), (1, 3), (1, 2), (0, 2)]), 2, domain), ], Polygon([(0, 0), (3, 0), (3, 3), (0, 3)]) ) still_consistent = True for source_variable in original_problem.variables: for destination_variable in original_problem.variables: if source_variable is destination_variable: continue if not source_variable.is_arc_consistent_with(destination_variable): still_consistent = False self.assertFalse(still_consistent) consistent_problem = arc_consistency_checking_algorithm(original_problem) for source_variable in consistent_problem.variables: for destination_variable in consistent_problem.variables: if source_variable is destination_variable: continue self.assertTrue(source_variable.is_arc_consistent_with(destination_variable))
def test_simple_csp_to_logic_conversion(self): domain = [] for x in range(3): for y in range(3): for rotation in range(0, 360, 90): domain.append(Block.Value(x, y, rotation)) original_problem = BlockCSPProblem([ Block(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]), 1, domain), Block(Polygon([(0, 0), (3, 0), (3, 3), (1, 3), (1, 2), (0, 2)]), 2, domain), ], Polygon([(0, 0), (3, 0), (3, 3), (0, 3)])) logic_problem = original_problem.get_propositional_logic_cnf() solved_problem = dfs_with_ac3(original_problem) self.assertIsNotNone(solved_problem) self.assertTrue( TestBlockLogicProblem.does_solution_satisfy_logic_problem( solved_problem, logic_problem)) self.assertFalse( TestBlockLogicProblem.does_solution_satisfy_logic_problem( original_problem, logic_problem))
def test_simple_block_logic_problem(self): domain = [] for x in range(2): for y in range(1): for rotation in range(0, 360, 90): domain.append(Block.Value(x, y, rotation)) original_problem = BlockCSPProblem([ Block(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]), 1, domain), Block(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]), 0, domain), ], Polygon([(0, 0), (3, 0), (3, 3), (0, 3)])) logic_problem = original_problem.get_propositional_logic_cnf() solved_problem = dpll(logic_problem) self.assertIsNotNone(solved_problem) self.assertTrue( evaluate_clauses_with_model(logic_problem, solved_problem)) original_problem.import_cnf_model(solved_problem) self.assertTrue(BlockCSPProblem.is_solution_sound(original_problem)) domain = [] for x in range(3): for y in range(3): for rotation in range(0, 360, 90): domain.append(Block.Value(x, y, rotation)) original_problem = BlockCSPProblem([ Block(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]), 1, domain), Block(Polygon([(0, 0), (3, 0), (3, 3), (1, 3), (1, 2), (0, 2)]), 2, domain), ], Polygon([(0, 0), (3, 0), (3, 3), (0, 3)])) logic_problem = original_problem.get_propositional_logic_cnf() solved_problem = dpll(logic_problem) self.assertIsNotNone(solved_problem) self.assertTrue( evaluate_clauses_with_model(logic_problem, solved_problem)) original_problem.import_cnf_model(solved_problem) self.assertTrue(BlockCSPProblem.is_solution_sound(original_problem))
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) ))
def test_dfs_with_ac3_simple(self): domain = [] for x in range(3): for y in range(3): for rotation in range(0, 360, 90): domain.append( Block.Value(x, y, rotation) ) original_problem = BlockCSPProblem([ Block(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]), 1, domain), Block(Polygon([(0, 0), (3, 0), (3, 3), (1, 3), (1, 2), (0, 2)]), 2, domain), ], Polygon([(0, 0), (3, 0), (3, 3), (0, 3)])) self.help_test_dfs_with_ac3_with_problem(original_problem)
from algorithm.logic import dpll from model import Block, BlockCSPProblem from algorithm.csp import dfs_with_ac3 from shapely.geometry import Polygon, Point import timeit m, n, p = map(int, input().split()) space = Polygon([(0, 0), (n, 0), (n, m), (0, m)]) domain = [] for i in range(n): for j in range(m): for rotation in range(0, 360, 90): domain.append(Block.Value(i, j, rotation)) blocks = [] for i in range(p): k, c = map(int, input().split()) pieces = [] for j in range(k): line = input() for x in range(len(line)): if line[x] == '*': pieces.append( Polygon([(x, j), (x + 1, j), (x + 1, j + 1), (x, j + 1)])) block_polygon = pieces[0] for piece in pieces: block_polygon = block_polygon.union(piece).simplify(0)