示例#1
0
    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)
示例#2
0
    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)
示例#3
0
    def test_getCol(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)}}

        row= csp.get_col((0,0))
        self.assertSetEqual(row, {(0,0),(1,0)})
示例#4
0
    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')
示例#5
0
    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))
from collections import namedtuple
import tnt_board as tb

easy1 = ((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))

easyBoardObj = tb.board(easy1)

csp = bt.CSP(easy1)
solution= csp.backtracking_search()
#print(solution.vals.values())
print('Easy 1')
easyBoardObj.print_board(solution.vals.values())
solution=None


easy2 = ((1,1,1,2,2,3,3,3,3),
(1,1,1,1,2,3,3,3,3),
(1,4,4,1,2,3,5,5,3),
(1,1,4,1,2,3,3,5,3),
(1,4,4,6,2,6,5,5,3),
(1,4,6,6,6,6,6,5,3),
(1,4,4,7,7,7,5,5,3),
(8,8,8,7,7,7,9,9,9),