-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodels.py
298 lines (238 loc) · 11 KB
/
models.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
from simulator import DrivingSimulation, GymSimulation, MujocoSimulation
import numpy as np
class Driver(DrivingSimulation):
def __init__(self, total_time=50, recording_time=[0,50]):
super(Driver ,self).__init__(name='driver', total_time=total_time, recording_time=recording_time)
self.ctrl_size = 10
self.state_size = 0
self.feed_size = self.ctrl_size + self.state_size
self.ctrl_bounds = [(-1,1)]*self.ctrl_size
self.state_bounds = []
self.feed_bounds = self.state_bounds + self.ctrl_bounds
self.num_of_features = 4
def get_features(self):
recording = self.get_recording(all_info=False)
recording = np.array(recording)
# staying in lane (higher is better)
staying_in_lane = np.mean(np.exp(-30*np.min([np.square(recording[:,0,0]-0.17), np.square(recording[:,0,0]), np.square(recording[:,0,0]+0.17)], axis=0)))
# keeping speed (lower is better)
keeping_speed = np.mean(np.square(recording[:,0,3]-1))
# heading (higher is better)
heading = np.mean(np.sin(recording[:,0,2]))
# collision avoidance (lower is better)
collision_avoidance = np.mean(np.exp(-(7*np.square(recording[:,0,0]-recording[:,1,0])+3*np.square(recording[:,0,1]-recording[:,1,1]))))
return [staying_in_lane, keeping_speed, heading, collision_avoidance]
@property
def state(self):
return [self.robot.x, self.human.x]
@state.setter
def state(self, value):
self.reset()
self.initial_state = value.copy()
def set_ctrl(self, value):
arr = [[0]*self.input_size]*self.total_time
interval_count = len(value)//self.input_size
interval_time = int(self.total_time / interval_count)
arr = np.array(arr).astype(float)
j = 0
for i in range(interval_count):
arr[i*interval_time:(i+1)*interval_time] = [value[j], value[j+1]]
j += 2
self.ctrl = list(arr)
def feed(self, value):
ctrl_value = value[:]
self.set_ctrl(ctrl_value)
class LunarLander(GymSimulation):
def __init__(self, total_time=200, recording_time=[0,200]):
super(LunarLander ,self).__init__(name='LunarLanderContinuous-v2', total_time=total_time, recording_time=recording_time)
self.frame_delay_ms = 20
self.ctrl_size = 10
self.state_size = 4
self.feed_size = self.ctrl_size + self.state_size
eps = np.finfo(np.float32).eps
self.ctrl_bounds = [(-1+eps,1-eps)]*self.ctrl_size
self.state_bounds = [(-0.05, 0.05),(-0.5,0.5),(-4,4),(-4,4)]
self.feed_bounds = self.state_bounds + self.ctrl_bounds
self.num_of_features = 6
self.reset()
def get_features(self):
recording = self.get_recording()
recording = np.array(recording)
# mean angle relative to the vertical axis
mean_angle = np.mean(np.arccos(np.cos(recording[:,0])))
# final distance to landing pad
final_dist = np.exp(-0.33*np.linalg.norm([recording[len(recording)-1,4]-10, recording[len(recording)-1,5]-3.9]))
# absolute value of total (vectorel) rotation
total_rotation = np.abs(recording[len(recording)-1,0] - recording[0,0])/(2*np.pi)
# path length
path_length = np.sum([np.linalg.norm([recording[i,4]-recording[i-1,4], recording[i,5]-recording[i-1,5]]) for i in range(1,len(recording))])/15
# final vertical velocity
final_vertical_velocity = np.mean(recording[len(recording)-5:,3])/15
# crash time (normalized by total time)
crash_time = len(recording)/self.total_time
return [mean_angle, final_dist, total_rotation, path_length, final_vertical_velocity, crash_time]
@property
def state(self):
l = self.sim.unwrapped.lander
res = [l.angle, l.angularVelocity]
res = np.append(res, list(l.linearVelocity))
res = np.append(res, list(l.position))
return np.append(res, self.done)
@state.setter
def state(self, value):
self.reset_seed()
self.sim.reset()
self.done = False
if value is None:
value = self.initial_state if self.initial_state is not None else [0]*4
self.sim.unwrapped.lander.angle = value[0]
self.sim.unwrapped.lander.angularVelocity = value[1]
self.sim.unwrapped.lander.linearVelocity[0] = value[2]
self.sim.unwrapped.lander.linearVelocity[1] = value[3]
def set_ctrl(self, value):
arr = [[0]*self.input_size]*self.total_time
interval_count = len(value)//self.input_size
interval_time = int(self.total_time / interval_count)
arr = np.array(arr).astype(float)
j = 0
for i in range(interval_count):
arr[i*interval_time:(i+1)*interval_time] = [value[j], value[j+1]]
j += 2
self.ctrl = list(arr)
def feed(self, value):
initial_state = value[0:self.state_size]
ctrl_value = value[self.state_size:self.feed_size]
self.initial_state = initial_state
self.set_ctrl(ctrl_value)
class MountainCar(GymSimulation):
def __init__(self, total_time=900, recording_time=[0,900]):
super(MountainCar ,self).__init__(name='MountainCarContinuous-v0', total_time=total_time, recording_time=recording_time)
self.frame_delay_ms = 10
self.ctrl_size = 12
self.state_size = 2
self.feed_size = self.ctrl_size + self.state_size
self.ctrl_bounds = [(-1,1)]*self.ctrl_size
self.state_bounds = [(-0.6,-0.4),(-0.01,0.01)]
self.feed_bounds = self.state_bounds + self.ctrl_bounds
self.num_of_features = 3
self.reset()
def get_features(self):
recording = self.get_recording()
recording = np.array(recording)
# the coordinate of the point closest to the flag during trajectory
closest_coordinate = np.max(recording[:,0])
# the coordinate of the point farthest from the flag during trajectory
farthest_coordinate = np.min(recording[:,0])
# simulation time (normalized with the total allowed time)
total_displacement = np.sum(np.abs(recording[1:,0] - recording[:len(recording)-1,0]))
return [closest_coordinate, farthest_coordinate, total_displacement]
@property
def state(self):
return np.append(self.sim.unwrapped.state, self.done)
@state.setter
def state(self, value):
self.sim.reset()
self.done = False
if value is None:
self.sim.unwrapped.state = [-0.5, 0]
else:
self.sim.unwrapped.state = value.copy()
def set_ctrl(self, value):
arr = [[0]*self.input_size]*self.total_time
interval_count = len(value)
interval_time = int(self.total_time / interval_count)
arr = np.array(arr).astype(float)
for i in range(interval_count):
arr[i*interval_time:(i+1)*interval_time] = value[i]
self.ctrl = list(arr)
def feed(self, value):
initial_state = value[0:self.state_size]
ctrl_value = value[self.state_size:self.feed_size]
self.initial_state = initial_state
self.set_ctrl(ctrl_value)
class Swimmer(MujocoSimulation):
def __init__(self, total_time=420, recording_time=[0,420]):
super(Swimmer ,self).__init__(name='swimmer', total_time=total_time, recording_time=recording_time)
self.ctrl_size = 24
self.state_size = 3
self.feed_size = self.ctrl_size + self.state_size
self.ctrl_bounds = [(-1,1)]*self.ctrl_size
self.state_bounds = [(-np.pi/2,np.pi/2)]*self.state_size
self.feed_bounds = self.state_bounds + self.ctrl_bounds
self.num_of_features = 3
def get_features(self):
recording = self.get_recording(all_info=False)
recording = np.array(recording)
# horizontal range
horizontal_range = recording[len(recording)-1,0]
# vertical range
vertical_range = recording[len(recording)-1,1]
# total displacement
total_displacement = np.sum(np.linalg.norm(recording[1:,0:2]-recording[:len(recording)-1,0:2], axis=1))
return [horizontal_range, vertical_range, total_displacement]
@property
def state(self):
return self.sim.get_state()
@state.setter
def state(self, value):
self.reset()
temp_state = self.initial_state
temp_state.qpos[:] = value[:]
self.initial_state = temp_state
def set_ctrl(self, value):
arr = [[0]*self.input_size]*self.total_time
interval_count = len(value)//self.input_size
interval_time = int(self.total_time / interval_count)
arr = np.array(arr).astype(float)
j = 0
for i in range(interval_count):
arr[i*interval_time:(i+1)*interval_time] = [value[j], value[j+1]]
j += 2
self.ctrl = list(arr)
def feed(self, value):
initial_state = value[0:self.state_size]
ctrl_value = value[self.state_size:self.feed_size]
self.initial_state.qpos[0:2] = 0.0
self.initial_state.qpos[2:] = initial_state[:]
self.set_ctrl(ctrl_value)
class Tosser(MujocoSimulation):
def __init__(self, total_time=1000, recording_time=[200,1000]):
super(Tosser ,self).__init__(name='tosser', total_time=total_time, recording_time=recording_time)
self.ctrl_size = 4
self.state_size = 5
self.feed_size = self.ctrl_size + self.state_size
self.ctrl_bounds = [(-1,1)]*self.ctrl_size
self.state_bounds = [(-0.2,0.2),(-0.785,0.785),(-0.1,0.1),(-0.1,-0.07),(-1.5,1.5)]
self.feed_bounds = self.state_bounds + self.ctrl_bounds
self.num_of_features = 4
def get_features(self):
recording = self.get_recording(all_info=False)
recording = np.array(recording)
# horizontal range
horizontal_range = -np.min([x[3] for x in recording])
# maximum altitude
maximum_altitude = np.max([x[2] for x in recording])
# number of flips
num_of_flips = np.sum(np.abs([recording[i][4] - recording[i-1][4] for i in range(1,len(recording))]))/(np.pi*2)
# distance to closest basket (gaussian fit)
dist_to_basket = np.exp(-3*np.linalg.norm([np.minimum(np.abs(recording[len(recording)-1][3] + 0.9), np.abs(recording[len(recording)-1][3] + 1.4)), recording[len(recording)-1][2]+0.85]))
return [horizontal_range, maximum_altitude, num_of_flips, dist_to_basket]
@property
def state(self):
return self.sim.get_state()
@state.setter
def state(self, value):
self.reset()
temp_state = self.initial_state
temp_state.qpos[:] = value[:]
self.initial_state = temp_state
def set_ctrl(self, value):
arr = [[0]*self.input_size]*self.total_time
arr[150:175] = [value[0:self.input_size]]*25
arr[175:200] = [value[self.input_size:2*self.input_size]]*25
self.ctrl = arr
def feed(self, value):
initial_state = value[0:self.state_size]
ctrl_value = value[self.state_size:self.feed_size]
self.initial_state.qpos[:] = initial_state
self.set_ctrl(ctrl_value)