Skip to content

Commit

Permalink
feat: New method in play_vs_alphazero. We can now let the alphazero-a…
Browse files Browse the repository at this point in the history
…lgorithm play against a standard MCTS algorithm. Sadly, alphazero is still not able to win against the standard MCTS algorithm when both are given 1000 simulations.
  • Loading branch information
ChristianFredrikJohnsen committed Apr 30, 2024
1 parent 0037e4d commit 0e2f35e
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/play/play_vs_alphazero.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from src.neuralnet.neural_network import NeuralNetwork
from src.alphazero.agents.alphazero_play_agent import AlphaZero
from src.mcts.mcts import Mcts
from src.utils.game_context import GameContext

def player(state):
Expand All @@ -17,11 +17,38 @@ def player(state):
print("Invalid move, try again!")
state.apply_action(action)


def ai(state, alphazero: AlphaZero):
def ai(state, alphazero: AlphaZero | Mcts):
action = alphazero.run_simulation(state, num_simulations = 1000)
state.apply_action(action)

def play_mcts_vs_alphazero(context: GameContext, first: bool):
"""
Lets the MCTS agent play against the AlphaZero agent.
If first is True, the MCTS agent will play first.
Both agents are given 1000 simulations before making a move.
"""

mcts = Mcts()
alphazero = AlphaZero(context=context)
state = alphazero.context.game.new_initial_state()

if not first:
print('~~~~~~~~~~~~~~~ ALPHAZERO ~~~~~~~~~~~~~~~~')
print(state)
ai(state, alphazero)

while not state.is_terminal():
print('~~~~~~~~~~~~~~~ MONTE CARLO ~~~~~~~~~~~~~~~~')
print(state)
ai(state, mcts)
print('~~~~~~~~~~~~~~~ ALPHAZERO ~~~~~~~~~~~~~~~~')
print(state)
if state.is_terminal():
break
ai(state, alphazero)
if state.is_terminal():
print('~~~~~~~~~~~~~~~ MONTE CARLO ~~~~~~~~~~~~~~~~')
print(state)

def play_game(player1, player2, state, alphazero: AlphaZero, first: bool):

Expand All @@ -42,7 +69,11 @@ def play_game(player1, player2, state, alphazero: AlphaZero, first: bool):
print(state)


def main(context: GameContext, first: bool):
def main(context: GameContext, first: bool, mcts: bool = False):

if mcts:
play_mcts_vs_alphazero(context, first)
return
alphazero = AlphaZero(context=context)
state = alphazero.context.game.new_initial_state()
play_game(player, ai, state, alphazero, first)

0 comments on commit 0e2f35e

Please sign in to comment.