graphs.greedy_best_first ======================== .. py:module:: graphs.greedy_best_first .. autoapi-nested-parse:: https://en.wikipedia.org/wiki/Best-first_search#Greedy_BFS Attributes ---------- .. autoapisummary:: graphs.greedy_best_first.Path graphs.greedy_best_first.TEST_GRIDS graphs.greedy_best_first.delta graphs.greedy_best_first.init Classes ------- .. autoapisummary:: graphs.greedy_best_first.GreedyBestFirst graphs.greedy_best_first.Node Module Contents --------------- .. py:class:: GreedyBestFirst(grid: list[list[int]], start: tuple[int, int], goal: tuple[int, int]) >>> grid = TEST_GRIDS[2] >>> gbf = GreedyBestFirst(grid, (0, 0), (len(grid) - 1, len(grid[0]) - 1)) >>> [x.pos for x in gbf.get_successors(gbf.start)] [(1, 0), (0, 1)] >>> (gbf.start.pos_y + delta[3][0], gbf.start.pos_x + delta[3][1]) (0, 1) >>> (gbf.start.pos_y + delta[2][0], gbf.start.pos_x + delta[2][1]) (1, 0) >>> gbf.retrace_path(gbf.start) [(0, 0)] >>> gbf.search() # doctest: +NORMALIZE_WHITESPACE [(0, 0), (1, 0), (2, 0), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)] .. py:method:: get_successors(parent: Node) -> list[Node] Returns a list of successors (both in the grid and free spaces) .. py:method:: retrace_path(node: Node | None) -> Path Retrace the path from parents to parents until start node .. py:method:: search() -> Path | None Search for the path, if a path is not found, only the starting position is returned .. py:attribute:: closed_nodes :type: list[Node] :value: [] .. py:attribute:: grid .. py:attribute:: open_nodes .. py:attribute:: reached :value: False .. py:attribute:: start .. py:attribute:: target .. py:class:: Node(pos_x: int, pos_y: int, goal_x: int, goal_y: int, g_cost: float, parent: Node | None) >>> k = Node(0, 0, 4, 5, 0, None) >>> k.calculate_heuristic() 9 >>> n = Node(1, 4, 3, 4, 2, None) >>> n.calculate_heuristic() 2 >>> l = [k, n] >>> n == l[0] False >>> l.sort() >>> n == l[0] True .. py:method:: __eq__(other) -> bool .. py:method:: __lt__(other) -> bool .. py:method:: calculate_heuristic() -> float The heuristic here is the Manhattan Distance Could elaborate to offer more than one choice .. py:attribute:: f_cost .. py:attribute:: g_cost .. py:attribute:: goal_x .. py:attribute:: goal_y .. py:attribute:: parent .. py:attribute:: pos .. py:attribute:: pos_x .. py:attribute:: pos_y .. py:data:: Path .. py:data:: TEST_GRIDS :value: [[[0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0],... .. py:data:: delta .. py:data:: init :value: (0, 0)