-
Notifications
You must be signed in to change notification settings - Fork 39
/
plot_time_windows.py
46 lines (38 loc) · 1.17 KB
/
plot_time_windows.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
from typing import Optional
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import LineCollection
from pyvrp import ProblemData
def plot_time_windows(
data: ProblemData,
title: str = "Time windows",
ax: Optional[plt.Axes] = None,
):
"""
Plots client time windows, as vertical bars sorted by time window.
Parameters
----------
data
Data instance.
title
Title to add to the plot.
ax
Axes object to draw the plot on. One will be created if not provided.
"""
if not ax:
_, ax = plt.subplots()
tw = np.array(
[
[data.location(loc).tw_early, data.location(loc).tw_late]
for loc in range(data.num_locations)
]
)
# Lexicographic sort so for equal start we get shorter TW first
tw = tw[np.lexsort((tw[:, 1], tw[:, 0]))]
lines = [((i, early), (i, late)) for i, (early, late) in enumerate(tw)]
ax.add_collection(LineCollection(lines, linewidths=1))
ax.set_xlim([0, data.num_locations])
ax.set_ylim([tw.min(), tw.max()])
ax.set_title(title)
ax.set_xlabel("Client (sorted by TW)")
ax.set_ylabel("Time window")