-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcp_solve.py
352 lines (276 loc) · 10.4 KB
/
lcp_solve.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import numpy as np
import copy
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
from generate_traj import generate_traj
from lemke_howson import LCP_lemke_howson
import os
from tqdm import tqdm
import time
""" hyperparams - can be adjusted for a parameter study"""
T = 50 # game length (25, 50, 100, 200)
L = 5 # turn length
H = 10 # predictive horizon
nT = int(T/L) # number of turns in game (5, 10, 20, 40)
N = 5 # number of candidate trajectories
dof = 8 # dof in dynamics model
dt = .5 # timestep size
tol = 1 # tolerance for checking if trajectories intersect
startA = [4, 0, -7] # blue
goalA = [-8, 8, -13]
startB = [-8, 8, -13] # orange
goalB = [4, 0, -7]
cx = [0, 0, -3, 3, 0]
cy = [3, -3, 3, -3, 0]
cz = [-3, 3, 0, 0, 0]
""" check if a single pair of trajectories a and b intersect at any point
to be used to weight against collision
a, b: np.array representing a trajectory of shape (dof, H+1) for a single turn A and B
returns: a single integer indicating whether the passed trajectories intersect within
tolerance at any point in the trajectory
"""
def intersect(a, b):
assert a.shape == (dof, H+1) and b.shape == (dof, H+1)
dists = []
for h in range(H):
axyz = a[:3, h]
bxyz = b[:3, h]
dists.append(np.linalg.norm(axyz - bxyz))
dists = np.array(dists)
# print(np.array([int(d < tol) for d in dists]))
#print(f"intersect return {int(np.any(dists < tol))}")
return int(np.any(dists < tol))
""" get the minimum distance to goal (L2 norm in 3-space) over the next H trajectories
traj: np.array representing the trajectory of shape (dof, H+1) for the indicated player
player: a string representing the player of interest
"""
def distToGoal(traj, player="a"):
#print(f"traj shape {traj.shape}")
goal = goalA if player == "a" else goalB
dist = np.inf
dist = min(dist, np.linalg.norm(traj[:3, H] - goal))
return dist
"""
takes the vector of 4D control inputs over the H steps of the computed trajectory
"""
def accPenalty(accs):
# print("in accpenalty")
# print("accs.shape")
# print(accs.shape)
avAcc = np.average(accs, axis=1)
# print(avAcc.shape)
# print(avAcc.T.dot(avAcc))
return avAcc.T.dot(avAcc)
""" get cost functions to be solved for each
Za: the state vector for A
Ua: the control input for A
Same for Zb, Ub
"""
def getCostMats(Za, Ua, Zb, Ub):
mats = []
# for turn in range(nT):
A = np.zeros((N, N))
B = np.zeros((N, N))
for i in range(N):
for j in range(N): # TODO are these costs symmetric?
#print(f"Za shape {Za[i, :, :].shape}")
A[i, j] = distToGoal(Za[i, :, :]) + (1e6 * intersect(Za[i, :, :], Zb[j, :, :])) +(.5 * accPenalty(Ua[i, :, :]))
B[i, j] = distToGoal(Zb[i, :, :], player="b") + (1e6 * intersect(Za[j, :, :], Zb[i, :, :])) + (.5 * accPenalty(Ub[i, :, :]))
mats.append([A, B])
return np.array(mats)
"""" solve the LCPs given by A and B at each turn in the game using nashpy """
def solveLCPs(mats):
# print("in solve LCPS")
# print(type(mats))
# print(type(mats[0]))
sols = []
for x in list(mats):
#print(x[0], x[1])
#gme = nash.Game(x[1], x[0])
#sols.append(lemke_howson_lex(x[0], x[1]))
sols.append(LCP_lemke_howson(x[0], x[1]))
return np.array(sols)
"""
xcur:
goal: x,y,z goal for player
"""
def generateTrajectories(start, vstart, phi_start, vphi_start, goal, cx, cy, cz):
traj, acc = [], []
for i in range(N):
state, control = generate_traj(start, vstart, phi_start, vphi_start, goal, H, dt, cx[i], cy[i], cz[i])
traj.append(state)
acc.append(control)
return np.array(traj), np.array(acc)
"""
Compute the error at a single turn for the start position and goal for a single player
start: the start position for the player
goal: the goal position for the player
returns: a list of percent error in the x, y, z directions
"""
def getErrorAtTurn(nextStart, goal):
x, y, z = nextStart
xg, yg, zg = goal
xerr = round((abs(x-xg)/xg)*100, 2)
yerr = round((abs(y-yg)/yg)*100, 2)
zerr = round((abs(z-zg)/zg)*100, 2)
err = [xerr, yerr, zerr]
return err
"""
Run the simulation
startA, B: the point in 3-space (x, y, z) where A/B starts from
goalA, B: the point in 3-space (x, y, z) where A/B wants to go to
cx, cy, cz: scalar distance in each direction from the midpoint of the start to goal from
the current trajectory
returns: a tuple of (tA, eA) - the actual trajectories for A and B and the percent error at each turn
in each direction for A and B's distance to their respective goals
"""
def sim(startA, goalA, startB, goalB, cx, cy, cz):
# print("startA")
# print(startA)
# print("startB")
# print(startB)
nextStartA = startA
nextStartB = startB
vStartA = [0,0,0]
vStartB = [0,0,0]
phi_a = 0
phi_b = 0
vphi_a = 0
vphi_b = 0
# store actual chosen trajectories
trajAactual = np.zeros((dof, T))
trajBactual = np.zeros((dof, T))
# update initial error
errorA = []
errorB = []
for turn in tqdm(range(nT)):
# print(f"nextStartA {nextStartA}")
# print(f"nextStartB {nextStartB}")
trajA, accA = generateTrajectories(nextStartA, vStartA, phi_a, vphi_a, goalA, cx, cy, cz)
trajB, accB = generateTrajectories(nextStartB, vStartB, phi_b, vphi_b, goalB, cx, cy, cz)
# print("traj and acc shapes")
# print(f"trajA {trajA.shape} accA {accA.shape}")
# only consider first 10 points
costs = getCostMats(trajA, accA, trajB, accB)
sols = solveLCPs(costs)
#print(f"sols shape {sols.shape}")
# choose action
asol = np.argmax(sols[0,0])
bsol = np.argmax(sols[0,1])
#print(asol, bsol)
# update actual trajectories
trajAactual[:, turn*L:(turn+1)*L] = trajA[asol, :, 0:L]
trajBactual[:, turn*L:(turn+1)*L] = trajB[bsol, :, 0:L]
#print(f"trajAactual {trajAactual.shape}")
#print(f"trajBactual {trajAactual.shape}")
# update next start position
nextStartA = trajA[asol, :3, L] # assign next start to xyz
nextStartB = trajB[bsol, :3, L]
# update next start velocity
vStartA = trajA[asol, 4:7, L] # assign next velocity to xyz
vStartB = trajB[bsol, 4:7, L]
phi_a = trajA[asol, 3, L]
phi_b = trajB[bsol, 3, L]
vphi_a = trajA[asol, 7, L]
vphi_b = trajB[bsol, 7, L]
#print(f"turn number {turn+1}/{nT}")
# update error
errorA.append(getErrorAtTurn(nextStartA, goalA))
errorB.append(getErrorAtTurn(nextStartB, goalB))
return (trajAactual, errorA), (trajBactual, errorB)
"""
Callback for animating the simulated game,
t: the current iteration to update the figure from [0, T-1]
trajAactual: the actual path taken by vehicle A
trajBactual: the actual path taken by vehicle B
"""
def animate(t, fig, trajAactual, trajBactual):
ax = plt.axes(projection='3d')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title(f"A and B trajectories with dt={dt}, nT={nT}, L={L}, H={H}, tol={tol}")
l1 = plt.plot(trajAactual[0, 0], trajAactual[1, 0], trajAactual[2, 0], label="A")[0]
l2 = plt.plot(trajBactual[0, 0], trajBactual[1, 0], trajBactual[2, 0], label="B")[0]
#print(f"trajAactual shape {trajAactual.shape}")
l1.set_data(trajAactual[0, :t], trajAactual[1, :t])
l1.set_3d_properties(trajAactual[2, :t])
l2.set_data(trajBactual[0, :t], trajBactual[1, :t])
l2.set_3d_properties(trajBactual[2, :t])
ax.legend(loc="lower left")
"""
save the animation as mp4 with title containing number of turns
and timestamp
ani: animation object representing simulated game
"""
def save(ani):
timestr = time.strftime("%Y%m%d-%H%M%S")
fname = f"results/avoid_{nT}_{timestr}.mp4"
ani.save(fname)
print(f"successfully saved {fname}")
"""
Plot the error for a player over all turns
err: a list of (%x, %y, %z) tuples for % error loss at each turn for A/B
return: plots for percent error at each turn of a player A/B
"""
def plotError(errA, errB):
fig, (ax1, ax2) = plt.subplots(2)
fig.suptitle('Percent error for A and B')
ax1.plot(range(1, nT+1), [x[0] for x in errA], label="x error A", color='r')
ax1.plot(range(1, nT+1), [x[1] for x in errA], label="y error", color='b')
ax1.plot(range(1, nT+1), [x[2] for x in errA], label="z error", color='y')
ax2.plot(range(1, nT+1), [x[0] for x in errB], label="x error B")
ax2.plot(range(1, nT+1), [x[1] for x in errB], label="y error")
ax2.plot(range(1, nT+1), [x[2] for x in errB], label="z error")
ax1.legend()
ax2.legend()
plt.xlabel("turn number")
plt.ylabel("percent error")
#plt.show()
timestr = time.strftime("%Y%m%d-%H%M%S")
fname = f"results/error_{nT}_{timestr}.png"
plt.savefig(fname)
print(f"successfully saved {fname}")
"""
Plot the static result of the trajectories
"""
def plotStatic(trajAactual, trajBactual):
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title(f"A and B final trajectories with dt={dt}, nT={nT}, L={L}, H={H}, tol={tol}")
#ax.plot(trajA[asol, 0, :], trajA[asol, 1, :], trajA[asol, 2, :], label="A")
#ax.plot(trajB[bsol, 0, :], trajB[bsol, 1, :], trajB[bsol, 2, :], label="B")
ax.plot(trajAactual[0, :], trajAactual[1, :], trajAactual[2, :], label="A")
ax.plot(trajBactual[0, :], trajBactual[1, :], trajBactual[2, :], label="B")
ax.legend(loc="upper left")
plt.show()
""" Driver code """
# startA = [6, 6, 6] # blue
# goalA = [1, 1, 1]
# startB = [1, 1, 1] # orange
# goalB = [6, 6, 6]
#
#
# cx = [0, 0, -3, 3, 0]
# cy = [3, -3, 3, -3, 0]
# cz = [-3, 3, 0, 0, 0]
#
# # do the simulation
# (trajA, errA), (trajB, errB) = sim(startA, goalA, startB, goalB, cx, cy, cz)
# print(f"len errA {len(errA), len(errA[0])}")
#
# plotError(errA, errB)
# # animation based on real trajectories
# fig = plt.figure()
# ani = animation.FuncAnimation(fig,
# animate,
# save_count=T,
# fargs = (fig, trajA, trajB), # total number of calls to animate
# interval=dt * 500) # interval = miliseconds between frames
# save(ani)
# plotStatic(trajA, trajB)