コード例 #1
0
def start():
    config = OptimizerConfig()
    tf_util.update_memory(config.gpu_mem_fraction)
    util.set_high_process_priority()
    AIPlayer.create_if_nonexistant(config)
    models = sorted(glob.glob(config.data.model_location+"*.h5"))
    ai = AIPlayer(config.buffer_size, 1, model=models[-1], compile=True)
    train(ai, config)
コード例 #2
0
def start():
    config = OptimizerConfig()
    tf_util.update_memory(config.gpu_mem_fraction)
    util.set_high_process_priority()
    AIPlayer.create_if_nonexistant(config)
    models = sorted(glob.glob(config.data.model_location + "*.h5"))
    ai = AIPlayer(config.buffer_size, 1, model=models[-1], compile=True)
    train(ai, config)
コード例 #3
0
def run_games(config):
    game = Othello()
    model = ""
    x = config.iterations
    while(x != 0):
        x -= 1
        models = sorted(glob.glob(config.data.model_location+"*.h5"))
        if model == "":
            model = models[-1]
            print("Loading new model: %s" % util.getPlayerName(model))
            ai = AIPlayer(config.buffer_size, config.game.simulation_num_per_move, model=model)
        elif models[-1] != model:
            model = models[-1]
            print("Loading new model: %s" % util.getPlayerName(model))
            ai.load(model)
		
        start=time()
        for j in range(config.nb_game_in_file):
            util.print_progress_bar(j, config.nb_game_in_file, start=start)
            side = -1
            turn = 1
            while not game.game_over():
                ai.tau = config.game.tau_1
                if config.game.tau_swap < turn:
                    ai.tau = config.game.tau_2
                t = ai.pick_move(game, side)
                game.play_move(t[0], t[1], side)
                side *= -1
                turn += 1
            ai.update_buffer(game.get_winner())
            game.reset_board()
        #print("Average Game Time: ", (time()-start)/(config.nb_game_in_file))
        util.print_progress_bar(config.nb_game_in_file, config.nb_game_in_file, start=start)
        save_games(config, ai.buffer)
    t.join()
コード例 #4
0
def create_player(player_name, config):
    if player_name == "random":
        model = "random"
        player = RandomPlayer()
    elif player_name == "newest":
        model = sorted(glob.glob(config.data.model_location+"*.h5"))[-1]
        #print("Loading model: %s" % model)
        player = AIPlayer(0, config.game.simulation_num_per_move, train=False, model=model, tau=config.game.tau_1)
    else:
        model = config.data.model_location+player_name
        player = AIPlayer(0, config.game.simulation_num_per_move, train=False, model=model, tau=config.game.tau_1)
    return player
コード例 #5
0
 def run(self):
     self.game_gui = Canvas(self.root, width=600, height=600, background='green')
     self.game_gui.bind("<Button-1>", self.click)
     self.game_gui.focus_set()
     self.game_gui.bind("<Key>", self.key)
     self.game_gui.pack()
     for i in range(1, 8):
         self.game_gui.create_line(0, i*75, 600, i*75)
         self.game_gui.create_line(i*75, 0, i*75, 600)
     
     self.pieces = []
     for i in range(8):
         self.pieces.append([])
         for j in range(8):
             self.pieces[i].append(self.game_gui.create_oval(i*75+5, j*75+5, (i+1)*75-5, (j+1)*75-5, fill="green", outline="green"))
     
     self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
     self.root.resizable(0,0)
     self.running = True
     config = EvaluateConfig()
     tf_util.update_memory(config.gpu_mem_fraction)
     AIPlayer.create_if_nonexistant(config)
     self.game = Othello()
     if(random() > 0.5):
         self.human = 1
     else:
         self.human = -1
     
     ai = create_player(config.model_1, config)
     #print("You are playing against", config.model_1)
     #print("Playing games with %d simulations per move" % config.game.simulation_num_per_move)
     self.side = -1
     self.draw_board()
     self.value = ai.evaluate(self.game, self.side)
     while self.running and not self.game.game_over():
         #play move
         if self.side != self.human:
             self.value = ai.evaluate(self.game, self.side)
             self.root.title("Othello (Thinking of Move) Current Value: %0.2f (1 white wins, -1 black wins)" % self.value)
             self.root.config(cursor="wait")
             t = ai.pick_move(self.game, self.side)
             self.game.play_move(t[0], t[1], self.side)
             self.draw_board()
             self.side *= -1
             self.value = ai.evaluate(self.game, self.side)
         else:
             if len(self.game.possible_moves(self.side)) == 0:
                 self.side *= -1
                 continue
             if self.side == -1:
                 color = "black"
             else:
                 color = "white"
             self.root.title("Othello (Play as %s) Current Value: %0.2f (1 white wins, -1 black wins)" % (color, self.value))
             self.root.config(cursor="")
             if self.update:
                 self.update = False
                 if (self.x, self.y) in self.game.possible_moves(self.side):
                     self.game.play_move(self.x, self.y, self.side)
                     self.draw_board()
                     self.side *= -1
         time.sleep(0.01)
     if self.human == self.game.get_winner():
         self.root.title("Othello (You Win!)")
     elif self.game.get_winner() == 0:
         self.root.title("Othello (Its a draw!)")
     else:
         self.root.title("Othello (You Lose!)")
コード例 #6
0
def start():
    config = EvaluateConfig()
    tf_util.update_memory(config.gpu_mem_fraction)
    AIPlayer.create_if_nonexistant(config)
    run_games(config)
コード例 #7
0
def start():
    config = EvaluateConfig()
    tf_util.update_memory(config.gpu_mem_fraction)
    AIPlayer.create_if_nonexistant(config)
    run_games(config)
コード例 #8
0
def calc_ranking(config):
    models = sorted(glob.glob(config.data.model_location + "*.h5"))
    players = []
    for i, model in enumerate(models):
        if i % config.model_skip == 0 or i == len(models):
            players.append(model)

    wtl = np.zeros((len(players), 3))
    win_matrix = np.zeros((len(players), len(players)))
    game = Othello()

    king_index = len(players) - 1
    king = AIPlayer(0,
                    config.game.simulation_num_per_move,
                    train=False,
                    model=players[king_index],
                    tau=config.game.tau_1)
    challenger = AIPlayer(0,
                          config.game.simulation_num_per_move,
                          train=False,
                          model=players[0],
                          tau=config.game.tau_1)
    total_games = (config.game_num_per_model * (len(players))) // 2
    played_games = 0
    start = time()
    print("Playing king of the hill with %d players and %d games per player" %
          (len(players), config.game_num_per_model))
    if config.game_num_per_model < len(players):
        print(
            "We suggest that you increase games per player to be greater than players"
        )
    for i in range(math.ceil(total_games / (len(players) - 1))):
        AIPlayer.clear()
        king_index = getKingIndex(win_matrix)
        if king_index == -1:
            king_index = (len(players) - 1) - i % len(players)
            msg = "No King Yet"
        else:
            msg = "King is " + os.path.basename(
                players[king_index]).split(".")[0]
        king.load(players[king_index])
        if config.print_king:
            print(msg.ljust(90))
        for j in range(len(players)):
            util.print_progress_bar(played_games, total_games, start=start)

            if j == king_index:
                continue

            challenger.load(players[j])

            if random.random() < 0.5:
                king_side = -1
                p1 = king
                p2 = challenger
            else:
                king_side = 1
                p1 = challenger
                p2 = king
            side = -1
            turn = 1
            while not game.game_over():
                tau = config.game.tau_1
                if config.game.tau_swap < turn:
                    tau = config.game.tau_2
                p1.tau = tau
                p2.tau = tau
                if side == -1:
                    t = p1.pick_move(game, side)
                else:
                    t = p2.pick_move(game, side)
                game.play_move(t[0], t[1], side)
                side *= -1
                turn += 1
            if game.get_winner() == king_side:
                win_matrix[king_index, j] += 1
                wtl[king_index, 0] += 1
                wtl[j, 2] += 1
            elif game.get_winner() == -1 * king_side:
                win_matrix[j, king_index] += 1
                wtl[king_index, 2] += 1
                wtl[j, 0] += 1
            else:
                win_matrix[king_index, j] += 0.5
                win_matrix[j, king_index] += 0.5
                wtl[king_index, 1] += 1
                wtl[j, 1] += 1
            game.reset_board()
            played_games += 1
            if played_games == total_games:
                break
    util.print_progress_bar(total_games, total_games, start=start)
    try:
        params = choix.ilsr_pairwise_dense(win_matrix)
        print("\nRankings:")
        for i, player in enumerate(np.argsort(params)[::-1]):
            print(
                "%d. %s (expected %d) with %0.2f rating and results of %d-%d-%d"
                % (i + 1, os.path.basename(players[player]).split(".")[0],
                   len(players) - player, params[player], wtl[player, 0],
                   wtl[player, 1], wtl[player, 2]))
        print(
            "\n(Rating Diff, Winrate) -> (0.5, 62%), (1, 73%), (2, 88%), (3, 95%), (5, 99%)"
        )
    except Exception:
        print("\n Not Enough data to calculate rankings")
        print("\nWin Matrix:")
        print(win_matrix)
        print("\nResults:")
        for player in range(win_matrix.shape[0]):
            print("%s results of %d-%d-%d" %
                  (os.path.basename(players[player]).split(".")[0],
                   wtl[player, 0], wtl[player, 1], wtl[player, 2]))
コード例 #9
0
def start():
    config = RankingConfig()
    tf_util.update_memory(config.gpu_mem_fraction)
    AIPlayer.create_if_nonexistant(config)
    calc_ranking(config)
コード例 #10
0
 def run(self):
     self.game_gui = Canvas(self.root, width=600, height=600, background='green')
     self.game_gui.bind("<Button-1>", self.click)
     self.game_gui.focus_set()
     self.game_gui.bind("<Key>", self.key)
     self.game_gui.pack()
     for i in range(1, 8):
         self.game_gui.create_line(0, i*75, 600, i*75)
         self.game_gui.create_line(i*75, 0, i*75, 600)
     
     self.pieces = []
     for i in range(8):
         self.pieces.append([])
         for j in range(8):
             self.pieces[i].append(self.game_gui.create_oval(i*75+5, j*75+5, (i+1)*75-5, (j+1)*75-5, fill="green", outline="green"))
     
     self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
     self.root.resizable(0,0)
     self.running = True
     config = EvaluateConfig()
     tf_util.update_memory(config.gpu_mem_fraction)
     AIPlayer.create_if_nonexistant(config)
     self.game = Othello()
     if(random() > 0.5):
         self.human = 1
     else:
         self.human = -1
     
     ai = create_player(config.model_1, config)
     #print("You are playing against", config.model_1)
     #print("Playing games with %d simulations per move" % config.game.simulation_num_per_move)
     self.side = -1
     self.draw_board()
     self.value = ai.evaluate(self.game, self.side)
     while self.running and not self.game.game_over():
         #play move
         if self.side != self.human:
             self.value = ai.evaluate(self.game, self.side)
             self.root.title("Othello (Thinking of Move) Current Value: %0.2f (1 white wins, -1 black wins)" % self.value)
             self.root.config(cursor="wait")
             t = ai.pick_move(self.game, self.side)
             self.game.play_move(t[0], t[1], self.side)
             self.draw_board()
             self.side *= -1
             self.value = ai.evaluate(self.game, self.side)
         else:
             if len(self.game.possible_moves(self.side)) == 0:
                 self.side *= -1
                 continue
             if self.side == -1:
                 color = "black"
             else:
                 color = "white"
             self.root.title("Othello (Play as %s) Current Value: %0.2f (1 white wins, -1 black wins)" % (color, self.value))
             self.root.config(cursor="")
             if self.update:
                 self.update = False
                 if (self.x, self.y) in self.game.possible_moves(self.side):
                     self.game.play_move(self.x, self.y, self.side)
                     self.draw_board()
                     self.side *= -1
         time.sleep(0.01)
     self.root.config(cursor="")
     if self.human == self.game.get_winner():
         self.root.title("Othello (You Win!)")
     elif self.game.get_winner() == 0:
         self.root.title("Othello (Its a draw!)")
     else:
         self.root.title("Othello (You Lose!)")
コード例 #11
0
def calc_ranking(config):
    models = sorted(glob.glob(config.data.model_location + "*.h5"))
    players = []
    for i, model in enumerate(models):
        if i % config.model_skip == 0 or i == len(models):
            players.append(model)

    wtl = np.zeros((len(players), 3))
    win_matrix = np.zeros((len(players), len(players)))
    game = Othello()

    ##give every player a random order to play games against opponents
    order = []
    for i in range(len(players)):
        nums = [x for x in range(len(players))]
        nums.remove(i)
        random.shuffle(nums)
        order.append(nums)

    p1 = AIPlayer(1, config.game.simulation_num_per_move, model=players[0])
    p2 = AIPlayer(1,
                  config.game.simulation_num_per_move,
                  model=players[order[0][0]])

    start = time()
    print(
        "Playing random round robin with %d players and %d games per player" %
        (len(players), config.game_num_per_model))
    for i in range(config.game_num_per_model // 2):
        util.print_progress_bar(i, config.game_num_per_model // 2, start=start)
        ordering = [x for x in range(len(players))]
        random.shuffle(ordering)
        for j in ordering:
            AIPlayer.clear()
            x = i
            if x >= len(order[j]):
                x %= len(order[j])
                if x == 0:
                    random.shuffle(order[j])

            p1.load(players[j])
            p2.load(players[order[j][x]])

            side = -1
            turn = 1
            while not game.game_over():
                tau = config.game.tau_1
                if config.game.tau_swap < turn:
                    tau = config.game.tau_2
                p1.tau = tau
                p2.tau = tau
                if side == -1:
                    t = p1.pick_move(game, side)
                else:
                    t = p2.pick_move(game, side)
                game.play_move(t[0], t[1], side)
                side *= -1
                turn += 1
            if game.get_winner() == -1:
                win_matrix[j, order[j][x]] += 1
                wtl[j, 0] += 1
                wtl[order[j][x], 2] += 1
            elif game.get_winner() == 1:
                win_matrix[order[j][x], j] += 1
                wtl[j, 2] += 1
                wtl[order[j][x], 0] += 1
            else:
                win_matrix[j, order[j][x]] += 0.5
                win_matrix[order[j][x], j] += 0.5
                wtl[j, 1] += 1
                wtl[order[j][x], 1] += 1
            game.reset_board()
    util.print_progress_bar(config.game_num_per_model // 2,
                            config.game_num_per_model // 2,
                            start=start)
    params = choix.ilsr_pairwise_dense(win_matrix)
    print("\nRankings:")
    for i, player in enumerate(np.argsort(params)[::-1]):
        print(
            "%d. %s (expected %d) with %0.2f rating and results of %d-%d-%d" %
            (i + 1, os.path.basename(players[player]), len(players) - player,
             params[player], wtl[player, 0], wtl[player, 1], wtl[player, 2]))
    print(
        "\n(Rating Diff, Winrate) -> (0.5, 62%), (1, 73%), (2, 88%), (3, 95%), (5, 99%)"
    )
コード例 #12
0
def calc_ranking(config):
    models = sorted(glob.glob(config.data.model_location+"*.h5"))
    players = []
    for i, model in enumerate(models):
        if i % config.model_skip == 0 or i == len(models):
            players.append(model)
    
    wtl = np.zeros((len(players), len(players), 3))
    win_matrix = np.zeros((len(players),len(players)))
    game = Othello()
    
    challenger1 = AIPlayer(0, config.game.simulation_num_per_move, train=False, model=players[-1], tau=config.game.tau_1)
    challenger2 = AIPlayer(0, config.game.simulation_num_per_move, train=False, model=players[0], tau=config.game.tau_1)
    total_games = (config.game_num_per_model * (len(players)))//2
    played_games = 0
    finished = False
    start = time()
    print("Ranking with %d players and %d games per player" % (len(players), config.game_num_per_model))
    if config.game_num_per_model < len(players):
        print("We suggest that you increase games per player to be greater than players")
        
    for i in itertools.count():
        ranks = getRankings(win_matrix)

        if len(ranks) == 0:
            msg = "No Clear Best Yet"
        else:
            msg = "Current Best is "+util.getPlayerName(players[ranks[-1]])   
        if config.print_best:
            print(msg.ljust(90))
        for j in range(len(players)):
            util.print_progress_bar(played_games, total_games, start=start)
            
            challenger1_index = getLeastPlayed(win_matrix, j)
            
            AIPlayer.clear()
            challenger1.load(players[challenger1_index])
            challenger2.load(players[j])
            
            if random.random() < 0.5:
                challenger1_side = -1
                p1 = challenger1
                p2 = challenger2
            else:
                challenger1_side = 1
                p1 = challenger2
                p2 = challenger1
            side = -1
            turn = 1
            while not game.game_over():
                tau = config.game.tau_1
                if config.game.tau_swap < turn:
                    tau = config.game.tau_2
                p1.tau = tau
                p2.tau = tau
                if side == -1:
                    t = p1.pick_move(game, side)
                else:
                    t = p2.pick_move(game, side)
                game.play_move(t[0], t[1], side)
                side *= -1
                turn += 1
            if game.get_winner() == challenger1_side:
                win_matrix[challenger1_index,j] += 1
                wtl[challenger1_index, j,0] += 1
            elif game.get_winner() == -1*challenger1_side:
                win_matrix[j, challenger1_index] += 1
                wtl[challenger1_index, j,2] += 1
            else:
                win_matrix[challenger1_index,j] += 0.5
                win_matrix[j, challenger1_index] += 0.5
                wtl[challenger1_index, j, 1] += 1
            game.reset_board()
            played_games += 1
            if played_games >= total_games:
                finished = True
                break
        saveWTL(config, players, wtl)
        wtl = np.zeros((len(players), len(players), 3))
        if finished:
            break
    util.print_progress_bar(total_games, total_games, start=start) 
    
    print("\n",[util.getPlayerName(player) for player in players])
    print("\nWin Matrix(row beat column):")
    print(win_matrix)
    try:
        with np.errstate(divide='ignore', invalid='ignore'):
            params = choix.ilsr_pairwise_dense(win_matrix)
        print("\nRankings:")
        for i, player in enumerate(np.argsort(params)[::-1]):
            print("%d. %s (expected %d) with %0.2f rating"% 
                  (i+1, util.getPlayerName(players[player]), len(players)-player, params[player]))
        print("\n(Rating Diff, Winrate) -> (0.5, 62%), (1, 73%), (2, 88%), (3, 95%), (5, 99%)")
    except Exception:
        print("\nNot Enough data to calculate rankings")
コード例 #13
0
def start():
    config = RankingConfig()
    tf_util.update_memory(config.gpu_mem_fraction)
    AIPlayer.create_if_nonexistant(config)
    calc_ranking(config)
コード例 #14
0
def calc_ranking(config):
    models = sorted(glob.glob(config.data.model_location + "*.h5"))
    players = []
    for i, model in enumerate(models):
        if i % config.model_skip == 0 or i == len(models):
            players.append(model)

    wtl = np.zeros((len(players), len(players), 3))
    win_matrix = np.zeros((len(players), len(players)))
    game = Othello()

    challenger1 = AIPlayer(0,
                           config.game.simulation_num_per_move,
                           train=False,
                           model=players[-1],
                           tau=config.game.tau_1)
    challenger2 = AIPlayer(0,
                           config.game.simulation_num_per_move,
                           train=False,
                           model=players[0],
                           tau=config.game.tau_1)
    total_games = (config.game_num_per_model * (len(players))) // 2
    played_games = 0
    finished = False
    start = time()
    print("Ranking with %d players and %d games per player" %
          (len(players), config.game_num_per_model))
    if config.game_num_per_model < len(players):
        print(
            "We suggest that you increase games per player to be greater than players"
        )

    for i in itertools.count():
        ranks = getRankings(win_matrix)

        if len(ranks) == 0:
            msg = "No Clear Best Yet"
        else:
            msg = "Current Best is " + util.getPlayerName(players[ranks[-1]])
        if config.print_best:
            print(msg.ljust(90))
        for j in range(len(players)):
            util.print_progress_bar(played_games, total_games, start=start)

            challenger1_index = getLeastPlayed(win_matrix, j)

            AIPlayer.clear()
            challenger1.load(players[challenger1_index])
            challenger2.load(players[j])

            if random.random() < 0.5:
                challenger1_side = -1
                p1 = challenger1
                p2 = challenger2
            else:
                challenger1_side = 1
                p1 = challenger2
                p2 = challenger1
            side = -1
            turn = 1
            while not game.game_over():
                tau = config.game.tau_1
                if config.game.tau_swap < turn:
                    tau = config.game.tau_2
                p1.tau = tau
                p2.tau = tau
                if side == -1:
                    t = p1.pick_move(game, side)
                else:
                    t = p2.pick_move(game, side)
                game.play_move(t[0], t[1], side)
                side *= -1
                turn += 1
            if game.get_winner() == challenger1_side:
                win_matrix[challenger1_index, j] += 1
                wtl[challenger1_index, j, 0] += 1
            elif game.get_winner() == -1 * challenger1_side:
                win_matrix[j, challenger1_index] += 1
                wtl[challenger1_index, j, 2] += 1
            else:
                win_matrix[challenger1_index, j] += 0.5
                win_matrix[j, challenger1_index] += 0.5
                wtl[challenger1_index, j, 1] += 1
            game.reset_board()
            played_games += 1
            if played_games >= total_games:
                finished = True
                break
        saveWTL(config, players, wtl)
        wtl = np.zeros((len(players), len(players), 3))
        if finished:
            break
    util.print_progress_bar(total_games, total_games, start=start)

    print("\n", [util.getPlayerName(player) for player in players])
    print("\nWin Matrix(row beat column):")
    print(win_matrix)
    try:
        with np.errstate(divide='ignore', invalid='ignore'):
            params = choix.ilsr_pairwise_dense(win_matrix)
        print("\nRankings:")
        for i, player in enumerate(np.argsort(params)[::-1]):
            print("%d. %s (expected %d) with %0.2f rating" %
                  (i + 1, util.getPlayerName(
                      players[player]), len(players) - player, params[player]))
        print(
            "\n(Rating Diff, Winrate) -> (0.5, 62%), (1, 73%), (2, 88%), (3, 95%), (5, 99%)"
        )
    except Exception:
        print("\nNot Enough data to calculate rankings")
コード例 #15
0
def start():
    config = SelfPlayConfig()
    tf_util.update_memory(config.gpu_mem_fraction)
    util.set_low_process_priority()
    AIPlayer.create_if_nonexistant(config)
    run_games(config)