Plots the given solution. Parameters ---------- solution Solution to plot. data Data instance underlying the solution. plot_clients Whether to plot all clients as dots. Default False, which plots only the solution's routes. ax Axe
(
solution: Solution,
data: ProblemData,
plot_clients: bool = False,
ax: plt.Axes | None = None,
)
| 5 | |
| 6 | |
| 7 | def plot_solution( |
| 8 | solution: Solution, |
| 9 | data: ProblemData, |
| 10 | plot_clients: bool = False, |
| 11 | ax: plt.Axes | None = None, |
| 12 | ): |
| 13 | """ |
| 14 | Plots the given solution. |
| 15 | |
| 16 | Parameters |
| 17 | ---------- |
| 18 | solution |
| 19 | Solution to plot. |
| 20 | data |
| 21 | Data instance underlying the solution. |
| 22 | plot_clients |
| 23 | Whether to plot all clients as dots. Default False, which plots only |
| 24 | the solution's routes. |
| 25 | ax |
| 26 | Axes object to draw the plot on. One will be created if not provided. |
| 27 | """ |
| 28 | if not ax: |
| 29 | _, ax = plt.subplots() |
| 30 | |
| 31 | num_locs = data.num_locations |
| 32 | x_coords = np.array([data.location(loc).x for loc in range(num_locs)]) |
| 33 | y_coords = np.array([data.location(loc).y for loc in range(num_locs)]) |
| 34 | |
| 35 | # These are the depots, as big red stars. |
| 36 | kwargs = dict(label="Depot", c="tab:red", marker="*", zorder=3, s=500) |
| 37 | ax.scatter( |
| 38 | x_coords[: data.num_depots], |
| 39 | y_coords[: data.num_depots], |
| 40 | **kwargs, |
| 41 | ) |
| 42 | |
| 43 | colors = plt.get_cmap("tab10") |
| 44 | in_solution = np.zeros(data.num_locations, dtype=bool) |
| 45 | for idx, route in enumerate(solution.routes()): |
| 46 | color = colors(idx % colors.N) |
| 47 | in_solution[route] = True |
| 48 | |
| 49 | if len(route) == 1 or plot_clients: # explicit client coordinate plot |
| 50 | kwargs = dict(label=f"Route {idx + 1}", zorder=3, s=75) |
| 51 | ax.scatter(x_coords[route], y_coords[route], **kwargs, color=color) |
| 52 | |
| 53 | for trip in route.trips(): |
| 54 | if len(trip) == 0: |
| 55 | continue |
| 56 | |
| 57 | x = x_coords[trip] |
| 58 | y = y_coords[trip] |
| 59 | |
| 60 | # Clients visited by this trip, as a line segment or single dot (in |
| 61 | # case of a singleton trip). Trips of the same route share colour. |
| 62 | if len(trip) == 1: |
| 63 | ax.scatter(x, y, zorder=3, s=75, color=color) |
| 64 | else: |
no test coverage detected