<jupyter_start><jupyter_text>Test Training Run Checkpoints Against a Random Agent to see if Training SucceededThis notebook performs a basic check to compare each checkpoint of a training run against a random agent. If the training run is successful, we should observe improved performance over time.<jupyter_code>CHKPT_DIR = 'results/chkpts_board5_24hrs/'  # The path to the checkpoints directory
NUM_CHKPTS = 6                              # The number of checkpoints in the checkpoints directory
NUM_MCTS_SIMS = 100                         # This should match the number of MCTS simulations used during training
BOARD_SIZE = 5                              # This should match the board size used during training
SCORE_TARGET = 20                           # This should match the score target used during training
N_GAMES = 30                                # The number of games to play for each evaluation
CPUS = 4.0                                  # Number of CPU cores to use<jupyter_output><empty_output><jupyter_text>Imports<jupyter_code>import pickle
import sys
sys.path.append('..')

import numpy as np
import matplotlib.pyplot as plt

import Arena
from MCTS import MCTS
from utils import *

from blooms.BloomsGame import BloomsGame
from blooms.BloomsPlayers import RandomPlayer, GreedyPlayer
from blooms.pytorch.NNet import NNetWrapper as BloomsPyTorchNNet<jupyter_output><empty_output><jupyter_text>Evaluation Function<jupyter_code>def evaluate_agent(n_games, game, opponent, chkpt_dir, chkpt_filename):
    """Compare the performance of an agent against an Opponent.
    """
    model = BloomsPyTorchNNet(game)
    model.load_checkpoint(chkpt_dir, chkpt_filename)
    args = dotdict({'numMCTSSims': NUM_MCTS_SIMS, 'cpuct': CPUS})
    mcts = MCTS(game, model, args)
    agent = lambda x: np.argmax(mcts.getActionProb(x, temp=0))

    arena = Arena.Arena(agent, opponent, game)
    wins, losses, draws = arena.playGames(n_games, verbose=False)
    
    return wins, losses, draws<jupyter_output><empty_output><jupyter_text>Evaluate Against a Random Agent<jupyter_code>results_vs_random = []
for i, chkpt in enumerate(range(1, NUM_CHKPTS + 1)):
    n_games = N_GAMES
    game = BloomsGame(size=BOARD_SIZE, score_target=SCORE_TARGET)

    random_agent = RandomPlayer(game).play

    wins, losses, draws = evaluate_agent(n_games, game, random_agent, CHKPT_DIR, f'checkpoint_{chkpt}.pth.tar')
    win_percentage = wins / n_games

    results_vs_random.append(win_percentage)
    
pickle.dump(results_vs_random, open(CHKPT_DIR + 'chkpt_evals_vs_random_agent.pkl', 'wb'))
fig, ax = plt.subplots(1)

results_vs_random = pickle.load(open(CHKPT_DIR + 'chkpt_evals_vs_random_agent.pkl', 'rb'))
checkpoints = range(1, NUM_CHKPTS + 1)
plt.plot(checkpoints, results_vs_random)

plt.xlabel('Iteration')
plt.ylabel('Win Percentage')
plt.ylim(0, 1)
plt.title('Progressive Performance Against a Random Agent')
plt.show()<jupyter_output><empty_output><jupyter_text>Evaluate Against a Greedy Agent<jupyter_code>results_vs_greedy = []
for i, chkpt in enumerate(range(1, NUM_CHKPTS + 1)):
    n_games = N_GAMES
    game = BloomsGame(size=BOARD_SIZE, score_target=SCORE_TARGET)

    greedy_agent = GreedyPlayer(game).play

    wins, losses, draws = evaluate_agent(n_games, game, greedy_agent, CHKPT_DIR, f'checkpoint_{chkpt}.pth.tar')
    win_percentage = wins / n_games

    results_vs_greedy.append(win_percentage)
    
pickle.dump(results_vs_greedy, open(CHKPT_DIR + 'chkpt_evals_vs_greedy_agent.pkl', 'wb'))
fig, ax = plt.subplots(1)

results_vs_greedy = pickle.load(open(CHKPT_DIR + 'chkpt_evals_vs_greedy_agent.pkl', 'rb'))
checkpoints = range(1, NUM_CHKPTS + 1)
plt.plot(checkpoints, results_vs_greedy)

plt.xlabel('Iteration')
plt.ylabel('Win Percentage')
plt.ylim(0, 1)
plt.title('Progressive Performance Against a Greedy Agent')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09069767441860466, 'fraction_numerical': 0.007493540051679587, 'mean_word_length': 4.4675141242937855, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '284518', 'license': 'MIT', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1307, 'n_words': 314, 'path': 'notebooks/test_agent.ipynb', 'repo_name': 'adamtupper/alphablooms', 'size': '3870'}