-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_double_integrator.py
129 lines (99 loc) · 4.38 KB
/
run_double_integrator.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
import argparse
import matplotlib.pyplot as plt
import torch
import numpy as np
from model.model import STATE_DIM, POLICY_DIM
from model.model_setup_for_verification import setup_model
from solver.get_RBPOA import get_RBPOA
from utils.visualization_utils import plot_polytope, plot_util_sets
def generate_A_matrix(agent_num):
A = torch.zeros((4, 8))
A[:, (agent_num*4):((agent_num+1)*4)] = torch.eye(4)
A[:2, (agent_num*4+2):((agent_num+1)*4)] = torch.eye(2)
return A
def parse_arguments():
parser = argparse.ArgumentParser(description="2 Agent double integrator RVO Policy RBPOA")
parser.add_argument('--lb_x', type=float, default=0, help="input lower bound of agents x coord")
parser.add_argument('--ub_x', type=float, default=10., help="input upper bound of agents x coord")
parser.add_argument('--lb_y', type=float, default=0, help="input lower bound of agents y coord")
parser.add_argument('--ub_y', type=float, default=10., help="input upper bound of agents y coord")
parser.add_argument('--hidden_dim_1', type=int, default=10, help="first hidden dim of controller")
parser.add_argument('--hidden_dim_2', type=int, default=10, help='second hidden dim of controller')
parser.add_argument('--checkpoint_1', type=str, default=None, help='if model_init == load, init agent 1 weights from this checkpoint file')
parser.add_argument('--checkpoint_2', type=str, default=None, help='if model_init == load, init agent 2 weights from this checkpoint file')
parser.add_argument('--state_uncertainty', type=float, default=0.5, help="size of L-inf epsilon ball around uncertain state we consider")
parser.add_argument('-r', type=float, default=1., help="Minimum Safety Radius Between Agents")
parser.add_argument('--num_cs', type=int, default=20, help="number of half-planes for backprojection set.")
parser.add_argument('--RBPUA', action="store_true", help="whether to sample RBPUA or not")
parser.add_argument('--plot', action='store_true', help="whether to plot results or not")
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_arguments()
if torch.cuda.is_available():
torch.set_default_tensor_type('torch.cuda.FloatTensor')
torch.set_default_device('cuda:0')
# defines the matrices for computing pairwise distances
H = torch.zeros((4, 2*STATE_DIM+2*POLICY_DIM))
H[:2, :STATE_DIM] = torch.eye(STATE_DIM)
H[:2, STATE_DIM+POLICY_DIM:2*STATE_DIM+POLICY_DIM] = -torch.eye(STATE_DIM)
H[2:, :STATE_DIM] = -torch.eye(STATE_DIM)
H[2:, STATE_DIM+POLICY_DIM:2*STATE_DIM+POLICY_DIM] = torch.eye(STATE_DIM)
# matrix of minimum safety radius
r = args.r
d = -torch.Tensor([r, r, r, r])
dist_max = r + 2
A1 = generate_A_matrix(0)
A2 = generate_A_matrix(1)
B = torch.Tensor(
[
[0.5, 0.],
[0., 0.5],
[1., 0.],
[0., 1.]
]
)
accel_lb = torch.tensor([-2., -2.]).cuda()
accel_ub = torch.tensor([2., 2.]).cuda()
state_space_lbs = [args.lb_x, args.lb_y, -1, -1, args.lb_x, args.lb_y, -1, -1]
state_space_ubs = [args.ub_x, args.ub_y, 1, 1, args.ub_x, args.ub_y, 1, 1]
num_cs = args.num_cs
cs = [[np.cos(2*np.pi*t / (num_cs*2)), np.sin(2*np.pi*t / (num_cs*2))] for t in range(num_cs)]
cs = torch.tensor(cs)
ckpt1 = args.checkpoint_1
ckpt2 = args.checkpoint_2
model, _, _ = setup_model(
A1, A2, B, B,
ckpt1, ckpt2,
hidden_dim_1 = args.hidden_dim_1,
hidden_dim_2 = args.hidden_dim_2,
n = 2,
u_lb = accel_lb,
u_ub = accel_ub
)
As, Bs, Aus, Bus, runtime = get_RBPOA(
model = model,
H = H,
d = d,
cs = cs,
state_space_lbs = state_space_lbs,
state_space_ubs = state_space_ubs,
dist_max = dist_max,
num_agents = 2,
agent_1_id = 0,
agent_2_id = 1,
uncertainty = args.state_uncertainty,
monte_carlo = args.RBPUA
)
if args.plot:
# intialize matplotlib figure
plt.figure(0)
plt.rcParams.update({'font.size': 50})
axis = plt.gca()
plot_polytope(As[0], Bs[0], 0, axis)
plot_polytope(-Aus[0], Bus[0], 0, axis, color='blue')
plot_util_sets(r, dist_max, 0, axis)
plt.xlim(-dist_max-1, dist_max+1)
plt.ylim(-dist_max-1, dist_max+1)
plt.show()
exit(0)