Exemple #1
0
 def test_maze(self):
     a = pf.AStar()
     walls = ((0, 5), (1, 0), (1, 1), (1, 5), (2, 3), (3, 1), (3, 2),
              (3, 5), (4, 1), (4, 4), (5, 1))
     a.init_grid(6, 6, walls, (0, 0), (5, 5))
     path = a.solve()
     self.assertEqual(path, [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 4),
                             (2, 4), (3, 4), (3, 3), (4, 3), (5, 3), (5, 4),
                             (5, 5)])
Exemple #2
0
 def test_maze_no_walls(self):
     a = pf.AStar()
     walls, expected_path, start, end = self.from_sketch([
         "[...  ",
         "   .  ",
         "   .. ",
         "    . ",
         "    . ",
         "    .]",
     ])
     a.init_grid(6, 6, walls, start, end)
     self.assertPathEquals(walls, expected_path, a.solve())
Exemple #3
0
 def test_maze_diagonal(self):
     a = pf.AStar()
     walls, expected_path, start, end = self.from_sketch([
         "[#    ",
         ".# ###",
         ". #...",
         " .#.#.",
         " ...#.",
         "## # ]",
     ])
     a.init_grid(6, 6, walls, start, end)
     self.assertPathEquals(walls, expected_path, a.solve(True))
Exemple #4
0
 def test_maze_closest_solution3(self):
     a = pf.AStar()
     walls, expected_path, start, end = self.from_sketch([
         "[#    ",
         ".# ###",
         ".# #  ",
         ".##   ",
         "....##",
         "## ###",
     ])
     a.init_grid(6, 6, walls, start, (5, 5))
     self.assertPathEquals(walls, expected_path, a.solve(True, True))
Exemple #5
0
 def test_maze_no_solution(self):
     a = pf.AStar()
     walls, expected_path, start, end = self.from_sketch([
         "(#    ",
         " # ###",
         " # #  ",
         " ##   ",
         "    ##",
         "##  #)",
     ])
     a.init_grid(6, 6, walls, start, end)
     self.assertPathEquals(walls, expected_path, a.solve())
Exemple #6
0
 def test_start_eq_end(self):
     a = pf.AStar()
     walls, expected_path, start, end = self.from_sketch([
         ".     ",
         "      ",
         "      ",
         "      ",
         "      ",
         "      ",
     ])
     a.init_grid(6, 6, walls, (0, 0), (0, 0))
     self.assertPathEquals(walls, expected_path, a.solve())
Exemple #7
0
    def is_valid_map(self, pos_agent, pos_walls, pos_rewards):
        a = pf.AStar()
        pos_walls
        walls = [tuple(w) for w in pos_walls]
        start = tuple(pos_agent)
        for r in pos_rewards:
            end = tuple(r)
            a.init_grid(self._size_maze, self._size_maze, walls, start, end)
            maze = a
            optimal_path = maze.solve()
            if (optimal_path == None):
                return False

        return True
import algorithms.a_star_path_finding as pf
import astar_utils as autils
reload(autils)

cnt = 0
pmax = 0
for jj in range(1000):
    a = pf.AStar()
    N = 100
    walls = []
    start = (0, 0)
    end = (N - 1, N - 1)
    for ii in range(int((N**2) * .75)):
        w = (randint(0, N), randint(0, N))
        if (w == start) or (w == end):
            pass
        else:
            walls.append(w)

    a.init_grid(N, N, tuple(walls), start, end)
    path = a.solve()

    if not (path is None):
        cnt += 1
        if len(path) > pmax:
            pmax = len(path)
            fig = figure(jj)
            clf()
            autils.plotAstar(a, path)

show()
Exemple #9
0
 def test_maze_no_solution(self):
     a = pf.AStar()
     walls = ((0, 5), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5),
              (2, 3), (3, 1), (3, 2), (3, 5), (4, 1), (4, 4), (5, 1))
     a.init_grid(6, 6, walls, (0, 0), (5, 5))
     self.assertIsNone(a.solve())
Exemple #10
0
 def test_maze_no_walls(self):
     a = pf.AStar()
     walls = ()
     a.init_grid(6, 6, walls, (0, 0), (5, 5))
     path = a.solve()
     self.assertEqual(len(path), 11)