-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Added A* algorithm #1913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Added A* algorithm #1913
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
91ce618
a* algorithm
jeffin07 09b6f24
changes after build error
jeffin07 1b0cf9d
intent changes
jeffin07 557d66d
fix after review
jeffin07 5774981
ImportMissmatchError
jeffin07 ded07e3
Build failed fix
jeffin07 602470f
doctest changes
jeffin07 7112695
doctest changes
jeffin07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
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 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 | ||
''' | ||
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) | ||
|
||
|
||
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 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) | ||
|
||
''' | ||
_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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.