-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo_plot.py
122 lines (92 loc) · 3.57 KB
/
info_plot.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
import matplotlib.pyplot as plt
from numpy import genfromtxt
from test_agent_clean import test_LunarLander as test
import os
import csv
"""Here can be found various methods to plot the learning progress of an agent"""
# agent data is saved in losses.csv
# we save it in the following format
#1. test reward
#2. constrained value
#3. condition number of H
#4. residual of H^{-1}g
#5. residual of H^{-1}b
#6. 2-norm of g
#7. time per iteration
#8. training reward
def plot_speed(render=False):
#Use this method to plot the trajectories of the speed of the aircraft of multiple runs
speed = test(render=render, record_speed=True)
#print(speed)
n = len(speed[1])
a = 0.2
for s in speed[1:-2]:
plt.plot(range(1, n+1), s, color='black', alpha=a)
plt.plot(range(1, n + 1), speed[-2], label='speed', color='black', alpha=a)
plt.plot(range(1, n + 1), n * [1.5], label='limit', color='red')
plt.xlabel('speed')
plt.ylabel('time steps')
plt.legend()
plt.show()
def plot_learning_prog(file):
# plots the reward, the constraint value of a model
df = genfromtxt(file + '/losses.csv', delimiter=',')
params = genfromtxt(file + '/parameters.csv', delimiter=',')
_, n = df.shape
fig, (ax1, ax2) = plt.subplots(2)
# fig.suptitle(f'{int(params[3])} delta')
ax1.plot(range(1, n + 1), df[4])
plt.setp(ax1, ylabel='reward')
ax2.plot(range(1, n + 1), df[1])
plt.setp(ax2, ylabel='constraint')
plt.setp(ax2, xlabel='iterations')
ax2.plot(range(1, n + 1), n * [20], label='limit', color='black')
ax2.legend()
plt.show()
def plot_residual(file):
#plot the residual and the normwise forward error which arises in the calculation of H^{-1}b, H^{-1}g
df = genfromtxt(file + '/losses.csv', delimiter=',')
params = genfromtxt(file + '/parameters.csv', delimiter=',')
_, n = df.shape
fig, (ax3, ax4) = plt.subplots(2)
fig.suptitle(f'{int(params[3])} CG iterations')
ax3.plot(range(1, n + 1), df[3], label='r1')
ax3.plot(range(1, n + 1), df[4], label='r2')
plt.setp(ax3, ylabel='residual')
ax3.legend()
ax4.plot(range(1, n + 1), df[2] * df[3] / df[5], label='k(H)*r1/||g||')
ax4.plot(range(1, n + 1), df[2] * df[4], label='k(H)*r2')
plt.setp(ax4, ylabel='relative forward error')
plt.setp(ax4, xlabel='iterations')
ax4.legend()
plt.show()
def plot_mult(files):
# plots all four plots of the latter two functions for multiple models
dfs = []
kls = []
for file in files:
dfs.append(genfromtxt(file+'/losses.csv', delimiter=','))
kls.append(genfromtxt(file+'/parameters.csv', delimiter=',')[2])
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4)
for df,i in zip(reversed(dfs), reversed(kls)):
_, n = df.shape
ax1.plot(range(1, n + 1), df[7], label=f'{i} delta')
plt.setp(ax1, ylabel='reward')
ax1.legend()
ax2.plot(range(1, n + 1), df[1])
plt.setp(ax2, ylabel='constraint')
if i == kls[0]:
ax2.plot(range(1, n + 1), n*[3], label='limit', color='black')
ax2.legend()
ax3.plot(range(1, n + 1), df[3], label=f'{i} CG iterations')
plt.setp(ax3, ylabel='residual')
ax4.plot(range(1, n + 1), df[2] * df[3]/df[5], label=f'k(A)*r1/||g||, {i} CG iterations')
plt.setp(ax4, ylabel='k(A)*r1/||g||')
plt.setp(ax4, xlabel='iterations')
plt.show()
if __name__ == '__main__':
#example plot
files=[]
for i in range(0,5):
files.append(f'assets/learned_models/CPO/CartPole_new_kl/2023-03-23-exp-{i}-CartPole-v1')
plot_mult(files)