-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoving_object.py
320 lines (256 loc) · 8.84 KB
/
moving_object.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# -*- coding: utf-8 -*-
import numpy as np
from net import Controller
from director import vtkAll as vtk
from director.debugVis import DebugData
from director import ioUtils, filterUtils
class MovingObject(object):
"""Moving object."""
def __init__(self, velocity, polydata):
"""Constructs a MovingObject.
Args:
velocity: Velocity.
polydata: Polydata.
"""
self._state = np.array([0., 0., 0.])
self._velocity = float(velocity)
self._raw_polydata = polydata
self._polydata = polydata
self._sensors = []
@property
def x(self):
"""X coordinate."""
return self._state[0]
@x.setter
def x(self, value):
"""X coordinate."""
next_state = self._state.copy()
next_state[0] = float(value)
self._update_state(next_state)
@property
def y(self):
"""Y coordinate."""
return self._state[1]
@y.setter
def y(self, value):
"""Y coordinate."""
next_state = self._state.copy()
next_state[1] = float(value)
self._update_state(next_state)
@property
def theta(self):
"""Yaw in radians."""
return self._state[2]
@theta.setter
def theta(self, value):
"""Yaw in radians."""
next_state = self._state.copy()
next_state[2] = float(value) % (2 * np.pi)
self._update_state(next_state)
@property
def velocity(self):
"""Velocity."""
return self._velocity
@velocity.setter
def velocity(self, value):
"""Velocity."""
self._velocity = float(value)
@property
def sensors(self):
"""List of attached sensors."""
return self._sensors
def attach_sensor(self, sensor):
"""Attaches a sensor.
Args:
sensor: Sensor.
"""
self._sensors.append(sensor)
def _dynamics(self, state, t, controller=None):
"""Dynamics of the object.
Args:
state: Initial condition.
t: Time.
Returns:
Derivative of state at t.
"""
dqdt = np.zeros_like(state)
dqdt[0] = self._velocity * np.cos(state[2])
dqdt[1] = self._velocity * np.sin(state[2])
dqdt[2] = self._control(state, t)
return dqdt * t
def _control(self, state, t):
"""Returns the yaw given state.
Args:
state: State.
t: Time.
Returns:
Yaw.
"""
raise NotImplementedError
def _simulate(self, dt):
"""Simulates the object moving.
Args:
dt: Time length of step.
Returns:
New state.
"""
return self._state + self._dynamics(self._state, dt)
def move(self, dt=1.0/30.0):
"""Moves the object by a given time step.
Args:
dt: Length of time step.
"""
state = self._simulate(dt)
self._update_state(state)
def _update_state(self, next_state):
"""Updates the moving object's state.
Args:
next_state: New state.
"""
t = vtk.vtkTransform()
t.Translate([next_state[0], next_state[1], 0.])
t.RotateZ(np.degrees(next_state[2]))
self._polydata = filterUtils.transformPolyData(self._raw_polydata, t)
self._state = next_state
list(map(lambda s: s.update(*self._state), self._sensors))
def to_positioned_polydata(self):
"""Converts object to visualizable poly data.
Note: Transformations have been already applied to this.
"""
return self._polydata
def to_polydata(self):
"""Converts object to visualizable poly data.
Note: This is centered at (0, 0, 0) and is not rotated.
"""
return self._raw_polydata
class Robot(MovingObject):
"""Robot."""
def __init__(self, velocity=25.0, scale=0.15, exploration=0.5,
model="A10.obj"):
"""Constructs a Robot.
Args:
velocity: Velocity of the robot in the forward direction.
scale: Scale of the model.
exploration: Exploration rate.
model: Object model to use.
"""
self._target = (0, 0)
self._exploration = exploration
t = vtk.vtkTransform()
t.Scale(scale, scale, scale)
polydata = ioUtils.readPolyData(model)
polydata = filterUtils.transformPolyData(polydata, t)
super(Robot, self).__init__(velocity, polydata)
self._ctrl = Controller()
def move(self, dt=1.0/30.0):
"""Moves the object by a given time step.
Args:
dt: Length of time step.
"""
gamma = 0.9
prev_xy = self._state[0], self._state[1]
prev_state = self._get_state()
prev_utilities = self._ctrl.evaluate(prev_state)
super(Robot, self).move(dt)
next_state = self._get_state()
next_utilities = self._ctrl.evaluate(next_state)
print("action: {}, utility: {}".format(
self._selected_i, prev_utilities[self._selected_i]))
terminal = self._sensors[0].has_collided()
curr_reward = self._get_reward(prev_xy)
total_reward =\
curr_reward if terminal else \
curr_reward + gamma * next_utilities[self._selected_i]
rewards = [total_reward if i == self._selected_i else prev_utilities[i]
for i in range(len(next_utilities))]
self._ctrl.train(prev_state, rewards)
def set_target(self, target):
self._target = target
def set_controller(self, ctrl):
self._ctrl = ctrl
def at_target(self, threshold=3):
"""Return whether the robot has reached its target.
Args:
threshold: Target distance threshold.
Returns:
True if target is reached.
"""
return (abs(self._state[0] - self._target[0]) <= threshold and
abs(self._state[1] - self._target[1]) <= threshold)
def _get_reward(self, prev_state):
prev_dx = self._target[0] - prev_state[0]
prev_dy = self._target[1] - prev_state[1]
prev_distance = np.sqrt(prev_dx ** 2 + prev_dy ** 2)
new_dx = self._target[0] - self._state[0]
new_dy = self._target[1] - self._state[1]
new_distance = np.sqrt(new_dx ** 2 + new_dy ** 2)
if self._sensors[0].has_collided():
return -20
elif self.at_target():
return 15
else:
delta_distance = prev_distance - new_distance
angle_distance = -abs(self._angle_to_destination()) / 4
obstacle_ahead = self._sensors[0].distances[8] - 1
return delta_distance + angle_distance + obstacle_ahead
def _angle_to_destination(self):
x, y = self._target[0] - self.x, self._target[1] - self.y
return self._wrap_angles(np.arctan2(y, x) - self.theta)
def _wrap_angles(self, a):
return (a + np.pi) % (2 * np.pi) - np.pi
def _get_state(self):
dx, dy = self._target[0] - self.x, self._target[1] - self.y
curr_state = [dx / 1000, dy / 1000, self._angle_to_destination()]
return np.hstack([curr_state, self._sensors[0].distances])
def _control(self, state, t):
"""Returns the yaw given state.
Args:
state: State.
t: Time.
Returns:
Yaw.
"""
actions = [-np.pi/2, 0., np.pi/2]
utilities = self._ctrl.evaluate(self._get_state())
optimal_i = np.argmax(utilities)
if np.random.random() <= self._exploration:
optimal_i = np.random.choice([0, 1, 2])
optimal_a = actions[optimal_i]
self._selected_i = optimal_i
return optimal_a
class Obstacle(MovingObject):
"""Obstacle."""
def __init__(self, velocity, radius, bounds, height=1.0):
"""Constructs a Robot.
Args:
velocity: Velocity of the robot in the forward direction.
radius: Radius of the obstacle.
"""
data = DebugData()
self._bounds = bounds
self._radius = radius
self._height = height
center = [0, 0, height / 2 - 0.5]
axis = [0, 0, 1] # Upright cylinder.
data.addCylinder(center, axis, height, radius)
polydata = data.getPolyData()
super(Obstacle, self).__init__(velocity, polydata)
def _control(self, state, t):
"""Returns the yaw given state.
Args:
state: State.
t: Time.
Returns:
Yaw.
"""
x_min, x_max, y_min, y_max = self._bounds
x, y, theta = state
if x - self._radius <= x_min:
return np.pi
elif x + self._radius >= x_max:
return np.pi
elif y - self._radius <= y_min:
return np.pi
elif y + self._radius >= y_max:
return np.pi
return 0.