forked from Yusufma03/pfrnns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils.py
193 lines (147 loc) · 5.13 KB
/
data_utils.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import random
import math
import numpy as np
class Maze(object):
def __init__(self, maze):
self.maze = maze
self.width = len(maze[0])
self.height = len(maze)
self.blocks = []
self.update_cnt = 0
self.beacons = []
for y, line in enumerate(self.maze):
for x, block in enumerate(line):
if block:
nb_y = self.height - y - 1
self.blocks.append((x, nb_y))
if block == 2:
self.beacons.extend(((x, nb_y), (x + 1, nb_y), (x, nb_y + 1), (x + 1, nb_y + 1)))
def is_in(self, x, y):
if x < 0 or y < 0 or x > self.width or y > self.height:
return False
return True
def is_free(self, x, y):
if not self.is_in(x, y):
return False
yy = self.height - int(y) - 1
xx = int(x)
return self.maze[yy][xx] == 0
def random_place(self):
x = random.uniform(0, self.width)
y = random.uniform(0, self.height)
return x, y
def random_free_place(self):
while True:
x, y = self.random_place()
if self.is_free(x, y):
return x, y
def distance(self, x1, y1, x2, y2):
return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
def distance_to_beacons(self, x, y, obs_num=5):
distances = []
for c_x, c_y in self.beacons:
d = self.distance(c_x, c_y, x, y)
distances.append(d)
return sorted(distances)[:obs_num]
def add_noise(level, *coords):
return [x + random.uniform(-level, level) for x in coords]
def add_noise_gauss(level, *coords):
return [x + np.random.normal(scale=level) for x in coords]
# return [x + random.uniform(-level, level) for x in coords]
def add_little_noise(*coords):
return add_noise(0.02, *coords)
def add_some_noise(*coords):
return add_noise(0.1, *coords)
class Point(object):
def __init__(self, x, y, heading=None, w=1, noisy=False):
if heading is None:
heading = random.uniform(0, 360)
if noisy:
x, y, heading = add_some_noise(x, y, heading)
self.x = x
self.y = y
self.h = heading
self.w = w
def __repr__(self):
return "(%f, %f, w=%f)" % (self.x, self.y, self.w)
@property
def xy(self):
return self.x, self.y
@property
def xyh(self):
return self.x, self.y, self.h
@classmethod
def create_random(cls, count, maze):
return [cls(*maze.random_free_place()) for _ in range(0, count)]
def read_sensor(self, maze, obs_num):
return maze.distance_to_beacons(*self.xy, obs_num)
def advance_by(self, speed, checker=None, noisy=False):
h = self.h
if noisy:
speed, h = add_little_noise(speed, h)
h += random.uniform(-3, 3) # needs more noise to disperse better
r = math.radians(h)
dx = math.sin(r) * speed
dy = math.cos(r) * speed
if checker is None or checker(self, dx, dy):
self.move_by(dx, dy)
return True
return False
def move_by(self, x, y):
self.x += x
self.y += y
class Robot(Point):
def __init__(self, maze, speed=0.2):
super(Robot, self).__init__(*maze.random_free_place(), heading=90)
self.chose_random_direction()
self.step_count = 0
self.speed = speed
def chose_random_direction(self):
heading = random.uniform(0, 360)
self.h = heading
def read_sensor(self, maze, obs_num):
obs = super(Robot, self).read_sensor(maze, obs_num)
level = 0.1
return [x + random.uniform(-level, level) for x in obs]
def move(self, maze):
"""
Move the robot. Note that the movement is stochastic too.
"""
while True:
self.step_count += 1
if self.advance_by(self.speed, noisy=True,
checker=lambda r, dx, dy: maze.is_free(r.x + dx, r.y + dy)):
break
# Bumped into something or too long in same direction,
# chose random new direction
self.chose_random_direction()
def gen_traj(traj_len=100, obs_num=5):
maze_data = np.loadtxt('maze.csv', delimiter=',')
world = Maze(maze_data)
speed = 0.2
robbie = Robot(world, speed=speed)
traj_ret = []
for _ in range(traj_len):
r_d = robbie.read_sensor(world, obs_num)
step_data = [robbie.x, robbie.y, robbie.h]
old_heading = robbie.h
old_x = robbie.x
old_y = robbie.y
robbie.move(world)
d_h = robbie.h - old_heading
d_x = robbie.x - old_x
d_y = robbie.y - old_y
action = [d_x, d_y, d_h]
step_data = step_data + action + r_d
traj_ret.append(step_data)
return np.array(traj_ret), maze_data
def gen_data(num_trajs, traj_len=50, obs_num=5):
data = {
'trajs': []
}
from tqdm import tqdm
for _ in tqdm(range(num_trajs)):
traj_data, maze = gen_traj(traj_len, obs_num)
data['trajs'].append(traj_data)
data['map'] = maze
return data