-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmujoco.py
More file actions
186 lines (147 loc) · 5.28 KB
/
mujoco.py
File metadata and controls
186 lines (147 loc) · 5.28 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import pandas as pd
import numpy as np
from scipy.interpolate import make_interp_spline
from tensorboard.backend.event_processing import event_accumulator
from tqdm import tqdm
import matplotlib.pyplot as plt
def plot_function(result, algo):
plot_mean = result['eval/mean_reward'].reshape(-1)
plot_samples = np.array(result['step']).reshape(-1)
var = np.array(result['var']).reshape(-1)
X = plot_samples/1.e6
Y = plot_mean
Z = var
plt.plot(X, Y, label=algo.upper())
plt.fill_between(X, Y - Z, Y + Z, alpha=0.2)
def plot_function_pt3(result, algo):
plot_mean = result['eval/mean_reward'].reshape(-1)
plot_samples = np.array(result['step']).reshape(-1)
var = np.array(result['var']).reshape(-1)
X = plot_samples/1.e6
Y = plot_mean
Z = var
plt.plot(X, Y, label="Epi.TD3")
plt.fill_between(X, Y - Z, Y + Z, alpha=0.2)
def plot_function_promp(result, algo):
plot_mean = result['eval/mean_reward'].reshape(-1)
plot_samples = np.array(result['step']).reshape(-1)
var = np.array(result['var']).reshape(-1)
X = plot_samples/1.e6 /0.889 * 2
Y = plot_mean
Z = var
plt.plot(X, Y, label="PMP")
plt.fill_between(X, Y - Z, Y + Z, alpha=0.2)
def csv_save(folder, name, algo):
# save csv file
steps = []
rewards = []
result = {}
for i in range(1, 6):
path = "./" \
+ folder + "/" + name
in_path = path + '_' + f'{i}' + '/' + algo + '_1'
print("path",path)
ex_path = path + '_' + f'_{i}' + '/' + "eval_reward_mean.csv"
event_data = event_accumulator.EventAccumulator(in_path) # a python interface for loading Event data
event_data.Reload() # synchronously loads all of the data written so far b
# print(event_data.Tags()) # print all tags
event_data.Reload()
tags = event_data.Tags()
keys = event_data.scalars.Keys() # get all tags,save in a list
for hist in tags['scalars']:
if hist == 'eval/mean_reward':
histograms = event_data.scalars.Items("eval/mean_reward")
rewards.append(np.array(
[np.array(h.value) for
h in histograms]))
steps.append(np.array(
[np.array(h.step) for
h in histograms]))
rewards = np.array(rewards)[:, ::10]
steps = np.array(steps)[:, ::10]
var = np.std(rewards, axis=0)
rewards = rewards.mean(axis=0)
steps = steps.mean(axis=0)
result['eval/mean_reward'] = rewards
result['step'] = steps
result['var'] = var
return result
def csv_save_promp(folder, name, algo):
# save csv file
steps = []
rewards = []
result = {}
for i in range(1,6):
print(i)
path = "./" \
+ folder + "/" + name
in_path = path + '_' + f'{i}' + '/' + algo
ex_path = path + '_' + f'_{i}' + '/' + "eval_reward_mean.csv"
event_data = event_accumulator.EventAccumulator(in_path) # a python interface for loading Event data
event_data.Reload() # synchronously loads all of the data written so far b
# print(event_data.Tags()) # print all tags
event_data.Reload()
tags = event_data.Tags()
keys = event_data.scalars.Keys() # get all tags,save in a list
for hist in tags['scalars']:
if hist == 'eval/mean_reward':
histograms = event_data.Scalars(hist)
rewards.append(np.array(
[np.array(h.value) for
h in histograms]))
steps.append(np.array(
[np.array(h.step) for
h in histograms]))
# print(steps[-1][-1], steps[-1].shape)
# assert 1==123
rewards = np.array(rewards)[:, ::10]
steps = np.array(steps)[:, ::10]
var = np.std(rewards, axis=0)
rewards = rewards.mean(axis=0)
steps = steps.mean(axis=0)
result['eval/mean_reward'] = rewards
result['step'] = steps
result['var'] = var
return result
folder = "data/MujocoReacher"
value = "eval/mean_reward"
id = "3"
env = "ALRReacherBalanceIP-v" + id
env_promp = "ALRReacherBalanceProMPIP-v" + id
for v in range(1):
algo = "td3"
name = algo + "/" + env
result = csv_save(folder, name, 'TD3')
plot_function(result, algo)
algo = "sac"
name = algo + "/" + env
result = csv_save(folder, name, "SAC")
plot_function(result, algo)
algo = "ppo"
name = algo + "/" + env
result = csv_save(folder, name, 'PPO')
plot_function(result, algo)
algo = "promp"
name = algo + "/" + env_promp
result = csv_save_promp(folder, name, "")
plot_function_promp(result, algo)
algo = "episodic_td3"
name = algo + "/" + env # + algo + "-v{}".format(v)
result = csv_save(folder, name, "run")
plot_function_pt3(result, algo)
# csv_save(folder, name)
# plt.title("ALR Reacher - Line trajectory")
#plt.title("ALR Reacher - Line Trajectory")
#plt.title("Fetch - Line Trajectory")
plt.xlabel("timesteps(1e6)")
plt.ylabel("rewards")
plt.ylim(ymin=-100)
# plt.title("ALRReacher-v3")
# plt.ylim(ymin=-100)
plt.ylim(ymax=0)
plt.legend()
#plt.show()
plt.savefig("fetchv1.png")
import tikzplotlib
# tikzplotlib.save("latex/alr3.tex")
tikzplotlib.save("./data/MujocoReacher/mujoco" + id + ".tex")