Compare commits

..

3 Commits

View File

@ -1,3 +1,19 @@
##################### -- Learning Source -- #####################
# 1. https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
# 2. https://www.educative.io/answers/what-is-uniform-cost-search
# In above 1 no. link, you will get about dijkstra algorithm.
# You may have to scroll down a little bit.
# And you will get a section titled as "Practical optimizations and infinite graphs"
# This section explains about Uniform Cost Search algorithm.
# Uniform search cost is slightly similar to dijkstra.
# The main different between them is that UCS is having multiple goals.
# Below code is fully based on 2D Grid environment
##################### --------------------- #####################
# diagonal clockwise
dxy1 = [
[1, 1],
@ -81,12 +97,14 @@ class UniformCostSearch:
) -> list[list[float]] | list[list[int]]:
"""
Return 2D list where optimal path is stored.
# >>> start = [0,0]
# >>> end = [1, 2]
# >>> dist = [[1,0],[0,2]]
# >>> dxy = [(1, 1),(1, 0),(1, -1),(0, -1),(-1, -1),(-1, 0),(-1, 1),(0, 1)]
# >>> get_shortest_path(start,end,dist,dxy)
# [[0,0],[1,1]]
>>> start = [0,0]
>>> end = [1, 1]
>>> dist = [[1,0],[0,2]]
>>> grid = [[0,0],[0,0]]
>>> dxy = [[1, 1],[1, 0],[1, -1],[0, -1],[-1, -1],[-1, 0],[-1, 1],[0, 1]]
>>> obj = UniformCostSearch(grid)
>>> obj.get_shortest_path(start,end,dist,dxy)
[[1, 1], [0, 1], [1, 0], [0, 0]]
"""
shortest_path = []
curr_node = None
@ -121,13 +139,46 @@ class UniformCostSearch:
) -> list[list[float]] | list[list[int]] | None:
"""
Return 2D list where optimal path is stored.
# >>> current = [0, 0]
# >>> final = [[1, 1]]
# >>> grid = [[0,0],[0,0]]
# >>> dxy = [(1, 1),(1, 0),(1, -1),(0, -1),(-1, -1),(-1, 0),(-1, 1),(0, 1)]
# >>> goal_answer = [1000000, 100000]
# >>> ucs(current,final,grid,dxy,goal_answer)
# [[0,2],[1,1]]
Examples:
>>> current = [0, 0]
>>> final = [[1, 1]]
>>> grid = [[0,0],[0,0]]
>>> goal_answer = [100000000]
>>> dxy = [[1, 1],[1, 0],[1, -1],[0, -1],[-1, -1],[-1, 0],[-1, 1],[0, 1]]
>>> obj = UniformCostSearch(grid)
>>> obj.ucs(current, final, grid, dxy, goal_answer)
Increment
Extended distance
[[1, 1], [0, 0]]
>>> current = [0, 0]
>>> final = [[2, 2]]
>>> grid = [[0,1,0],[1,0,1],[1,0,0]]
>>> goal_answer = [100000000]
>>> dxy = [[1, 1],[1, 0],[1, -1],[0, -1],[-1, -1],[-1, 0],[-1, 1],[0, 1]]
>>> obj = UniformCostSearch(grid)
>>> obj.ucs(current, final, grid, dxy, goal_answer)
Increment
Extended distance
[[2, 2], [1, 1], [0, 0]]
>>> current = [0, 0]
>>> final = [[2, 2],[2,1]] # multiple goals
>>> grid = [[0,1,0],[1,0,1],[1,0,0]]
>>> goal_answer = [100000000, 100000000]
>>> dxy = [[1, 1],[1, 0],[1, -1],[0, -1],[-1, -1],[-1, 0],[-1, 1],[0, 1]]
>>> obj = UniformCostSearch(grid)
>>> obj.ucs(current, final, grid, dxy, goal_answer)
Increment
Extended distance
Increment
Extended distance
[[2, 2], [1, 1], [0, 0]]
"""
dist = [[float("inf") for _ in range(self.m)] for _ in range(self.n)]
@ -163,26 +214,39 @@ class UniformCostSearch:
ry = dy + y
if 0 <= rx < self.m and 0 <= ry < self.n and grid[rx][ry] != 1:
weight = 1
# diagonal_weight = 1.414
# if (
# (dx == -1 and dy == -1)
# or (dx == 1 and dy == 1)
# or (dx == 1 and dy == -1)
# or (dx == -1 and dy == 1)
# ):
# new_dist = d + diagonal_weight
# else:
new_dist = d + weight
if new_dist < dist[rx][ry]:
dist[rx][ry] = new_dist
heap.append([new_dist, rx, ry])
return None
def your_algorithm(
def execute_algorithm(
self, start_point: list[int], end_point: list[int], grid: list[list[int]]
) -> list[list[float]] | list[list[int]] | None:
"""
Return 2D list where optimal path is stored.
>>> start_point = [0, 0]
>>> end_point = [1, 1]
>>> grid = [[0,0],[0,0]]
>>> dxy = [[1, 1],[1, 0],[1, -1],[0, -1],[-1, -1],[-1, 0],[-1, 1],[0, 1]]
>>> goal_answer = [100000000]
>>> obj = UniformCostSearch(grid)
>>> obj.execute_algorithm(start_point, end_point, grid)
Increment
Extended distance
[[1, 1], [0, 0]]
>>> start_point = [0, 0]
>>> end_point = [2, 2]
>>> grid = [[0,1,0],[1,0,1],[1,0,0]]
>>> dxy = [[1, 1],[1, 0],[1, -1],[0, -1],[-1, -1],[-1, 0],[-1, 1],[0, 1]]
>>> goal_answer = [100000000]
>>> obj = UniformCostSearch(grid)
>>> obj.execute_algorithm(start_point, end_point, grid)
Increment
Extended distance
[[2, 2], [1, 1], [0, 0]]
"""
dxy = []
if start_point[1] - end_point[1] == 0 and start_point[0] - end_point[0] < 0:
@ -206,55 +270,44 @@ class UniformCostSearch:
return path
if __name__ == "__main__":
executed_object = UniformCostSearch(
def run() -> None:
"""
Return nothing. Its a run whole algorithm and print the answer.
>>> run()
Increment
Extended distance
[[7, 7], [6, 6], [5, 5], [4, 4], [3, 3], [2, 2], [1, 1], [0, 0]]
"""
ucs_object = UniformCostSearch(
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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