class TestGridDB(unittest.TestCase):
    def __init__(self, methodName='runTest'):
        super(TestGridDB, self).__init__(methodName)
        self.g1 = DynamicBoundGrid(4, 4)
        self.g1.set_search((0, 0), (3, 3))
        self.g2 = DynamicBoundGridWithShortcuts(4, 4)
        self.g2.set_search((0, 0), (3, 3))

    def test_case1(self):
        self.assertSetEqual(set(self.g1.neighbors((0, 0))),
                            set([(1, 0), (0, 1), (1, 1)]))

    def test_case2(self):
        self.assertSetEqual(set(self.g2.neighbors((0, 0))),
                            set([(1, 0), (0, 1), (1, 1)]))
class TestAstar(unittest.TestCase):
    def __init__(self, methodName='runTest'):
        super(TestAstar, self).__init__(methodName)
        self.g1 = GridWithWeights(4, 4)
        self.g2 = EightDirectionGrid(4, 4)
        self.g3 = DynamicBoundGrid(4, 4)
        self.g4 = DynamicBoundGridWithShortcuts(4, 4)
        self.g5 = DenseGraph(self.g4)
        self.g6 = DualityGraph(4, 4)
        
    def test_case1(self):
        start = (0, 0)
        goal = (3, 3)
        came_from, cost_so_far = a_star_search(self.g1, start, goal)
        path  = reconstruct_path(came_from, start, goal)
        answer = [(0, 0), (0, 1), (1, 1), (1, 2), (2, 2), (2, 3), (3, 3)]
        self.assertEqual(path, answer)

    def test_case2(self):
        start = (0, 0)
        goal = (3, 3)
        came_from, cost_so_far = a_star_search(self.g2, start, goal)
        path  = reconstruct_path(came_from, start, goal)
        answer = [(0, 0), (1, 1), (2, 2), (3, 3)]
        self.assertEqual(path, answer)
    
    def test_case3(self):
        start = (0, 0)
        goal = (3, 3)
        self.g3.set_search(start, goal)
        came_from, cost_so_far = a_star_search(self.g3, start, goal)
        path  = reconstruct_path(came_from, start, goal)
        answer = [(0, 0), (1, 1), (2, 2), (3, 3)]
        self.assertEqual(path, answer)
    
    def test_case4(self):
        start = (0, 0)
        goal = (3, 3)
        self.g4.set_search(start, goal)
        came_from, cost_so_far = a_star_search(self.g4, start, goal)
        path  = reconstruct_path(came_from, start, goal)
        answer = [(0, 0), (1, 1), (2, 2), (3, 3)]
        self.assertEqual(path, answer)
    
    def test_case5(self):
        start = (0, 0)
        goal = (3, 3)
        self.g5.set_search(start, goal)
        came_from, cost_so_far = a_star_search(self.g5, start, goal)
        path  = reconstruct_path(came_from, start, goal)
        answer = [(0, 0), (1, 1), (2, 2), (3, 3)]
        self.assertEqual(path, answer)
    
    def test_case6(self):
        start = (0, 0)
        goal = (3, 3)
        self.g6.set_search(start, goal)
        came_from, cost_so_far = a_star_search(self.g6, start, goal)
        path  = reconstruct_path(came_from, start, goal)
        answer = [(0, 0), (3, 3)]
        self.assertEqual(path, answer)
Exemple #3
0
#    start1, goal1 = (00, 500), (100, 5100)
#    start2, goal2 = (10, 500), (100, 5200)
#    start3, goal3 = (20, 500), (100, 5300)

    start1, goal1 = (500, 500), (600, 5100)
    start2, goal2 = (510, 500), (600, 5200)
    start3, goal3 = (520, 500), (600, 5300)

    plt.figure()
    plt.scatter([pos[0] for pos in grid2.walls],
                [pos[1] for pos in grid2.walls],
                color='black')

    t0 = time.time()
    print('1')
    grid1.set_search(start1, goal1)

    t1 = time.time()
    came_from, cost_so_far = a_star_search(grid1, start1, goal1, p=p)
    ts1 = time.time() - t1

    path = reconstruct_path(came_from, start1, goal1)
    path1 = reduce_path(path)
    for i in range(1, len(path1)):
        grid1.walls |= solid_octagon_line(path1[i - 1], path1[i], 5)
    print('2')
    grid1.set_search(start2, goal2)

    t1 = time.time()
    came_from, cost_so_far = a_star_search(grid1, start2, goal2, p=p)
    ts2 = time.time() - t1