This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
generated from ortec/euro-neurips-vrp-2022-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplotting.py
130 lines (101 loc) · 3.57 KB
/
plotting.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
import numpy as np
_N_POINTS = 100
def x_axis(stats, step, plot_runtimes):
if plot_runtimes:
return stats.run_times()[::step], "Run-time (s)"
else:
return np.arange(0, stats.num_iters(), step), "Iteration (#)"
def plot_instance(ax, instance, routes=()):
"""
Plot an instance and optionally a solution. This plot contains the depot
location (yellow star) and customer locations. A client is represented by a
blue dot, with a size relative to when its time window opens. Around this
dot, the relative size of the blue circle represents when a time windows
closes.
A given list of routes can also be plotted, if provided.
"""
is_client = ~instance["is_depot"]
coords = instance["coords"][is_client].T
tws_open = instance["time_windows"][is_client, 0]
tws_close = instance["time_windows"][is_client, 1]
depot_coords = instance["coords"][~is_client].T
kwargs = dict(s=(0.0003 * tws_open) ** 2, zorder=3)
ax.scatter(*coords, c="tab:blue", label="TW open", **kwargs)
kwargs = dict(s=(0.0008 * tws_close) ** 2, alpha=0.1, zorder=3)
ax.scatter(*coords, c="tab:blue", label="TW close", **kwargs)
kwargs = dict(marker="*", zorder=3, s=750)
ax.scatter(*depot_coords, c="tab:red", label="Depot", **kwargs)
for route in routes:
ax.plot(*instance["coords"][[0] + route + [0]].T)
ax.grid(color="grey", linestyle="--", linewidth=0.25)
ax.set_title("Solution" if routes else "Instance")
ax.set_aspect("equal", "datalim")
ax.legend(frameon=False, ncol=3)
def plot_population(ax, stats, step=None, plot_runtimes=False):
if step is None:
step = max(1, stats.num_iters() // _N_POINTS)
x_vals, x_label = x_axis(stats, step, plot_runtimes)
ax.set_title("Population diversity")
ax.set_xlabel(x_label)
ax.set_ylabel("Avg. diversity")
ax.plot(
x_vals,
stats.feas_avg_diversity()[::step],
label="Feas. diversity",
c="tab:green",
)
ax.plot(
x_vals,
stats.infeas_avg_diversity()[::step],
label="Infeas. diversity",
c="tab:red",
)
ax.legend(frameon=False)
def plot_objectives(ax, stats, step=None, plot_runtimes=False):
if step is None:
step = max(1, stats.num_iters() // _N_POINTS)
x_vals, x_label = x_axis(stats, step, plot_runtimes)
global_best = np.minimum.accumulate(stats.feas_best_cost())
ax.plot(x_vals, global_best[::step], label="Global best", c="tab:blue")
ax.plot(
x_vals,
stats.feas_best_cost()[::step],
label="Feas best",
c="tab:green",
)
ax.plot(
x_vals,
stats.feas_avg_cost()[::step],
label="Feas avg.",
c="tab:green",
alpha=0.3,
linestyle="dashed",
)
ax.plot(
x_vals,
stats.infeas_best_cost()[::step],
label="Infeas best",
c="tab:red",
)
ax.plot(
x_vals,
stats.infeas_avg_cost()[::step],
label="Infeas avg.",
c="tab:red",
alpha=0.3,
linestyle="dashed",
)
ax.set_title("Population objectives")
ax.set_xlabel(x_label)
ax.set_ylabel("Objective")
# Use global best objectives to set reasonable y-limits
best = min(global_best)
ax.set_ylim(best * 0.995, best * 1.03)
ax.legend(frameon=False)
def plot_incumbents(ax, stats):
times, objs = list(zip(*stats.incumbents()))
ax.plot(times, objs)
ax.set_title("Improving objective values")
ax.set_xlabel("Run-time (s)")
ax.set_ylabel("Objective")
ax.set_ylim(min(objs) * 0.995, min(objs) * 1.03)