-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.py
259 lines (190 loc) · 7.69 KB
/
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
from abc import ABCMeta, abstractmethod
import numpy as np
from humblerl import Callback, Environment, MDP
from humblerl.environments import Discrete
class GameState(metaclass=ABCMeta):
"""Game state interface.
Args:
state (object): Game state, type depending on implementation.
"""
def __init__(self, state):
self.raw = state
@abstractmethod
def __hash__(self):
pass
@abstractmethod
def __eq__(self, other):
pass
class BoardState(GameState):
"""Board games state.
Args:
state (np.ndarray): Board state.
"""
def __init__(self, state):
super(BoardState, self).__init__(state)
def __hash__(self):
return hash(self.raw.tostring())
def __eq__(self, other):
return np.all(self.raw == other.raw)
class BoardGameMDP(MDP):
"""Define board game MDP.
Args:
game (Game): Board game object.
"""
def __init__(self, game):
self._game = game
self._first_player = 1
self._action_space = Discrete(num=game.getActionSize())
def transition(self, state, action):
"""Perform `action` in `state`. Return outcome.
Args:
state (BoardState): Canonical board game (from perspective of current player).
action (int): Board game action.
Returns:
BoardState: Next canonical board game state (from perspective of next player).
float: 1 if current player won, -1 if current player lost, 0 for draw (it it's
terminal state).
"""
# In whole MDP we operate only on canonical board representations.
# Canonical means, that it's from perspective of current player.
# From perspective of some player means that he is 1 on the board.
next_state = self._game.getNextState(state.raw, 1, action)
# Draw has some small value, truncate it and leave only:
# -1 (lose), 0 (draw), 1 (win)
reward = float(int(self._game.getGameEnded(next_state[0], 1)))
canonical_state = self._game.getCanonicalForm(*next_state)
return BoardState(canonical_state), reward
def get_init_state(self):
"""Prepare and return initial state.
Returns:
BoardState: Initial state.
"""
# We need to represent init state from perspective of starting player.
# Otherwise different first players could have different starting conditions e.g in Othello.
init_state = self._game.getCanonicalForm(self._game.getInitBoard(), self.first_player)
return BoardState(init_state)
def get_valid_actions(self, state):
"""Get available actions in `state`.
Args:
state (BoardState): Canonical board game (from perspective of current player).
Returns:
np.ndarray: Array with available moves numbers in given state.
"""
valid_moves_map = self._game.getValidMoves(state.raw, 1).astype(bool)
return np.arange(valid_moves_map.shape[0])[valid_moves_map]
def is_terminal_state(self, state):
"""Check if `state` is terminal.
Args:
state (BoardState): MDP's state.
Returns:
bool: Whether state is terminal or not.
"""
return self._game.getGameEnded(state.raw, 1) != 0
@property
def action_space(self):
"""Discrete: Discrete action space."""
return self._action_space
@property
def state_space(self):
"""tuple: A tuple of board dimensions."""
return self._game.getBoardSize()
@property
def first_player(self):
"""Access first player in initial state."""
return self._first_player
@first_player.setter
def first_player(self, value):
"""value (int): Set first player in initial state."""
assert value == 1 or value == -1, "First player can be only 1 or -1!"
self._first_player = value
class BoardGameEnv(Callback, Environment):
"""Environment for board games from https://github.com/suragnair/alpha-zero-general
Args:
game (Game): Board game object.
Note:
step(...) returns reward from perspective of player one!
Also to alternate starting player between episodes add this object to loop as callback too.
"""
def __init__(self, game):
self._game = game
self._first_player = 1
self._last_action = -1
self._last_player = -1
self._action_space = Discrete(num=game.getActionSize())
def step(self, action):
next_state = self._game.getNextState(*self.current_state, action)
# Current player took action, get reward from perspective of player one
end = self._game.getGameEnded(next_state[0], 1)
# Draw has some small value, truncate it and leave only:
# -1 (lose), 0 (draw), 1 (win)
reward = float(int(end))
self._last_action = action
self._last_player = self.current_state[1]
self._current_state = next_state
return next_state, reward, end != 0, None
def reset(self, train_mode=True):
self.train_mode = train_mode
# We need to represent init state from perspective of starting player.
# Otherwise different first players could have different starting conditions e.g in Othello.
self._current_state = (self._game.getCanonicalForm(self._game.getInitBoard(), self._first_player),
self._first_player)
return self.current_state
def render(self, fancy=False):
"""Display board when environment is in test mode.
Args:
fancy (bool): Display a fancy 2D board.
"""
print("Player {}, Action {}".format(
self._last_player, self._last_action))
if fancy and self.current_state[0].ndim == 2:
self.render_fancy_board()
else:
print(self.current_state[0])
def render_fancy_board(self):
def line_sep(length):
print(" ", end="")
for _ in range(length):
print("=", end="")
print("")
state = self.current_state[0].astype(int)
m, n = state.shape
line_sep(3 * n + 1)
legend = {1: "X", -1: "O"}
for i in range(m):
print("|", end=" ")
for j in range(n):
s = legend.get(state[i][j], "-")
if (i * m + j) == self._last_action:
print("\033[1m{:2}\033[0m".format(s), end=" ")
else:
print("{:2}".format(s), end=" ")
print("|")
line_sep(3 * n + 1)
def on_episode_end(self, episode, train_mode):
"""Event after environment was reset.
Args:
episode (int): Episode number.
train_mode (bool): Informs whether episode is in training or evaluation mode.
Note:
You can assume, that this event occurs after step to terminal state.
"""
# Alternate starting player between episodes
self._first_player *= -1
@property
def action_space(self):
"""Discrete: Discrete action space."""
return self._action_space
@property
def state_space(self):
"""tuple: A tuple of board dimensions."""
return self._game.getBoardSize()
@property
def current_state(self):
"""object: Current state."""
return self._current_state
@property
def valid_actions(self):
"""np.ndarray: A binary vector of length self.action_space(), 1 for moves that are
valid from the current state, 0 for invalid moves."""
valid_moves_map = self._game.getValidMoves(*self.current_state).astype(bool)
return np.arange(valid_moves_map.shape[0])[valid_moves_map]