-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·251 lines (222 loc) · 10.1 KB
/
run.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import os
import gym
import envs
import numpy as np
import tensorflow as tf
from collections import deque
from mpc import MPC
from model import PENN
from agent import Agent, RandomPolicy
# Training params
TASK_HORIZON = 40
PLAN_HORIZON = 5
# CEM params
POPSIZE = 200
NUM_ELITES = 20
MAX_ITERS = 5
# Model params
LR = 1e-3
# Dims
STATE_DIM = 8
class ExperimentGTDynamics(object):
def __init__(self, env_name='Pushing2D-v1', mpc_params=None):
self.env = gym.make(env_name)
self.task_horizon = TASK_HORIZON
self.agent = Agent(self.env)
self.warmup = False # Does not need model
mpc_params['use_gt_dynamics'] = True
self.cem_policy = MPC(self.env, PLAN_HORIZON, None, POPSIZE,
NUM_ELITES, MAX_ITERS, **mpc_params,
use_random_optimizer=False)
self.random_policy = MPC(self.env, PLAN_HORIZON, None, POPSIZE,
NUM_ELITES, MAX_ITERS, **mpc_params,
use_random_optimizer=True)
def test(self, num_episodes, optimizer='cem'):
samples = []
print('Optimizer:', optimizer)
for j in range(num_episodes):
print('Test episode {}'.format(j + 1))
samples.append(
self.agent.sample(
self.task_horizon, self.cem_policy if
optimizer == 'cem' else self.random_policy
)
)
avg_return = np.mean([sample["reward_sum"] for sample in samples])
avg_success = np.mean([sample["rewards"][-1] == 0
for sample in samples])
return avg_return, avg_success
class ExperimentModelDynamics:
def __init__(self, env_name='Pushing2D-v1', num_nets=1, mpc_params=None):
self.env = gym.make(env_name)
self.task_horizon = TASK_HORIZON
self.agent = Agent(self.env)
mpc_params['use_gt_dynamics'] = False
self.model = PENN(num_nets, STATE_DIM,
len(self.env.action_space.sample()), LR)
self.cem_policy = MPC(self.env, PLAN_HORIZON, self.model, POPSIZE,
NUM_ELITES, MAX_ITERS, **mpc_params,
use_random_optimizer=False)
self.random_policy = MPC(self.env, PLAN_HORIZON, self.model, POPSIZE,
NUM_ELITES, MAX_ITERS, **mpc_params,
use_random_optimizer=True)
self.random_policy_no_mpc = RandomPolicy(
len(self.env.action_space.sample()))
def test(self, num_episodes, optimizer='cem'):
samples = list()
for j in range(num_episodes):
print('Test episode {}'.format(j + 1))
samples.append(
self.agent.sample(
self.task_horizon, self.cem_policy
if optimizer == 'cem' else self.random_policy
)
)
avg_return = np.mean([sample["reward_sum"] for sample in samples])
avg_success = np.mean([sample["rewards"][-1] == 0
for sample in samples])
return avg_return, avg_success
def model_warmup(self, num_episodes, num_epochs):
"""Train a single probabilistic model using a random policy
"""
samples = list()
for i in range(num_episodes):
samples.append(self.agent.sample(
self.task_horizon, self.random_policy_no_mpc))
print('')
print('*'*20, 'TRAINING', '*'*20)
self.cem_policy.train(
[sample["obs"] for sample in samples],
[sample["ac"] for sample in samples],
[sample["rewards"] for sample in samples],
epochs=num_epochs
)
def train(self,
num_train_epochs,
num_episodes_per_epoch,
evaluation_interval,
use_buffer=True):
"""Jointly training the model and the policy
"""
def save(filename, arr):
np.save(filename, arr)
# Buffer has a max size of 100 episodes worth of data
obs_buffer = deque(maxlen=40 * 100)
ac_buffer = deque(maxlen=40 * 100)
rewards_buffer = deque(maxlen=40 * 100)
success_cem = list()
success_random = list()
success_name_cem = 'success_pets_cem.npy'
success_name_ran = 'success_pets_random.npy'
for i in range(num_train_epochs):
print("Starting training epoch %d." % (i + 1))
# Collect data using the current dynamics model
samples = list()
for j in range(num_episodes_per_epoch):
samples.append(
self.agent.sample(self.task_horizon, self.cem_policy))
# Update replay buffers
new_obs = [sample["obs"] for sample in samples]
new_ac = [sample["ac"] for sample in samples]
new_rewards = [sample["rewards"] for sample in samples]
obs_buffer.extend(new_obs)
ac_buffer.extend(new_ac)
rewards_buffer.extend(new_rewards)
print("Rewards obtained:", [sample["reward_sum"]
for sample in samples])
# Train the dynamics model using the data
if use_buffer:
self.cem_policy.train(obs_buffer, ac_buffer, rewards_buffer,
epochs=5)
else:
self.cem_policy.train(new_obs, new_ac, new_rewards, epochs=5)
# Test
num_test_episodes = 20
if (i + 1) % evaluation_interval == 0:
avg_return, avg_success_cem = self.test(
num_test_episodes, optimizer='cem')
print('Test success CEM + MPC:', avg_success_cem)
success_cem.append(avg_success_cem)
save(success_name_cem, success_cem)
avg_return, avg_success_ran = self.test(
num_test_episodes, optimizer='random')
print('Test success Random + MPC:', avg_success_ran)
success_random.append(avg_success_ran)
save(success_name_ran, success_random)
def test_cem_gt_dynamics(num_episode=10):
mpc_params = {'use_mpc': False, 'num_particles': 1}
exp = ExperimentGTDynamics(env_name='Pushing2D-v1', mpc_params=mpc_params)
avg_reward, avg_success = exp.test(num_episode)
print('CEM PushingEnv: avg_reward: {}, avg_success: {}'.format(
avg_reward, avg_success))
mpc_params = {'use_mpc': True, 'num_particles': 1}
exp = ExperimentGTDynamics(env_name='Pushing2D-v1', mpc_params=mpc_params)
avg_reward, avg_success = exp.test(num_episode)
print('MPC PushingEnv: avg_reward: {}, avg_success: {}'.format(
avg_reward, avg_success))
mpc_params = {'use_mpc': False, 'num_particles': 1}
exp = ExperimentGTDynamics(env_name='Pushing2DNoisyControl-v1',
mpc_params=mpc_params)
avg_reward, avg_success = exp.test(num_episode)
print('CEM PushingEnv Noisy: avg_reward: {}, avg_success: {}'.format(
avg_reward, avg_success))
mpc_params = {'use_mpc': True, 'num_particles': 1}
exp = ExperimentGTDynamics(env_name='Pushing2DNoisyControl-v1',
mpc_params=mpc_params)
avg_reward, avg_success = exp.test(num_episode)
print('CEM+MPC PushingEnv Noisy: avg_reward: {}, avg_success: {}'.format(
avg_reward, avg_success))
mpc_params = {'use_mpc': False, 'num_particles': 1}
exp = ExperimentGTDynamics(env_name='Pushing2D-v1', mpc_params=mpc_params)
avg_reward, avg_success = exp.test(num_episode, optimizer='random')
print('Random PushingEnv: avg_reward: {}, avg_success: {}'.format(
avg_reward, avg_success))
mpc_params = {'use_mpc': True, 'num_particles': 1}
exp = ExperimentGTDynamics(env_name='Pushing2D-v1', mpc_params=mpc_params)
avg_reward, avg_success = exp.test(num_episode, optimizer='random')
print('Random+MPC PushingEnv: avg_reward: {}, avg_success: {}'.format(
avg_reward, avg_success))
mpc_params = {'use_mpc': False, 'num_particles': 1}
exp = ExperimentGTDynamics(env_name='Pushing2DNoisyControl-v1',
mpc_params=mpc_params)
avg_reward, avg_success = exp.test(num_episode, optimizer='random')
print('Random PushingEnv Noisy: avg_reward: {}, avg_success: {}'.format(
avg_reward, avg_success))
mpc_params = {'use_mpc': True, 'num_particles': 1}
exp = ExperimentGTDynamics(env_name='Pushing2DNoisyControl-v1',
mpc_params=mpc_params)
avg_reward, avg_success = exp.test(num_episode, optimizer='random')
print('Random+MPC PushingEnv Noisy: avg_reward: {}, avg_success: {}'.format(
avg_reward, avg_success))
def train_single_dynamics(num_test_episode=50):
num_nets = 1
num_episodes = 1000
num_epochs = 100
mpc_params = {'use_mpc': True, 'num_particles': 1}
exp = ExperimentModelDynamics(env_name='Pushing2D-v1', num_nets=num_nets,
mpc_params=mpc_params)
print('*'*20, 'WARMUP', '*'*20)
exp.model_warmup(num_episodes=num_episodes, num_epochs=num_epochs)
avg_reward, avg_success = exp.test(num_test_episode, optimizer='random')
print('MPC PushingEnv: avg_reward: {}, avg_success: {}'.format(
avg_reward, avg_success))
def train_pets():
num_nets = 2
num_epochs = 500
evaluation_interval = 50
num_episodes_per_epoch = 1
mpc_params = {'use_mpc': True, 'num_particles': 6}
exp = ExperimentModelDynamics(env_name='Pushing2D-v1', num_nets=num_nets,
mpc_params=mpc_params)
print('*'*20, 'WARMUP', '*'*20)
exp.model_warmup(num_episodes=100, num_epochs=10)
exp.train(num_train_epochs=num_epochs,
num_episodes_per_epoch=num_episodes_per_epoch,
evaluation_interval=evaluation_interval)
if __name__ == "__main__":
# Disable AVX/FMA and TF warnings
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
test_cem_gt_dynamics(50)
train_single_dynamics(50)
train_pets()