-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrt_demo_exploration.py
executable file
·122 lines (106 loc) · 2.58 KB
/
rrt_demo_exploration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
IMPORTS
"""
import matplotlib.pyplot as plt
import numpy as np
import random
from math import *
import time
def env_setup():
plt.xlim(0,10)
plt.ylim(0,10)
def nearestneighbour(new_V,V):
dlist = []
x_new = new_V[0]
y_new = new_V[1]
for vertex in V:
x = vertex[0]
y = vertex[1]
dlist.append(sqrt((x-x_new)**2 + (y-y_new)**2))
minind = dlist.index(min(dlist)) #returns the minimum dist value index in V
return minind
def plotline(E,i):
env_setup()
pt1 = E[i][0]
pt2 = E[i][1]
x1 = pt1[0]
y1 = pt1[1]
x2 = pt2[0]
y2 = pt2[1]
plt.plot([x1,x2],[y1,y2],"g")
def get_new_node(X_rnd,X_near):
x_rnd = X_rnd[0]
y_rnd = X_rnd[1]
x_near = X_near[0]
y_near = X_near[1]
trav_dist = 0.2
theta = atan2(y_rnd - y_near, x_rnd - x_near)
x_new = x_near + trav_dist*cos(theta)
y_new = y_near + trav_dist*sin(theta)
return ([x_new,y_new])
def rrt():
tic = time.time()
print("time taken")
iter = 300
root = [5,5]
V = [] #vertex list
E = [] #edge list
env_setup()
for i in range(iter):
if i == 0:
V.append(root)
continue
if i == 1:
x = random.uniform(0,10)
y = random.uniform(0,10)
nind = nearestneighbour([x,y],V)
X_new = get_new_node([x,y],V[nind])
E.append([V[i-1],X_new])
V.append(X_new)
plotline(E,i-1)
continue
x = random.uniform(0,10)
y = random.uniform(0,10)
nind = nearestneighbour([x,y],V)
#get new node
X_new = get_new_node([x,y],V[nind])
E.append([V[nind],X_new])
V.append(X_new)
plotline(E,i-1)
plt.pause(0.00000000000000000000001)
toc = time.time()
print(toc-tic)
plt.show()
def plotting_together(E):
for pts in E:
pt1 = pts[0]
pt2 = pts[1]
x1 = pt1[0]
x2 = pt2[0]
y1 = pt1[1]
y2 = pt2[1]
print(pts)
env_setup()
plt.plot([x1,x2],[y1,y2])
def random_walk():
root = [5,5]
iter = 300
V = []
E = []
for i in range(iter):
if i==0:
V.append(root)
continue
if i==1:
x = random.uniform(0,10)
y = random.uniform(0,10)
E.append([root,[x,y]])
V.append([x,y])
continue
x = random.uniform(0,10)
y = random.uniform(0,10)
V.append([x,y])
E.append([V[i-1],V[i]])
plotline(E,i-1)
plt.pause(0.000000001)
rrt()