-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathrun_pg.py
executable file
·66 lines (60 loc) · 2.47 KB
/
run_pg.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
#!/usr/bin/env python
"""
This script runs a policy gradient algorithm
"""
from gym.envs import make
from modular_rl import *
import argparse, sys, cPickle
from tabulate import tabulate
import shutil, os, logging
import gym
if __name__ == "__main__":
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
update_argument_parser(parser, GENERAL_OPTIONS)
parser.add_argument("--env",required=True)
parser.add_argument("--agent",required=True)
parser.add_argument("--plot",action="store_true")
args,_ = parser.parse_known_args([arg for arg in sys.argv[1:] if arg not in ('-h', '--help')])
env = make(args.env)
env_spec = env.spec
mondir = args.outfile + ".dir"
if os.path.exists(mondir): shutil.rmtree(mondir)
os.mkdir(mondir)
env = gym.wrappers.Monitor(env, mondir, video_callable=None if args.video else VIDEO_NEVER)
agent_ctor = get_agent_cls(args.agent)
update_argument_parser(parser, agent_ctor.options)
args = parser.parse_args()
if args.timestep_limit == 0:
args.timestep_limit = env_spec.timestep_limit
cfg = args.__dict__
np.random.seed(args.seed)
agent = agent_ctor(env.observation_space, env.action_space, cfg)
if args.use_hdf:
hdf, diagnostics = prepare_h5_file(args)
gym.logger.setLevel(logging.WARN)
COUNTER = 0
def callback(stats):
global COUNTER
COUNTER += 1
# Print stats
print "*********** Iteration %i ****************" % COUNTER
print tabulate(filter(lambda (k,v) : np.asarray(v).size==1, stats.items())) #pylint: disable=W0110
# Store to hdf5
if args.use_hdf:
for (stat,val) in stats.items():
if np.asarray(val).ndim==0:
diagnostics[stat].append(val)
else:
assert val.ndim == 1
diagnostics[stat].extend(val)
if args.snapshot_every and ((COUNTER % args.snapshot_every==0) or (COUNTER==args.n_iter)):
hdf['/agent_snapshots/%0.4i'%COUNTER] = np.array(cPickle.dumps(agent,-1))
# Plot
if args.plot:
animate_rollout(env, agent, min(500, args.timestep_limit))
run_policy_gradient_algorithm(env, agent, callback=callback, usercfg = cfg)
if args.use_hdf:
hdf['env_id'] = env_spec.id
try: hdf['env'] = np.array(cPickle.dumps(env, -1))
except Exception: print "failed to pickle env" #pylint: disable=W0703
env.close()