def main(): parser = argparse.ArgumentParser() parser.add_argument('--bind-address', default='127.0.0.1') parser.add_argument('--port', '-p', type=int, default=5000) parser.add_argument('--pg-agent') parser.add_argument('--predict-agent') parser.add_argument('--q-agent') parser.add_argument('--ac-agent') args = parser.parse_args() bots = {'mcts': mcts.MCTSAgent(800, temperature=0.7)} if args.pg_agent: bots['pg'] = agent.load_policy_agent(h5py.File(args.pg_agent)) if args.predict_agent: bots['predict'] = agent.load_prediction_agent( h5py.File(args.predict_agent)) if args.q_agent: q_bot = rl.load_q_agent(h5py.File(args.q_agent)) q_bot.set_temperature(0.01) bots['q'] = q_bot if args.ac_agent: ac_bot = rl.load_ac_agent(h5py.File(args.ac_agent)) ac_bot.set_temperature(0.05) bots['ac'] = ac_bot web_app = httpfrontend.get_web_app(bots) web_app.run(host=args.bind_address, port=args.port, threaded=False)
def generate_games(board_size, rounds, max_moves, temperature): boards, moves = [], [ ] #In boards you store encoded board state; moves is for encoded moves encoder = get_encoder_by_name( 'oneplane', board_size ) #Initialize a OnePlaneEncoder by name with given board size game = goboard.GameState.new_game( board_size) #A new game of size board_size is instantiated bot = mcts.MCTSAgent(rounds, temperature) num_moves = 0 while not game.is_over(): print_board(game.board) move = bot.select_move(game) if move.is_play: boards.append(encoder.encode(game)) move_one_hot = np.zeros(encoder.num_point()) move_ont_hot[encoder.encode_point(move.point)] = 1 moves.append(move_one_hot) print_move(game.next_player, move) game = game.apply_move(move) num_moves += 1 if num_moves > max_moves: break return np.array(boards), np.array(moves)
def generate_game(board_size, rounds, max_moves, temperature): boards, moves = [], [] encoder = get_encoder_by_name('oneplane', board_size) game = goboard.GameState.new_game(board_size) bot = mcts.MCTSAgent(rounds, temperature) num_moves = 0 while not game.is_over(): print_board(game.board) move = bot.select_move(game) if move.is_play: boards.append(encoder.encode(game)) move_one_hot = np.zeros(encoder.num_points()) move_one_hot[encoder.encode_point(move.point)] = 1 moves.append(move_one_hot) print_move(game.next_player, move) game = game.apply_move(move) num_moves += 1 if num_moves > max_moves: break return np.array(boards), np.array(moves) # <10>
def main(): game = goboard.GameState.new_game(BOARD_SIZE) bot = mcts.MCTSAgent(500, temperature=1.4) while not game.is_over(): print_board(game.board) if game.next_player == gotypes.Player.black: human_move = input('-- ') point = point_from_coords(human_move.strip()) move = goboard.Move.play(point) else: move = bot.select_move(game) print_move(game.next_player, move) game = game.apply_move(move)
def generate_game(board_size, rounds, max_moves, temperature): # boardsにはエンコードされた盤の状態が格納され、movesにはエンコードされた着手が格納される boards, moves = [], [] # <1> # OnePlaneEncoderを指定された盤のサイズで初期化する encoder = get_encoder_by_name('oneplane', board_size) # <2> # サイズboard_sizeの新しいゲームがインスタンス化される game = goboard.GameState.new_game(board_size) # <3> # ラウンド数と温度が指定されたモンテカルロ木探索エージェントがボットになる bot = mcts.MCTSAgent(rounds, temperature) # <4> num_moves = 0 while not game.is_over(): print_board(game.board) # 次の着手がボットによって選択される move = bot.select_move(game) # <5> if move.is_play: # エンコードされた盤の状態がboardsに追加される boards.append(encoder.encode(game)) # <6> move_one_hot = np.zeros(encoder.num_points()) move_one_hot[encoder.encode_point(move.point)] = 1 # one-hotエンコードされた次の着手がmovesに追加される moves.append(move_one_hot) # <7> print_move(game.next_player, move) # その後、ボットの着手が盤に適用される game = game.apply_move(move) # <8> num_moves += 1 # 最大手数に達していない限り、次の手番を続ける if num_moves > max_moves: # <9> break return np.array(boards), np.array(moves) # <10>
def main(): workdir = '//home/nail//Code_Go//checkpoints//' os.chdir(workdir) bind_address = '127.0.0.1' port = 5000 predict_agent, pg_agent, q_agent, ac_agent = '', '', '', '' agent_type = input('Агент(pg/predict/q/ac = ').lower() if agent_type == 'pg': pg_agent = input( 'Введите имя файла для игры с ботом политика градиентов =') pg_agent = workdir + pg_agent + '.h5' if agent_type == 'predict': predict_agent = input( 'Введите имя файла для игры с ботом предсказания хода =') predict_agent = workdir + predict_agent + '.h5' if agent_type == 'q': q_agent = input( 'Введите имя файла для игры с ботом ценность действия =') q_agent = workdir + q_agent + '.h5' if agent_type == 'ac': ac_agent = input('Введите имя файла для игры с ботом актор-критик =') ac_agent = workdir + ac_agent + '.h5' bots = {'mcts': mcts.MCTSAgent(800, temperature=0.7)} if agent_type == 'pg': bots['pg'] = agent.load_policy_agent(h5py.File(pg_agent, 'r')) if agent_type == 'predict': bots['predict'] = agent.load_prediction_agent( h5py.File(predict_agent, 'r')) if agent_type == 'q': q_bot = rl.load_q_agent(h5py.File(q_agent, 'r')) q_bot.set_temperature(0.01) bots['q'] = q_bot if agent_type == 'ac': ac_bot = rl.load_ac_agent(h5py.File(ac_agent, 'r')) ac_bot.set_temperature(0.05) bots['ac'] = ac_bot web_app = httpfrontend.get_web_app(bots) web_app.run(host=bind_address, port=port, threaded=False)
def generate_game(board_size, rounds, max_moves, temperature): # initialize encoded board state and encoded moves boards, moves = [], [] # initialize a OnePlaneEncoder by name with given board size encoder = get_encoder_by_name('oneplane', board_size) # Instantiate a new game with board_size game = goboard.GameState.new_game(board_size) # MCTS agent bot with specified rounds and temp bot = mcts.MCTSAgent(rounds, temperature) num_moves = 0 while not game.is_over(): print_board(game.board) # bot picks next move move = bot.select_move(game) if move.is_play: # append encoded board to board boards.append(encoder.encode(game)) # The one-hot-encoded next move is appended to moves move_one_hot = np.zeros(encoder.num_points()) move_one_hot[encoder.encode_point(move.point)] = 1 moves.append(move_one_hot) # apply bots move to the board print_move(game.next_player, move) game = game.apply_move(move) num_moves += 1 # keep going until max number of moves is reached. if num_moves > max_moves: break return np.array(boards), np.array(moves)
def generate_game(board_size, rounds, max_moves, temperature): # In `boards` we store encoded board state, `moves` is for encoded moves. boards, moves = [], [] # We initialize a OnePlaneEncoder by name with given board size. encoder = get_encoder_by_name('oneplane', board_size) # An new game of size `board_size` is instantiated. game = goboard.GameState.new_game(board_size) # A Monte Carlo tree search agent with specified number of rounds and temperature will serve as our bot. bot = mcts.MCTSAgent(rounds, temperature) num_moves = 0 while not game.is_over(): print_board(game.board) # The next move is selected by the bot. move = bot.select_move(game) if move.is_play: boards.append(encoder.encode( game)) # The encoded board situation is appended to `boards`. move_one_hot = np.zeros(encoder.num_points()) move_one_hot[encoder.encode_point(move.point)] = 1 moves.append( move_one_hot ) # The one-hot-encoded next move is appended to `moves`. print_move(game.next_player, move) game = game.apply_move( move) # Afterwards the bot move is applied to the board. num_moves += 1 if num_moves > max_moves: # continue with the next move, unless the maximum number of moves has been reached break return np.array(boards), np.array(moves)
def main(): bot = mcts.MCTSAgent(700, temperature=1.4) web_app = httpfrontend.get_web_app({'mcts': bot}) web_app.run()
def main(): bot = mcts.MCTSAgent(700, temperature=1.4) web_app = httpfrontend.get_web_app(bot, BOARD_SIZE) web_app.run()
def main(): print("******************************************************************") print("* *") print("* <3 <3 <3 <3 <3 WELCOME TO GAME GO <3 <3 <3 <3 <3 *") print("* *") print("******************************************************************") print("* *") print("* 1. Play game on terminal *") print("* *") print("* a. Human vs Bot AlphaBeta on Board 9x9 *") print("* b. Human vs Bot Depthprune on Board 9x9 *") print("* c. Human vs Bot MCTS on Board 9x9 *") print("* d. Bot AlphaBeta vs Bot MCTS on Board 9x9 *") print("* *") print("* 2. Play game on web *") print("* *") print("* a. Human vs Bot MCTS on Board 9x9 *") print("* b. Human vs Bot DeepLearning on Board 19x19 *") print("* *") print("******************************************************************") print(" ") print(" ***************************************** ") print(" ") choices_A = int(input(" Choose Terminal or Web: ")) choices_B = input(" Choose type bot: ") print(" ") print(" ***************************************** ") BOARD_SIZE = 9 game = goboard.GameState.new_game(BOARD_SIZE) if choices_A == 1: if choices_B == 'a': bot = minimax.AlphaBetaAgent(4, capture_diff) if choices_B == 'b': bot = minimax.DepthPrunedAgent(4, capture_diff) if choices_B == 'c': bot = mcts.MCTSAgent(500, temperature=1.4) if choices_B == 'd': bots = { gotypes.Player.black: minimax.AlphaBetaAgent(4, capture_diff), gotypes.Player.white: mcts.MCTSAgent(500, temperature=1.4), } while not game.is_over(): time.sleep(0.3) print_board(game.board) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) game = game.apply_move(bot_move) if choices_B == 'a' or choices_B == 'b' or choices_B == 'c': while not game.is_over(): print_board(game.board) if game.next_player == gotypes.Player.black: human_move = input('-- ') point = point_from_coords(human_move.strip()) move = goboard.Move.play(point) else: move = bot.select_move(game) print_move(game.next_player, move) game = game.apply_move(move) else: if choices_B == 'a': bot = mcts.MCTSAgent(700, temperature=1.4) web_app = get_web_app({'mcts': bot}) web_app.run()
def main(): game = goboard.GameState.new_game(BOARD_SIZE) bot = mcts.MCTSAgent(500, temperature=1.4) s = get_usb_port() #grab a port print("USB Port: " + str(s)) #print it if you got if s: ser = serial.Serial(port=s, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=0.01) #auto-connects already I guess? print("Serial Connected!") if ser.isOpen(): print(ser.name + ' is open...') else: print("No Serial Device :/ Check USB cable connections/device!") exit() while not game.is_over(): print(chr(27) + "[2J") print_board(game.board) if game.next_player == gotypes.Player.black: while True: # human_move = input('-- ') read_data = ser.read( 1) #read the buffer (99/100 timeout will hit) if read_data != b'': #if not nothing there. fpga_move = read_data.hex() if fpga_move == "ff": move = goboard.Move.pass_turn() fpga_passed = True break else: fpga_passed = False fpga_row = 10 - (int(fpga_move[0], base=16) + 1) fpga_col = int(fpga_move[1], base=16) col = COLS[fpga_col] print(col, fpga_row) print("RXd: ") print(fpga_move) point = point_from_coords(col + str(fpga_row)) move = goboard.Move.play(point) fpga_move = move break else: move = bot.select_move(game) if move.is_pass: low_nibble = 15 high_nibble = 15 else: low_nibble = move.point.col - 1 high_nibble = move.point.row time.sleep(.5) if fpga_passed: move = goboard.Move.pass_turn() if move.is_pass: write_data = (255).to_bytes(1, byteorder="big") else: write_data = (abs((int(high_nibble) - 9)) * 16 + (int(low_nibble))).to_bytes(1, byteorder="big") print(write_data) ser.write(write_data) print_move(game.next_player, move) game = game.apply_move(move)