]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/blob - scripts/plot_multidir_cost.py
9e330dbdd11e191121b3f9af7f86069c8a0941fe
[hubacji1/iamcar2.git] / scripts / plot_multidir_cost.py
1 #!/usr/bin/env python3
2 """Plot scenario's runs cost."""
3 from json import loads
4 from os import listdir
5 from sys import argv, exit
6 import matplotlib.pyplot as plt
7 import numpy as np
8
9 FN = "test54"
10 RUNS = 10000
11 ITERS = 1000
12
13 def load(fn=None):
14     """Load scenario from file.
15
16     Keyword arguments
17     -----------------
18
19     - `fn` -- File name.
20     """
21     if fn is None:
22         raise ValueError("File name as argument needed")
23     s = None
24     with open(fn, "r") as f:
25         s = loads(f.read())
26     return s
27
28 def load_dir(dn=None, fn=None):
29     """Load scenarios from directory.
30
31     Keyword arguments
32     -----------------
33
34     - `dn` -- Directory name.
35     """
36     if dn is None:
37         raise ValueError("Directory name as argument needed")
38     scenarios = []
39     for d in listdir(dn):
40         if int(d) >= RUNS:
41             continue
42         s = load("{}/{}/{}.json".format(dn, d, fn))
43         s["dn"] = dn
44         s["subdn"] = d
45         s["fn"] = fn
46         scenarios.append(s)
47     return scenarios
48
49 def perc(li=[], p=0):
50     return np.percentile(li, p) if len(li) > 0 else p
51
52 if __name__ == "__main__":
53     WHAT_PLOT = 1
54     colors = [
55         "tab:red",
56         "tab:orange",
57         "tab:blue",
58         "tab:green",
59         "tab:red"]
60     names = [
61         "OSP-WK",
62         "OSP-WK + cost heur.",
63         "OSP-WK + goal zone",
64         "OSP-WK + path opt.",
65         "OSP-All"]
66     #names = [
67     #    "RRT",
68     #    "No cost heuristics",
69     #    "No goal zone",
70     #    "No path optimization",
71     #    "Out-of-slot planner"]
72     titles = [
73         "Failure rate",
74         "Average final path cost",
75         "Maximum final path cost"]
76
77     plt.rcParams["figure.figsize"] = [14, 7]
78     plt.rcParams["font.size"] = 24
79     plt.rcParams["font.family"] = "cmr10"
80     plt.rcParams["hatch.linewidth"] = 1.0
81     plt.rcParams["lines.linewidth"] = 1.0
82     plt.rc('axes', unicode_minus=False)
83
84     f, ax = plt.subplots()
85     ax.grid(which="major", linestyle=":", color="tab:gray")
86     #eax = ax.twinx()
87
88     #ax.set_title("Scenario 3: {}".format(titles[WHAT_PLOT - 1]))
89     #ax.set_title("Scenario 3: {}".format(titles[WHAT_PLOT - 1]))
90
91     #ax.set_title("Scenario 8: Final path cost after optimization")
92     #ax.set_title("Scenario 2: Final path cost after optimization")
93     #ax.set_title("Scenario 7: Final path cost after optimization")
94     ax.set_xlabel("Number of iterations [-]")
95     if WHAT_PLOT == 1:
96         ax.set_ylabel("Failure rate [%]")
97     else:
98         ax.set_ylabel("Cost [m]")
99     #ax.set_ylim([20.1, 57]) # scenario 7
100     #ax.set_ylim([18, 43]) # scenario 2
101     if WHAT_PLOT > 1:
102         ax.set_ylim([10, 40])
103     #ax.set_ylim([0, 100])
104
105     ci = -1
106     p1 = 0
107     p2 = 0
108     c1 = 0
109     c2 = 0
110     result_dir = "rwps-additional/rwps-1"
111     for test_fn in [
112         "test54-no-improvement",
113         "test54-w-cost-heuristics",
114         "test54-w-goal-zone",
115         "test54-w-opt",
116         "test55"]:
117         ci += 1
118         scenarios = load_dir(result_dir, test_fn)
119         L = ITERS
120         err_hist = [0 for i in range(L)]
121         val_list = [[] for i in range(L)]
122         icnt_list = []
123         bcnt_list = []
124         rcnt_list = []
125         time_list = []
126         otime_list = []
127         min_s = 0
128         min_c = 0.0
129         max_s = 0
130         max_c = 0.0
131         for s in scenarios:
132             lpc = s["log_path_cost"]
133             for i in range(L):
134                 if len(lpc) <= i:
135                     lpc.append(lpc[-1])
136                 if lpc[i] == 0:
137                     err_hist[i] += 1
138                 else:
139                     val_list[i].append(lpc[i])
140             if min_c == 0.0 or s["goal_cc"] < min_c:
141                 min_c = s["goal_cc"]
142                 min_s = s["subdn"]
143             if s["goal_cc"] > max_c:
144                 max_c = s["goal_cc"]
145                 max_s = s["subdn"]
146             try:
147                 icnt_list.append(s["icnt"])
148                 bcnt_list.append(s["bcnt"])
149                 rcnt_list.append(s["rcnt"])
150             except:
151                 pass
152             try:
153                 time_list.append(s["time"])
154             except:
155                 pass
156             try:
157                 otime_list.append(s["otime"])
158             except:
159                 pass
160         o_err_hist = list(err_hist)
161         err_hist = [i * 100.0 / RUNS for i in o_err_hist]
162         #print("{} {}".format(min_s, max_s))
163         icnt_avg = np.average(icnt_list)
164         bcnt_avg = np.average(bcnt_list)
165         rcnt_avg = np.average(rcnt_list)
166         br_avg = np.average([b / r for (b, r) in zip(bcnt_list, rcnt_list)])
167         #print("avg. icnt = {}".format(icnt_avg))
168         #print("avg. bcnt = {}".format(bcnt_avg))
169         #print("avg. rcnt = {}".format(rcnt_avg))
170         #print("b/r avg. {}".format(br_avg))
171         print("last avg. cost = {}".format(np.average(val_list[-1])))
172         #print("last max. cost = {}".format(perc(val_list[-1], 100)))
173         print("failure rate = {}".format(err_hist[-1]))
174         for i in range(len(err_hist)):
175             if err_hist[i] == 0:
176                 print(i)
177                 break
178
179         #eax.fill_between(
180         #    x=range(L),
181         #    y1=0,
182         #    y2=[i for i in err_hist],
183         #    color=colors[ci],
184         #    alpha=0.2,
185         #    label="Succes rate {}".format(ci),
186         #)
187         if WHAT_PLOT == 1:
188             ax.plot(
189                 range(L),
190                 [i for i in err_hist],
191                 color=colors[ci],
192                 #alpha=0.45,
193                 label=names[ci],
194                 linestyle=(0, (5, 5)) if ci == 0 else "solid",
195             )
196         if WHAT_PLOT == 2:
197             ax.plot(
198                 range(L),
199                 [np.average(val_list[i]) for i in range(L)],
200                 color=colors[ci],
201                 #alpha=0.45,
202                 label=names[ci],
203                 linestyle=(0, (5, 5)) if ci == 0 else "solid",
204             )
205         if WHAT_PLOT == 3:
206             ax.plot(
207                 range(L),
208                 [perc(val_list[i], 100) for i in range(L)],
209                 color=colors[ci],
210                 #alpha=0.45,
211                 label=names[ci],
212                 linestyle=(0, (5, 5)) if ci == 0 else "solid",
213             )
214
215     ax.minorticks_on()
216     ax.tick_params(axis='x', labelrotation=45)
217     h1, la1 = ax.get_legend_handles_labels()
218     #h2, la2 = eax.get_legend_handles_labels()
219
220     h = h1# + h2
221     la = la1# + la2
222
223     ax.legend(h, la, loc="upper right", ncol=1)
224     if WHAT_PLOT == 2:
225         ax.legend(h, la, loc="upper right", ncol=2)
226     plt.savefig("out.pdf", bbox_inches="tight")
227     plt.close()