-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstate.py
323 lines (270 loc) · 13 KB
/
state.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
321
322
323
from __future__ import division
import numpy as np
from sklearn.linear_model import LinearRegression
from collections import OrderedDict
def get_state_names(all=False, obst=False):
names = ['pelvis_' + n for n in ('rot', 'x', 'y')]
names += ['pelvis_vel_' + n for n in ('rot', 'x', 'y')]
names += ['hip_right', 'knee_right', 'ankle_right', 'hip_left', 'knee_left', 'ankle_left']
names += ['hip_right_vel', 'knee_right_vel', 'ankle_right_vel', 'hip_left_vel', 'knee_left_vel', 'ankle_left_vel']
names += ['mass_x', 'mass_y']
names += ['mass_x_vel', 'mass_y_vel']
if all:
names += [b + '_' + i for b in ['head', 'pelvis2', 'torso', 'toes_left',
'toes_right', 'talus_left', 'talus_right'] for i in
['x', 'y']]
else:
names += [b + '_' + i for b in ['head', 'torso', 'toes_left', 'toes_right',
'talus_left', 'talus_right'] for i in
['x', 'y']]
names += ['muscle_left', 'muscle_right']
if obst:
names += ['obst_dist', 'obst_y', 'obst_r']
return names
def get_names_to_center(centr):
if centr == 'pelvis':
pelvis_or_mass = 'mass'
elif centr == 'mass':
pelvis_or_mass = 'pelvis'
else:
raise ValueError('centr should be in [mass or pelvis], not {}'.format(centr))
return [b + '_x' for b in ['head', pelvis_or_mass, 'torso', 'toes_left',
'toes_right', 'talus_left', 'talus_right']]
def get_bodies_names():
return [b + '_' + i for b in ['head', 'torso', 'toes_left', 'toes_right', 'talus_left', 'talus_right']
for i in ['x', 'y']]
def get_names_obstacles():
return ['toes_left', 'toes_right', 'talus_left', 'talus_right']
def calculate_velocity(cur, prev):
if prev is None:
return np.zeros_like(cur)
return 100.*(cur - prev)
def _get_pattern_idxs(lst, pattern):
idxs = [i for i, x in enumerate(lst) if pattern in x]
return idxs
class State(object):
def __init__(self, obstacles_mode='bodies_dist', obst_grid_dist=1, grid_points=100, last_n_bodies=0):
assert obstacles_mode in ['exclude', 'grid', 'bodies_dist', 'standard']
self.state_idxs = [i for i, n in enumerate(get_state_names(True, True)) if n not in ['pelvis2_x', 'pelvis2_y']]
self.state_names = get_state_names()
self.step = 0
self.obstacles_mode = obstacles_mode
self.obstacles = OrderedDict()
self.obst_names = []
if obstacles_mode == 'standard':
self.obst_names = ['obst_dist', 'obst_y', 'obst_r']
elif obstacles_mode == 'grid':
self.obst_names = ['obst_grid_{}'.format(i) for i in range(grid_points)]
self.obst_grid_dist = obst_grid_dist
self.obst_grid_points = grid_points
self.obst_grid_size = obst_grid_dist * 2 / grid_points
self.state_names = self.state_names[:-3]
elif obstacles_mode == 'bodies_dist':
self._obst_names = get_names_obstacles()
for i in range(3):
for n in self._obst_names:
self.obst_names.append('{}_{}_obst_x_start'.format(n, i))
self.obst_names.append('{}_{}_obst_x_end'.format(n, i))
self.obst_names.append('{}_{}_obst_y'.format(n, i))
self.obst_names.append('is_obstacle')
self.predict_bodies = last_n_bodies > 0
self.last_n_bodies = last_n_bodies
self.bodies_idxs = [self.state_names.index(n) for n in get_bodies_names()]
if self.predict_bodies:
# 2 last dimensions, first for emulator values, second for predicted
self.last_bodies = np.zeros(shape=(1001, len(self.bodies_idxs), 2))
self._x = np.arange(self.last_n_bodies).reshape(-1, 1)
self._x_pred = np.asarray([[self.last_n_bodies]])
self._reg = LinearRegression()
self.bodies_flt = np.zeros(len(self.state_names), dtype='bool')
self.bodies_flt[self.bodies_idxs] = 1
self.state_names_out = self.state_names
self._set_left_right()
def _set_left_right(self):
self.left_idxs = _get_pattern_idxs(self.state_names, '_left')
self.right_idxs = _get_pattern_idxs(self.state_names, '_right')
def reset(self):
self.step = 0
self.obstacles = OrderedDict()
def _predict_bodies(self, state):
#print 'state before', state
self._update_bodies(state, 0)
# if enough steps check if prediction is needed
if self.step >= self.last_n_bodies:
bodies_predict_flt = self.last_bodies[self.step, :, 0] == self.last_bodies[self.step-1, :, 0]
if np.any(bodies_predict_flt):
_state_bodies = state[self.bodies_flt]
#print '\npredicting', self.step
#print 'current vals', _state_bodies[bodies_predict_flt]
y = self.last_bodies[self.step - self.last_n_bodies:self.step, bodies_predict_flt, 1]
#_y = self.last_bodies[self.step - self.last_n_bodies:self.step, bodies_predict_flt, 0]
#print 'last_vals', _y
self._reg.fit(self._x, y)
y_pred = self._reg.predict(self._x_pred)[0]
#print 'predicted vals', y_pred
_state_bodies[bodies_predict_flt] = y_pred
state[self.bodies_flt] = _state_bodies
#print 'state after', state[self.bodies_idxs]
def _update_bodies(self, state, axis):
if self.predict_bodies and self.step < 1000:
self.last_bodies[self.step, :, axis] = state[self.bodies_idxs]
def _add_obstacle(self, state):
pelvis_x = state[1]
obstacle_x = state[-3]
if obstacle_x != 100:
obstacle_x += pelvis_x
if round(obstacle_x, 5) not in self.obstacles:
self.obstacles[round(obstacle_x, 5)] = [obstacle_x, state[-2], state[-1]]
#print('obstacles {}, step {}'.format(self.obstacles.keys(), self.step))
if len(self.obstacles) > 3:
Warning('more than 3 obstacles')
def _get_obstacle_state_reward(self, state):
is_obst = float(state[-3] != 100)
if self.obstacles_mode == 'exclude':
return [is_obst], 0.
elif self.obstacles_mode == 'standard':
if not is_obst:
return [-1., 0., 0., is_obst], 0.
obst_features = np.clip(state[-3:], -10., 10.)
return np.append(obst_features, is_obst), 0.
elif self.obstacles_mode == 'gird':
mass_x = state[self.state_names.index('mass_x')]
obst_grid = np.zeros(self.obst_grid_points)
for k, v in self.obstacles.iteritems():
obst_x, obst_y, obst_r = v
obst_h = obst_y + obst_r
obst_left = int(np.ceil((obst_x - mass_x - obst_r) / self.obst_grid_size) + self.obst_grid_points // 2)
obst_right = int(np.ceil((obst_x - mass_x + obst_r) / self.obst_grid_size) + self.obst_grid_points // 2)
obst_left = max(obst_left, 0)
obst_right = max(obst_right, -1)
obst_grid[obst_left:obst_right + 1] = obst_h
obst_features = np.append(obst_grid, is_obst)
return obst_features, 0
else:
obst_state = []
obst_reward = 0
for i in range(3):
if i >= len(self.obstacles):
for n in self._obst_names:
body_y = state[self.state_names.index(n + '_y')]
obst_state.extend([10, 10, body_y])
else:
v = self.obstacles.values()[i]
obst_x, obst_y, obst_r = v
obst_h = obst_y + obst_r
obst_x_start = obst_x - obst_r
obst_x_end = obst_x + obst_r
for n in self._obst_names:
body_x = state[self.state_names.index(n + '_x')]
body_y = state[self.state_names.index(n + '_y')]
obst_state.append(obst_x_start - body_x)
obst_state.append(obst_x_end - body_x)
obst_state.append(body_y - obst_h)
if obst_reward>=0 and body_x >= (obst_x_start - obst_r/2) \
and (body_x<=obst_x_end+obst_r/2) and (obst_h +obst_r/2) >= body_y:
obst_reward = -0.5
obst_state.append(is_obst)
return np.asarray(obst_state), obst_reward
def process(self, state):
state = np.asarray(state)
state = state[self.state_idxs]
if self.step == 0:
state[-3:] = [100, 0, 0]
self._add_obstacle(state)
obst_state, obst_reward = self._get_obstacle_state_reward(state)
state = state[:-3]
# update last bodies
#state_no_pred = state.copy()
if self.predict_bodies:
self._predict_bodies(state)
#state_out = (state_no_pred + state)/2
#state_out = state
self._update_bodies(state, 1)
#else:
# state_out = state_no_pred
self.step += 1
#return (state_no_pred + state)/2.
return (state, obst_state), obst_reward
#return np.concatenate(state, obst_state), obst_reward
def flip_state(self, state, copy=True):
assert np.ndim(state) == 1
state = np.asarray(state)
state = self.flip_states(state.reshape(1, -1), copy)
return state.ravel()
def flip_states(self, states, copy=True):
assert np.ndim(states) == 2
states = np.asarray(states)
if copy:
states = states.copy()
left = states[:, self.left_idxs]
right = states[:, self.right_idxs]
states[:, self.left_idxs] = right
states[:, self.right_idxs] = left
return states
@property
def state_size(self):
return len(self.state_names_out) + len(self.obst_names)
class StateVel(State):
def __init__(self, vel_states=get_bodies_names(), obstacles_mode='bodies_dist', last_n_bodies=0):
super(StateVel, self).__init__(obstacles_mode=obstacles_mode, last_n_bodies=last_n_bodies)
self.vel_idxs = [self.state_names.index(k) for k in vel_states]
self.prev_vals = None
self.state_names += [n + '_vel' for n in vel_states]
self.state_names_out = self.state_names
# left right idxs
self._set_left_right()
def reset(self):
super(StateVel, self).reset()
self.prev_vals = None
def process(self, state):
(state, obst_state), obst_reward = super(StateVel, self).process(state)
cur_vals = state[self.vel_idxs]
vel = calculate_velocity(cur_vals, self.prev_vals)
self.prev_vals = cur_vals
state = np.concatenate((state, vel, obst_state))
return state, obst_reward
class StateVelCentr(State):
def __init__(self, centr_state='pelvis_x', vel_states=get_bodies_names(),
states_to_center=get_names_to_center('pelvis'),
vel_before_centr=True, obstacles_mode='bodies_dist',
exclude_centr=False, last_n_bodies=0):
super(StateVelCentr, self).__init__(obstacles_mode=obstacles_mode, last_n_bodies=last_n_bodies)
# center
self.centr_idx = self.state_names.index(centr_state)
self.states_to_center = [self.state_names.index(k) for k in states_to_center]
# velocities
self.prev_vals = None
self.vel_idxs = [self.state_names.index(k) for k in vel_states]
self.vel_before_centr = vel_before_centr
self.state_names += [n + '_vel' for n in vel_states]
self.exclude_centr = exclude_centr
if self.exclude_centr:
self.state_names_out = self.state_names[:max(0, self.centr_idx)] + \
self.state_names[self.centr_idx + 1:]
else:
self.state_names_out = self.state_names
# left right idxs
self._set_left_right()
def _set_left_right(self):
state_names = self.state_names_out
self.left_idxs = _get_pattern_idxs(state_names, '_left')
self.right_idxs = _get_pattern_idxs(state_names, '_right')
def reset(self):
super(StateVelCentr, self).reset()
self.prev_vals = None
def process(self, state):
(state, obst_state), obst_reward = super(StateVelCentr, self).process(state)
if self.vel_before_centr:
cur_vals = state[self.vel_idxs]
vel = calculate_velocity(cur_vals, self.prev_vals)
self.prev_vals = cur_vals
state[self.states_to_center] -= state[self.centr_idx]
else:
state[self.states_to_center] -= state[self.centr_idx]
cur_vals = state[self.vel_idxs]
vel = calculate_velocity(cur_vals, self.prev_vals)
self.prev_vals = cur_vals
if self.exclude_centr:
state = np.concatenate([state[:max(0, self.centr_idx)], state[self.centr_idx+1:]])
state = np.concatenate((state, vel, obst_state))
return state, obst_reward