|
| 1 | + |
| 2 | +class SquareGrid(): |
| 3 | + |
| 4 | + def __init__(self, width, height): |
| 5 | + self.width = width |
| 6 | + self.height = height |
| 7 | + self.walls = set() |
| 8 | + |
| 9 | + def is_inside(self, point): |
| 10 | + """ Helper function to know if a given point is inside the Grid. |
| 11 | +
|
| 12 | + :param point: Tuple of two ints |
| 13 | + :return: Boolean |
| 14 | + """ |
| 15 | + x, y = point |
| 16 | + return 0 <= x < self.width and 0 <= y < self.height |
| 17 | + |
| 18 | + def is_wall(self, point): |
| 19 | + """ Helper function to know if a given point is a wall. |
| 20 | +
|
| 21 | + :param point: Tuple of two ints |
| 22 | + :return: Boolean |
| 23 | + """ |
| 24 | + return point not in self.walls |
| 25 | + |
| 26 | + def neighbors(self, point): |
| 27 | + """ Yields the valid neighbours of a given point. |
| 28 | +
|
| 29 | + :param point: Tuple of two ints |
| 30 | + :return: Generator of tuples of ints |
| 31 | + """ |
| 32 | + |
| 33 | + x, y = point |
| 34 | + candidates = [(x + 1, y), (x, y - 1), (x - 1, y), (x, y + 1)] |
| 35 | + candidates = filter(self.is_inside, candidates) |
| 36 | + candidates = filter(self.is_wall, candidates) |
| 37 | + yield from candidates |
| 38 | + |
| 39 | + def _draw_tile(self, point, style, width): |
| 40 | + """ Returns a symbol for the current point given the style dictionary and the drawing width. |
| 41 | +
|
| 42 | + :param point: Tuple of two ints |
| 43 | + :param style: Hash map of the form (int,int) : int or (int,int) : (int,int) |
| 44 | + :param width: Integer representing the width of the graph |
| 45 | + :return: string (character) |
| 46 | + """ |
| 47 | + |
| 48 | + character = "." |
| 49 | + if 'number' in style and point in style['number']: |
| 50 | + character = "%d" % style['number'][point] |
| 51 | + if 'point_to' in style and style['point_to'].get(point, None) is not None: |
| 52 | + (x1, y1) = point |
| 53 | + (x2, y2) = style['point_to'][point] |
| 54 | + if x2 == x1 + 1: |
| 55 | + character = "\u2192" |
| 56 | + if x2 == x1 - 1: |
| 57 | + character = "\u2190" |
| 58 | + if y2 == y1 + 1: |
| 59 | + character = "\u2193" |
| 60 | + if y2 == y1 - 1: |
| 61 | + character = "\u2191" |
| 62 | + if 'start' in style and point == style['start']: |
| 63 | + character = "S" |
| 64 | + if 'goal' in style and point == style['goal']: |
| 65 | + character = "E" |
| 66 | + if 'path' in style and point in style['path']: |
| 67 | + character = "@" |
| 68 | + if point in self.walls: |
| 69 | + character = "#" * width |
| 70 | + return character |
| 71 | + |
| 72 | + def draw(self, width=2, **style): |
| 73 | + """ |
| 74 | + Draws the grid given the style dictionary and the width of the drawin. |
| 75 | + :param style: Hash map of the form (int,int) : int or (int,int) : (int,int) |
| 76 | + :param width: Integer representing the width of the graph |
| 77 | + :return: None |
| 78 | + """ |
| 79 | + for y in range(self.height): |
| 80 | + for x in range(self.width): |
| 81 | + print("%%-%ds" % width % |
| 82 | + self._draw_tile((x, y), style, width), end="") |
| 83 | + print() |
| 84 | + |
| 85 | + |
| 86 | +class GridWithWeights(SquareGrid): |
| 87 | + |
| 88 | + def __init__(self, width, height): |
| 89 | + super().__init__(width, height) |
| 90 | + self.weights = {} |
| 91 | + |
| 92 | + def cost(self, from_node, to_node): |
| 93 | + """ |
| 94 | + Gives the cost of going from from_node to to_node. |
| 95 | +
|
| 96 | + :param from_node: Tuple of two ints |
| 97 | + :param to_node: Tuple of two ints |
| 98 | + :returns: int |
| 99 | + """ |
| 100 | + return self.weights.get(to_node, 1) |
| 101 | + |
| 102 | + |
| 103 | +def from_id_width(point, width): |
| 104 | + """ |
| 105 | + Helper function to truncate a int to a point in a grid. |
| 106 | +
|
| 107 | + :param point: integer |
| 108 | + :param width: integer |
| 109 | + :returns: Tuple of two ints |
| 110 | + """ |
| 111 | + return (point % width, point // width) |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + |
| 115 | + import a_star |
| 116 | + import random |
| 117 | + |
| 118 | + # Construct a cool wall collection from this aparently arbitraty points. |
| 119 | + WALLS = [from_id_width(point, width=30) for point in [21, 22, 51, 52, 81, 82, 93, 94, 111, 112, |
| 120 | + 123, 124, 133, 134, 141, 142, 153, 154, 163, |
| 121 | + 164, 171, 172, 173, 174, 175,183, 184, 193, |
| 122 | + 194, 201, 202, 203, 204, 205, 213, 214, 223, |
| 123 | + 224, 243, 244, 253, 254, 273, 274, 283, 284, |
| 124 | + 303, 304, 313, 314, 333, 334, 343, 344, 373, |
| 125 | + 374, 403, 404, 433, 434]] |
| 126 | + |
| 127 | + # Instantiate a grid of 10 x 10 |
| 128 | + graph = GridWithWeights(10, 10) |
| 129 | + |
| 130 | + # Set the walls of the grid |
| 131 | + graph.walls = set(WALLS) |
| 132 | + |
| 133 | + # Set the weighs of some points in the maze |
| 134 | + graph.weights = {location: random.randint(1,10) for location in [(3, 4), (3, 5), (4, 1), (4, 2), |
| 135 | + (4, 3), (4, 4), (4, 5), (4, 6), |
| 136 | + (4, 7), (4, 8), (5, 1), (5, 2), |
| 137 | + (5, 3), (5, 4), (5, 5), (5, 6), |
| 138 | + (5, 7), (5, 8), (6, 2), (6, 3), |
| 139 | + (6, 4), (6, 5), (6, 6), (6, 7), |
| 140 | + (7, 3), (7, 4), (7, 5)]} |
| 141 | + |
| 142 | + # Call the A* algorithm and get the frontier |
| 143 | + frontier = a_star.a_star(graph = graph, start=(1, 4), end=(7, 8)) |
| 144 | + |
| 145 | + # Print the results |
| 146 | + |
| 147 | + graph.draw(width=5, point_to = frontier.visited, start=(1, 4), goal=(7, 8)) |
| 148 | + |
| 149 | + print() |
| 150 | + |
| 151 | + graph.draw(width=5, number = frontier.costs, start=(1, 4), goal=(7, 8)) |
0 commit comments