Plots demands for clients, as vertical bars sorted by demand. Parameters ---------- data Data instance. dimension Load dimension to plot. Defaults to the first dimension. title Title to add to the plot. ax Axes object to draw the plot on.
(
data: ProblemData,
dimension: int = 0,
title: str | None = None,
ax: plt.Axes | None = None,
)
| 5 | |
| 6 | |
| 7 | def plot_demands( |
| 8 | data: ProblemData, |
| 9 | dimension: int = 0, |
| 10 | title: str | None = None, |
| 11 | ax: plt.Axes | None = None, |
| 12 | ): |
| 13 | """ |
| 14 | Plots demands for clients, as vertical bars sorted by demand. |
| 15 | |
| 16 | Parameters |
| 17 | ---------- |
| 18 | data |
| 19 | Data instance. |
| 20 | dimension |
| 21 | Load dimension to plot. Defaults to the first dimension. |
| 22 | title |
| 23 | Title to add to the plot. |
| 24 | ax |
| 25 | Axes object to draw the plot on. One will be created if not provided. |
| 26 | |
| 27 | Raises |
| 28 | ------ |
| 29 | ValueError |
| 30 | When the load dimension is out of bounds for the given data instance. |
| 31 | """ |
| 32 | if dimension >= data.num_load_dimensions: |
| 33 | raise ValueError(f"Load dimension '{dimension}' is not understood.") |
| 34 | |
| 35 | if not ax: |
| 36 | _, ax = plt.subplots() |
| 37 | |
| 38 | clients = data.clients() |
| 39 | demand = np.array([client.delivery[dimension] for client in clients]) |
| 40 | demand = np.sort(demand) |
| 41 | |
| 42 | ax.bar(np.arange(data.num_depots, data.num_locations), demand) |
| 43 | |
| 44 | if title is None: |
| 45 | veh_types = data.vehicle_types() |
| 46 | weights = [veh_type.num_available for veh_type in veh_types] |
| 47 | capacities = [veh_type.capacity[dimension] for veh_type in veh_types] |
| 48 | mean_capacity = np.average(capacities, weights=weights) |
| 49 | |
| 50 | title = ( |
| 51 | f"Demands (avg. cap = {mean_capacity:.2f}, " |
| 52 | f"{mean_capacity / demand.mean():.2f} stops/route)" |
| 53 | ) |
| 54 | |
| 55 | ax.set_title(title) |
| 56 | ax.set_xlabel("Client (sorted by demand)") |
| 57 | ax.set_ylabel("Demand") |
no test coverage detected