def train(): config = Config() while True: print("NEW GAME") winner = None mcts = MCTS(config) train_configs = [] target_policys = [] target_values = [] while winner == None: mcts.search(config) policy, best_move = mcts.get_mcts_policy() prepare_training(config, policy, train_configs, target_policys, target_values) config.apply_move(best_move, True) show(config) winner, _ = config.winner() objgraph.show_most_common_types() target_values = np.array(target_values) * winner train_configs = np.array(train_configs) target_policys = np.array(target_policys) mcts.model.fit(train_configs, [target_values, target_policys], epochs=1, batch_size=4, shuffle=True) mcts.model.save("bot/chess_model.h5") config.reset()
def play(): config = Config() winner = None while winner == None: show(config) start, dest = ask() if config.try_play(start, dest): winner, _ = config.winner() else: print("Invalid move, please replay") show(config) show_winner(winner)
def test(): config = Config() with open('chess/engine/games.csv', newline='') as games: reader = csv.DictReader(games) nb = 0 for row in reader: nb += 1 moves = row['moves'].split() i = 0 over = None if row['victory_status'] == 'mate' and nb > 0: while over == None: start, dest, prom_not_queen = ask_test(moves[i]) # auto promotion in queen so others are not handled if prom_not_queen: over = 0 elif config.try_play(start,dest): # show(config) over,_ = config.winner() else: sys.exit("fail at move : " + moves[i]) i += 1 config.reset()