-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.py
More file actions
198 lines (161 loc) · 5.37 KB
/
visualization.py
File metadata and controls
198 lines (161 loc) · 5.37 KB
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
import sys
import matplotlib.pyplot as plt
import numpy as np
from Constants import Constants
from utils import *
try:
J_opt = np.load("workspaces/J_opt.npy")
u_opt = np.load("workspaces/u_opt.npy")
except FileNotFoundError:
print("Please run the main.py script first to compute the optimal cost and policy.")
sys.exit()
M = Constants.M
N = Constants.N
START_POS = Constants.START_POS
GOAL_POS = Constants.GOAL_POS
DRONE_POS = Constants.DRONE_POS
STATE_SPACE = Constants.STATE_SPACE
INPUT_SPACE = Constants.INPUT_SPACE
swan_x = None
swan_y = None
fig, ax = plt.subplots(figsize=(8, 6))
ax.set_title("Click on the grid to place the swan", fontsize=14)
def setup_axes(ax):
ax.set_xlabel("X-axis (East-West)")
ax.set_ylabel("Y-axis (South-North)")
ax.set_xlim(-0.5, M - 0.5)
ax.set_ylim(-0.5, N - 0.5)
ax.set_aspect("equal")
ax.set_xticks(range(M), minor=False)
ax.set_yticks(range(N), minor=False)
ax.set_xticks(np.arange(-0.5, M - 0.5 + 1), minor=True)
ax.set_yticks(np.arange(-0.5, N - 0.5 + 1), minor=True)
ax.grid(which="minor", color="gray", linestyle="-", linewidth=0.5)
ax.grid(which="major", linestyle="", linewidth=0)
ax.set_axisbelow(True)
setup_axes(ax)
ax.scatter(START_POS[0], START_POS[1], color="green", s=100, label="Start", zorder=3)
ax.scatter(GOAL_POS[0], GOAL_POS[1], color="red", s=100, label="Goal", zorder=3)
ax.scatter(
DRONE_POS[:, 0],
DRONE_POS[:, 1],
color="orange",
s=100,
label="Static Drones",
zorder=3,
)
(swan_marker,) = ax.plot(
[], [], "o", color="purple", markersize=10, label="Swan", zorder=3
)
cbar = None
def update_plots(swan_x, swan_y):
global cbar # Declare cbar as global to modify it inside the function
swan_positions = (STATE_SPACE[:, 2] == swan_x) & (STATE_SPACE[:, 3] == swan_y)
if not np.any(swan_positions):
print(f"No states found for swan at position ({swan_x}, {swan_y}).")
return
robot_positions = STATE_SPACE[swan_positions][:, :2]
robot_indices = np.where(swan_positions)[0]
J_opt_grid = np.full((N, M), np.nan) # Use np.nan to mask positions
u_opt_grid = np.full((N, M, 2), np.nan)
# Fill in the cost-to-go and optimal policy for each robot position
for idx, pos in zip(robot_indices, robot_positions):
x, y = pos # x and y positions of the robot
if 0 <= x < M and 0 <= y < N:
J_opt_grid[y, x] = J_opt[idx]
u_opt_grid[y, x, :] = Constants.INPUT_SPACE[u_opt[idx]]
# Mask the static drone positions
for drone_pos in DRONE_POS:
x, y = drone_pos
if 0 <= x < M and 0 <= y < N:
J_opt_grid[y, x] = np.nan # Mask the cost-to-go value
u_opt_grid[y, x, :] = np.nan # Mask the control input
# Remove existing colorbar if any
if cbar is not None:
cbar.remove()
# Clear previous plots
ax.cla()
# Update plot settings
ax.set_title(f"Policy with Swan at ({swan_x}, {swan_y}).", fontsize=14)
setup_axes(ax)
# Plot the cost-to-go heatmap, using a masked array to handle np.nan
im = ax.imshow(
J_opt_grid,
origin="lower",
cmap="viridis",
interpolation="nearest",
extent=(-0.5, M - 0.5, -0.5, N - 0.5),
zorder=1,
)
# Create color bar
cbar = fig.colorbar(im, ax=ax)
# Plot start, goal, drones, and swan positions
ax.scatter(
START_POS[0], START_POS[1], color="green", s=100, label="Start", zorder=3
)
ax.scatter(GOAL_POS[0], GOAL_POS[1], color="red", s=100, label="Goal", zorder=3)
ax.scatter(
DRONE_POS[:, 0],
DRONE_POS[:, 1],
color="orange",
s=100,
label="Static Drones",
zorder=3,
)
ax.scatter(swan_x, swan_y, color="purple", s=100, label="Swan", zorder=3)
X, Y = np.meshgrid(np.arange(M), np.arange(N))
u_x = u_opt_grid[:, :, 0]
u_y = u_opt_grid[:, :, 1]
# Mask the static drone positions in the action vectors
u_x_masked = np.ma.array(u_x, mask=np.isnan(u_x))
u_y_masked = np.ma.array(u_y, mask=np.isnan(u_y))
# Plot the vector field (arrows based on the flow field)
ax.quiver(
X,
Y,
Constants.FLOW_FIELD[:, :, 0],
Constants.FLOW_FIELD[:, :, 1],
angles="xy",
scale_units="xy",
color="red",
width=0.003,
headwidth=3,
headlength=4,
zorder=3,
label="Flow Field",
)
# Plot the vector field (arrows based on the input)
ax.quiver(
X,
Y,
u_x_masked,
u_y_masked,
angles="xy",
scale_units="xy",
color="blue",
width=0.003,
headwidth=3,
headlength=4,
zorder=2,
label="Input",
)
# Remove legend and readd
leg = ax.get_legend()
if leg is not None:
leg.remove()
ax.legend(loc="upper left", framealpha=0.5)
fig.canvas.draw_idle()
def on_click(event):
if event.inaxes != ax:
return
x = int(round(event.xdata))
y = int(round(event.ydata))
if event.button == 1: # Left mouse button
if 0 <= x < M and 0 <= y < N:
global swan_x, swan_y
swan_x, swan_y = x, y
print(f"Swan placed at position ({swan_x}, {swan_y})")
swan_marker.set_data([swan_x], [swan_y])
update_plots(swan_x, swan_y)
cid = fig.canvas.mpl_connect("button_press_event", on_click)
plt.show()