<commit_before>from __future__ import division

from random import random, choice
from math import log, sqrt
import datetime


class BasePlayer(object):
    def __init__(self, board, *args, **kwargs):
        self.board = board
        self.player = None
        self.states = []

    def update(self, state):
        self.states.append(state)

    def display(self, state, play):
        return self.board.display(state, play)

    def winner_message(self, msg):
        return self.board.winner_message(msg)


class MonteCarlo(BasePlayer):
    def __init__(self, board, **kwargs):
        super(MonteCarlo, self).__init__(board, **kwargs)
        self.max_moves = 9 * 9
        self.max_time = datetime.timedelta(seconds=30)
        self.C = kwargs.get('C', 1.4)
        self.wins = {1: {}, 2: {}}
        self.plays = {1: {}, 2: {}}

    def get_play(self):
        state = self.states[-1]
        player = state[-1]
        legal = self.board.legal_plays(state)

        if not legal:
            return
        if len(legal) == 1:
            return legal[0]

        states = [(p, self.board.play(state, p)) for p in legal]

        begin, games = datetime.datetime.utcnow(), 0
        while datetime.datetime.utcnow() - begin < self.max_time:
            self.random_game()
            games += 1

        print games, datetime.datetime.utcnow() - begin
        move = max(
            (self.wins[player].get(S,0) / self.plays[player].get(S,1), p)
            for p, S in states
        )[1]<commit_msg>Dropped the thinking time for the AI down to 5 seconds.<commit_after>from __future__ import division

from random import random, choice
from math import log, sqrt
import datetime


class BasePlayer(object):
    def __init__(self, board, *args, **kwargs):
        self.board = board
        self.player = None
        self.states = []

    def update(self, state):
        self.states.append(state)

    def display(self, state, play):
        return self.board.display(state, play)

    def winner_message(self, msg):
        return self.board.winner_message(msg)


class MonteCarlo(BasePlayer):
    def __init__(self, board, **kwargs):
        super(MonteCarlo, self).__init__(board, **kwargs)
        self.max_moves = 9 * 9
        self.max_time = datetime.timedelta(seconds=5)
        self.C = kwargs.get('C', 1.4)
        self.wins = {1: {}, 2: {}}
        self.plays = {1: {}, 2: {}}

    def get_play(self):
        state = self.states[-1]
        player = state[-1]
        legal = self.board.legal_plays(state)

        if not legal:
            return
        if len(legal) == 1:
            return legal[0]

        states = [(p, self.board.play(state, p)) for p in legal]

        begin, games = datetime.datetime.utcnow(), 0
        while datetime.datetime.utcnow() - begin < self.max_time:
            self.random_game()
            games += 1

        print games, datetime.datetime.utcnow() - begin
        move = max(
            (self.wins[player].get(S,0) / self.plays[player].get(S,1), p)
            for p, S in states
        )[1]
{'fraction_non_alphanumeric': 0.12838941522378308, 'fraction_numerical': 0.012414243711205489, 'mean_word_length': 2.176348547717842, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4753842', 'n_tokens_mistral': 990, 'n_tokens_neox': 941, 'n_words': 245}