资源简介
a*算法的python版
代码片段和文件信息
“““
A_star 2D
@author: huiming zhou
“““
import os
import sys
import math
import heapq
sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
“/../../Search_based_Planning/“)
from Search_2D import plotting env
class AStar:
“““AStar set the cost + heuristics as the priority
“““
def __init__(self s_start s_goal heuristic_type):
self.s_start = s_start
self.s_goal = s_goal
self.heuristic_type = heuristic_type
self.Env = env.Env() # class Env
self.u_set = self.Env.motions # feasible input set
self.obs = self.Env.obs # position of obstacles
self.OPEN = [] # priority queue / OPEN set
self.CLOSED = [] # CLOSED set / VISITED order
self.PARENT = dict() # recorded parent
self.g = dict() # cost to come
def searching(self):
“““
A_star Searching.
:return: path visited order
“““
self.PARENT[self.s_start] = self.s_start
self.g[self.s_start] = 0
self.g[self.s_goal] = math.inf
heapq.heappush(self.OPEN
(self.f_value(self.s_start) self.s_start))
while self.OPEN:
_ s = heapq.heappop(self.OPEN)
self.CLOSED.append(s)
if s == self.s_goal: # stop condition
break
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:
self.g[s_n] = math.inf
if new_cost < self.g[s_n]: # conditions for updating Cost
self.g[s_n] = new_cost
self.PARENT[s_n] = s
heapq.heappush(self.OPEN (self.f_value(s_n) s_n))
return self.extract_path(self.PARENT) self.CLOSED
def searching_repeated_astar(self e):
“““
repeated A*.
:param e: weight of A*
:return: path and visited order
“““
path visited = [] []
while e >= 1:
p_k v_k = self.repeated_searching(self.s_start self.s_goal e)
path.append(p_k)
visited.append(v_k)
e -= 0.5
return path visited
def repeated_searching(self s_start s_goal e):
“““
run A* with weight e.
:param s_start: starting state
:param s_goal: goal state
:param e: weight of a*
:return: path and visited order.
“““
g = {s_start: 0 s_goal: float(“inf“)}
PARENT = {s_start: s_start}
OPEN = []
CLOSED = []
heapq.heappush(OPEN
(g[s_start] + e * self.heuristic(s_start) s_start))
while OPEN:
_ s = heapq.heappop(OPEN)
CLOSED.append(s)
if s == s_goal:
break
for s_n in self.get_neighbor(s):
new_cost = g[s] + self.cost(s s_n)
if s_n not in g:
相关资源
- python入门全套PPT
- AWD靶机防御脚本(Linux_file_montor.py)
- python爬虫爬取微博热搜
- 二维码识别+RGB识别+色环识别+通过串
- python爬虫爬取旅游信息(附源码,c
- python爬虫爬取豆瓣电影信息
- 深度学习YOLOv3分类算法
- 视觉处理(test_shape.py)
- 网页视频并合并(2heiPage.py)
- 网页遥控小车 Python web (基于RPi.GPI
- BP算法手写梯度计算
- 呼吸灯(IO.py)
- python 采集360的图片地址到文本文件
- Python简单小游戏 五子棋
- python基础题库(附答案).docx(共54页
- Python RC4算法
- 微信防撤回.py
- python实现的日历
- Python源代码:以web方式管理自己的常
- 电赛电磁炮.py
- 基于Python实现的简易画气球
- 画一朵可自定义的花.py
- python 井字棋 游戏源码
- 《Python从小白到大牛》源代码
- 基于表面肌电的手势识别.py
- 查找两个路径中相同文件(get_same_f
- Python爬虫实战入门教程
- 70行代码实现贪吃蛇完整游戏功能
- 机器学习numpy和pandas基础
- Python 3 Web Development. Beginners Guide
评论
共有 条评论