-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparse_PPO.py
155 lines (132 loc) · 5.44 KB
/
parse_PPO.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import argparse
import numpy as np
import os
import matplotlib.pyplot as plt
from operator import add, sub
from scipy.signal import savgol_filter
def smooth(scalars, weight): # Weight between 0 and 1
last = scalars[0] # First value in the plot (first timestep)
smoothed = list()
for point in scalars:
smoothed_val = last * weight + (1 - weight) * point # Calculate smoothed value
smoothed.append(smoothed_val) # Save it
last = smoothed_val # Anchor the last smoothed value
return smoothed
def read_max_load_link(standard_out_file):
pre_optim_max_load_link, post_optim_max_load_link = 0, 0
with open(standard_out_file) as fd:
while (True):
line = fd.readline()
if line.startswith("pre-optimization"):
camps = line.split(" ")
pre_optim_max_load_link = float(camps[-1].split('\n')[0])
elif line.startswith("post-optimization"):
camps = line.split(" ")
post_optim_max_load_link = float(camps[-1].split('\n')[0])
break
return (pre_optim_max_load_link, post_optim_max_load_link)
if __name__ == "__main__":
# python parse_PPO.py -d ./Logs/expSP_3top_15_B_NEWLogs.txt
parser = argparse.ArgumentParser(description='Parse file and create plots')
parser.add_argument('-d', help='data file', type=str, required=True, nargs='+')
args = parser.parse_args()
aux = args.d[0].split(".")
aux = aux[1].split("exp")
differentiation_str = str(aux[1].split("Logs")[0])
actor_loss = []
critic_loss = []
avg_std = []
max_link_uti = []
min_link_uti = []
defo_max_uti = []
error_links = []
avg_rewards = []
learning_rate = []
cummulative_rewards = []
if not os.path.exists("./Images"):
os.makedirs("./Images")
if not os.path.exists("./Images/TRAINING/"+differentiation_str):
os.makedirs("./Images/TRAINING/"+differentiation_str)
path_to_dir = "./Images/TRAINING/"+differentiation_str+"/"
model_id = 0
# Load best model
with open(args.d[0]) as fp:
for line in reversed(list(fp)):
arrayLine = line.split(":")
if arrayLine[0]=='MAX REWD':
model_id = int(arrayLine[2].split(",")[0])
break
print("Model with maximum reward: ", model_id)
with open(args.d[0]) as fp:
for line in fp:
arrayLine = line.split(",")
if arrayLine[0]=="<":
max_link_uti.append(float(arrayLine[1]))
elif arrayLine[0]==">":
min_link_uti.append(float(arrayLine[1]))
elif arrayLine[0]=="a":
actor_loss.append(float(arrayLine[1]))
elif arrayLine[0]=="lr":
learning_rate.append(float(arrayLine[1]))
elif arrayLine[0]==";":
avg_std.append(float(arrayLine[1]))
elif arrayLine[0]=="+":
error_links.append(float(arrayLine[1]))
elif arrayLine[0]=="REW":
if float(arrayLine[1])<-3000:
avg_rewards.append(-3000)
else:
avg_rewards.append(float(arrayLine[1]))
elif arrayLine[0]=="c":
critic_loss.append(float(arrayLine[1]))
plt.plot(actor_loss)
plt.xlabel("Training Episode")
plt.ylabel("ACTOR Loss")
plt.savefig(path_to_dir+"ACTORLoss" + differentiation_str)
plt.close()
plt.plot(critic_loss)
plt.xlabel("Training Episode")
plt.ylabel("CRITIC Loss (MSE)")
plt.yscale("log")
plt.savefig(path_to_dir+"CRITICLoss" + differentiation_str)
plt.close()
plt.plot(max_link_uti, label="DRL Max Link Uti")
plt.plot(defo_max_uti, label="DEFO Max Link Uti", c="tab:red")
print("DRL MAX reward: ", np.amax(avg_rewards))
plt.xlabel("Episodes")
lgd = plt.legend(loc="lower left", bbox_to_anchor=(0.07, -0.22), ncol=2, fancybox=True, shadow=True)
plt.title("GNN+DQN Testing score")
plt.ylabel("Maximum link utilization")
#plt.yscale('log')
plt.savefig(path_to_dir+"MaxLinkUti" + differentiation_str, bbox_extra_artists=(lgd,), bbox_inches='tight')
plt.close()
plt.plot(min_link_uti)
plt.xlabel("Episodes")
plt.title("GNN+DQN Testing score")
plt.ylabel("Minimum link utilization")
plt.savefig(path_to_dir+"MinLinkUti" + differentiation_str)
plt.close()
plt.plot(avg_rewards)
plt.xlabel("Episodes")
plt.title("GNN+DQN Testing score")
plt.ylabel("Average reward")
plt.savefig(path_to_dir+"AvgReward" + differentiation_str)
plt.close()
plt.plot(learning_rate)
plt.xlabel("Episodes")
plt.title("GNN+DQN Testing score")
plt.ylabel("Learning rate")
plt.savefig(path_to_dir+"Lr_" + differentiation_str)
plt.close()
plt.plot(error_links)
plt.xlabel("Episodes")
plt.title("GNN+DQN Testing score")
plt.ylabel("Error link (sum_total_TM/num_links")
plt.savefig(path_to_dir+"ErrorLinks" + differentiation_str)
plt.close()
plt.plot(avg_std)
plt.xlabel("Episodes")
plt.title("GNN+DQN Testing score")
plt.ylabel("Avg std of link utilization")
plt.savefig(path_to_dir+"AvgStdUti" + differentiation_str)
plt.close()