From 44eb9ba3b292afadd6b65f550965c1c3f077d1fb Mon Sep 17 00:00:00 2001 From: AHTESHAM ZAIDI Date: Sat, 1 Oct 2022 15:16:13 +0530 Subject: [PATCH 1/4] Update astar.py Improved comments added punctuations. --- machine_learning/astar.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/machine_learning/astar.py b/machine_learning/astar.py index ee3fcff0b7bf..109028627f52 100644 --- a/machine_learning/astar.py +++ b/machine_learning/astar.py @@ -8,7 +8,7 @@ from node n to a goal.A* algorithm introduces a heuristic into a regular graph-searching algorithm, essentially planning ahead at each step so a more optimal decision -is made.A* also known as the algorithm with brains +is made. A* also known as the algorithm with brains. """ import numpy as np @@ -17,12 +17,12 @@ class Cell: """ Class cell represents a cell in the world which have the property position : The position of the represented by tupleof x and y - coordinates initially set to (0,0) + coordinates initially set to (0,0). parent : This contains the parent cell object which we visited - before arrinving this cell + before arrinving this cell. g,h,f : The parameters for constructing the heuristic function - which can be any function. for simplicity used line - distance + which can be any function. + For simplicity used line distance. """ def __init__(self): @@ -34,8 +34,8 @@ def __init__(self): self.f = 0 """ - overrides equals method because otherwise cell assign will give - wrong results + Overrides equals method because otherwise cell assign will give + wrong results. """ def __eq__(self, cell): @@ -48,8 +48,8 @@ def showcell(self): class Gridworld: """ Gridworld class represents the external world here a grid M*M - matrix - world_size: create a numpy array with the given world_size default is 5 + matrix. + world_size: create a numpy array with the given world_size default is 5. """ def __init__(self, world_size=(5, 5)): @@ -90,10 +90,10 @@ def get_neigbours(self, cell): def astar(world, start, goal): """ - Implementation of a start algorithm - world : Object of the world object - start : Object of the cell as start position - stop : Object of the cell as goal position + Implementation of a start algorithm. + world : Object of the world object. + start : Object of the cell as start position. + stop : Object of the cell as goal position. >>> p = Gridworld() >>> start = Cell() @@ -137,14 +137,14 @@ def astar(world, start, goal): if __name__ == "__main__": world = Gridworld() - # stat position and Goal + # Stat position and goal. start = Cell() start.position = (0, 0) goal = Cell() goal.position = (4, 4) print(f"path from {start.position} to {goal.position}") s = astar(world, start, goal) - # Just for visual reasons + # Just for visual reasons. for i in s: world.w[i] = 1 print(world.w) From 2b92cd7f2ff9ff6b6e05447e99d7e6f22feca079 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 2 Oct 2022 23:40:59 +0200 Subject: [PATCH 2/4] Update astar.py --- machine_learning/astar.py | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/machine_learning/astar.py b/machine_learning/astar.py index 109028627f52..27ba5dabd012 100644 --- a/machine_learning/astar.py +++ b/machine_learning/astar.py @@ -1,34 +1,31 @@ """ -The A* algorithm combines features of uniform-cost search and pure -heuristic search to 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), -where g(n) is the cost of the path from the initial state to node n and -h(n) is the heuristic estimate or the cost or a path -from node n to a goal.A* algorithm introduces a heuristic into a -regular graph-searching algorithm, -essentially planning ahead at each step so a more optimal decision -is made. A* also known as the algorithm with brains. +The A* algorithm combines features of uniform-cost search and pure heuristic search to +efficiently compute optimal solutions. + +The A* algorithm is a best-first search algorithm in which the cost associated with a +node is f(n) = g(n) + h(n), where g(n) is the cost of the path from the initial state to +node n and h(n) is the heuristic estimate or the cost or a path from node n to a goal. + +The A* algorithm introduces a heuristic into a regular graph-searching algorithm, +essentially planning ahead at each step so a more optimal decision is made. For this +reason, A* is known as an algorithm with brains. + +https://en.wikipedia.org/wiki/A*_search_algorithm """ import numpy as np class Cell: """ - Class cell represents a cell in the world which have the property - position : The position of the represented by tupleof x and y - coordinates initially set to (0,0). - parent : This contains the parent cell object which we visited - before arrinving this cell. - g,h,f : The parameters for constructing the heuristic function - which can be any function. - For simplicity used line distance. + Class cell represents a cell in the world which have the properties: + position: represented by tuple of x and y coordinates initially set to (0,0). + parent: Contains the parent cell object visited before we arrived at this cell. + g, h, f: Parameters used when calling our heuristic function. """ def __init__(self): self.position = (0, 0) self.parent = None - self.g = 0 self.h = 0 self.f = 0 From 0ef1cc5deefbc0c676e7607767ad0be6e0a918d3 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 2 Oct 2022 23:41:49 +0200 Subject: [PATCH 3/4] Update machine_learning/astar.py Co-authored-by: Caeden --- machine_learning/astar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/astar.py b/machine_learning/astar.py index 27ba5dabd012..ca884d7fc2e9 100644 --- a/machine_learning/astar.py +++ b/machine_learning/astar.py @@ -134,7 +134,7 @@ def astar(world, start, goal): if __name__ == "__main__": world = Gridworld() - # Stat position and goal. + # Stat position and goal start = Cell() start.position = (0, 0) goal = Cell() From 93f20d835329408f226916947fcc56f8ad22163a Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 2 Oct 2022 23:42:42 +0200 Subject: [PATCH 4/4] Update astar.py --- machine_learning/astar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/astar.py b/machine_learning/astar.py index ca884d7fc2e9..7a60ed225a2d 100644 --- a/machine_learning/astar.py +++ b/machine_learning/astar.py @@ -134,7 +134,7 @@ def astar(world, start, goal): if __name__ == "__main__": world = Gridworld() - # Stat position and goal + # Start position and goal start = Cell() start.position = (0, 0) goal = Cell()