-
Notifications
You must be signed in to change notification settings - Fork 214
/
do_rollouts.py
executable file
·55 lines (48 loc) · 1.56 KB
/
do_rollouts.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import json
import argparse
import gym
from ray.rllib.utils.policy_client import PolicyClient
parser = argparse.ArgumentParser()
parser.add_argument(
"--no-train", action="store_true", help="Whether to disable training.")
parser.add_argument(
"--off-policy",
action="store_true",
help="Whether to take random instead of on-policy actions.")
if __name__ == "__main__":
args = parser.parse_args()
import pong_py
env = pong_py.PongJSEnv()
client = PolicyClient("http://localhost:8900")
eid = client.start_episode(training_enabled=not args.no_train)
obs = env.reset()
rewards = 0
episode = []
f = open("out.txt", "w")
while True:
if args.off_policy:
action = env.action_space.sample()
client.log_action(eid, obs, action)
else:
action = client.get_action(eid, obs)
next_obs, reward, done, info = env.step(action)
episode.append({
"obs": obs.tolist(),
"action": float(action),
"reward": reward,
})
obs = next_obs
rewards += reward
client.log_returns(eid, reward, info=info)
if done:
print("Total reward:", rewards)
f.write(json.dumps(episode))
f.write("\n")
f.flush()
rewards = 0
client.end_episode(eid, obs)
obs = env.reset()
eid = client.start_episode(training_enabled=not args.no_train)