Пример #1
0
 def __init__(self, role, name, timeout, ip_address, minus_inf, plus_inf, max_depth):
     TablutPlayer.__init__(self, role, name, timeout, ip_address)
     AlphaBeta.__init__(self, minus_inf, plus_inf, max_depth)
     file_path = 'zobrist_table.pkl'
     if os.path.exists(file_path):
         self.zobrist_table = cPickle.load(open(file_path, "rb"))
     else:
         self.zobrist_table_init()
Пример #2
0
 def __init__(self, iid: str, board: Board, ai: str, thinking_time: int):
     self.iid = iid
     self.board = board
     self.alphaBeta = AlphaBeta()
     self.mcts = MonteCarloTreeSearch()
     self.player_names = ["HUMAN", str(ai)]
     self.set_ai(ai)
     self.thinking_time = thinking_time
     self.max_depth = 30
     self.last_move = []
     self.last_move_score = []
Пример #3
0
    def choose_move(self, board):
        if len(self.boardList) == 0:
            self.boardList.append(board)

        match = self.match_state(board)
        if match is None:
            one_tree = board.generate_game_tree('red', 0)
            alg = AlphaBeta(one_tree)
            match = alg.alpha_beta_search(alg.root)
        board.move_piece(match.frm.x, match.frm.y, match.to.x, match.to.y)
        self.boardList.append(board)
        self.moveList.append(match)
        return match
Пример #4
0
    def makeMove(self, state):

        # moves = state.getActualPossMoves()
        # move = moves[int(random.random() * len(moves))]

        move = AlphaBeta(state, self.depth, self.heuristic)

        return state.applyMoveChain(move)
Пример #5
0
    def move(self, state, timeLimit):
        print("{0}'s turn.  {0} is {1}".format(self.name, self.color))

        if self.difficulty == 6:
            m = AlphaBeta(state)
            start = time.clock()
            best_move, value, depth = m.bestMove(30, state, self.color,
                                                 timeLimit)
            print("Alpha: ", value)
            print("Elapsed:", time.clock() - start)
            print("Depth Reached:", depth)
            return best_move, depth

        elif self.difficulty == 7:
            m = Greedy(state)
            time.sleep(randrange(8, 17, 1) / 10.0)
            best_move = m.best_move(state, self.color)
            print("guess greedy worked")
            return best_move, 1

        else:
            m = Minimax(state)
            start = time.clock()
            best_move, value, depth = m.bestMove(30, state, self.color,
                                                 timeLimit)
            print("Alpha: ", value)
            print("Elapsed:", time.clock() - start)
            print("Depth Reached:", depth)
            return best_move, depth
Пример #6
0
            winner = board.other_player
        else:
            winner = 0

        for state in visits:
            plays[state] += 1
            if winner == state[0]:
                wins[state] += 1


if __name__ == '__main__':
    from board import Board

    board_string = """
        2
        32 00 31 42
        11 42 32 00
        31 11 42 32
        41 31 00 41
    """
    board = Board.from_string(board_string)
    board.print()

    mcts = MonteCarloTreeSearch()
    mcts.get_best_move(board, 1, show_perft=True)

    from alphabeta import AlphaBeta

    alpha = AlphaBeta()
    alpha.get_best_move(board, thinking_time=30, show_perft=True)
Пример #7
0
def main():
    # print "Keren MinimaxPlayer vs AlphaBeta"
    # b = board("./game_boards/Keren.txt");
    # p1 = AlphaBeta(1);
    # p2 = MinimaxPlayer(2);
    # s = Simulation(b);

    # s.run(p1,p2, False);
    # print "Player 1 expanded Nodes: ", p1.Nodes;
    # print "Player 1 Moves: ", p1.moves;
    # print "Player 1 avg node per move:", p1.Nodes/p1.moves
    # print "Player 1 avg time:",float(p1.timerun/p1.moves)

    # print "Player 2 Expanded Nodes: ", p2.Nodes;
    # print "Player 2 Moves: ", p2.moves;
    # print "Player 2 avg node per move:", p2.Nodes/p2.moves
    # print "Player 2 avg time:",float(p2.timerun/p2.moves)
    # print;

    # print "Narvik AlphaBeta vs Minimax"
    # b = board("./game_boards/Narvik.txt");
    # p1 = AlphaBeta(1);
    # p2 = MinimaxPlayer(2);
    # s = Simulation(b);
    # s.run(p1,p2, False);
    # print "Player 1 expanded Nodes: ", p1.Nodes;
    # print "Player 1 Moves: ", p1.moves;
    # print "Player 1 avg node per move:", p1.Nodes/p1.moves

    # print "Player 2 Expanded Nodes: ", p2.Nodes;
    # print "Player 2 Moves: ", p2.moves;
    # print "Player 2 avg node per move:", p2.Nodes/p2.moves

    # print;

    # print "Sevastopol AlphaBeta vs Minimax"
    # b = board("./game_boards/Sevastopol.txt");
    # p1 = AlphaBeta(1);
    # p2 = MinimaxPlayer(2);
    # s = Simulation(b);
    # s.run(p1,p2, False);
    # print "Player 1 expanded Nodes: ", p1.Nodes;
    # print "Player 1 Moves: ", p1.moves;
    # print "Player 1 avg node per move:", p1.Nodes/p1.moves

    # print "Player 2 Expanded Nodes: ", p2.Nodes;
    # print "Player 2 Moves: ", p2.moves;
    # print "Player 2 avg node per move:", p2.Nodes/p2.moves

    # print;

    # print "Smolensk AlphaBeta vs Minimax"
    # b = board("./game_boards/Smolensk.txt");
    # p1 = AlphaBeta(1);
    # p2 = MinimaxPlayer(2);
    # s = Simulation(b);
    # s.run(p1,p2, False);

    # print "Player 1 expanded Nodes: ", p1.Nodes;
    # print "Player 1 Moves: ", p1.moves;
    # print "Player 1 avg node per move:", p1.Nodes/p1.moves

    # print "Player 2 Expanded Nodes: ", p2.Nodes;
    # print "Player 2 Moves: ", p2.moves;
    # print "Player 2 avg node per move:", p2.Nodes/p2.moves

    # print;

    print "Westerplatte AlphaBeta vs Minimax"
    b = board("./game_boards/Westerplatte.txt")
    p1 = AlphaBeta(1)
    p2 = MinimaxPlayer(2)
    s = Simulation(b)
    s.run(p1, p2, False)
Пример #8
0
from qlearning import QLearning
import time

game = TicTacToe()
user = int(input("Player1(X) or Player2(O):"))
ai = 2 if user == 1 else 1
ai_algorithm = input("""Choose your opponent:
                     1. MiniMax Algorithm
                     2. MiniMax with Alpha-Beta Pruning
                     3. Q-Learning Agent
                     """)

if ai_algorithm == "1":
    agent = MiniMax(player=ai)
elif ai_algorithm == "2":
    agent = AlphaBeta(player=ai)
elif ai_algorithm == "3":
    agent = QLearning(player=ai)
    agent.epsilon = 0
    agent.load_q_table()

while True:
    game.render()
    print("-----------------------------------------------------------------")
    if game.turn == user:
        action = int(input("Action (0-8):"))
        done = game.step(action)
        if done:
            game.render()
            break
    elif game.turn == ai:
Пример #9
0
# -*- coding: utf-8 -*-
"""
@author: turksoyomer
"""

from tictactoe import TicTacToe
from qlearning import QLearning
from alphabeta import AlphaBeta

game = TicTacToe()
agent1 = QLearning(player=1)
agent1.epsilon = 0
agent1.load_q_table()
agent2 = AlphaBeta(player=2)

score_board = {"Agent1": 0, "AlphaBeta": 0, "Tie": 0}

for e in range(100):
    game.reset()
    while True:
        if game.turn == 1:
            action = agent1.action(game.state)
            done = game.step(action)
            if done:
                break
        elif game.turn == 2:
            action = agent2.action(game.state)
            done = game.step(action)
            if done:
                break
    if game.winner == "X":
Пример #10
0
class Game:
    ALPHA_BETA = 'alpha'
    MCTS = 'mcts'

    COLORS = ['black', 'green', 'red']

    def __init__(self, iid: str, board: Board, ai: str, thinking_time: int):
        self.iid = iid
        self.board = board
        self.alphaBeta = AlphaBeta()
        self.mcts = MonteCarloTreeSearch()
        self.player_names = ["HUMAN", str(ai)]
        self.set_ai(ai)
        self.thinking_time = thinking_time
        self.max_depth = 30
        self.last_move = []
        self.last_move_score = []

    def set_ai(self, ai):
        self.ai = ai
        if ai == Game.ALPHA_BETA:
            self.player_names[-1] = str(self.alphaBeta)
        else:
            self.player_names[-1] = str(self.mcts)

    def is_mcts(self):
        return 'selected' if not self.is_alpha() else ''

    def is_alpha(self):
        return 'selected' if self.ai == Game.ALPHA_BETA else ''

    def get_best_move(self):
        if self.ai == Game.ALPHA_BETA:
            move, score = self.alphaBeta.get_best_move(
                self.board,
                thinking_time=self.thinking_time,
                max_depth=self.max_depth)
        else:
            move, score = self.mcts.get_best_move(
                self.board,
                thinking_time=self.thinking_time,
                max_depth=self.max_depth)

        return move, score

    def current_player(self):
        return self.player_names[self.board.current_player - 1]

    def current_player_color(self):
        return Game.COLORS[self.board.current_player]

    def board_to_template(self):
        display = []
        for y, row in enumerate(self.board.board):
            display_row = []  #f"{y}-x", Game.COLORS[0], 'left', y]
            for x, field in enumerate(row):
                display_row.append(
                    (f"{x}-{y}", Game.COLORS[self.board.player[y][x]],
                     'left' if x == 0 else '', field))
            display.append(display_row)
        return display

    def move(self, x, y, score=0):
        self.last_move.append((x, y))
        self.last_move_score.append(score)
        return self.board.move(x, y)

    def undo(self):
        self.board.undo()
        self.board.undo()

        self.last_move_score.pop()
        self.last_move_score.pop()

        self.last_move.pop()
        self.last_move.pop()

    def get_last_move(self):
        return self.last_move[-1] if len(self.last_move) > 0 else ''

    def get_last_move_score(self):
        return self.last_move_score[-1] if len(
            self.last_move_score) > 0 else ''

    def perft(self):
        if self.ai == Game.ALPHA_BETA:
            return self.alphaBeta.perft
        else:
            return self.mcts.perft
Пример #11
0
def run_game():
    THINKING_TIME = 35
    HUMAN = 'human'

    PLAYERS = {1: HUMAN, 2: AlphaBeta()}

    board_string = """
            1
            32 00 31 42
            11 42 32 00
            31 11 42 32
            41 31 00 41
        """
    board = Board.from_string(board_string)

    command = ''
    while True:
        for p, t in PLAYERS.items():
            print(f"Player {p}: {t}")

        board.print()

        if PLAYERS[board.current_player] == HUMAN:
            command = input('Please input a move! e.g.: 0,0\n>>').strip()

        if command == 'exit':
            break
        elif command.startswith('new'):
            if command == 'new':
                board = Board()
            else:
                cmd, size_x, size_y = command.split(' ')
                board = Board(int(size_x), int(size_y))
        elif command == 'search':
            move, score = AlphaBeta().get_best_move(
                board, thinking_time=THINKING_TIME, show_perft=True)
        elif command.startswith('set'):
            if command == 'set':
                print("Usage: set <player_number> <player_type>")
                print(f"player_type: {HUMAN}, alpha, mcts")
            else:
                cmd, player, player_type = command.split(' ')
                if player_type == HUMAN:
                    PLAYERS[int(player)] = HUMAN
                elif player_type == 'alpha':
                    PLAYERS[int(player)] = AlphaBeta()
                elif player_type == 'mcts':
                    PLAYERS[int(player)] = MonteCarloTreeSearch()
        else:
            if PLAYERS[board.current_player] == HUMAN:
                split_move = (int(x) for x in command.split(' '))
                board.move(*split_move, display=True)
            else:
                move, score = PLAYERS[board.current_player].get_best_move(
                    board,
                    thinking_time=THINKING_TIME,
                    max_depth=20,
                    show_perft=True)
                print(f"Player {board.current_player}, move: {move}")
                board.move(*move, display=True)

        command = ''
Пример #12
0
 def pickMove(self):
     alphabeta = AlphaBeta(self.board)
     if self.me == "x":
         return alphabeta.playAlphabeta(self.board, 3, -100000, 100000, True)
     elif self.me == "o":
         return alphabeta.playAlphabeta(self.board, 3, -100000, 100000, False)