2 """Plot scenario's runs cost."""
5 from sys import argv, exit
6 import matplotlib.pyplot as plt
13 """Load scenario from file.
21 raise ValueError("File name as argument needed")
23 with open(fn, "r") as f:
27 def load_dir(dn=None, fn=None):
28 """Load scenarios from directory.
33 - `dn` -- Directory name.
36 raise ValueError("Directory name as argument needed")
41 s = load("{}/{}/{}.json".format(dn, d, fn))
49 return np.percentile(li, p) if len(li) > 0 else p
51 if __name__ == "__main__":
53 print("""Usage: ./plot_cost.py DIR FN
56 - DIR is the directory with the results.
57 - FN is the desired test filename.
62 scenarios = load_dir(result_dir, test_fn)
64 err_hist = [0 for i in range(L)]
65 val_list = [[] for i in range(L)]
76 lpc = s["log_path_cost"]
83 val_list[i].append(lpc[i])
84 if min_c == 0.0 or s["goal_cc"] < min_c:
87 if s["goal_cc"] > max_c:
91 icnt_list.append(s["icnt"])
92 bcnt_list.append(s["bcnt"])
93 rcnt_list.append(s["rcnt"])
97 time_list.append(s["time"])
101 otime_list.append(s["otime"])
104 o_err_hist = list(err_hist)
105 err_hist = [i * 100.0 / RUNS for i in o_err_hist]
106 print("{} {}".format(min_s, max_s))
107 icnt_avg = np.average(icnt_list)
108 bcnt_avg = np.average(bcnt_list)
109 rcnt_avg = np.average(rcnt_list)
110 br_avg = np.average([b / r for (b, r) in zip(bcnt_list, rcnt_list)])
111 print("avg. icnt = {}".format(icnt_avg))
112 print("avg. bcnt = {}".format(bcnt_avg))
113 print("avg. rcnt = {}".format(rcnt_avg))
114 print("b/r avg. {}".format(br_avg))
116 plt.rcParams["figure.figsize"] = [12, 8]
117 plt.rcParams["font.size"] = 24
118 plt.rcParams["font.family"] = "cmr10"
119 plt.rcParams["hatch.linewidth"] = 1.0
120 plt.rcParams["lines.linewidth"] = 1.0
121 plt.rc('axes', unicode_minus=False)
123 f, ax = plt.subplots()
125 ax.set_title(scenarios[0]["fn"])
126 ax.set_title("Scenario 8: Final path cost after optimization")
127 #ax.set_title("Scenario 2: Final path cost after optimization")
128 #ax.set_title("Scenario 7: Final path cost after optimization")
129 ax.set_xlabel("Number of iterations [-]")
130 ax.set_ylabel("Cost [m]")
131 eax.set_ylabel("Success rate [%]")
132 #ax.set_ylim([20.1, 57]) # scenario 7
133 #ax.set_ylim([18, 43]) # scenario 2
134 ax.set_ylim([10, 60])
135 eax.set_ylim([0, 100])
140 y2=[i for i in err_hist],
157 y1=[perc(val_list[i], 50 - (5-j)*10) for i in range(L)],
158 y2=[perc(val_list[i], 50 + (5-j)*10) for i in range(L)],
165 [np.average(val_list[i]) for i in range(L)],
168 label="Average cost",
172 ax.tick_params(axis='x', labelrotation=45)
173 h1, la1 = ax.get_legend_handles_labels()
174 h2, la2 = eax.get_legend_handles_labels()
179 ax.legend(h, la, loc="upper right", ncol=1)
180 plt.savefig("out.pdf", bbox_inches="tight")