forked from google-deepmind/hanabi-learning-environment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rl_env_example.py
81 lines (73 loc) · 3.01 KB
/
rl_env_example.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
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A simple episode runner using the RL environment."""
from __future__ import print_function
import sys
import getopt
import rl_env
from agents.random_agent import RandomAgent
from agents.simple_agent import SimpleAgent
AGENT_CLASSES = {'SimpleAgent': SimpleAgent, 'RandomAgent': RandomAgent}
class Runner(object):
"""Runner class."""
def __init__(self, flags):
"""Initialize runner."""
self.flags = flags
self.agent_config = {'players': flags['players']}
self.environment = rl_env.make('Hanabi-Full', num_players=flags['players'])
self.agent_class = AGENT_CLASSES[flags['agent_class']]
def run(self):
"""Run episodes."""
rewards = []
for episode in range(flags['num_episodes']):
observations = self.environment.reset()
agents = [self.agent_class(self.agent_config)
for _ in range(self.flags['players'])]
done = False
episode_reward = 0
while not done:
for agent_id, agent in enumerate(agents):
observation = observations['player_observations'][agent_id]
action = agent.act(observation)
if observation['current_player'] == agent_id:
assert action is not None
current_player_action = action
else:
assert action is None
# Make an environment step.
print('Agent: {} action: {}'.format(observation['current_player'],
current_player_action))
observations, reward, done, unused_info = self.environment.step(
current_player_action)
episode_reward += reward
rewards.append(episode_reward)
print('Running episode: %d' % episode)
print('Max Reward: %.3f' % max(rewards))
return rewards
if __name__ == "__main__":
flags = {'players': 2, 'num_episodes': 1, 'agent_class': 'SimpleAgent'}
options, arguments = getopt.getopt(sys.argv[1:], '',
['players=',
'num_episodes=',
'agent_class='])
if arguments:
sys.exit('usage: rl_env_example.py [options]\n'
'--players number of players in the game.\n'
'--num_episodes number of game episodes to run.\n'
'--agent_class {}'.format(' or '.join(AGENT_CLASSES.keys())))
for flag, value in options:
flag = flag[2:] # Strip leading --.
flags[flag] = type(flags[flag])(value)
runner = Runner(flags)
runner.run()