def test_inferencesReturnsColOfNewCoord(self): board = ((1, 2, 2, 2, 2, 2, 2, 2, 2), (1, 2, 2, 2, 2, 4, 4, 4, 2), (1, 3, 3, 3, 3, 4, 4, 4, 2), (1, 3, 5, 3, 5, 4, 6, 4, 9), (1, 3, 5, 5, 5, 4, 6, 4, 9), (1, 3, 5, 7, 5, 9, 6, 9, 9), (1, 1, 1, 7, 7, 9, 9, 9, 8), (1, 1, 7, 7, 8, 8, 8, 8, 8), (1, 1, 1, 7, 8, 8, 8, 8, 8)) csp = bt.CSP(board) csp.X={'1A', '1B', '2A', '2B', '3A', '3B'} csp.D={ '1A':{(0,0), (0,1), (0,2)}, '1B':{(0,0), (0,1), (0,2)}, '2A':{(1,0), (1,1), (1,2), (2,0), (2,1), (2,2)}, '2B':{(1,0), (1,1), (1,2), (2,0), (2,1), (2,2)}, '3A':{(3,0), (3,1), (3,2), (3,3), (2,3), (1,3), (0,3), (4,0), (4,1), (4,2), (4,3), (4,4), (3,4), (2,4), (1,4), (0,4)}, '3B':{(3,0), (3,1), (3,2), (3,3), (2,3), (1,3), (0,3), (4,0), (4,1), (4,2), (4,3), (4,4), (3,4), (2,4), (1,4), (0,4)}} vars=bt.init_variables([1,2]) assignments=bt.init_assignments(vars) assignment = bt.Assignment(assignments,set()) assignment.vals['3B']=(0,4) assignment.vals['3A']=(4,4) inferences = csp.inference( '3A', assignment) self.assertIn((2,4), inferences)
def test_inferencesReturnsEightNeighborsAndPosOfNewCoord(self): board = ((1, 2, 2, 2, 2, 2, 2, 2, 2), (1, 2, 2, 2, 2, 4, 4, 4, 2), (1, 3, 3, 3, 3, 4, 4, 4, 2), (1, 3, 5, 3, 5, 4, 6, 4, 9), (1, 3, 5, 5, 5, 4, 6, 4, 9), (1, 3, 5, 7, 5, 9, 6, 9, 9), (1, 1, 1, 7, 7, 9, 9, 9, 8), (1, 1, 7, 7, 8, 8, 8, 8, 8), (1, 1, 1, 7, 8, 8, 8, 8, 8)) csp = bt.CSP(board) csp.X={'1A', '1B', '2A', '2B'} csp.D={ '1A':{(0,0), (0,1), (0,2)}, '1B':{(0,0), (0,1), (0,2)}, '2A':{(1,0), (1,1), (1,2), (2,0), (2,1), (2,2)}, '2B':{(1,0), (1,1), (1,2), (2,0), (2,1), (2,2)}} vars=bt.init_variables([1,2]) assignments=bt.init_assignments(vars) assignment = bt.Assignment(assignments,set()) assignment.vals['2A']=(1,1) inferences = csp.inference('2A', assignment) self.assertIn((0,0), inferences) self.assertIn((0,1), inferences) self.assertIn((0,2), inferences) self.assertIn((1,0), inferences) self.assertIn((1,1), inferences) self.assertIn((1,2), inferences) self.assertIn((2,0), inferences) self.assertIn((2,1), inferences) self.assertIn((2,2), inferences)
def test_selectUnassignedVariable(self): board = ((1, 2, 2, 2, 2, 2, 2, 2, 2), (1, 2, 2, 2, 2, 4, 4, 4, 2), (1, 3, 3, 3, 3, 4, 4, 4, 2), (1, 3, 5, 3, 5, 4, 6, 4, 9), (1, 3, 5, 5, 5, 4, 6, 4, 9), (1, 3, 5, 7, 5, 9, 6, 9, 9), (1, 1, 1, 7, 7, 9, 9, 9, 8), (1, 1, 7, 7, 8, 8, 8, 8, 8), (1, 1, 1, 7, 8, 8, 8, 8, 8)) csp = bt.CSP(board) csp.X={'1A', '1B', '2A', '2B'} csp.D={ '1A':{(0,0)}, '1B':{(0,0), (0,1)}, '2A':{(1,0), (1,1)}, '2B':{(1,0), (1,1)}} assignment= bt.Assignment(bt.init_assignments(['1A', '1B', '2A', '2B']), set()) minVar= csp.select_unassigned_var(assignment) self.assertEqual(minVar, '1A')
def test_inference_returns_assignments(self): board = ((1, 2, 2, 2, 2, 2, 2, 2, 2), (1, 2, 2, 2, 2, 4, 4, 4, 2), (1, 3, 3, 3, 3, 4, 4, 4, 2), (1, 3, 5, 3, 5, 4, 6, 4, 9), (1, 3, 5, 5, 5, 4, 6, 4, 9), (1, 3, 5, 7, 5, 9, 6, 9, 9), (1, 1, 1, 7, 7, 9, 9, 9, 8), (1, 1, 7, 7, 8, 8, 8, 8, 8), (1, 1, 1, 7, 8, 8, 8, 8, 8)) csp = bt.CSP(board) csp.X={'1A', '1B', '2A', '2B'} csp.D={ '1A':{(0,0), (0,1)}, '1B':{(0,0), (0,1)}, '2A':{(1,0), (1,1)}, '2B':{(1,0), (1,1)}} var='1A' assignments=bt.Assignment({'1A':(0,1)}, set()) self.assertIn((0,1), csp.inference(var, assignments))
def test_isCompleteReturnsFalseWhenValIsNone(self): assignment = bt.Assignment({1:2, 2:None, 4:5}, set()) self.assertFalse(bt.is_complete(assignment))
def test_isCompleteReturnsTrueWhenAllKeysHaveVal(self): assignment = bt.Assignment({1:'a', 2:3, 4:5}, set()) self.assertTrue(bt.is_complete(assignment))