From 91ce618fdc6f34e8628780f549826368c3fd0bc3 Mon Sep 17 00:00:00 2001 From: Jeffin Francis Date: Wed, 29 Apr 2020 19:07:23 +0530 Subject: [PATCH 1/8] a* algorithm --- machine_learning/a_star.py | 152 +++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 machine_learning/a_star.py diff --git a/machine_learning/a_star.py b/machine_learning/a_star.py new file mode 100644 index 000000000000..4abd5533a95c --- /dev/null +++ b/machine_learning/a_star.py @@ -0,0 +1,152 @@ +import numpy as np + +''' +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 +''' + +class Cell(object): + ''' + Class cell represents a cell in the world which have the property + position : The position of the represented by tupleof x and y + co-ordinates initially set to (0,0) + parent : This contains the parent cell object which we visted + before arrinving this cell + g,h,f : The parameters for constructing the huristic function + which can be any function. for simplicity used line + distance + ''' + def __init__(self): + self.position=(0,0) + self.parent=None + + self.g = 0 + self.h = 0 + self.f = 0 + ''' + overides equals method because otherwise cell assing will give + wrong results + ''' + def __eq__(self, cell): + return self.position == cell.position + + def showcell(self): + print(self.position) + + +class Gridworld(object): + + ''' + Gridworld class represents the external world here a grid M*M + matrix + w : create a numpy array with the given world_size default is 5 + ''' + + def __init__(self, world_size=(5, 5)): + self.w = np.zeros(world_size) + self.world_x_limit = world_size[0] + self.world_y_limit = world_size[1] + + def show(self): + print(self.w) + + ''' + get_neighbours + as the name suggests this function will return the neighbours of + the a particular cell + ''' + def get_neigbours(self, cell): + neughbour_cord = [(-1, -1), (-1, 0), (-1, 1), + (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] + current_x = cell.position[0] + current_y = cell.position[1] + neighbours = [] + for n in neughbour_cord: + x = current_x + n[0] + y = current_y + n[1] + if (x >=0 and x < self.world_x_limit and + y >=0 and y < self.world_y_limit): + c = Cell() + c.position = (x,y) + c.parent = cell + neighbours.append(c) + return neighbours + +''' +Implementation of a start algotithm +world : Object of the world object +start : Object of the cell as start position +stop : Object of the cell as goal position +''' +def astar(world, start, goal): + ''' + >>> p = Gridworld() + >>> start = Cell() + >>> start.position = (0,0) + >>> goal = Cell() + >>> goal.position = (4,4) + >>> astar(p, start, goal) + >>> [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)] + + ''' + _open = [] + _closed = [] + _open.append(start) + + while _open: + min_f = np.argmin([n.f for n in _open]) + current = _open[min_f] + _closed.append(_open.pop(min_f)) + + + if current == goal: + break + + for n in world.get_neigbours(current): + for c in _closed: + if c == n: + continue + n.g = current.g + 1 + x1, y1 = n.position + x2, y2 = goal.position + n.h = (y2 - y1)**2 + (x2 - x1)**2 + n.f = n.h + n.g + + for c in _open: + if c == n and c.f < n.f: + continue + _open.append(n) + path = [] + while current.parent is not None: + path.append(current.position) + current = current.parent + path.append(current.position) + path = path[::-1] + print(path) + return path + +if __name__ == '__main__': + ''' + sample run + ''' + # object for the world + p = Gridworld() + #stat position and Goal + start = Cell() + start.position = (0,0) + goal = Cell() + goal.position = (4,4) + print("path from {} to {} ".format(start.position, goal.position)) + s=astar(p, start, goal) + #Just for visual Purpose + for i in s: + p.w[i] = 1 + print(p.w) From 09b6f248c795854ec70afa0532d466f37f604039 Mon Sep 17 00:00:00 2001 From: Jeffin Francis Date: Thu, 30 Apr 2020 18:40:27 +0530 Subject: [PATCH 2/8] changes after build error --- machine_learning/a_star.py | 230 ++++++++++++++++++------------------- 1 file changed, 115 insertions(+), 115 deletions(-) diff --git a/machine_learning/a_star.py b/machine_learning/a_star.py index 4abd5533a95c..48b71f70fb93 100644 --- a/machine_learning/a_star.py +++ b/machine_learning/a_star.py @@ -14,139 +14,139 @@ ''' class Cell(object): - ''' - Class cell represents a cell in the world which have the property - position : The position of the represented by tupleof x and y - co-ordinates initially set to (0,0) - parent : This contains the parent cell object which we visted - before arrinving this cell - g,h,f : The parameters for constructing the huristic function - which can be any function. for simplicity used line - distance - ''' - def __init__(self): - self.position=(0,0) - self.parent=None + ''' + Class cell represents a cell in the world which have the property + position : The position of the represented by tupleof x and y + co-ordinates 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 + ''' + def __init__(self): + self.position=(0,0) + self.parent=None - self.g = 0 - self.h = 0 - self.f = 0 - ''' - overides equals method because otherwise cell assing will give - wrong results - ''' - def __eq__(self, cell): - return self.position == cell.position + self.g = 0 + self.h = 0 + self.f = 0 + ''' + overrides equals method because otherwise cell assign will give + wrong results + ''' + def __eq__(self, cell): + return self.position == cell.position - def showcell(self): - print(self.position) + def showcell(self): + print(self.position) class Gridworld(object): - ''' - Gridworld class represents the external world here a grid M*M - matrix - w : create a numpy array with the given world_size default is 5 - ''' - - def __init__(self, world_size=(5, 5)): - self.w = np.zeros(world_size) - self.world_x_limit = world_size[0] - self.world_y_limit = world_size[1] - - def show(self): - print(self.w) + ''' + Gridworld class represents the external world here a grid M*M + matrix + w : create a numpy array with the given world_size default is 5 + ''' + + def __init__(self, world_size=(5, 5)): + self.w = np.zeros(world_size) + self.world_x_limit = world_size[0] + self.world_y_limit = world_size[1] + + def show(self): + print(self.w) - ''' - get_neighbours - as the name suggests this function will return the neighbours of - the a particular cell - ''' - def get_neigbours(self, cell): - neughbour_cord = [(-1, -1), (-1, 0), (-1, 1), - (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] - current_x = cell.position[0] - current_y = cell.position[1] - neighbours = [] - for n in neughbour_cord: - x = current_x + n[0] - y = current_y + n[1] - if (x >=0 and x < self.world_x_limit and - y >=0 and y < self.world_y_limit): - c = Cell() - c.position = (x,y) - c.parent = cell - neighbours.append(c) - return neighbours + ''' + get_neighbours + as the name suggests this function will return the neighbours of + the a particular cell + ''' + def get_neigbours(self, cell): + neughbour_cord = [(-1, -1), (-1, 0), (-1, 1), + (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] + current_x = cell.position[0] + current_y = cell.position[1] + neighbours = [] + for n in neughbour_cord: + x = current_x + n[0] + y = current_y + n[1] + if (x >=0 and x < self.world_x_limit and + y >=0 and y < self.world_y_limit): + c = Cell() + c.position = (x,y) + c.parent = cell + neighbours.append(c) + return neighbours ''' -Implementation of a start algotithm +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 ''' def astar(world, start, goal): - ''' - >>> p = Gridworld() - >>> start = Cell() - >>> start.position = (0,0) - >>> goal = Cell() - >>> goal.position = (4,4) - >>> astar(p, start, goal) - >>> [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)] + ''' + >>> p = Gridworld() + >>> start = Cell() + >>> start.position = (0,0) + >>> goal = Cell() + >>> goal.position = (4,4) + >>> astar(p, start, goal) + >>> [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)] - ''' - _open = [] - _closed = [] - _open.append(start) + ''' + _open = [] + _closed = [] + _open.append(start) - while _open: - min_f = np.argmin([n.f for n in _open]) - current = _open[min_f] - _closed.append(_open.pop(min_f)) - + while _open: + min_f = np.argmin([n.f for n in _open]) + current = _open[min_f] + _closed.append(_open.pop(min_f)) + - if current == goal: - break + if current == goal: + break - for n in world.get_neigbours(current): - for c in _closed: - if c == n: - continue - n.g = current.g + 1 - x1, y1 = n.position - x2, y2 = goal.position - n.h = (y2 - y1)**2 + (x2 - x1)**2 - n.f = n.h + n.g + for n in world.get_neigbours(current): + for c in _closed: + if c == n: + continue + n.g = current.g + 1 + x1, y1 = n.position + x2, y2 = goal.position + n.h = (y2 - y1)**2 + (x2 - x1)**2 + n.f = n.h + n.g - for c in _open: - if c == n and c.f < n.f: - continue - _open.append(n) - path = [] - while current.parent is not None: - path.append(current.position) - current = current.parent - path.append(current.position) - path = path[::-1] - print(path) - return path + for c in _open: + if c == n and c.f < n.f: + continue + _open.append(n) + path = [] + while current.parent is not None: + path.append(current.position) + current = current.parent + path.append(current.position) + path = path[::-1] + print(path) + return path if __name__ == '__main__': - ''' - sample run - ''' - # object for the world - p = Gridworld() - #stat position and Goal - start = Cell() - start.position = (0,0) - goal = Cell() - goal.position = (4,4) - print("path from {} to {} ".format(start.position, goal.position)) - s=astar(p, start, goal) - #Just for visual Purpose - for i in s: - p.w[i] = 1 - print(p.w) + ''' + sample run + ''' + # object for the world + p = Gridworld() + #stat position and Goal + start = Cell() + start.position = (0,0) + goal = Cell() + goal.position = (4,4) + print("path from {} to {} ".format(start.position, goal.position)) + s=astar(p, start, goal) + #Just for visual Purpose + for i in s: + p.w[i] = 1 + print(p.w) From 1b0cf9d8720ce2bd854150c0be25b66b699f8576 Mon Sep 17 00:00:00 2001 From: Jeffin Francis Date: Thu, 30 Apr 2020 19:26:41 +0530 Subject: [PATCH 3/8] intent changes --- machine_learning/a_star.py | 74 +++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/machine_learning/a_star.py b/machine_learning/a_star.py index 48b71f70fb93..048036a4ccac 100644 --- a/machine_learning/a_star.py +++ b/machine_learning/a_star.py @@ -1,38 +1,39 @@ -import numpy as np +import numpy as np ''' -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 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 +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 +essentially planning ahead at each step so a more optimal decision is made.A* also known as the algorithm with brains ''' + class Cell(object): ''' Class cell represents a cell in the world which have the property - position : The position of the represented by tupleof x and y + position : The position of the represented by tupleof x and y co-ordinates initially set to (0,0) - parent : This contains the parent cell object which we visited - before arrinving this cell + 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 + which can be any function. for simplicity used line distance ''' def __init__(self): - self.position=(0,0) - self.parent=None + self.position = (0, 0) + self.parent = None self.g = 0 self.h = 0 self.f = 0 ''' - overrides equals method because otherwise cell assign will give + overrides equals method because otherwise cell assign will give wrong results ''' def __eq__(self, cell): @@ -45,47 +46,51 @@ def showcell(self): class Gridworld(object): ''' - Gridworld class represents the external world here a grid M*M + Gridworld class represents the external world here a grid M*M matrix w : create a numpy array with the given world_size default is 5 ''' - + def __init__(self, world_size=(5, 5)): self.w = np.zeros(world_size) self.world_x_limit = world_size[0] self.world_y_limit = world_size[1] - + def show(self): print(self.w) ''' get_neighbours - as the name suggests this function will return the neighbours of - the a particular cell + as the name suggests this function will return the neighbours of + the a particular cell ''' def get_neigbours(self, cell): - neughbour_cord = [(-1, -1), (-1, 0), (-1, 1), - (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] + neughbour_cord = [ + (-1, -1), (-1, 0), (-1, 1), (0, -1), + (0, 1), (1, -1), (1, 0), (1, 1)] current_x = cell.position[0] current_y = cell.position[1] neighbours = [] for n in neughbour_cord: x = current_x + n[0] y = current_y + n[1] - if (x >=0 and x < self.world_x_limit and - y >=0 and y < self.world_y_limit): + if ( + (x >= 0 and x < self.world_x_limit) and + (y >= 0 and y < self.world_y_limit)): c = Cell() - c.position = (x,y) + c.position = (x, y) c.parent = cell neighbours.append(c) return neighbours ''' -Implementation of a start algorithm -world : Object of the world object +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 ''' + + def astar(world, start, goal): ''' >>> p = Gridworld() @@ -105,11 +110,8 @@ def astar(world, start, goal): min_f = np.argmin([n.f for n in _open]) current = _open[min_f] _closed.append(_open.pop(min_f)) - - if current == goal: break - for n in world.get_neigbours(current): for c in _closed: if c == n: @@ -137,16 +139,16 @@ def astar(world, start, goal): ''' sample run ''' - # object for the world +# object for the world p = Gridworld() - #stat position and Goal +# stat position and Goal start = Cell() - start.position = (0,0) + start.position = (0, 0) goal = Cell() - goal.position = (4,4) + goal.position = (4, 4) print("path from {} to {} ".format(start.position, goal.position)) - s=astar(p, start, goal) - #Just for visual Purpose + s = astar(p, start, goal) +# Just for visual Purpose for i in s: p.w[i] = 1 print(p.w) From 557d66db270eef4d8657ef5045e4161c71df6603 Mon Sep 17 00:00:00 2001 From: Jeffin Francis Date: Thu, 30 Apr 2020 19:46:40 +0530 Subject: [PATCH 4/8] fix after review --- machine_learning/a_star.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/a_star.py b/machine_learning/a_star.py index 048036a4ccac..4a055cc18ee1 100644 --- a/machine_learning/a_star.py +++ b/machine_learning/a_star.py @@ -61,7 +61,7 @@ def show(self): ''' get_neighbours - as the name suggests this function will return the neighbours of + As the name suggests this function will return the neighbours of the a particular cell ''' def get_neigbours(self, cell): From 57749819f3a604d5bd5029284dc5187faa335125 Mon Sep 17 00:00:00 2001 From: Jeffin Francis Date: Sat, 2 May 2020 11:14:40 +0530 Subject: [PATCH 5/8] ImportMissmatchError --- machine_learning/{a_star.py => astar.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename machine_learning/{a_star.py => astar.py} (100%) diff --git a/machine_learning/a_star.py b/machine_learning/astar.py similarity index 100% rename from machine_learning/a_star.py rename to machine_learning/astar.py From ded07e3c8da7d51808c17233708319be349cf8e0 Mon Sep 17 00:00:00 2001 From: Jeffin Francis Date: Sat, 2 May 2020 11:34:45 +0530 Subject: [PATCH 6/8] Build failed fix --- machine_learning/astar.py | 1 - 1 file changed, 1 deletion(-) diff --git a/machine_learning/astar.py b/machine_learning/astar.py index 4a055cc18ee1..42cc6836cf48 100644 --- a/machine_learning/astar.py +++ b/machine_learning/astar.py @@ -99,7 +99,6 @@ def astar(world, start, goal): >>> goal = Cell() >>> goal.position = (4,4) >>> astar(p, start, goal) - >>> [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)] ''' _open = [] From 602470f61a8a22ebe391c6dbb6f1039ea8bb659b Mon Sep 17 00:00:00 2001 From: Jeffin Francis Date: Mon, 4 May 2020 17:50:50 +0530 Subject: [PATCH 7/8] doctest changes --- 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 42cc6836cf48..e4e803c4c12b 100644 --- a/machine_learning/astar.py +++ b/machine_learning/astar.py @@ -99,7 +99,7 @@ def astar(world, start, goal): >>> goal = Cell() >>> goal.position = (4,4) >>> astar(p, start, goal) - + [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)] ''' _open = [] _closed = [] From 7112695cd228a522a541f66dea52df5af4a7724e Mon Sep 17 00:00:00 2001 From: Jeffin Francis Date: Mon, 4 May 2020 19:08:30 +0530 Subject: [PATCH 8/8] doctest changes --- machine_learning/astar.py | 1 - 1 file changed, 1 deletion(-) diff --git a/machine_learning/astar.py b/machine_learning/astar.py index e4e803c4c12b..2dd10b1d5fa7 100644 --- a/machine_learning/astar.py +++ b/machine_learning/astar.py @@ -131,7 +131,6 @@ def astar(world, start, goal): current = current.parent path.append(current.position) path = path[::-1] - print(path) return path if __name__ == '__main__':