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()
def train(ai, config): loaded_files = [] x = config.iterations i = len(glob.glob(config.data.model_location + "*.h5")) while (x != 0): if i > config.iter3: ai.update_lr(config.learning_rate3) elif i > config.iter2: ai.update_lr(config.learning_rate2) else: ai.update_lr(config.learning_rate1) loaded_files = load_games(ai, loaded_files, config) length = 0 start = time() print("Iteration %04d" % i) progress_start = ( i - 2 ) * config.min_new_game_files + config.min_game_files if i > 1 else 0 progress_end = (i - 1) * config.min_new_game_files + config.min_game_files util.print_progress_bar(0, progress_end - progress_start, start=start) while (len(loaded_files) < config.min_game_files or ((len(loaded_files) - config.min_game_files) // config.min_new_game_files) + 1 < i): if length != len(loaded_files): length = len(loaded_files) util.print_progress_bar(length - progress_start, progress_end - progress_start, start=start) sleep(5) loaded_files = load_games(ai, loaded_files, config) util.print_progress_bar(progress_end - progress_start, progress_end - progress_start, start=start) print("Training for %d batches on %d samples" % (config.batches_per_iter, len(ai.buffer.buffer))) start = time() history = ai.train_batches(config.batch_size, config.batches_per_iter, config.verbose) for val in history.history.keys(): print("%s: %0.4f" % (val, history.history[val][-1])) if i % config.save_model_cycles == 0: ai.save(config.data.model_location + "model_" + str(i) + ".h5") file = open( config.data.history_location + "hist_" + str(i) + ".pickle", 'wb') pickle.dump(pickle.dumps(history.history), file) file.close() print("Iteration Time: %0.2f" % (time() - start)) x -= 1 i += 1
def train(ai, config): loaded_files = [] x = config.iterations i = len(glob.glob(config.data.model_location + "*.h5")) loaded_files, _ = load_games(ai, loaded_files, config) while (x != 0): if i > config.iter3: ai.update_lr(config.learning_rate3) elif i > config.iter2: ai.update_lr(config.learning_rate2) else: ai.update_lr(config.learning_rate1) loaded_files, diff = load_games(ai, loaded_files, config) total_diff = diff start = time() print("Iteration %04d" % i) end = config.min_new_game_files if i > 0 else config.min_game_file util.print_progress_bar(0, end, start=start) while (total_diff < end): if diff > 0: total_diff += diff util.print_progress_bar(total_diff, end, start=start) sleep(5) loaded_files, diff = load_games(ai, loaded_files, config) util.print_progress_bar(end, end, start=start) print("Training for %d batches on %d samples" % (config.batches_per_iter, len(ai.buffer.buffer))) start = time() history = ai.train_batches(config.batch_size, config.batches_per_iter, config.verbose) for val in history.history.keys(): print("%s: %0.4f" % (val, history.history[val][-1])) if i % config.save_model_cycles == 0: ai.save("%smodel_%04d.h5" % (config.data.model_location, i)) file = open("%shist_%04d.pickle" % (config.data.history_location, i), 'wb') pickle.dump(pickle.dumps(history.history), file) file.close() print("Iteration Time: %0.2f" % (time() - start)) x -= 1 i += 1
def train(ai, config): loaded_files = [] x = config.iterations i = len(glob.glob(config.data.model_location+"*.h5")) loaded_files, _ = load_games(ai, loaded_files, config) while(x != 0): if i > config.iter3: ai.update_lr(config.learning_rate3) elif i > config.iter2: ai.update_lr(config.learning_rate2) else: ai.update_lr(config.learning_rate1) loaded_files, diff = load_games(ai, loaded_files, config) total_diff = diff start = time() print("Iteration %04d"%i) end = config.min_new_game_files if i> 0 else config.min_game_file util.print_progress_bar(0, end, start=start) while(total_diff < end): if diff > 0: total_diff += diff util.print_progress_bar(total_diff, end, start=start) sleep(5) loaded_files, diff = load_games(ai, loaded_files, config) util.print_progress_bar(end, end, start=start) print("Training for %d batches on %d samples" % (config.batches_per_iter, len(ai.buffer.buffer))) start = time() history = ai.train_batches(config.batch_size, config.batches_per_iter, config.verbose) for val in history.history.keys(): print("%s: %0.4f" % (val, history.history[val][-1])) if i % config.save_model_cycles == 0: ai.save("%smodel_%04d.h5" % (config.data.model_location, i)) file = open("%shist_%04d.pickle" % (config.data.history_location, i), 'wb') pickle.dump(pickle.dumps(history.history), file) file.close() print("Iteration Time: %0.2f" % (time()-start)) x -= 1 i += 1
def run_games(config): game = Othello() model_1 = "" model_2 = "" p1, new_1 = create_player(config.model_1, model_1, config) p2, new_2 = create_player(config.model_2, model_2, config) i = len(glob.glob(config.data.model_location+"*.h5")) avg_wins = [] while True: i += 1 new_1 = load_player(p1, config.model_1, model_1, config) new_2 = load_player(p2, config.model_2, model_2, config) while((config.model_1 == "newest" and new_1 == model_1) or (config.model_2 == "newest" and new_2 == model_2)): #print("Waiting on new model. Sleeping for 1 minute.") sleep(60) new_1 = load_player(p1, config.model_1, model_1, config) new_2 = load_player(p2, config.model_2, model_2, config) model_1 = new_1 model_2 = new_2 wins = 0 losses = 0 ties = 0 print("Iteration %04d"%i) print("Playing %d games with %d simulations per move" % (config.game_num, config.game.simulation_num_per_move)) start=time() for j in range(config.game_num): util.print_progress_bar(j, config.game_num, start=start) 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 if config.model_1 != "random": p1.tau =tau if config.model_2 != "random": p2.tau = tau if j % 2 == 0: if side == -1: t = p1.pick_move(game, side) else: t = p2.pick_move(game, side) else: 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() == 0: ties += 1 elif j % 2 == 0 and game.get_winner() == -1: wins += 1 elif j % 2 == 1 and game.get_winner() == 1: wins += 1 else: losses += 1 game.reset_board() util.print_progress_bar(config.game_num, config.game_num, start=start) print("%s vs %s: (%0.2f%% wins|%0.2f%% ties|%0.2f%% losses) of %d games" % (config.model_1, config.model_2, 100*wins/config.game_num, 100*ties/config.game_num, 100*losses/config.game_num, config.game_num)) avg_wins.append(100*wins/config.game_num) if len(avg_wins) > config.rolling_avg_amount: avg_wins = avg_wins[-1*config.rolling_avg_amount:] print("Average Win Percent: %0.2f%%" % (sum(avg_wins)/float(len(avg_wins)))) if not (config.repeat_with_new_model and (config.model_1 == "newest" or config.model_2 == "newest")): break
def run_games(config): game = Othello() model_1 = "" model_2 = "" p1, new_1 = create_player(config.model_1, model_1, config) p2, new_2 = create_player(config.model_2, model_2, config) if config.model_1 == "newest" or config.model_2 == "newest": i = len(glob.glob(config.data.model_location+"*.h5"))-1 else: i = 0 avg_wins = [] while True: i += 1 new_1 = load_player(p1, config.model_1, model_1, config) new_2 = load_player(p2, config.model_2, model_2, config) while((config.model_1 == "newest" and new_1 == model_1) or (config.model_2 == "newest" and new_2 == model_2)): #print("Waiting on new model. Sleeping for 1 minute.") sleep(60) new_1 = load_player(p1, config.model_1, model_1, config) new_2 = load_player(p2, config.model_2, model_2, config) model_1 = new_1 model_2 = new_2 wins = 0 losses = 0 ties = 0 print("Iteration %04d"%i) print("Playing games between %s and %s" % (config.model_1, config.model_2)) print("Playing %d games with %d simulations per move" % (config.game_num, config.game.simulation_num_per_move)) start=time() for j in range(config.game_num): util.print_progress_bar(j, config.game_num, start=start) 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 if config.model_1 != "random": p1.tau =tau if config.model_2 != "random": p2.tau = tau if j % 2 == 0: if side == -1: t = p1.pick_move(game, side) else: t = p2.pick_move(game, side) else: 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() == 0: ties += 1 savePerformance(config, model_1, model_2, 0, 1, 0) elif (j % 2 == 0 and game.get_winner() == -1) or (j % 2 == 1 and game.get_winner() == 1): wins += 1 savePerformance(config, model_1, model_2, 1, 0, 0) else: losses += 1 savePerformance(config, model_1, model_2, 0, 0, 1) game.reset_board() util.print_progress_bar(config.game_num, config.game_num, start=start) print("%s vs %s: (%0.2f%% wins|%0.2f%% ties|%0.2f%% losses) of %d games" % (config.model_1, config.model_2, 100*wins/config.game_num, 100*ties/config.game_num, 100*losses/config.game_num, config.game_num)) avg_wins.append(100*wins/config.game_num) if len(avg_wins) > config.rolling_avg_amount: avg_wins = avg_wins[-1*config.rolling_avg_amount:] print("Average Win Percent: %0.2f%%" % (sum(avg_wins)/float(len(avg_wins)))) if not (config.repeat_with_new_model and (config.model_1 == "newest" or config.model_2 == "newest")): break
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]))
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%)" )
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")
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")