-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_stl_1_car.py
1517 lines (1350 loc) · 60.6 KB
/
run_stl_1_car.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from lib_stl_core import *
from matplotlib.patches import Polygon, Rectangle, Ellipse
from matplotlib.collections import PatchCollection
plt.rcParams.update({'font.size': 20})
import utils
from utils import to_np, uniform_tensor, rand_choice_tensor, generate_gif, build_relu_nn, soft_step_hard, get_exp_dir, xxyy_2_Ab, eval_proc
from lib_cem import solve_cem_func
from utils_mbrl import get_mbrl_models, get_mbrl_u
class Policy(nn.Module):
def __init__(self, args):
super(Policy, self).__init__()
self.args = args
self.net = build_relu_nn(7, args.nt, args.hiddens, activation_fn=nn.ReLU)
def forward(self, x):
u = self.net(x)
if args.no_tanh:
u = torch.clip(u, -10.0, 10.0)
else:
u = torch.tanh(u) * 10.0
return u
def soft_step(x):
if args.hard_soft_step:
return soft_step_hard(args.tanh_ratio * x)
else:
return (torch.tanh(500 * x) + 1)/2
def soft_step_ft(x):
hard = (x>=0).float()
soft = (torch.tanh(args.tanh_ratio * x) + 1)/2
return soft
def dynamics(x0, u, include_first=False, finetune=False):
# input: x, (n, 6) # xe, ve, i, t, x_dist, vo, trigger
# input: u, (n, T, 1)
# return: s, (n, T, 6)
t = u.shape[1]
x = x0.clone()
segs = []
if include_first:
segs.append(x)
for ti in range(t):
new_x = dynamics_per_step(x, u[:, ti:ti+1], finetune=finetune)
segs.append(new_x)
x = new_x
return torch.stack(segs, dim=1)
def dynamics_per_step(x, u, finetune=False):
new_x = torch.zeros_like(x)
new_x[:, 0] = x[:, 0] + x[:, 1] * args.dt
mask = (torch.logical_and(x[:, 0]<args.stop_x, torch.logical_and(x[:, 2]==0, x[:, 4]<0))).float() # stop sign, before the stop region
if args.test:
if args.finetune:
new_x[:, 1] = torch.clip(x[:, 1] + (u[:, 0]) * args.dt, -0.01, 10) * (1-mask) + torch.clip(x[:, 1] + (u[:, 0]) * args.dt, 0.1, 10) * mask
else:
new_x[:, 1] = torch.clip(x[:, 1] + (u[:, 0]) * args.dt, -0.01, 10) * (1-mask) + torch.clip(x[:, 1] + (u[:, 0]) * args.dt, 0.1, 10) * mask
else:
new_x[:, 1] = torch.clip(x[:, 1] + (u[:, 0]) * args.dt, -0.01, 10)
new_x[:, 2] = x[:, 2]
if args.finetune and finetune:
stop_timer = (x[:, 3] + args.dt * soft_step_ft((x[:,0]-args.stop_x))) * soft_step_ft(-x[:,0])
else:
stop_timer = (x[:, 3] + args.dt * soft_step(x[:,0]-args.stop_x)) * soft_step(-x[:,0])
light_timer = (x[:, 3] + args.dt) % args.phase_t
new_x[:, 3] = (1-x[:, 2]) * stop_timer + x[:, 2] * light_timer
new_x[:, 4] = x[:, 4] + (x[:, 5] - x[:, 1]) * args.dt * (x[:, 4]>=0).float()
new_x[:, 5] = x[:, 5]
new_x[:, 6] = x[:, 6]
return new_x
def get_rl_xs_us(x, policy, nt, include_first=False):
xs = []
us = []
dt_minus=0
if include_first:
xs.append(x)
for ti in range(nt):
tt1=time.time()
if args.rl:
u, _ = policy.predict(x.cpu(), deterministic=True)
u = torch.from_numpy(u * args.amax)
else:
if args.mbpo:
u = get_mbrl_u(x, None, policy, mbpo=True)
elif args.pets:
u_list=[]
for iii in range(x.shape[0]):
u = get_mbrl_u(x[iii], None, policy, mbpo=False)
u_list.append(u)
u = torch.stack(u_list, dim=0)
u = u * args.amax
u = torch.clip(u, -args.amax, args.amax).cuda()
new_x = dynamics_per_step(x, u)
xs.append(new_x)
us.append(u)
x = new_x
tt2=time.time()
if ti>0:
dt_minus += tt2-tt1
xs = torch.stack(xs, dim=1)
us = torch.cat(us, dim=1) # because u [N,1] => [N,T]
return xs, us, dt_minus
class Lane():
def __init__(self, id, from_xy, to_xy, lane_width, lane_length, from_id, to_id, viz_xy, viz_w, viz_h, lane_type):
self.lane_id = id
self.from_xy = from_xy
self.to_xy = to_xy
self.lane_width = lane_width
self.lane_length = lane_length
self.from_id = from_id
self.to_id = to_id
self.veh_ids = []
self.viz_xy = viz_xy
self.viz_w = viz_w
self.viz_h = viz_h
self.lane_type = lane_type
def dist(self):
# > 0, remaining
# < 0, passed
return self.lane_length
def get_dx_dy(self, ds):
if self.lane_type == 0:
return 0.0, ds
elif self.lane_type == 1:
return ds, 0.0
elif self.lane_type == 2:
return 0.0, -ds
else:
return -ds, 0.0
class Intersection():
def __init__(self, id, xy, width, is_light, timer, viz_xy, viz_w, viz_h):
self.inter_id = id
self.xy = xy
self.width = width
self.length = width
self.is_light = is_light
self.timer = timer
self.veh_ids = []
self.viz_xy = viz_xy
self.viz_w = viz_w
self.viz_h = viz_h
def update(self):
if self.is_light:
self.timer = (self.timer + args.dt) % args.phase_t
def dist(self, direction):
# dir = -1, left; 0, straight; +1, right
if direction==0:
return self.width
elif direction == -1:
return 0.75 * self.width * np.pi / 2
elif direction == 1:
return 0.25 * self.width * np.pi / 2
def get_dx_dy(self, remain_s, ds, lane_type, direction):
if direction == 0:
dx = 0
dy = ds
elif direction == -1:
r = 0.75 * self.width
arc = r * np.pi / 2
th0 = (1 - remain_s / arc) * np.pi / 2
th1 = (1 - (remain_s - ds) / arc) * np.pi / 2
dx = r * (np.cos(th1) - np.cos(th0))
dy = r * (np.sin(th1) - np.sin(th0))
elif direction == 1:
r = 0.25 * self.width
arc = r * np.pi / 2
th0 = (1 - remain_s / arc) * np.pi / 2
th1 = (1 - (remain_s - ds) / arc) * np.pi / 2
dx = - r * (np.cos(th1) - np.cos(th0))
dy = r * (np.sin(th1) - np.sin(th0))
if lane_type == 1:
dx, dy = dy, -dx
if lane_type == 2:
dx, dy = -dx, -dy
if lane_type == 3:
dx, dy = -dy, dx
return dx, dy
class Vehicle():
def __init__(self, id, v, in_lane, in_id, dist, xy, timer, base_i, is_light, hist, tri, out_a, is_hybrid):
self.id = id
self.v = v
self.in_lane = in_lane
self.in_id = in_id
self.dist = dist
self.xy = xy
self.timer = timer
self.base_i = base_i
self.is_light = is_light
self.hist = hist
self.tri = tri
self.out_a = out_a
self.is_hybrid = is_hybrid
def get_dir(prev_i, prev_j, curr_i, curr_j, next_i, next_j):
if prev_i is None:
return False
di0 = curr_i - prev_i
dj0 = curr_j - prev_j
di1 = next_i - curr_i
dj1 = next_j - curr_j
lefts = [(1, 0, 0, 1), (0, -1, 1, 0), (-1, 0, 0, -1), (0, 1, -1, 0)]
rights = [(1, 0, 0, -1), (-1, 0, 0, 1), (0, 1, 1, 0), (0, -1, -1, 0)]
if (di0, dj0, di1, dj1) in lefts:
return -1
if (di0, dj0, di1, dj1) in rights:
return 1
return 0
def compute_lane_dist(ego_xy, other_xy, lane_type):
if lane_type == 0:
return other_xy[1] - ego_xy[1]
if lane_type == 1:
return other_xy[0] - ego_xy[0]
if lane_type == 2:
return -other_xy[1] + ego_xy[1]
if lane_type == 3:
return -other_xy[0] + ego_xy[0]
def other_controller(veh, x):
def cruise(v_ref):
if veh.v < v_ref:
u_output = min((v_ref - veh.v) / args.dt, args.amax)
else:
u_output = max((v_ref- veh.v) / args.dt, -args.amax)
return u_output
def stop():
u_output = max((0 - veh.v) / args.dt, -args.amax)
return u_output
v_ref = 6.0
if not veh.in_lane: # in the intersection
u_output = cruise(v_ref)
else: # on the lane
# exists leading vehicle too close
the_dist = -args.safe_thres
if x[4]>=0 and (veh.v*veh.v- x[5]*x[5])/2/args.amax-x[4]+the_dist>=0:
u_output = -args.amax
else:
if x[0] + x[1]*x[1]/2/args.amax < args.stop_x: # too far from the stop sign/traffic light
u_output = cruise(v_ref)
else:
if x[2]<=0.5: # stop sign case
if x[3]<args.stop_t or x[6]>0.5: # not enough stop time or not triggered
u_output = stop()
else:
u_output = cruise(v_ref)
else: # traffic light case
if x[3]<args.phase_red or x[3]>args.phase_t-1: # red time
u_output = stop()
else: # green light
u_output = cruise(v_ref)
return u_output
def solve_planner(tti, x_init, vid):
from lib_pwlplan import plan, Node
from gurobipy import GRB
tt1=time.time()
def func1(m, PWLs, di):
v = m.addVars(args.nt+1, lb=-GRB.INFINITY, ub=GRB.INFINITY, name="the_v")
u = m.addVars(args.nt, lb=-GRB.INFINITY, ub=GRB.INFINITY, name="the_u")
m.addConstr(v[0] == di["v"])
for i in range(args.nt):
m.addConstr(PWLs[0][i+1][0][1] == PWLs[0][i][0][1])
m.addConstr(PWLs[0][i+1][0][0] == PWLs[0][i][0][0] + v[i+1] * args.dt)
if di["head_x"] > -0.5:
m.addConstr(-PWLs[0][i+1][0][0]+PWLs[0][0][0][0] + di["head_x"] + di["head_v"] * (i+1) * args.dt >= args.safe_thres)
m.addConstr(v[i+1] == v[i] + u[i] * args.dt)
m.addConstr(v[i] >= 0)
m.addConstr(v[i] <= args.vmax)
m.addConstr(u[i] <= args.amax)
m.addConstr(u[i] >= -args.amax)
return -100 * sum(PWLs[0][i][0][0] for i in range(args.nt))
nt = args.nt
dt = args.dt
tmax = (nt+1)*dt
vmax = args.vmax
anti_bloat = 0.1
A_int, b_int = xxyy_2_Ab([args.stop_x*0.5+anti_bloat, 5-anti_bloat, -2, 2])
A_stop, b_stop = xxyy_2_Ab([args.stop_x+anti_bloat, -anti_bloat, -2, 2])
A_tra, b_tra = xxyy_2_Ab([args.traffic_x+anti_bloat, 0.5-anti_bloat, -2, 2])
A_free, b_free = xxyy_2_Ab([5, 10, 5, 10])
# state: x, v, is_traffic=0/1, timer, head_x, head_v, triggered
_x, _v, _I, _T, x_head, v_head, triggered = to_np(x_init)[0]
if _I <= 0.5:
# wait_until_inside the stop region
if triggered:
always_not_in_intesection = Node('A', deps=[Node("negmu", info={"A":A_int, "b":b_int})], info={'int':[0, tmax]})
specs = [always_not_in_intesection]
else:
wait_until_stop = Node("F", deps=[Node("A", deps=[Node("mu", info={"A":A_stop, "b":b_stop})], info={"int":[0, max(0,args.stop_t-_T)]})], info={"int":[0, tmax]})
specs = [wait_until_stop]
else: # green/red light
A_list = [A_tra if (_T + dt * (i + 1)) % args.phase_t <= args.phase_red else A_free for i in range(nt)]
b_list = [b_tra if (_T + dt * (i + 1)) % args.phase_t <= args.phase_red else b_free for i in range(nt)]
not_in_int_when_red = Node("A", deps=[Node("negmus", info={"A":A_list, "b":b_list})], info={"int":[0, tmax]})
specs = [not_in_int_when_red]
x0s = [np.array([x_init[0,0].item(), 0.0])]
cache_dict = {"v": x_init[0, 1].item(), "head_x":x_init[0, 4].item(), "head_v":x_init[0,5].item()}
PWL, u_out = plan(x0s, specs, bloat=0.01, MIPGap=0.05, num_segs=args.nt, tmax=tmax, vmax=vmax, extra_fn_list=[func1], return_u=True, quiet=True, cache_dict=cache_dict)
if vid==1:
print("x:%.2f v:%.2f I:%.1f t:%.2f xh:%.2f vh:%.2f tri:%.1f"%(
x_init[0, 0].item(), x_init[0, 1].item(), x_init[0, 2].item(),
x_init[0, 3].item(), x_init[0, 4].item(), x_init[0, 5].item(), x_init[0, 6].item(),
))
if PWL[0] is None:
print(vid, "FAILED~~~~")
u_out = -args.amax * np.ones(args.nt)
else:
if vid==1:
for i in range(nt+1):
print(i, PWL[0][i][0][0], PWL[0][i][0][1])
# avoid front car is encoded in the constraints
return torch.from_numpy(u_out[None]).cuda()
def gradient_solve(tti, x_init, stl, multi_test=False, init_guess=None):
relu = torch.nn.ReLU()
if init_guess is None:
u_lat = torch.zeros(x_init.shape[0], args.nt).requires_grad_()
else:
u_lat = init_guess.detach().clone().cpu().requires_grad_()
x_init = x_init.cpu()
optimizer = torch.optim.Adam([u_lat], lr=args.grad_lr)
tt1=time.time()
for i in range(args.grad_steps):
if init_guess is None:
u = torch.nn.Tanh()(u_lat) * args.amax
else:
u = u_lat
seg = dynamics(x_init, u, include_first=True, finetune=True)
if init_guess is not None:
for tti_debug in range(args.nt+1):
s_ = seg[0, tti_debug]
print("tti=%02d x:%.2f v:%.2f I:%.2f t:%.2f dx_head:%.2f v_head:%.2f tri:%.2f"%(tti_debug,
s_[0], s_[1], s_[2], s_[3], s_[4], s_[5], s_[6]
))
score = stl(seg, args.smoothing_factor)[:, :1]
acc = (stl(seg, args.smoothing_factor, d={"hard":True})[:, :1]>=0).float()
stl_loss = torch.mean(relu(0.5-score))
green_mask = (torch.logical_or(seg[:, :, 2]==0, seg[:, :, 3] > args.phase_red)).float()
v_loss = torch.mean(acc * torch.relu(5-seg[:,:,1]) * green_mask) * args.v_loss
s_loss = torch.mean(acc * torch.relu(args.traffic_x-seg[:,:,0])) * args.s_loss
if init_guess is None:
loss = stl_loss + v_loss + s_loss
else:
loss = 1-seg[0, -1, 3]
print("loss", loss)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if i % args.grad_print_freq == 0:
print(i, loss.item())
tt2=time.time()
print("%05d t:%.4f seconds"%(tti, tt2-tt1))
return u.detach().cuda()
def gurobi_solve(tti, x_init):
import gurobipy as gp
from gurobipy import GRB
nt = args.nt
dt = args.dt
M = 10^6
VMAX = 10
VMIN = -0.01
UMAX = args.amax
UMIN = -args.amax
stop_x = args.stop_x
safe_thres = args.safe_thres
phase_t = args.phase_t
phase_red = args.phase_red
traffic_x = args.traffic_x
stop_t = args.stop_t
x_init = to_np(x_init)
print(x_init)
is_triggered = x_init[0, 6] >= 0.5
is_traffic_light = x_init[0, 2] >= 0.5
has_head_veh = x_init[0,4] >= -0.5
t_start=time.time()
m = gp.Model("mip1")
m.setParam( 'OutputFlag', False)
x = m.addVars(nt+1, name="x", lb=float('-inf'), ub=float('inf'))
v = m.addVars(nt+1, name="v", lb=float('-inf'), ub=float('inf'))
if is_triggered:
gamma_tri = m.addVars(nt, name="gamma_tri", lb=float('-inf'), ub=float('inf'))
if is_traffic_light:
pt = [(x_init[0, 3] + dt * i) % (phase_t) for i in range(nt+1)]
passover = m.addVars(nt, vtype=GRB.BINARY, name="passover")
gamma = m.addVars(nt, name="gamma", lb=float('-inf'), ub=float('inf'))
else:
pt = None
counter_t = x_init[0, 3]
stays = m.addVars(nt, vtype=GRB.BINARY, name="stays")
gamma_stop = m.addVars(nt, name="gamma_stop", lb=float('-inf'), ub=float('inf'))
if has_head_veh:
head_v = x_init[0, 5]
x1 = m.addVars(nt+1, name="x1", lb=float('-inf'), ub=float('inf'))
gamma_head = m.addVars(nt, name="gamma_head", lb=float('-inf'), ub=float('inf'))
u = m.addVars(nt, name="u", lb=float('-inf'), ub=float('inf'))
# initial setup
m.addConstr(x[0] == x_init[0, 0])
m.addConstr(v[0] == x_init[0, 1])
if has_head_veh:
m.addConstr(x1[0] == x_init[0, 4])
# constraints
for ti in range(nt):
# clip
m.addConstr(v[ti]<= VMAX)
m.addConstr(v[ti]>= VMIN)
m.addConstr(u[ti]<= UMAX)
m.addConstr(u[ti]>= UMIN)
# dynamics
m.addConstr(x[ti+1] == x[ti] + v[ti] * dt)
m.addConstr(v[ti+1] == v[ti] + u[ti] * dt)
if has_head_veh:
m.addConstr(x1[ti+1] == x1[ti] + (head_v - v[ti]) * dt)
# constraints
if is_triggered: # stop at stop sign if triggered
m.addConstr(x[ti+1] <= 0.5 * stop_x + gamma_tri[ti])
if has_head_veh:
m.addConstr(x1[ti+1] >= safe_thres - gamma_head[ti]) # safety ~ leading vehicle
if is_traffic_light and pt[ti] < phase_red: # stop at traffic light if red
m.addConstr(x[ti+1] <= traffic_x + passover[ti] * M + gamma[ti])
m.addConstr(x[ti+1] >= 0 - (1-passover[ti]) * M - gamma[ti])
elif is_traffic_light == False: # stop sign case, update rules, wait until
m.addConstr(x[ti+1] <= 0 + (1-stays[ti]) * M + gamma_stop[ti])
m.addConstr(x[ti+1] >= stop_x - (1-stays[ti]) * M + gamma_stop[ti])
if ti==nt-1:
m.addConstr(gp.quicksum(stays) * dt + counter_t >= stop_t)
d_max = x[nt-1]
u_sum = gp.quicksum(u[i]*u[i] for i in range(nt))
if is_traffic_light:
g_sum = 100*gp.quicksum(gamma[i]*gamma[i] for i in range(nt))
gs_sum = 0
else:
g_sum = 0
gs_sum = gp.quicksum(gamma_stop[i]*gamma_stop[i] for i in range(nt))
if has_head_veh:
gh_sum = gp.quicksum(gamma_head[i]*gamma_head[i] for i in range(nt))
else:
gh_sum = 0
if is_triggered:
gt_sum = gp.quicksum(gamma_tri[i]*gamma_tri[i] for i in range(nt))
else:
gt_sum = 0
m.setObjective(-10 * d_max + u_sum + M * 100 * (g_sum + gs_sum + gh_sum + gt_sum), GRB.MINIMIZE)
m.optimize()
t_end=time.time()
print("%.5f seconds"%(t_end-t_start))
u_torch = []
for ti in range(nt):
if has_head_veh:
print("%02d x:%5.2f v:%5.2f x1:%5.2f u:%5.2f"%(ti, x[ti].X, v[ti].X, x1[ti].X, u[ti].X))
else:
print("%02d x:%5.2f v:%5.2f u:%5.2f"%(ti, x[ti].X, v[ti].X, u[ti].X))
u_torch.append(u[ti].X)
u_torch = torch.tensor(u_torch).unsqueeze(0)
return u_torch.cuda()
def solve_cem(ti, x_input, vid, stl, args):
def dynamics_step_func(x, u):
return dynamics_per_step(x, u)
def reward_func(trajs):
return stl(trajs, args.smoothing_factor, d={"hard":True})[:, 0]
u_min = torch.tensor([-args.amax]).cuda()
u_max = torch.tensor([args.amax]).cuda()
u, _, info = solve_cem_func(
(x_input[0]).cuda(), state_dim=x_input.shape[-1], nt=args.nt, action_dim=u_min.shape[0],
num_iters=500, n_cand=10000, n_elites=100, policy_type="direct",
dynamics_step_func=dynamics_step_func, reward_func=reward_func,
transform=None, u_clip=(u_min, u_max), seed=None, args=None,
extra_dict=None, quiet=False, device="gpu", visualize=False
)
return u
def sim_multi(net, rl_policy, stl):
metrics_str=["acc", "reward", "score", "t", "safety", "avg_x", "avg_v"]
metrics = {xx:[] for xx in metrics_str}
from envs.car_env import CarEnv
car_env = CarEnv(args)
nt = 150 # 350
N = 1
if args.hybrid:
n_vehs = 10
n_hybrids = 10
nx = 4
ny = 5
else:
n_vehs = 15 # 10
n_hybrids = 0
nx = 4
ny = 5
M = int(nx * ny * 0.6)
IW = 6
n_roads = 16
dx_choices = [10., 12, 15, 18] # [6, 8, 10, 15]
dy_choices = [12., 13, 15, 20] # [7, 10, 13, 15]
dx_list = np.random.choice(dx_choices, nx)
dy_list = np.random.choice(dy_choices, ny)
dx_list += IW / 2
dy_list += IW / 2
map_xy = np.zeros((ny, nx, 2))
# randomly assign traffic light (phases) and stop signs
timers = np.random.rand(ny, nx) * args.phase_t
is_light = np.random.choice(2, (ny, nx))
is_light[0, 0] = 0
is_light[0, nx-1] = 0
is_light[ny-1, 0] = 0
is_light[ny-1, nx-1] = 0
# generate intersections
inters = dict()
inters_2d = [[None for i in range(nx)] for j in range(ny)]
inter_id = 0
for i in range(ny):
for j in range(nx):
map_xy[i, j, 0] = np.sum(dx_list[:j]) if j>0 else 0
map_xy[i, j, 1] = np.sum(dy_list) - (np.sum(dy_list[:i]) if i>0 else 0)
viz_xy = map_xy[i, j] + np.array([-IW/2, -IW/2])
viz_w = IW
viz_h = IW
inters[inter_id] = Intersection(inter_id, map_xy[i, j], IW, is_light[i, j], timers[i, j], viz_xy, viz_w, viz_h)
inters_2d[i][j] = inters[inter_id]
inter_id += 1
# build the roads
lanes = dict()
lanes_2d = dict()
lane_id = 0
viz_from_dxy = np.array([[IW/4, IW/2], [IW/2, -IW/4], [-IW/4, -IW/2], [-IW/2, IW/4]])
viz_to_dxy = np.array([[IW/4, -IW/2], [-IW/2, -IW/4], [-IW/4, IW/2], [IW/2, IW/4]])
for i in range(ny):
for j in range(nx):
for dii, (di, dj) in enumerate([(-1, 0), (0, 1), (1, 0), (0, -1)]):
if 0<=i+di<ny and 0<=j+dj<nx:
from_id = inters_2d[i][j].inter_id
to_id = inters_2d[i+di][j+dj].inter_id
from_xy = map_xy[i, j] + viz_from_dxy[dii]
to_xy = map_xy[i+di, j+dj] + viz_to_dxy[dii]
lane_width = IW / 2
lane_length = np.linalg.norm(from_xy-to_xy)
if dii == 0:
viz_xy = map_xy[i, j] + np.array([0, IW/2])
elif dii == 1:
viz_xy = map_xy[i, j] + np.array([IW/2, -IW/2])
elif dii == 2:
viz_xy = map_xy[i+di, j+dj] + np.array([-IW/2, IW/2])
elif dii == 3:
viz_xy = map_xy[i+di, j+dj] + np.array([IW/2, 0])
viz_w = lane_width if dii in [0, 2] else lane_length
viz_h = lane_length if dii in [0, 2] else lane_width
lanes[lane_id] = Lane(lane_id, from_xy, to_xy, lane_width, lane_length, from_id, to_id, viz_xy, viz_w, viz_h, dii)
lanes_2d[from_id, to_id] = lanes[lane_id]
lane_id += 1
poss_starts = []
poss_starts_idx = list(range(ny*nx))
for i in range(ny):
for j in range(nx):
poss_starts.append([i, j])
# random routes:
moves = np.array([[-1, 0], [1, 0], [0, 1], [0, -1]])
routes = {}
is_used = []
vehicles = {}
for veh_id in range(n_vehs+n_hybrids):
_road_i = np.random.choice(poss_starts_idx)
_roads = [poss_starts[_road_i]]
poss_starts_idx.remove(_road_i)
is_used.append(_roads[0])
prev_i, prev_j = None, None
for k in range(n_roads):
curr_i, curr_j = _roads[-1]
done = False
while not done:
done = True
rand_dir = np.random.choice(4)
next_i, next_j = curr_i + moves[rand_dir][0], curr_j + moves[rand_dir][1]
if next_i<0 or next_i>=ny or next_j<0 or next_j>=nx:
done = False
continue
if k>0 and next_i == _roads[-2][0] and next_j == _roads[-2][1]:
done = False
continue
if is_light[curr_i, curr_j] and get_dir(prev_i, prev_j, curr_i, curr_j, next_i, next_j)==-1:
done = False
continue
prev_i, prev_j = curr_i, curr_j
_roads.append((next_i, next_j))
routes[veh_id] = _roads
# TODO vectorized the cars info
from_id = inters_2d[_roads[0][0]][_roads[0][1]].inter_id
to_id = inters_2d[_roads[1][0]][_roads[1][1]].inter_id
to_inter = inters_2d[_roads[1][0]][_roads[1][1]]
lane_id = lanes_2d[from_id, to_id].lane_id
lane = lanes[lane_id]
lane.veh_ids.append(veh_id)
if to_inter.is_light:
if lane.lane_type in [0, 2]:
car_timer = to_inter.timer
else:
car_timer = (to_inter.timer + args.phase_red) % args.phase_t
# red-3, green-5
# 1 2 3 4 5 6 7 0
# R R R G G G G R
# G G G R R R R G
# 5 6 7 0 1 2 3 4
else:
car_timer = 0
vehicles[veh_id] = Vehicle(
id=veh_id, v=5.0, in_lane=True, in_id=lane_id, dist=lane.dist(), xy=np.array(lane.from_xy),
timer=car_timer, base_i=0, is_light=to_inter.is_light, hist=[], tri=0, out_a=0, is_hybrid=veh_id>=n_vehs,
)
CAR_L = 2
CAR_W = 1.5
fs_list = []
for ti in range(nt):
# compute cars_tri LOGIC
cars_out_a = {}
for int_i in range(ny):
for int_j in range(nx):
veh_queue = []
inter = inters_2d[int_i][int_j]
for di, dj in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
if 0<=di+int_i<ny and 0<=dj+int_j<nx:
from_inter = inters_2d[di+int_i][dj+int_j]
# TODO check vehicles
link = lanes_2d[from_inter.inter_id, inter.inter_id]
for veh_id in link.veh_ids:
veh = vehicles[veh_id]
dist = veh.dist
v_ego = veh.v
lead = 1 if veh_id == link.veh_ids[0] else 0
weight = lead * 1000 - dist + v_ego**2/2/args.amax
veh_queue.append((veh_id, weight))
if len(veh_queue)>0:
veh_queue = sorted(veh_queue, key=lambda x:x[1], reverse=True)
veh = vehicles[veh_queue[0][0]]
veh.tri = 0.0
for veh_pair in veh_queue[1:]:
veh_id, weight = veh_pair
vehicles[veh_id].tri = 1.0
# get the info for cars on lanes
x_input = []
vid=[]
for lane_id in lanes:
lane = lanes[lane_id]
for veh_id in lane.veh_ids:
veh = vehicles[veh_id]
x_ego = -veh.dist
v_ego = veh.v
i_light = veh.is_light
timer = veh.timer
if veh_id != lane.veh_ids[0]:
other_id = lane.veh_ids[0]
other_veh = vehicles[other_id]
x_o = compute_lane_dist(veh.xy, other_veh.xy, lane.lane_type)
v_o = other_veh.v
x_o = np.clip(x_o-args.bloat_dist, 0, 10)
else:
x_o = -1
v_o = 0
i_tri = veh.tri
# clip
x_ego = np.clip(x_ego, -10, 0)
print("LANE t=%d|id=%d x:%.2f v:%.2f L:%.1f T:%.1f xo:%.2f vo:%.2f Trigger:%d"%(ti, veh_id, x_ego, v_ego, i_light, timer, x_o, v_o, i_tri))
x_input.append([x_ego, v_ego, i_light, timer, x_o, v_o, i_tri])
vid.append(veh_id)
x_input = torch.tensor(x_input).float().cuda()
dt_minus1 = 0
if x_input.shape[0]>0:
debug_t1=time.time()
if args.mpc:
u_output = []
for iii in range(x_input.shape[0]):
u_output_item = gurobi_solve(ti, x_input[iii:iii+1])
u_output.append(u_output_item)
u_output = torch.cat(u_output, dim=0)
elif args.rl or args.mbpo or args.pets:
tmp_xs, u_output, dt_minus1 = get_rl_xs_us(x_input, rl_policy, args.nt, include_first=True)
elif args.plan:
u_output = []
for iii in range(x_input.shape[0]):
u_output_item = solve_planner(ti, x_input[iii:iii+1], vid[iii])
u_output.append(u_output_item)
u_output = torch.cat(u_output, dim=0)
elif args.cem:
u_output = []
for iii in range(x_input.shape[0]):
u_output_item = solve_cem(ti, x_input[iii:iii+1], vid[iii], stl, args)
u_output.append(u_output_item[None,:])
u_output = torch.cat(u_output, dim=0)[..., 0]
elif args.grad:
u_output = gradient_solve(ti, x_input, stl)
else:
u_output = net(x_input)
if args.finetune:
seg = dynamics(x_input, u_output)
score_tmp = stl(seg, args.smoothing_factor, d={"hard":True})[:, :1]
acc_tmp = (score_tmp>=0).float()
err_idx = torch.where(acc_tmp<1)[0]
if err_idx.shape[0]>0:
ft_n = err_idx.shape[0]
print(ti, "[Before] Acc=%.2f, %d vehicles do not satisfy STL %s"%(torch.mean(acc_tmp), ft_n, err_idx))
u_output_fix = u_output.clone()
for iii in range(ft_n):
sel_ = err_idx[iii]
u_fix = back_solve(x_input[sel_:sel_+1], u_output[sel_:sel_+1], net, stl)
u_output_fix[sel_:sel_+1] = u_fix
u_output = u_output_fix
seg_fix = dynamics(x_input, u_output_fix)
acc_tmp_fix = (stl(seg_fix, args.smoothing_factor, d={"hard":True})[:, :1]>=0).float()
err_idx_fix = torch.where(acc_tmp_fix<1)[0]
ft_n_fix = err_idx_fix.shape[0]
print(ti, "[After] Acc=%.2f, %d vehicles do not satisfy STL %s"%(torch.mean(acc_tmp_fix), ft_n_fix, err_idx_fix))
debug_t2=time.time()
nn_idx=0
for lane_id in lanes:
lane = lanes[lane_id]
for veh_id in lane.veh_ids:
veh = vehicles[veh_id]
if veh_id>=n_vehs:
u_output[nn_idx, 0] = other_controller(veh, x_input[nn_idx])
nn_idx += 1
seg = dynamics(x_input, u_output)
nn_idx = 0
for lane_id in lanes:
lane = lanes[lane_id]
for veh_id in lane.veh_ids:
veh = vehicles[veh_id]
veh.out_a = u_output[nn_idx, 0].item()
veh.timer = seg[nn_idx, 0, 3].item()
veh.hist = seg[nn_idx].detach().cpu().numpy()
veh.v = seg[nn_idx, 0, 1].item()
nn_idx += 1
else:
debug_t2 = debug_t1 = 0
# get the info for cars in the intersections
x_input2 = []
vid=[]
for inter_id in inters:
inter = inters[inter_id]
for veh_id in inter.veh_ids:
# TODO write to veh class
veh = vehicles[veh_id]
curr_i, curr_j = routes[veh_id][veh.base_i]
next_i, next_j = routes[veh_id][veh.base_i+1]
next_inter = inters_2d[next_i][next_j]
to_id = next_inter.inter_id
next_lane = lanes_2d[inter_id, to_id]
x_ego = -(veh.dist + next_lane.lane_length)
v_ego = veh.v
i_light = next_inter.is_light
timer = veh.timer
x_o = -1
v_o = 0
i_tri = 0
x_ego = np.clip(x_ego, -10, 0)
print("INTER t=%d|id=%d x:%.2f v:%.2f L:%.1f T:%.1f xo:%.2f vo:%.2f Trigger:%d"%(ti, veh_id, x_ego, v_ego, i_light, timer, x_o, v_o, i_tri))
x_input2.append([x_ego, v_ego, i_light, timer, x_o, v_o, i_tri])
vid.append(veh_id)
x_input2 = torch.tensor(x_input2).float().cuda()
dt_minus2 = 0
if x_input2.shape[0]>0:
debug_t3=time.time()
if args.mpc:
u_output2 = []
for iii in range(x_input2.shape[0]):
u_output_item = gurobi_solve(ti, x_input2[iii:iii+1])
u_output2.append(u_output_item)
u_output2 = torch.cat(u_output2, dim=0)
elif args.rl or args.mbpo or args.pets:
tmp_xs, u_output2, dt_minus2 = get_rl_xs_us(x_input2, rl_policy, args.nt, include_first=True)
elif args.plan:
u_output2 = []
for iii in range(x_input2.shape[0]):
u_output_item = solve_planner(ti, x_input2[iii:iii+1], vid[iii])
u_output2.append(u_output_item)
u_output2 = torch.cat(u_output2, dim=0)
elif args.grad:
u_output2 = gradient_solve(ti, x_input2, stl)
else:
u_output2 = net(x_input2)
if args.finetune:
seg2 = dynamics(x_input2, u_output2)
acc_tmp = (stl(seg2, args.smoothing_factor, d={"hard":True})[:, :1]>=0).float()
err_idx = torch.where(acc_tmp<1)[0]
if err_idx.shape[0]>0:
ft_n = err_idx.shape[0]
print(ti, "[Before] Acc=%.2f, %d vehicles do not satisfy STL %s"%(torch.mean(acc_tmp), ft_n, err_idx))
u_output_fix = u_output2.clone()
for iii in range(ft_n):
sel_ = err_idx[iii]
u_fix = back_solve(x_input2[sel_:sel_+1], u_output2[sel_:sel_+1], net, stl)
u_output_fix[sel_:sel_+1] = u_fix
u_output2 = u_output_fix
seg_fix = dynamics(x_input2, u_output_fix)
acc_tmp_fix = (stl(seg_fix, args.smoothing_factor, d={"hard":True})[:, :1]>=0).float()
err_idx_fix = torch.where(acc_tmp_fix<1)[0]
ft_n_fix = err_idx_fix.shape[0]
print(ti, "[After] Acc=%.2f, %d vehicles do not satisfy STL %s"%(torch.mean(acc_tmp_fix), ft_n_fix, err_idx_fix))
debug_t4=time.time()
nn_idx = 0
for inter_id in inters:
inter = inters[inter_id]
for veh_id in inter.veh_ids:
veh = vehicles[veh_id]
if veh_id>=n_vehs:
u_output2[nn_idx, 0] = other_controller(veh, x_input2[nn_idx])
nn_idx += 1
seg2 = dynamics(x_input2, u_output2)
nn_idx = 0
for inter_id in inters:
inter = inters[inter_id]
for veh_id in inter.veh_ids:
veh = vehicles[veh_id]
veh.out_a = u_output2[nn_idx, 0].item()
veh.timer = seg2[nn_idx, 0, 3].item()
veh.hist = seg2[nn_idx].detach().cpu().numpy()
veh.v = seg2[nn_idx, 0, 1].item()
nn_idx += 1
else:
debug_t4 = debug_t3 = 0
seg2 = None
### UDPATE INFO
is_handled = [False for _ in routes]
for lane_id in lanes:
lane = lanes[lane_id]
for veh_id in lane.veh_ids:
veh = vehicles[veh_id]
is_handled[veh_id] = True
ds = veh.v * args.dt
dx, dy = lane.get_dx_dy(ds)
veh.xy[0] += dx
veh.xy[1] += dy
veh.dist -= ds
# NEW REGISTRATION
if veh.dist < 0:
veh.in_id = lane.to_id
inter = inters[lane.to_id]
new_ds = -veh.dist
lane.veh_ids.remove(veh_id)
inter.veh_ids.append(veh_id)
veh.base_i += 1
prev_i, prev_j = routes[veh_id][veh.base_i - 1]
curr_i, curr_j = routes[veh_id][veh.base_i]
next_i, next_j = routes[veh_id][veh.base_i + 1]
lane_type = lane.lane_type
direction = get_dir(prev_i, prev_j, curr_i, curr_j, next_i, next_j)
# clean the distance
veh.xy = np.array(lane.to_xy)
veh.dist = inter.dist(direction)
dx, dy = inter.get_dx_dy(veh.dist, new_ds, lane_type, direction)
veh.xy[0] += dx
veh.xy[1] += dy
veh.dist -= new_ds
next_inter = inters_2d[next_i][next_j]
veh.is_light = next_inter.is_light
if next_inter.is_light:
next_lane = lanes_2d[inter.inter_id, next_inter.inter_id]
if next_lane.lane_type in [0, 2]:
veh.timer = next_inter.timer
else:
veh.timer = (next_inter.timer + args.phase_red) % args.phase_t
else:
veh.timer = 0
for inter_id in inters:
inter = inters[inter_id]
for veh_id in inter.veh_ids:
veh = vehicles[veh_id]
if is_handled[veh_id]:
continue
is_handled[veh_id] = True
ds = veh.v * args.dt
prev_i, prev_j = routes[veh_id][veh.base_i - 1] if veh.base_i!=0 else (None, None)
curr_i, curr_j = routes[veh_id][veh.base_i]
next_i, next_j = routes[veh_id][veh.base_i + 1]
from_id = inters_2d[prev_i][prev_j].inter_id
to_id = inters_2d[curr_i][curr_j].inter_id
lane_id = lanes_2d[from_id, to_id].lane_id
lane_type = lanes[lane_id].lane_type
direction = get_dir(prev_i, prev_j, curr_i, curr_j, next_i, next_j)
dx, dy = inter.get_dx_dy(veh.dist, ds, lane_type, direction)
veh.xy[0] += dx
veh.xy[1] += dy
veh.dist -= ds
# NEW REGISTRATION
if veh.dist < 0:
next_i, next_j = routes[veh_id][veh.base_i + 1]
from_id = veh.in_id
to_id = inters_2d[next_i][next_j].inter_id
lane = lanes_2d[from_id, to_id]
veh.in_id = lane.lane_id
inter.veh_ids.remove(veh_id)
lane.veh_ids.append(veh_id)
# clean the distance
ds = -veh.dist
veh.xy = np.array(lane.from_xy)
veh.dist = lane.dist()
dx, dy = lane.get_dx_dy(ds)
veh.xy[0] += dx
veh.xy[1] += dy
veh.dist -= ds
# EVALUATION
debug_dt = debug_t4-debug_t3 - dt_minus2 + debug_t2-debug_t1 - dt_minus1
if seg is None:
seg_total = seg2
elif seg2 is None:
seg_total = seg
else:
seg_total = torch.cat([seg, seg2], dim=0)
score = stl(seg_total, args.smoothing_factor)[:, :1]
score_avg= torch.mean(score).item()
acc = (stl(seg_total, args.smoothing_factor, d={"hard":True})[:, :1]>=0).float()
acc_avg = torch.mean(acc).item()
reward = np.mean(car_env.generate_reward_batch(to_np(seg_total[:,0])))
safety = np.mean(np.logical_or(to_np(seg_total[:, 0, 4]) > 0, to_np(seg_total[:, 0, 4]) ==-1))
avg_x = np.mean(to_np(seg_total[:,-1, 0]) - to_np(seg_total[:,0, 0]))
avg_v = np.mean(to_np(seg_total[:,0, 1]))
metrics["t"].append(debug_dt)
metrics["safety"].append(safety)
metrics["avg_x"].append(avg_x)
metrics["avg_v"].append(avg_v)