|
@@ -4,29 +4,30 @@ import env
|
|
|
|
|
|
|
|
|
|
|
|
|
class Astar:
|
|
class Astar:
|
|
|
- def __init__(self, x_start, x_goal, heuristic_type):
|
|
|
|
|
|
|
+ def __init__(self, x_start, x_goal, e, heuristic_type):
|
|
|
self.xI, self.xG = x_start, x_goal
|
|
self.xI, self.xG = x_start, x_goal
|
|
|
|
|
+ self.e = e
|
|
|
|
|
+ self.heuristic_type = heuristic_type
|
|
|
|
|
|
|
|
self.Env = env.Env() # class Env
|
|
self.Env = env.Env() # class Env
|
|
|
- self.plotting = plotting.Plotting(self.xI, self.xG) # class Plotting
|
|
|
|
|
|
|
|
|
|
self.u_set = self.Env.motions # feasible input set
|
|
self.u_set = self.Env.motions # feasible input set
|
|
|
self.obs = self.Env.obs # position of obstacles
|
|
self.obs = self.Env.obs # position of obstacles
|
|
|
|
|
|
|
|
- self.path, self.policy, self.visited = self.searching(self.xI, self.xG, heuristic_type)
|
|
|
|
|
-
|
|
|
|
|
- self.fig_name = "A* Algorithm"
|
|
|
|
|
- self.plotting.animation(self.path, self.visited, self.fig_name) # animation generate
|
|
|
|
|
-
|
|
|
|
|
- def searching(self, xI, xG, heuristic_type):
|
|
|
|
|
|
|
+ def searching(self):
|
|
|
"""
|
|
"""
|
|
|
Searching using A_star.
|
|
Searching using A_star.
|
|
|
|
|
|
|
|
:return: planning path, action in each node, visited nodes in the planning process
|
|
:return: planning path, action in each node, visited nodes in the planning process
|
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
- q_astar = queue.QueuePrior() # priority queue
|
|
|
|
|
- q_astar.put(xI, 0)
|
|
|
|
|
|
|
+ xI = self.xI
|
|
|
|
|
+ xG = self.xG
|
|
|
|
|
+ heuristic_type = self.heuristic_type
|
|
|
|
|
+ e = self.e
|
|
|
|
|
+
|
|
|
|
|
+ q_astar = queue.QueuePrior() # priority queue / OPEN
|
|
|
|
|
+ q_astar.put(xI, e * self.Heuristic(xI, xG, heuristic_type))
|
|
|
parent = {xI: xI} # record parents of nodes
|
|
parent = {xI: xI} # record parents of nodes
|
|
|
action = {xI: (0, 0)} # record actions of nodes
|
|
action = {xI: (0, 0)} # record actions of nodes
|
|
|
visited = []
|
|
visited = []
|
|
@@ -43,7 +44,7 @@ class Astar:
|
|
|
new_cost = cost[x_current] + self.get_cost(x_current, u_next)
|
|
new_cost = cost[x_current] + self.get_cost(x_current, u_next)
|
|
|
if x_next not in cost or new_cost < cost[x_next]: # conditions for updating cost
|
|
if x_next not in cost or new_cost < cost[x_next]: # conditions for updating cost
|
|
|
cost[x_next] = new_cost
|
|
cost[x_next] = new_cost
|
|
|
- priority = new_cost + self.Heuristic(x_next, xG, heuristic_type)
|
|
|
|
|
|
|
+ priority = new_cost + e * self.Heuristic(x_next, xG, heuristic_type)
|
|
|
q_astar.put(x_next, priority) # put node into queue using priority "f+h"
|
|
q_astar.put(x_next, priority) # put node into queue using priority "f+h"
|
|
|
parent[x_next], action[x_next] = x_current, u_next
|
|
parent[x_next], action[x_next] = x_current, u_next
|
|
|
|
|
|
|
@@ -108,8 +109,18 @@ class Astar:
|
|
|
print("Please choose right heuristic type!")
|
|
print("Please choose right heuristic type!")
|
|
|
|
|
|
|
|
|
|
|
|
|
-if __name__ == '__main__':
|
|
|
|
|
- x_Start = (5, 5) # Starting node
|
|
|
|
|
- x_Goal = (49, 5) # Goal node
|
|
|
|
|
|
|
+def main():
|
|
|
|
|
+ x_start = (5, 5) # Starting node
|
|
|
|
|
+ x_goal = (49, 5) # Goal node
|
|
|
|
|
+
|
|
|
|
|
+ astar = Astar(x_start, x_goal, 1, "manhattan")
|
|
|
|
|
+ plot = plotting.Plotting(x_start, x_goal) # class Plotting
|
|
|
|
|
|
|
|
- astar = Astar(x_Start, x_Goal, "manhattan")
|
|
|
|
|
|
|
+ fig_name = "A* Algorithm"
|
|
|
|
|
+ path, policy, visited = astar.searching()
|
|
|
|
|
+
|
|
|
|
|
+ plot.animation(path, visited, fig_name) # animation generate
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == '__main__':
|
|
|
|
|
+ main()
|