MCPcopy Create free account
hub / github.com/PyVRP/PyVRP / plot_objectives

Function plot_objectives

pyvrp/plotting/plot_objectives.py:7–60  ·  view source on GitHub ↗

Plots each iteration's objective values. Parameters ---------- result Result for which to plot objectives. num_to_skip Number of initial iterations to skip when plotting. Early iterations often have very high objective values, and obscure what's going on

(
    result: Result,
    num_to_skip: int | None = None,
    ax: plt.Axes | None = None,
    ylim_adjust: tuple[float, float] = (0.99, 1.05),
)

Source from the content-addressed store, hash-verified

5
6
7def plot_objectives(
8 result: Result,
9 num_to_skip: int | None = None,
10 ax: plt.Axes | None = None,
11 ylim_adjust: tuple[float, float] = (0.99, 1.05),
12):
13 """
14 Plots each iteration's objective values.
15
16 Parameters
17 ----------
18 result
19 Result for which to plot objectives.
20 num_to_skip
21 Number of initial iterations to skip when plotting. Early iterations
22 often have very high objective values, and obscure what's going on
23 later in the search. The default skips the first 5% of iterations.
24 ax
25 Axes object to draw the plot on. One will be created if not provided.
26 ylim_adjust
27 Optional adjustment to bound the y-axis to ``(best * ylim_adjust[0],
28 best * ylim_adjust[1])`` where ``best`` denotes the best found feasible
29 objective value.
30 """
31 if not ax:
32 _, ax = plt.subplots()
33
34 if num_to_skip is None:
35 num_to_skip = int(0.05 * result.num_iterations)
36
37 def _plot(x, y, *args, **kwargs):
38 ax.plot(x[num_to_skip:], y[num_to_skip:], *args, **kwargs)
39
40 x = 1 + np.arange(result.num_iterations)
41
42 y = [datum.current_cost for datum in result.stats]
43 _plot(x, y, label="Current")
44
45 y = [datum.candidate_cost for datum in result.stats]
46 _plot(x, y, label="Candidate", alpha=0.2, zorder=1)
47
48 y = [datum.best_cost for datum in result.stats]
49 _plot(x, y, label="Best")
50
51 # Use best-found solution to set reasonable y-limits, if available.
52 if result.is_feasible():
53 best_cost = result.cost()
54 ax.set_ylim(best_cost * ylim_adjust[0], best_cost * ylim_adjust[1])
55
56 ax.set_title("Objectives")
57 ax.set_xlabel("Iteration (#)")
58 ax.set_ylabel("Objective")
59
60 ax.legend(frameon=False)

Callers 1

plot_resultFunction · 0.90

Calls 3

_plotFunction · 0.85
is_feasibleMethod · 0.80
costMethod · 0.45

Tested by

no test coverage detected