-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
103 lines (75 loc) · 3.15 KB
/
helpers.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
from robot_class import robot
from math import *
import random
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# --------
# this helper function displays the world that a robot is in
# it assumes the world is a square grid of some given size
# and that landmarks is a list of landmark positions(an optional argument)
def display_world(world_size, position, landmarks=None):
# using seaborn, set background grid to gray
sns.set_style("dark")
# Plot grid of values
world_grid = np.zeros((world_size+1, world_size+1))
# Set minor axes in between the labels
ax=plt.gca()
cols = world_size+1
rows = world_size+1
ax.set_xticks([x for x in range(1,cols)],minor=True )
ax.set_yticks([y for y in range(1,rows)],minor=True)
# Plot grid on minor axes in gray (width = 1)
plt.grid(which='minor',ls='-',lw=1, color='white')
# Plot grid on major axes in larger width
plt.grid(which='major',ls='-',lw=2, color='white')
# Create an 'o' character that represents the robot
# ha = horizontal alignment, va = vertical
ax.text(position[0], position[1], 'o', ha='center', va='center', color='r', fontsize=30)
# Draw landmarks if they exists
if(landmarks is not None):
# loop through all path indices and draw a dot (unless it's at the car's location)
for pos in landmarks:
if(pos != position):
ax.text(pos[0], pos[1], 'x', ha='center', va='center', color='purple', fontsize=20)
# Display final result
plt.show()
# --------
# this routine makes the robot data
# the data is a list of measurements and movements: [measurements, [dx, dy]]
# collected over a specified number of time steps, N
#
def make_data(N, num_landmarks, world_size, measurement_range, motion_noise,
measurement_noise, distance):
# check if data has been made
complete = False
while not complete:
data = []
# make robot and landmarks
r = robot(world_size, measurement_range, motion_noise, measurement_noise)
r.make_landmarks(num_landmarks)
seen = [False for row in range(num_landmarks)]
# guess an initial motion
orientation = random.random() * 2.0 * pi
dx = cos(orientation) * distance
dy = sin(orientation) * distance
for k in range(N-1):
# collect sensor measurements in a list, Z
Z = r.sense()
# check off all landmarks that were observed
for i in range(len(Z)):
seen[Z[i][0]] = True
# move
while not r.move(dx, dy):
# if we'd be leaving the robot world, pick instead a new direction
orientation = random.random() * 2.0 * pi
dx = cos(orientation) * distance
dy = sin(orientation) * distance
# collect/memorize all sensor and motion data
data.append([Z, [dx, dy]])
# we are done when all landmarks were observed; otherwise re-run
complete = (sum(seen) == num_landmarks)
print(' ')
print('Landmarks: ', r.landmarks)
print(r)
return data