-
Notifications
You must be signed in to change notification settings - Fork 2
/
optimization_demo.py
293 lines (252 loc) · 9.55 KB
/
optimization_demo.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
import os
import torch
import torch.optim as optim
import matplotlib.pyplot as plt
from rule_hierarchy.rule_hierarchy import RuleHierarchy
from rule_hierarchy.rules import AvoidCircle, StayWithinStrip
from tests.test_utils import Dynamics
from termcolor import colored
from typing import List
class StayLaneAvoidPothole:
"""
Solve an optimization problem for avoiding a pothole (circle)
while staying within a given lane (given by two lines).
"""
def __init__(self, device) -> None:
T = 20 # planning horizon
self.x_init = torch.zeros(2) # initial (x,y) position
self.x_init.requires_grad_(True)
self.u_init = torch.zeros(T)
self.dt = 0.1 # time step
self.v = 5.0 # constant speed
self.lr = 1.0 # learning rate for optimization
self.grad_tol = 1e-6 # termination grad tol
self.max_num_itr = 500 # termination max itr
self.device = device # cuda or cpu
def _cont_planner(self, rule_hierarchy: RuleHierarchy) -> None:
"""
Continuous Planner
"""
# vehicle is assumed to move with a constant speed self.v
# with the only control input being the steering angle self.u
# which is restricted between -pi/6 and pi/6
dyn = Dynamics(self.u_init.clone(), self.dt, self.v)
optimizer = optim.Adam(dyn.parameters(), self.lr)
u_grad_norm = 1e6
itr = 0
while u_grad_norm > self.grad_tol and itr < self.max_num_itr:
# reset optimizer
optimizer.zero_grad()
# forward rollout of the trajectory given the initial state
# and the current sequence of control inputs dyn.u
traj = dyn(self.x_init)
# compute rule hierarchy loss
loss = rule_hierarchy.diff_traj_cost(traj.to(self.device))
# backpropagate
loss.backward()
# update the sequence of control inputs according to the gradients
optimizer.step()
u_grad_norm = torch.linalg.norm(dyn.u.grad)
itr += 1
if itr % 50 == 0:
print(f"Itr {itr} | loss: {loss.item()}")
# extract the sequence of control inputs
self.u = dyn.u.data
# extract the "optimal" trajectory
self.traj: torch.Tensor = dyn(self.x_init)
@staticmethod
def _create_folder(folder_name: str) -> None:
"""
Creates a folder if it does not exist
"""
if not os.path.exists(folder_name):
os.makedirs(folder_name)
@staticmethod
def _color_text(status: bool) -> str:
"""
Color codes True to green and False to red
"""
if status:
color = "green"
else:
color = "red"
return colored(text=str(status), color=color)
@staticmethod
def _rule_status(scaled_robustness_vector: torch.Tensor) -> List[str]:
"""
Converts robustness vectors to color coded True / False
"""
satisfied: List[bool] = [
scaled_robustness_vector[i].item() > 0
for i in range(scaled_robustness_vector.numel())
]
return [
StayLaneAvoidPothole._color_text(rule_status) for rule_status in satisfied
]
def _visualize(
self,
obs_center_x: float,
obs_center_y: float,
obs_radius: float,
lower_bound_y: float,
upper_bound_y: float,
save_file_name: str,
) -> None:
"""
Plots the final trajectory
"""
fig, ax = plt.subplots()
circle = plt.Circle(
(obs_center_x, obs_center_y),
obs_radius,
edgecolor="black",
facecolor="none",
)
ax.add_patch(circle)
ax.set_xlim(0.0, 10.0)
ax.set_ylim(-5.0, 5.0)
ax.set_aspect("equal")
ax.axhline(lower_bound_y, color="black")
ax.axhline(upper_bound_y, color="black")
traj_np = self.traj.cpu().detach().numpy()
plt.plot(traj_np[..., 0], traj_np[..., 1], color="blue")
StayLaneAvoidPothole._create_folder("plots")
plt.savefig("plots/" + save_file_name)
plt.close()
def feasible(self):
"""
Feasible to avoid the pothole and stay within the lane
Rule Hierarchy: Pothole Avoidance > Stay Within Lane
"""
print("===========================================")
print("Feasible: Avoid Potholes > Stay Within Lane")
print("===========================================")
obs_center_x = 5.0
obs_center_y = 0.0
obs_radius = 2.0
lower_bound_y = -4.0
upper_bound_y = 4.0
scaling = [1.0, 1.0]
# Create rule hierarchy
rules = [
AvoidCircle(obs_center_x, obs_center_y, obs_radius),
StayWithinStrip(lower_bound_y, upper_bound_y),
]
rule_hierarchy = RuleHierarchy[torch.Tensor](rules, scaling, device=self.device)
# Plan with the rule hierarchy
self._cont_planner(rule_hierarchy)
# Visualize the optimizer's output
self._visualize(
obs_center_x,
obs_center_y,
obs_radius,
lower_bound_y,
upper_bound_y,
"demo_feasible.png",
)
# Print status
_, scaled_robustness_vector = rule_hierarchy.traj_cost(
self.traj.to(self.device), get_robustness_vector=True
)
scaled_robustness_vector.squeeze_()
rule_status = StayLaneAvoidPothole._rule_status(scaled_robustness_vector)
pothole_status = rule_status[0]
stay_within_lane_status = rule_status[1]
print(
f"\nPot Hole Avoidance: {pothole_status} \nStay Within Lane : {stay_within_lane_status}\n"
)
def infeasible_avoid_pothole(self):
"""
Infeasible to avoid the pothole and stay within the lane
Rule Hierarchy: Pothole Avoidance > Stay Within Lane
"""
print("=============================================")
print("Infeasible: Avoid Potholes > Stay Within Lane")
print("=============================================")
obs_center_x = 5.0
obs_center_y = 0.0
obs_radius = 2.0
lower_bound_y = -2.0
upper_bound_y = 2.0
scaling = [1.0, 1.0]
# Create rule hierarchy
rules = [
AvoidCircle(obs_center_x, obs_center_y, obs_radius),
StayWithinStrip(lower_bound_y, upper_bound_y),
]
rule_hierarchy = RuleHierarchy[torch.Tensor](rules, scaling, device=self.device)
# Plan with the rule hierarchy
self._cont_planner(rule_hierarchy)
# Visualize the optimizer's output
self._visualize(
obs_center_x,
obs_center_y,
obs_radius,
lower_bound_y,
upper_bound_y,
"demo_infeasible_avoid_pothole.png",
)
# Print status
_, scaled_robustness_vector = rule_hierarchy.traj_cost(
self.traj.to(self.device), get_robustness_vector=True
)
scaled_robustness_vector.squeeze_()
rule_status = StayLaneAvoidPothole._rule_status(scaled_robustness_vector)
pothole_status = rule_status[0]
stay_within_lane_status = rule_status[1]
print(
f"\nPot Hole Avoidance: {pothole_status} \nStay Within Lane : {stay_within_lane_status}\n"
)
def infeasible_stay_within_lane(self):
"""
Infeasible to avoid the pothole and stay within the lane.
Rule Hierarchy: Stay Within Lane > Pothole Avoidance
"""
print("=============================================")
print("Infeasible: Stay Within Lane > Avoid Potholes")
print("=============================================")
obs_center_x = 5.0
obs_center_y = 0.0
obs_radius = 2.0
lower_bound_y = -2.0
upper_bound_y = 2.0
scaling = [1.0, 1.0]
# Create rule hierarchy
rules = [
StayWithinStrip(lower_bound_y, upper_bound_y),
AvoidCircle(obs_center_x, obs_center_y, obs_radius),
]
rule_hierarchy = RuleHierarchy[torch.Tensor](rules, scaling, device=self.device)
self.lr = 0.01
# Plan with the rule hierarchy
self._cont_planner(rule_hierarchy)
# Visualize the optimizer's output
self._visualize(
obs_center_x,
obs_center_y,
obs_radius,
lower_bound_y,
upper_bound_y,
"demo_infeasible_stay_within_lane.png",
)
_, scaled_robustness_vector = rule_hierarchy.traj_cost(
self.traj.to(self.device), get_robustness_vector=True
)
# Print status
scaled_robustness_vector.squeeze_()
rule_status = StayLaneAvoidPothole._rule_status(scaled_robustness_vector)
pothole_status = rule_status[1]
stay_within_lane_status = rule_status[0]
print(
f"\nPot Hole Avoidance: {pothole_status} \nStay Within Lane : {stay_within_lane_status}\n"
)
if __name__ == "__main__":
stay_lane_avoid_pothole = StayLaneAvoidPothole("cuda:0")
# Both pothole avoidance and staying within lane is feasible
stay_lane_avoid_pothole.feasible()
# Both pothole avoidance and staying within lane is infeasible
# Pothole Avoidance > Staying Within Lane
stay_lane_avoid_pothole.infeasible_avoid_pothole()
# Both pothole avoidance and staying within lane is infeasible
# Staying Within Lane > Pothole Avoidance
stay_lane_avoid_pothole.infeasible_stay_within_lane()