-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathabalone_env.py
159 lines (124 loc) · 4.73 KB
/
abalone_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
import gym
from gym import error, spaces, utils, logger
from gym.utils import seeding
import numpy as np
from gym_abalone.game.graphics.abalonegui import AbaloneGui
from gym_abalone.game.engine.gamelogic import AbaloneGame
class Reward:
@staticmethod
def method_1(observation, move_type):
CONST_REWARDS = {
'winner' : 12,
'ejected' : 2,
'inline_push' : 0.5,
'sidestep_move' : -0.1,
'inline_move' : -0.1,
}
reward = CONST_REWARDS.get(move_type, 0)
return reward
class AbaloneEnv(gym.Env):
"""
Description:
Abalone game environment
Observation:
Type: Box(61, 61)
Actions:
Type: Box(2)
Reward:
see the Reward class' methods
Episode Termination:
Abalone gameover (6 marbles pushed)
Episode length is greater than max_turns.
"""
metadata = {
'render.modes': ['human', 'terminal'],
'video.frames_per_second' : 10,
}
def __init__(self, render_mode='human', max_turns=200):
super(AbaloneEnv, self).__init__()
# Every environment comes with an action_space and an observation_space.
# These attributes are of type Space
self.action_space = gym.spaces.Box(0, 60, shape=(2,), dtype=np.uint8)
self.observation_space = gym.spaces.Box(np.int8(0), np.int8(-1), shape=(11, 11), dtype=np.int8)
self.render_mode = render_mode
self.max_turns = max_turns
self.game = AbaloneGame()
self.gui = None
self._modifications = None
#self.reward_method = 'default'
def step(self, action):
"""
implementation of the classic “agent-environment loop”.
Args:
action (object) : the board
Returns:
observation (object):
reward (float)
done (boolean)
info (dict)
"""
#assert self.action_space.contains(action), f"{action} ({type(action)})"
reward = 0
info = {
'turn' : self.game.turns_count,
'move_type' : None,
'player' : self.game.current_player,
'player_name' : ['white', 'black'][self.game.current_player]
}
if self.done:
logger.warn("You are calling 'step()' even though this environment has already returned done = True."
"You should always call 'reset()' once you receive 'done = True'"
"-- any further steps are undefined behavior.")
else:
pos0, pos1 = action
move_check = self.game.action_handler(pos0, pos1, return_modif=True)
if move_check: # if the move is a valid move
move_type, self._modifications = move_check
reward = Reward.method_1(self.game.board, move_type)
# for debug
info['move_type'] = move_type
return self.observation, reward, self.done, info
def reset(self, player=0, random_player=True, variant_name='classical', random_pick=False):
self.game.reset(
player=player, random_player=random_player,
variant_name=variant_name, random_pick=random_pick
)
if self.render_mode == 'human' and self.gui:
self.gui.reset()
return self.observation
def render(self, fps=None):
if self.render_mode == 'human':
if self.gui is None:
self.gui = AbaloneGui(self.game)
self.gui.reset()
self.gui.update(self._modifications, fps=fps)
elif self.render_mode == 'terminal':
pass
def close(self):
if self.render_mode == 'human' and self.gui:
self.gui.close()
@property
def turns(self):
return self.game.turns_count
@property
def observation(self):
return np.copy(self.game.board)
@property
def done(self):
game_over = self.game.game_over
too_much_turn = (self.game.turns_count > self.max_turns)
return game_over or too_much_turn
@property
def current_player(self):
return self.game.current_player
def get_action_mask(self):
"""
return a action mask which as a 61*61 = 3721 numpy vector
with 0 and 1. 0 if the action is illegal and 1 otherwise.
"""
player = self.game.current_player
possible_moves = self.game.get_possible_moves(player, group_by_type=False)
possible_index = np.array([p0*61+p1 for p0,p1 in possible_moves])
action_mask = np.zeros(61**2)
action_mask[possible_index] = np.ones(possible_index.shape)
return action_mask