"""
version1.1,2018-05-09
《基于智能优化与RRT算法的无人机任务规划方法研究》博士论文
《基于改进人工势场法的路径规划算法研究》硕士论文 """ import matplotlib.pyplot as plt
import random
import math
import copy show_animation = True class Node(object):
"""
RRT Node
""" def __init__(self, x, y):
self.x = x
self.y = y
self.parent = None class RRT(object):
"""
Class for RRT Planning
""" def __init__(self, start, goal, obstacle_list, rand_area):
"""
Setting Parameter start:Start Position [x,y]
goal:Goal Position [x,y]
obstacleList:obstacle Positions [[x,y,size],...]
randArea:random sampling Area [min,max] """
self.start = Node(start[0], start[1])
self.end = Node(goal[0], goal[1])
self.min_rand = rand_area[0]
self.max_rand = rand_area[1]
self.expandDis = 1.0
self.goalSampleRate = 0.05 # 选择终点的概率是0.05
self.maxIter = 500
self.obstacleList = obstacle_list
self.nodeList = [self.start] def random_node(self):
"""
产生随机节点
:return:
"""
node_x = random.uniform(self.min_rand, self.max_rand)
node_y = random.uniform(self.min_rand, self.max_rand)
node = [node_x, node_y] return node @staticmethod
def get_nearest_list_index(node_list, rnd):
"""
:param node_list:
:param rnd:
:return:
"""
d_list = [(node.x - rnd[0]) ** 2 + (node.y - rnd[1]) ** 2 for node in node_list]
min_index = d_list.index(min(d_list))
return min_index @staticmethod
def collision_check(new_node, obstacle_list):
a = 1
for (ox, oy, size) in obstacle_list:
dx = ox - new_node.x
dy = oy - new_node.y
d = math.sqrt(dx * dx + dy * dy)
if d <= size:
a = 0 # collision return a # safe def planning(self):
"""
Path planning animation: flag for animation on or off
""" while True:
# Random Sampling
if random.random() > self.goalSampleRate:
rnd = self.random_node()
else:
rnd = [self.end.x, self.end.y] # Find nearest node
min_index = self.get_nearest_list_index(self.nodeList, rnd)
# print(min_index) # expand tree
nearest_node = self.nodeList[min_index] # 返回弧度制
theta = math.atan2(rnd[1] - nearest_node.y, rnd[0] - nearest_node.x) new_node = copy.deepcopy(nearest_node)
new_node.x += self.expandDis * math.cos(theta)
new_node.y += self.expandDis * math.sin(theta)
new_node.parent = min_index if not self.collision_check(new_node, self.obstacleList):
continue self.nodeList.append(new_node) # check goal
dx = new_node.x - self.end.x
dy = new_node.y - self.end.y
d = math.sqrt(dx * dx + dy * dy)
if d <= self.expandDis:
print("Goal!!")
break if True:
self.draw_graph(rnd) path = [[self.end.x, self.end.y]]
last_index = len(self.nodeList) - 1
while self.nodeList[last_index].parent is not None:
node = self.nodeList[last_index]
path.append([node.x, node.y])
last_index = node.parent
path.append([self.start.x, self.start.y]) return path def draw_graph(self, rnd=None):
"""
Draw Graph
"""
print('aaa')
plt.clf() # 清除上次画的图
if rnd is not None:
plt.plot(rnd[0], rnd[1], "^g")
for node in self.nodeList:
if node.parent is not None:
plt.plot([node.x, self.nodeList[node.parent].x], [
node.y, self.nodeList[node.parent].y], "-g") for (ox, oy, size) in self.obstacleList:
plt.plot(ox, oy, "sk", ms=10*size) plt.plot(self.start.x, self.start.y, "^r")
plt.plot(self.end.x, self.end.y, "^b")
plt.axis([self.min_rand, self.max_rand, self.min_rand, self.max_rand])
plt.grid(True)
plt.pause(0.01) def draw_static(self, path):
"""
画出静态图像
:return:
"""
plt.clf() # 清除上次画的图 for node in self.nodeList:
if node.parent is not None:
plt.plot([node.x, self.nodeList[node.parent].x], [
node.y, self.nodeList[node.parent].y], "-g") for (ox, oy, size) in self.obstacleList:
plt.plot(ox, oy, "sk", ms=10*size) plt.plot(self.start.x, self.start.y, "^r")
plt.plot(self.end.x, self.end.y, "^b")
plt.axis([self.min_rand, self.max_rand, self.min_rand, self.max_rand]) plt.plot([data[0] for data in path], [data[1] for data in path], '-r')
plt.grid(True)
plt.show() def main():
print("start RRT path planning") obstacle_list = [
(5, 1, 1),
(3, 6, 2),
(3, 8, 2),
(1, 1, 2),
(3, 5, 2),
(9, 5, 2)] # Set Initial parameters
rrt = RRT(start=[0, 0], goal=[8, 9], rand_area=[-2, 10], obstacle_list=obstacle_list)
path = rrt.planning()
print(path) # Draw final path
if show_animation:
plt.close()
rrt.draw_static(path) if __name__ == '__main__':
main()

RRT快速拓展随机树 python实现

[python] RRT快速拓展随机树的更多相关文章

  1. matlab练习程序(快速搜索随机树RRT)

    RRT快速搜索随机树英文全称Rapid-exploration Random Tree,和PRM类似,也是一种路径规划算法. 和PRM类似,算法也需要随机撒点,不过不同的是,该算法不是全局随机撒点,而 ...

  2. [matlab] 7.快速搜索随机树(RRT---Rapidly-exploring Random Trees) 路径规划

    RRT是一种多维空间中有效率的规划方法.它以一个初始点作为根节点,通过随机采样增加叶子节点的方式,生成一个随机扩展树,当随机树中的叶子节点包含了目标点或进入了目标区域,便可以在随机树中找到一条由从初始 ...

  3. LSH︱python实现局部敏感随机投影森林——LSHForest/sklearn(一)

    关于局部敏感哈希算法.之前用R语言实现过,可是由于在R中效能太低.于是放弃用LSH来做类似性检索.学了python发现非常多模块都能实现,并且通过随机投影森林让查询数据更快.觉得能够试试大规模应用在数 ...

  4. (数据科学学习手札114)Python+Dash快速web应用开发——上传下载篇

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 这是我的系列教程Python+Dash快速web ...

  5. P3830 [SHOI2012]随机树 题解

    P3830 随机树 坑题,别人的题解我看了一个下午没一个看得懂的,我还是太弱了. 题目链接 P3830 [SHOI2012]随机树 题目描述 输入输出格式 输入格式: 输入仅有一行,包含两个正整数 q ...

  6. (数据科学学习手札102)Python+Dash快速web应用开发——基础概念篇

    本文示例代码与数据已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 这是我的新系列教程Python+Dash快 ...

  7. (数据科学学习手札103)Python+Dash快速web应用开发——页面布局篇

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 这是我的系列教程Python+Dash快速web ...

  8. (数据科学学习手札104)Python+Dash快速web应用开发——回调交互篇(上)

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 这是我的系列教程Python+Dash快速web ...

  9. (数据科学学习手札109)Python+Dash快速web应用开发——静态部件篇(中)

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 这是我的系列教程Python+Dash快速web ...

随机推荐

  1. MySQL事务(学习笔记)

    MySQL事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数据库操作语句就构成一个事 ...

  2. 解密JavaScript闭包

    译者按: 从最简单的计数器开始,按照需求对代码一步步优化,我们可以领会闭包的神奇之处. 原文: Closures are not magic 译者: Fundebug 为了保证可读性,本文采用意译而非 ...

  3. Python importlib 动态加载模块

    # 创建一个 src 文件夹,里面有一个 commons.py 文件,内容如下 def add(): print("add ....") # 创建一个 app.py 文件,内容如下 ...

  4. jstack 排查 java 进程占用大量 CPU 问题

    1. top 看看哪个进程是罪魁祸首 2.将这个进程的jstack dump 到一个文件里面,以备使用. jstack -l 25886 > /tmp/jstack.log # 如果报错,则加 ...

  5. 95%的中国网站需要重写CSS

    95%的中国网站需要重写CSS 很长一段时间,我都使用12px作为网站的主要字体大小.10px太小,眼睛很容易疲劳,14px虽容易看清,却破坏页面的美感.唯独12px在审美和视力方面都恰到好处. 谁对 ...

  6. html + css3 demo

    最近,在做一个比较大的网站,主要服务于欧美地区,全站为英文版本,因为是电子产品,因此,要展示产品内在美(扯个蛋!)仿照小米.錘子.苹果等网站,着重于css3动效效果,搜集整理了一些网站中用到的动效图, ...

  7. 你的leader还在考核你的千行代码Bug率吗?

    管理学大师德鲁克说:你如果你无法度量它,就无法管理它.要想做有效的管理,就很难绕开度量的问题. 软件开发的过程或者技术团队的管理也存在着如何去合理的度量效率的问题.而度量是把双刃剑,度量具有极强的引导 ...

  8. SQL Server如何用触发器捕获DML操作的会话信息

    需求背景 上周遇到了这样一个需求,维护人员发现一个表的数据经常被修改,由于历史原因:文档缺少:以及维护人员的经常变更,导致他们对系统也业务也不完全熟悉,他们也不完全清楚哪些系统和应用程序会对这个表的数 ...

  9. java质数判断

    import java.util.Scanner; /** * Created by Admin on 2017/3/25. */ public class test01 { public stati ...

  10. [20180928]如何能在11g下执行.txt

    [20180928]如何能在11g下执行.txt --//链接问的问题: http://www.itpub.net/thread-2105467-1-1.html create table test( ...