Skip to content

Commit c7ed54d

Browse files
committed
Wrap fig title
1 parent b50d57f commit c7ed54d

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

gcaa/core/dta.py

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time
22
from dataclasses import dataclass
3+
from textwrap import wrap
34

45
import matplotlib.pyplot as plt
56
import numpy as np
@@ -120,15 +121,16 @@ def update_path(p, pos_a, pos_t, time_step, agents, nt):
120121

121122

122123
def optimal_control_dta(
123-
simu_number=7,
124-
simu_name="Dynamics",
125124
use_GCAA=0,
126-
uniform_agents=1,
127-
uniform_tasks=1,
128-
plot_range=0,
125+
uniform_agents=True,
126+
uniform_tasks=True,
129127
na=5,
130128
nt=4,
131-
n_rounds=50,
129+
n_rounds=20,
130+
limited_communication='both',
131+
plot_range=0,
132+
simu_number=7,
133+
simu_name="Dynamics",
132134
):
133135
"""
134136
Optimal Control Dynamic Task Assignment (DTA)
@@ -259,7 +261,15 @@ def optimal_control_dta(
259261
kdrag=kdrag
260262
)
261263

262-
for CommLimit in (0,):
264+
historical_path = np.zeros((n_rounds, na, 2))
265+
266+
if limited_communication == 'both':
267+
communication_limits = (0, 1)
268+
else:
269+
communication_limits = (bool(limited_communication),)
270+
print(communication_limits)
271+
272+
for CommLimit in communication_limits:
263273

264274
# Clear / reset variables used in loop
265275
J = None
@@ -269,6 +279,8 @@ def optimal_control_dta(
269279
S_GCAA_ALL = None
270280
X = None
271281

282+
comm_text = 'limited communication, ' if CommLimit else ''
283+
272284
n_rounds_loop = n_rounds
273285
simu_time_loop = simu_time
274286
time_start_loop = time_start
@@ -303,27 +315,47 @@ def optimal_control_dta(
303315
# Fully connected graph initially (no self links)
304316
G = ~np.eye(na, dtype=bool)
305317

306-
historical_path = np.zeros((n_rounds, na, 2))
318+
fig, ax = plt.subplots()
319+
320+
def wrap_title(event=None):
321+
# Width of the figure in pixels
322+
fig_width_px = fig.get_figwidth() * fig.dpi
323+
324+
# Pick characters-per-line empirically.
325+
# You can tune the scaling factor if needed.
326+
max_chars = int(fig_width_px / 7)
327+
328+
wrapped = "\n".join(wrap(title_text, max_chars))
329+
title.set_text(wrapped)
330+
fig.canvas.draw_idle()
331+
332+
for i_round in range(n_rounds):
333+
334+
ax.clear()
335+
ax.set_xlim(0, map_width)
336+
ax.set_ylim(0, map_width)
337+
# plt.xlabel("x [m]")
338+
# plt.ylabel("y [m]")
339+
340+
title_text = f"Task-Agent allocation ({na} agents, {nt} tasks, {comm_text}round {i_round + 1}/{n_rounds})"
341+
title = ax.set_title(title_text, wrap=True)
307342

308-
for i_round in range(n_rounds): # corresponds to MATLAB 1:n_rounds
343+
# Call once to set the initial wrapped title
344+
wrap_title()
309345

310-
plt.clf()
311-
plt.xlim(0, map_width)
312-
plt.ylim(0, map_width)
313-
plt.xlabel("x [m]")
314-
plt.ylabel("y [m]")
315-
plt.title("Task-Agent allocation")
346+
# Rewrap when figure is resized
347+
fig.canvas.mpl_connect("resize_event", wrap_title)
316348

317349
# plot agents
318350
for i in range(na):
319351
c = colors[i]
320-
plt.plot(pos_a_loop[i, 0], pos_a_loop[i, 1], marker='*',
321-
markersize=10,
322-
label='agents' if i == 0 else "", color=c)
352+
ax.plot(pos_a_loop[i, 0], pos_a_loop[i, 1], marker='*',
353+
markersize=10,
354+
label='agents' if i == 0 else "", color=c)
323355

324356
# plot tasks
325-
plt.plot(pos_t[:, 0], pos_t[:, 1], 'rs', markersize=10,
326-
label='Targets', markerfacecolor=(1, 0.6, 0.6))
357+
ax.plot(pos_t[:, 0], pos_t[:, 1], 'rs', markersize=10,
358+
label='Targets', markerfacecolor=(1, 0.6, 0.6))
327359

328360
# if plot_range:
329361
# external function; kept as-is
@@ -455,9 +487,9 @@ def optimal_control_dta(
455487

456488
U_tot_final = rt_completed - np.sum(J[-1, :])
457489
print("U_tot_final:", U_tot_final)
458-
return dict(historical_path=historical_path)
459490

460491
print("Simulation finished successfully.")
492+
return dict(historical_path=historical_path)
461493

462494

463495
if __name__ == "__main__":

0 commit comments

Comments
 (0)