Files
PathPlanning/Search-based Planning/Search_2D/ARAstar.py
T

201 lines
6.1 KiB
Python
Raw Normal View History

2020-06-27 00:38:38 -07:00
"""
2020-06-28 20:41:44 -07:00
ARA_star 2D (Anytime Repairing A*)
2020-06-27 00:38:38 -07:00
@author: huiming zhou
"""
2020-06-25 15:43:47 -07:00
2020-06-27 00:38:38 -07:00
import os
import sys
2020-07-02 21:23:38 -07:00
import math
2020-06-27 00:38:38 -07:00
sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
"/../../Search-based Planning/")
from Search_2D import plotting
from Search_2D import env
2020-06-25 17:59:40 -07:00
2020-06-25 15:43:47 -07:00
class AraStar:
2020-07-02 21:23:38 -07:00
def __init__(self, s_start, s_goal, e, heuristic_type):
self.s_start, self.s_goal = s_start, s_goal
2020-06-25 17:59:40 -07:00
self.heuristic_type = heuristic_type
2020-06-25 15:43:47 -07:00
2020-07-02 17:01:34 -07:00
self.Env = env.Env() # class Env
2020-06-25 15:43:47 -07:00
2020-07-02 17:01:34 -07:00
self.u_set = self.Env.motions # feasible input set
self.obs = self.Env.obs # position of obstacles
self.e = e # initial weight
self.g = {self.s_start: 0, self.s_goal: float("inf")} # cost to come
2020-06-25 15:43:47 -07:00
2020-07-02 17:01:34 -07:00
self.OPEN = {self.s_start: self.fvalue(self.s_start)} # priority queue / OPEN set
self.CLOSED = set() # CLOSED set
self.INCONS = {} # INCONS set
self.PARENT = {self.s_start: self.s_start} # relations
self.path = [] # planning path
self.visited = [] # order of visited nodes
2020-06-25 17:59:40 -07:00
2020-06-27 00:21:29 -07:00
def searching(self):
2020-06-25 17:59:40 -07:00
self.ImprovePath()
2020-06-27 00:21:29 -07:00
self.path.append(self.extract_path())
2020-06-25 17:59:40 -07:00
2020-07-02 17:01:34 -07:00
while self.update_e() > 1: # continue condition
self.e -= 0.5 # increase weight
self.OPEN.update(self.INCONS)
for s in self.OPEN:
self.OPEN[s] = self.fvalue(s)
2020-06-25 17:59:40 -07:00
2020-07-02 17:01:34 -07:00
self.INCONS = {}
2020-06-28 20:41:44 -07:00
self.CLOSED = set()
2020-07-02 17:01:34 -07:00
self.ImprovePath() # improve path
2020-06-27 00:21:29 -07:00
self.path.append(self.extract_path())
2020-06-25 17:59:40 -07:00
2020-06-27 00:21:29 -07:00
return self.path, self.visited
2020-06-25 17:59:40 -07:00
def ImprovePath(self):
2020-06-28 20:41:44 -07:00
"""
:return: a e'-suboptimal path
"""
2020-06-27 00:21:29 -07:00
visited_each = []
2020-06-28 20:41:44 -07:00
2020-07-02 17:01:34 -07:00
while True:
s, f_small = self.get_smallest_f()
if self.fvalue(self.s_goal) <= f_small:
break
self.CLOSED.add(s)
2020-06-25 17:59:40 -07:00
2020-07-02 21:23:38 -07:00
for s_n in self.get_neighbor(s):
new_cost = self.g[s] + self.cost(s, s_n)
if s_n not in self.g or new_cost < self.g[s_n]:
self.g[s_n] = new_cost
self.PARENT[s_n] = s
visited_each.append(s_n)
2020-06-27 00:21:29 -07:00
2020-07-02 21:23:38 -07:00
if s_n not in self.CLOSED:
2020-07-02 17:01:34 -07:00
self.OPEN[s_n] = self.fvalue(s_n)
2020-07-02 21:23:38 -07:00
else:
2020-07-02 17:01:34 -07:00
self.INCONS[s_n] = 0
2020-06-25 17:59:40 -07:00
2020-06-27 00:21:29 -07:00
self.visited.append(visited_each)
2020-07-02 17:01:34 -07:00
def get_smallest_f(self):
"""
:return: node with smallest f_value in OPEN set.
"""
s_list = {}
for s in self.OPEN:
s_list[s] = self.fvalue(s)
s_small = min(s_list, key=s_list.get)
self.OPEN.pop(s_small)
return s_small, s_list[s_small]
2020-07-02 21:23:38 -07:00
def get_neighbor(self, s):
"""
find neighbors of state s that not in obstacles.
:param s: state
:return: neighbors
"""
s_list = set()
for u in self.u_set:
s_next = tuple([s[i] + u[i] for i in range(2)])
if s_next not in self.obs:
s_list.add(s_next)
return s_list
2020-06-25 17:59:40 -07:00
def update_e(self):
2020-07-02 17:01:34 -07:00
v = float("inf")
2020-06-25 17:59:40 -07:00
2020-06-28 20:41:44 -07:00
if self.OPEN:
2020-07-02 17:01:34 -07:00
v = min(self.g[s] + self.h(s) for s in self.OPEN)
2020-06-28 20:41:44 -07:00
if self.INCONS:
2020-07-02 17:01:34 -07:00
v = min(v, min(self.g[s] + self.h(s) for s in self.INCONS))
2020-06-27 00:21:29 -07:00
2020-07-02 17:01:34 -07:00
return min(self.e, self.g[self.s_goal] / v)
2020-06-25 17:59:40 -07:00
def fvalue(self, x):
2020-07-02 17:01:34 -07:00
return self.g[x] + self.e * self.h(x)
2020-06-25 17:59:40 -07:00
def extract_path(self):
"""
2020-07-02 21:23:38 -07:00
Extract the path based on the PARENT set.
2020-06-25 17:59:40 -07:00
:return: The planning path
"""
2020-07-02 21:23:38 -07:00
path = [self.s_goal]
s = self.s_goal
2020-06-25 17:59:40 -07:00
while True:
2020-07-02 21:23:38 -07:00
s = self.PARENT[s]
path.append(s)
2020-06-25 17:59:40 -07:00
2020-07-02 21:23:38 -07:00
if s == self.s_start:
2020-06-25 17:59:40 -07:00
break
2020-07-02 21:23:38 -07:00
return list(path)
2020-07-02 17:01:34 -07:00
def h(self, s):
2020-07-02 21:23:38 -07:00
"""
Calculate heuristic.
:param s: current node (state)
:return: heuristic function value
"""
2020-07-02 17:01:34 -07:00
heuristic_type = self.heuristic_type # heuristic type
goal = self.s_goal # goal node
2020-07-02 21:23:38 -07:00
if heuristic_type == "manhattan":
return abs(goal[0] - s[0]) + abs(goal[1] - s[1])
else:
return math.hypot(goal[0] - s[0], goal[1] - s[1])
2020-06-25 17:59:40 -07:00
2020-07-04 18:23:30 -07:00
def cost(self, s_start, s_goal):
2020-06-25 17:59:40 -07:00
"""
Calculate cost for this motion
2020-07-02 21:23:38 -07:00
:param s_start: starting node
:param s_goal: end node
2020-06-25 17:59:40 -07:00
:return: cost for this motion
:note: cost function could be more complicate!
"""
2020-07-04 18:23:30 -07:00
if self.is_collision(s_start, s_goal):
return float("inf")
return math.hypot(s_goal[0] - s_start[0], s_goal[1] - s_start[1])
def is_collision(self, s_start, s_end):
if s_start in self.obs or s_end in self.obs:
return True
if s_start[0] != s_end[0] and s_start[1] != s_end[1]:
if s_end[0] - s_start[0] == s_start[1] - s_end[1]:
s1 = (min(s_start[0], s_end[0]), min(s_start[1], s_end[1]))
s2 = (max(s_start[0], s_end[0]), max(s_start[1], s_end[1]))
else:
s1 = (min(s_start[0], s_end[0]), max(s_start[1], s_end[1]))
s2 = (max(s_start[0], s_end[0]), min(s_start[1], s_end[1]))
if s1 in self.obs or s2 in self.obs:
return True
return False
2020-06-25 17:59:40 -07:00
2020-06-25 15:43:47 -07:00
def main():
2020-07-02 17:01:34 -07:00
s_start = (5, 5)
s_goal = (45, 25)
2020-06-25 15:43:47 -07:00
2020-07-02 17:01:34 -07:00
arastar = AraStar(s_start, s_goal, 2.5, "euclidean")
plot = plotting.Plotting(s_start, s_goal)
2020-06-25 17:59:40 -07:00
2020-06-27 00:21:29 -07:00
path, visited = arastar.searching()
2020-07-02 17:01:34 -07:00
plot.animation_ara_star(path, visited, "Anytime Repairing A* (ARA*)")
2020-06-25 17:59:40 -07:00
if __name__ == '__main__':
main()