Example #1
0
 def test_large_obstacle_grid(self):
     large_grid = DiagonalGrid(
         10, 10,
         [Coord(1, 8), Coord(5, 7),
          Coord(6, 0), Coord(7, 7)])
     obstacle_jps = JumpPointSearch(large_grid, diagonal_tie_breaker)
     path = obstacle_jps.execute((Coord(0, 0), Coord(9, 9)))
Example #2
0
 def test_forced_diagonal(self):
     obstacle_grid = DiagonalGrid(4, 4, [Coord(2, 1)])
     diag_jps = JumpPointSearch(obstacle_grid, diagonal)
     expected = Coord(3, 1)
     current = JPSNode(Coord(2, 2), Coord(1, 1))
     neighbors = set(
         diag_jps.forced_neighbors(current.coord, current.direction))
     self.assertTrue(expected in neighbors)
Example #3
0
 def test_diagonal_obstacle(self):
     expected = [
         JPSNode(Coord(0, 0), None),
         JPSNode(Coord(2, 2), Coord(1, 1)),
         JPSNode(Coord(3, 3), Coord(1, 1))
     ]
     diagonal_obstacle_grid = DiagonalGrid(4, 4, [Coord(2, 1)])
     obstacle_jps = JumpPointSearch(diagonal_obstacle_grid, diagonal)
     path = obstacle_jps.execute((Coord(0, 0), Coord(3, 3)))
     self.assertEqual(expected, path,
                      coordinate_mismatch_message(expected, path))
Example #4
0
class DiagonalGridTest(unittest.TestCase):
    def setUp(self):
        self.grid = DiagonalGrid(3, 3, [])

    def test_neighbors(self):
        coord = Coord(1, 1)
        neighbors = set(self.grid.neighbors(coord))
        self.assertEqual(
            {
                Coord(0, 0),
                Coord(0, 1),
                Coord(1, 0),
                Coord(2, 1),
                Coord(2, 2),
                Coord(0, 2),
                Coord(2, 0),
                Coord(1, 2)
            }, neighbors)

    def test_is_adjacent(self):
        self.assertFalse(self.grid.is_adjacent(Coord(0, 0), Coord(0, 0)))
        self.assertFalse(self.grid.is_adjacent(Coord(0, 0), Coord(1, 2)))
        self.assertFalse(self.grid.is_adjacent(Coord(-1, 0), Coord(1, 2)))
        self.assertFalse(self.grid.is_adjacent(Coord(-1, 0), Coord(-1, 2)))

        self.assertTrue(self.grid.is_adjacent(Coord(0, 0), Coord(0, 1)))
        self.assertTrue(self.grid.is_adjacent(Coord(1, 1), Coord(0, 1)))
        self.assertTrue(self.grid.is_adjacent(Coord(0, 0), Coord(1, 1)))

    def test_init_invalid_obstacles(self):
        obstacle_grid = DiagonalGrid(3, 3, [Coord(-1, -1), Coord(5, 5)])
        self.assertFalse(Coord(-1, -1) in obstacle_grid.obstacles())
        self.assertFalse(Coord(5, 5) in obstacle_grid.obstacles())
Example #5
0
def make_diagonal_grid(size: (int, int), obstacle_prob: int) -> DiagonalGrid:
    """
    :param size: tuple of max x and max y values
    :param obstacle_prob: Probability that an obstacle is on a tile.
    :return: AStar grid with randomly generated obstacles
    """
    obstacles = []
    xsize, ysize = size
    for i in range(0, xsize):
        for j in range(0, ysize):
            if random.randint(0, 100) < obstacle_prob:
                obstacles.append(Coord(i, j))
    return DiagonalGrid(xsize, ysize, obstacles)
Example #6
0
 def setUp(self):
     self.grid = DiagonalGrid(4, 4, [])
     self.obstacle_grid = DiagonalGrid(4, 4, [Coord(0, 2)])
     self.jps = JumpPointSearch(self.grid, diagonal)
Example #7
0
 def test_is_forced_neighbor(self):
     forced_neighbor_grid = DiagonalGrid(4, 4, [Coord(1, 2)])
     obstacle_jps = JumpPointSearch(forced_neighbor_grid, diagonal)
     neighbors = obstacle_jps.has_forced_neighbors(Coord(0, 2), Coord(0, 1))
     self.assertTrue(neighbors)
Example #8
0
 def test_obstacle_execute(self):
     obstacle_grid = DiagonalGrid(4, 4, [Coord(0, 2)])
     expected = [Coord(0, 0), Coord(0, 1), Coord(1, 0)]
Example #9
0
 def setUp(self):
     self.grid = DiagonalGrid(4, 4, [])
     self.jps = JumpPointSearch(self.grid, diagonal)
Example #10
0
 def setUp(self):
     self.grid = DiagonalGrid(3, 3, [])
Example #11
0
 def test_init_invalid_obstacles(self):
     obstacle_grid = DiagonalGrid(3, 3, [Coord(-1, -1), Coord(5, 5)])
     self.assertFalse(Coord(-1, -1) in obstacle_grid.obstacles())
     self.assertFalse(Coord(5, 5) in obstacle_grid.obstacles())