Skip to content

Instantly share code, notes, and snippets.

@masouduut94
Last active August 29, 2023 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masouduut94/e0a6a51888cdbc914decbe32f29142b4 to your computer and use it in GitHub Desktop.
Save masouduut94/e0a6a51888cdbc914decbe32f29142b4 to your computer and use it in GitHub Desktop.
class LGRMctsAgent(RaveMctsAgent):
def __init__(self, state: GameState = GameState(8)):
super().__init__(state)
self.black_reply = {}
self.white_reply = {}
def set_gamestate(self, state: GameState) -> None:
"""
Set the root_state of the tree to the passed gamestate, this clears all
the information stored in the tree since none of it applies to the new
state.
"""
super().set_gamestate(state)
self.white_reply = {}
self.black_reply = {}
def roll_out(self, state: GameState) -> tuple:
"""
Simulate a random game except that we play all known critical
cells first, return the winning player and record critical cells at the end.
"""
moves = state.moves()
first = state.turn()
if first == GameMeta.PLAYERS["black"]:
current_reply = self.black_reply
other_reply = self.white_reply
else:
current_reply = self.white_reply
other_reply = self.black_reply
black_moves = []
white_moves = []
last_move = None
while state.winner == GameMeta.PLAYERS["none"]:
if last_move in current_reply:
move = current_reply[last_move]
# Choose LGR policy with probability 0.5
if move not in moves or random() > MCTSMeta.RANDOMNESS:
move = choice(moves)
else:
move = choice(moves)
if state.turn() == GameMeta.PLAYERS["black"]:
black_moves.append(move)
else:
white_moves.append(move)
current_reply, other_reply = other_reply, current_reply
state.play(move)
moves.remove(move)
last_move = move
black_rave_pts = []
white_rave_pts = []
for x in range(state.size):
for y in range(state.size):
if state.board[(x, y)] == GameMeta.PLAYERS["black"]:
black_rave_pts.append((x, y))
elif state.board[(x, y)] == GameMeta.PLAYERS["white"]:
white_rave_pts.append((x, y))
# Now let's store the good replies in memory for both white and black players.
offset = 0
skip = 0
if state.winner == GameMeta.PLAYERS["black"]:
if first == GameMeta.PLAYERS["black"]:
offset = 1
if state.turn() == GameMeta.PLAYERS["black"]:
skip = 1
for i in range(len(white_moves) - skip):
self.black_reply[white_moves[i]] = black_moves[i + offset]
else:
if first == GameMeta.PLAYERS["white"]:
offset = 1
if state.turn() == GameMeta.PLAYERS["white"]:
skip = 1
for i in range(len(black_moves) - skip):
self.white_reply[black_moves[i]] = white_moves[i + offset]
return state.winner, black_rave_pts, white_rave_pts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment