-
Notifications
You must be signed in to change notification settings - Fork 4
/
run_env.py
executable file
·288 lines (232 loc) · 7.54 KB
/
run_env.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python
"""Run an environment with the chosen policy.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import ast
import os
import random
import pprint
import numpy as np
import tensorflow as tf
from tf_agents.environments import tf_py_environment
from tf_agents.policies import py_tf_policy
from tf_agents.policies import random_tf_policy
from tf_agents.utils import common as common_utils
from robovat.simulation.simulator import Simulator
from robovat.utils.logging import logger
from robovat.utils.yaml_config import YamlConfig
import policies
from utils import suite_env
from utils import py_driver
tf.compat.v1.disable_eager_execution()
def parse_args():
"""Parse arguments.
Returns:
args: The parsed arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
'--env',
dest='env',
type=str,
help='The environment name.',
required=True)
parser.add_argument(
'--env_config',
dest='env_config',
type=str,
help='The configuration file for the environment.',
default=None)
parser.add_argument(
'--use_simulator',
dest='use_simulator',
type=int,
help='Run experiments in the simulation is it is True.',
required=False,
default=1)
parser.add_argument(
'--assets',
dest='assets_dir',
type=str,
help='The assets directory.',
default='./assets')
parser.add_argument(
'--worker_id',
dest='worker_id',
type=int,
help='The worker ID for running multiple simulations in parallel.',
default=0)
parser.add_argument(
'--debug',
dest='debug',
type=int,
help='Use the debugging mode if it is True.',
default=0)
parser.add_argument(
'--num_episodes',
dest='num_episodes',
type=int,
help='Maximum number of episodes.',
default=1000000)
parser.add_argument(
'--episodic',
dest='episodic',
type=int,
help='If True, use episode driver, otherwise use the step driver.',
default=1)
parser.add_argument(
'--use_random_policy',
dest='use_random_policy',
type=int,
help='If True, use the random policy.',
default=None)
parser.add_argument(
'--policy',
dest='policy',
type=str,
help='The policy name.',
default=None)
parser.add_argument(
'--policy_config',
dest='policy_config',
type=str,
help='The configuration file for the policy.',
default=None)
parser.add_argument(
'--config_bindings',
dest='config_bindings',
type=str,
help='The configuration bindings for the environment and the policy.',
default=None)
parser.add_argument(
'--checkpoint',
dest='checkpoint',
type=str,
help='Directory of the checkpoint.',
default=None)
parser.add_argument(
'--problem',
dest='problem',
type=str,
help='Name of the problem.',
default=None)
parser.add_argument(
'--seed',
dest='seed',
type=int,
help='None for random; any fixed integers for deterministic.',
default=None)
parser.add_argument(
'--output',
dest='output_dir',
type=str,
help='Directory of the output data.',
default=None)
args = parser.parse_args()
return args
def parse_config_files_and_bindings(args):
"""Parse the config files and the bindings.
Args:
args: The arguments.
Returns:
env_config: Environment configuration.
policy_config: Policy configuration.
"""
if args.env_config is None:
env_config = dict()
else:
env_config = YamlConfig(args.env_config).as_easydict()
if args.policy_config is None:
policy_config = dict()
else:
policy_config = YamlConfig(args.policy_config).as_easydict()
if args.config_bindings is not None:
parsed_bindings = ast.literal_eval(args.config_bindings)
logger.info('Config Bindings: %r', parsed_bindings)
env_config.update(parsed_bindings)
policy_config.update(parsed_bindings)
logger.info('Environment Config:')
pprint.pprint(env_config)
logger.info('Policy Config:')
pprint.pprint(policy_config)
return env_config, policy_config
def main():
args = parse_args()
# Set the random seed.
if args.seed is not None:
random.seed(args.seed)
np.random.seed(args.seed)
tf.set_random_seed(args.seed)
# Configuration.
env_config, policy_config = parse_config_files_and_bindings(args)
# Simulator.
if args.use_simulator:
simulator = Simulator(worker_id=args.worker_id,
use_visualizer=bool(args.debug),
assets_dir=args.assets_dir)
else:
simulator = None
# Environment.
logger.info('Building the environment %s...', args.env)
py_env = suite_env.load(args.env,
simulator=simulator,
config=env_config,
debug=args.debug,
max_episode_steps=None)
tf_env = tf_py_environment.TFPyEnvironment(py_env)
# Policy.
if args.use_random_policy:
tf_policy = random_tf_policy.RandomTFPolicy(
time_step_spec=tf_env.time_step_spec(),
action_spec=tf_env.action_spec(),
)
else:
assert args.policy is not None
policy_class = getattr(policies, args.policy)
tf_policy = policy_class(time_step_spec=tf_env.time_step_spec(),
action_spec=tf_env.action_spec(),
config=policy_config)
py_policy = py_tf_policy.PyTFPolicy(tf_policy)
# This is required by tf-agents-nightly.
tf.compat.v1.enable_resource_variables()
sess_config = tf.compat.v1.ConfigProto()
sess_config.gpu_options.allow_growth = True
with tf.compat.v1.Session(config=sess_config) as sess:
observers = []
# Generate episodes.
if args.episodic:
driver = py_driver.PyEpisodeDriver(
py_env,
py_policy,
observers=observers,
max_episodes=args.num_episodes)
else:
driver = py_driver.PyStepDriver(
py_env,
py_policy,
observers=observers,
max_steps=None,
max_episodes=args.num_episodes)
# Initialize.
if len(tf_policy.variables()) > 0:
common_utils.initialize_uninitialized_variables(sess)
if args.checkpoint is not None:
if os.path.isdir(args.checkpoint):
train_dir = os.path.join(args.checkpoint, 'train')
checkpoint_path = tf.compat.v1.train.latest_checkpoint(
train_dir)
else:
checkpoint_path = args.checkpoint
restorer = tf.compat.v1.train.Saver(name='restorer')
logger.info('Restoring parameters from %s ...', checkpoint_path)
restorer.restore(sess, checkpoint_path)
# Run.
time_step = py_env.reset()
# policy_state = py_policy.get_initial_state(py_env.batch_size)
policy_state = ()
driver.run(time_step, policy_state)
if __name__ == '__main__':
main()