Update astar.py (#6456)

* Update astar.py

Improved comments added punctuations.

* Update astar.py

* Update machine_learning/astar.py

Co-authored-by: Caeden <caedenperelliharris@gmail.com>

* Update astar.py

Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Caeden <caedenperelliharris@gmail.com>
This commit is contained in:
AHTESHAM ZAIDI 2022-10-03 03:25:24 +05:30 committed by GitHub
parent 3d33b36e92
commit 707809b000
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,41 +1,38 @@
""" """
The A* algorithm combines features of uniform-cost search and pure The A* algorithm combines features of uniform-cost search and pure heuristic search to
heuristic search to efficiently compute optimal solutions. efficiently compute optimal solutions.
A* algorithm is a best-first search algorithm in which the cost
associated with a node is f(n) = g(n) + h(n), The A* algorithm is a best-first search algorithm in which the cost associated with a
where g(n) is the cost of the path from the initial state to node n and node is f(n) = g(n) + h(n), where g(n) is the cost of the path from the initial state to
h(n) is the heuristic estimate or the cost or a path node n and h(n) is the heuristic estimate or the cost or a path from node n to a goal.
from node n to a goal.A* algorithm introduces a heuristic into a
regular graph-searching algorithm, The A* algorithm introduces a heuristic into a regular graph-searching algorithm,
essentially planning ahead at each step so a more optimal decision essentially planning ahead at each step so a more optimal decision is made. For this
is made.A* also known as the algorithm with brains reason, A* is known as an algorithm with brains.
https://en.wikipedia.org/wiki/A*_search_algorithm
""" """
import numpy as np import numpy as np
class Cell: class Cell:
""" """
Class cell represents a cell in the world which have the property Class cell represents a cell in the world which have the properties:
position : The position of the represented by tupleof x and y position: represented by tuple of x and y coordinates initially set to (0,0).
coordinates initially set to (0,0) parent: Contains the parent cell object visited before we arrived at this cell.
parent : This contains the parent cell object which we visited g, h, f: Parameters used when calling our heuristic function.
before arrinving this cell
g,h,f : The parameters for constructing the heuristic function
which can be any function. for simplicity used line
distance
""" """
def __init__(self): def __init__(self):
self.position = (0, 0) self.position = (0, 0)
self.parent = None self.parent = None
self.g = 0 self.g = 0
self.h = 0 self.h = 0
self.f = 0 self.f = 0
""" """
overrides equals method because otherwise cell assign will give Overrides equals method because otherwise cell assign will give
wrong results wrong results.
""" """
def __eq__(self, cell): def __eq__(self, cell):
@ -48,8 +45,8 @@ class Cell:
class Gridworld: class Gridworld:
""" """
Gridworld class represents the external world here a grid M*M Gridworld class represents the external world here a grid M*M
matrix matrix.
world_size: create a numpy array with the given world_size default is 5 world_size: create a numpy array with the given world_size default is 5.
""" """
def __init__(self, world_size=(5, 5)): def __init__(self, world_size=(5, 5)):
@ -90,10 +87,10 @@ class Gridworld:
def astar(world, start, goal): def astar(world, start, goal):
""" """
Implementation of a start algorithm Implementation of a start algorithm.
world : Object of the world object world : Object of the world object.
start : Object of the cell as start position start : Object of the cell as start position.
stop : Object of the cell as goal position stop : Object of the cell as goal position.
>>> p = Gridworld() >>> p = Gridworld()
>>> start = Cell() >>> start = Cell()
@ -137,14 +134,14 @@ def astar(world, start, goal):
if __name__ == "__main__": if __name__ == "__main__":
world = Gridworld() world = Gridworld()
# stat position and Goal # Start position and goal
start = Cell() start = Cell()
start.position = (0, 0) start.position = (0, 0)
goal = Cell() goal = Cell()
goal.position = (4, 4) goal.position = (4, 4)
print(f"path from {start.position} to {goal.position}") print(f"path from {start.position} to {goal.position}")
s = astar(world, start, goal) s = astar(world, start, goal)
# Just for visual reasons # Just for visual reasons.
for i in s: for i in s:
world.w[i] = 1 world.w[i] = 1
print(world.w) print(world.w)