]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/commitdiff
Improve plot cost script
authorJiri Vlasak <hubacji1@fel.cvut.cz>
Wed, 19 Jan 2022 13:23:34 +0000 (14:23 +0100)
committerJiri Vlasak <hubacji1@fel.cvut.cz>
Wed, 19 Jan 2022 13:23:34 +0000 (14:23 +0100)
scripts/plot_cost.py

index 84d9bc9a965f2e466e77f9f4acb4827b5619f7f3..d247a1e9678394f6c33be476da4cc30c027167fb 100755 (executable)
@@ -6,6 +6,9 @@ from sys import argv, exit
 import matplotlib.pyplot as plt
 import numpy as np
 
+RUNS = 10000
+ITERS = 1000
+
 def load(fn=None):
     """Load scenario from file.
 
@@ -33,6 +36,8 @@ def load_dir(dn=None, fn=None):
         raise ValueError("Directory name as argument needed")
     scenarios = []
     for d in listdir(dn):
+        if int(d) >= RUNS:
+            continue
         s = load("{}/{}/{}.json".format(dn, d, fn))
         s["dn"] = dn
         s["subdn"] = d
@@ -55,7 +60,7 @@ Where:
     result_dir = argv[1]
     test_fn = argv[2]
     scenarios = load_dir(result_dir, test_fn)
-    L = 1000#len(scenarios[0]["log_path_cost"])
+    L = ITERS
     err_hist = [0 for i in range(L)]
     val_list = [[] for i in range(L)]
     icnt_list = []
@@ -70,6 +75,8 @@ Where:
     for s in scenarios:
         lpc = s["log_path_cost"]
         for i in range(L):
+            if len(lpc) <= i:
+                lpc.append(lpc[-1])
             if lpc[i] == 0:
                 err_hist[i] += 1
             else:
@@ -94,6 +101,8 @@ Where:
             otime_list.append(s["otime"])
         except:
             pass
+    o_err_hist = list(err_hist)
+    err_hist = [i * 100.0 / RUNS for i in o_err_hist]
     print("{} {}".format(min_s, max_s))
     icnt_avg = np.average(icnt_list)
     bcnt_avg = np.average(bcnt_list)
@@ -104,7 +113,7 @@ Where:
     print("avg. rcnt = {}".format(rcnt_avg))
     print("b/r avg. {}".format(br_avg))
 
-    plt.rcParams["figure.figsize"] = [12, 12]
+    plt.rcParams["figure.figsize"] = [12, 8]
     plt.rcParams["font.size"] = 24
     plt.rcParams["font.family"] = "cmr10"
     plt.rcParams["hatch.linewidth"] = 1.0
@@ -114,10 +123,15 @@ Where:
     f, ax = plt.subplots()
     eax = ax.twinx()
     ax.set_title(scenarios[0]["fn"])
+    ax.set_title("Scenario 8: Final path cost after optimization")
+    #ax.set_title("Scenario 2: Final path cost after optimization")
+    #ax.set_title("Scenario 7: Final path cost after optimization")
     ax.set_xlabel("Number of iterations [-]")
     ax.set_ylabel("Cost [m]")
-    eax.set_ylabel("Finished with no path [%]")
-    ax.set_ylim([0, 100])
+    eax.set_ylabel("Success rate [%]")
+    #ax.set_ylim([20.1, 57]) # scenario 7
+    #ax.set_ylim([18, 43]) # scenario 2
+    ax.set_ylim([10, 60])
     eax.set_ylim([0, 100])
 
     eax.fill_between(
@@ -126,18 +140,18 @@ Where:
         y2=[i for i in err_hist],
         color="tab:red",
         alpha=0.2,
-        label="No path found",
+        label="Succes rate",
     )
 
     la = [
-        "Minimum and maximum",
-        "10 % and 90 % percentile",
-        "20 % and 80 % percentile",
-        "30 % and 70 % percentile",
-        "40 % and 60 % percentile",
-        "Median",
+        "Min. to max.",
+        "10 - 90 %",
+        "20 - 80 %",
+        "30 - 70 %",
+        "40 - 60 %",
+        #"Median",
     ]
-    for j in range(6):
+    for j in range(5):
         ax.fill_between(
             x=range(L),
             y1=[perc(val_list[i], 50 - (5-j)*10) for i in range(L)],
@@ -156,7 +170,12 @@ Where:
 
     ax.minorticks_on()
     ax.tick_params(axis='x', labelrotation=45)
-    ax.legend(loc="upper left")
-    eax.legend(loc="upper right")
+    h1, la1 = ax.get_legend_handles_labels()
+    h2, la2 = eax.get_legend_handles_labels()
+
+    h = h1 + h2
+    la = la1 + la2
+
+    ax.legend(h, la, loc="upper right", ncol=1)
     plt.savefig("out.pdf", bbox_inches="tight")
     plt.close()