[Python] How to implement a Monte Carlo Tree Search?

Discussão em 'Python' iniciado por Stack, Novembro 1, 2024.

  1. Stack

    Stack Membro Participativo

    I'm working on a project that involves making a Monte Carlo Tree Search and I'm trying to implement it for Connect 4 before trying to apply it to a more complicated problem.

    Here is my code for the game:

    class board:

    def __init__(self):
    self.board = np.zeros((6,7))
    self.ROW_COUNT = 6
    self.COLUMN_COUNT = 7
    self.game_over = False
    self.turn = 0
    self.value = 0

    def isGameOver(self):
    return self.game_over

    def whatTurn(self):
    return self.turn

    def changeTurn(self):
    self.turn += 1
    self.turn = self.turn % 2

    def __hash__(self):
    return hash(self.value)

    def __eq__(self, other):
    if isinstance(other, board):
    return self.value == other.value
    else:
    return False

    def drop_piece(self, row, col, piece):
    self.board[row][col]= piece

    def is_valid_location(self ,col):
    #if this condition is true we will let the use drop piece here.
    #if not true that means the col is not vacant
    return self.board[5][col]==0

    def get_next_open_row(self, col):
    for r in range(self.ROW_COUNT):
    if self.board[r][col]==0:
    return r

    def print_board(self):
    print(np.flip(self.board,0))

    def winning_move(self, piece):
    # Check horizontal locations for win
    for c in range(self.COLUMN_COUNT-3):
    for r in range(self.ROW_COUNT):
    if self.board[r][c] == piece and self.board[r][c+1] == piece and self.board[r][c+2] == piece and self.board[r][c+3] == piece:
    self.game_over = True

    # Check vertical locations for win
    for c in range(self.COLUMN_COUNT):
    for r in range(self.ROW_COUNT-3):
    if self.board[r][c] == piece and self.board[r+1][c] == piece and self.board[r+2][c] == piece and self.board[r+3][c] == piece:
    self.game_over = True

    # Check positively sloped diaganols
    for c in range(self.COLUMN_COUNT-3):
    for r in range(self.ROW_COUNT-3):
    if self.board[r][c] == piece and self.board[r+1][c+1] == piece and self.board[r+2][c+2] == piece and self.board[r+3][c+3] == piece:
    self.game_over = True

    # Check negatively sloped diaganols
    for c in range(self.COLUMN_COUNT-3):
    for r in range(3, self.ROW_COUNT):
    if self.board[r][c] == piece and self.board[r-1][c+1] == piece and self.board[r-2][c+2] == piece and self.board[r-3][c+3] == piece:
    self.game_over = True


    Now it is my understanding that when I make an instance of a game board and save it to a node in my Monte Carlo Tree, once I have the board drop more pieces for later nodes, the initial node will change too since classes are mutable and the name of the "board" is just referring to the object. Is this the case? If so, any ideas on how I can use this class to make a search tree?

    Continue reading...

Compartilhe esta Página