-
Notifications
You must be signed in to change notification settings - Fork 156
/
sim_agent.py
executable file
·66 lines (56 loc) · 1.89 KB
/
sim_agent.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
"""
Load a snapshotted agent from an hdf5 file and animate it's behavior
"""
import argparse
import cPickle, h5py, numpy as np, time
from collections import defaultdict
import gym
def animate_rollout(env, agent, n_timesteps,delay=.01):
infos = defaultdict(list)
ob = env.reset()
if hasattr(agent,"reset"): agent.reset()
env.render()
for i in xrange(n_timesteps):
ob = agent.obfilt(ob)
a, _info = agent.act(ob)
(ob, rew, done, info) = env.step(a)
env.render()
if done:
print("terminated after %s timesteps"%i)
break
for (k,v) in info.items():
infos[k].append(v)
infos['ob'].append(ob)
infos['reward'].append(rew)
infos['action'].append(a)
time.sleep(delay)
return infos
def main():
parser = argparse.ArgumentParser()
parser.add_argument("hdf")
parser.add_argument("--timestep_limit",type=int)
parser.add_argument("--snapname")
args = parser.parse_args()
hdf = h5py.File(args.hdf,'r')
snapnames = hdf['agent_snapshots'].keys()
print "snapshots:\n",snapnames
if args.snapname is None:
snapname = snapnames[-1]
elif args.snapname not in snapnames:
raise ValueError("Invalid snapshot name %s"%args.snapname)
else:
snapname = args.snapname
env = gym.make(hdf["env_id"].value)
agent = cPickle.loads(hdf['agent_snapshots'][snapname].value)
agent.stochastic=False
timestep_limit = args.timestep_limit or env.spec.timestep_limit
while True:
infos = animate_rollout(env,agent,n_timesteps=timestep_limit,
delay=1.0/env.metadata.get('video.frames_per_second', 30))
for (k,v) in infos.items():
if k.startswith("reward"):
print "%s: %f"%(k, np.sum(v))
raw_input("press enter to continue")
if __name__ == "__main__":
main()