Skip to content

Instantly share code, notes, and snippets.

@masouduut94
Created August 29, 2023 14:59
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/654242ad5d89c570d80f0cdd324c3ed0 to your computer and use it in GitHub Desktop.
Save masouduut94/654242ad5d89c570d80f0cdd324c3ed0 to your computer and use it in GitHub Desktop.
class PoolRaveMctsAgent(RaveMctsAgent):
def __init__(self, state: GameState = GameState(8)):
super().__init__(state)
self.black_rave = {}
self.white_rave = {}
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.black_rave = {}
self.white_rave = {}
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()
# Sorting cells based on their poolrave scores.
black_rave_moves = sorted(self.black_rave.keys(),
key=lambda cell: self.black_rave[cell])
white_rave_moves = sorted(self.white_rave.keys(),
key=lambda cell: self.white_rave[cell])
black_pool = []
white_pool = []
i = 0
# Choose m best moves (the moves that appear a lot in simulations that eventuate in wins.
while len(black_pool) < MCTSMeta.POOLRAVE_CAPACITY and i < len(black_rave_moves):
if black_rave_moves[i] in moves:
black_pool.append(black_rave_moves[i])
i += 1
i = 0
while len(white_pool) < MCTSMeta.POOLRAVE_CAPACITY and i < len(white_rave_moves):
if white_rave_moves[i] in moves:
white_pool.append(white_rave_moves[i])
i += 1
num_pool = 0
while state.winner == GameMeta.PLAYERS["none"]:
move = None
# Pick up black move from pool
if len(black_pool) > 0 and state.turn() == GameMeta.PLAYERS["black"]:
move = choice(black_pool)
num_pool += 1
# Pick up white move from pool
elif len(white_pool) > 0:
move = choice(white_pool)
num_pool += 1
if random() > MCTSMeta.RANDOMNESS or not move or move not in moves:
move = choice(moves)
num_pool -= 1
state.play(move)
moves.remove(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))
if state.winner == GameMeta.PLAYERS["black"]:
# Increase/decrease the score of black and white rave points based on the simulation results.
if (x, y) in self.black_rave:
self.black_rave[(x, y)] += 1
else:
self.black_rave[(x, y)] = 1
else:
if (x, y) in self.black_rave:
self.black_rave[(x, y)] -= 1
else:
self.black_rave[(x, y)] = -1
elif state.board[(x, y)] == GameMeta.PLAYERS["white"]:
white_rave_pts.append((x, y))
if state.winner == GameMeta.PLAYERS["white"]:
if (x, y) in self.white_rave:
self.white_rave[(x, y)] += 1
else:
self.white_rave[(x, y)] = 1
else:
if (x, y) in self.white_rave:
self.white_rave[(x, y)] -= 1
else:
self.white_rave[(x, y)] = -1
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