Exemplo n.º 1
0
def start(config: Config):
    PlayWithHumanConfig().update_play_config(config.play)
    chess_model = PlayWithHuman(config)

    env = ChessEnv().reset()
    human_is_black = random() < 0.5
    chess_model.start_game(human_is_black)

    while not env.done:
        if env.board.turn == chess.BLACK:
            if not human_is_black:
                action = chess_model.move_by_ai(env)
                print("IA moves to: " + action)
            else:
                action = chess_model.move_by_human(env)
                print("You move to: " + action)
        else:
            if human_is_black:
                action = chess_model.move_by_ai(env)
                print("IA moves to: " + action)
            else:
                action = chess_model.move_by_human(env)
                print("You move to: " + action)
        board, info = env.step(action)
        env.render()
        print("Board fen = " + board.fen())

    print("\nEnd of the game.")
    print("Game result:")
    print(env.board.result())
def start(config: Config):

    PlayWithHumanConfig().update_play_config(config.play)

    me_player = None
    env = ChessEnv().reset()

    app = Flask(__name__)

    model = ChessModel(config)

    if not load_best_model_weight(model):
        raise RuntimeError("Best model not found!")

    player = ChessPlayer(config, model.get_pipes(config.play.search_threads))

    @app.route('/play', methods=["GET", "POST"])
    def play():
        data = request.get_json()
        print(data["position"])
        env.update(data["position"])
        env.step(data["moves"], False)
        bestmove = player.action(env, False)
        return jsonify(bestmove)

    app.run(host="0.0.0.0", port="8080")
Exemplo n.º 3
0
    def __init__(self):
        # Tensorflow session
        import tensorflow as tf
        log.debug("Initializing Tensorflow session...")
        tf_session_config = tf.ConfigProto()
        tf_session_config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=tf_session_config)

        from chess_zero.config import Config, PlayWithHumanConfig
        from chess_zero.env.chess_env import ChessEnv

        self.chess_env_class = ChessEnv

        default_config = Config()
        PlayWithHumanConfig().update_play_config(default_config.play)
        self.alpha_player = self.get_player_from_model(default_config)
Exemplo n.º 4
0
def start(config: Config):

    PlayWithHumanConfig().update_play_config(config.play)

    me_player = None
    env = ChessEnv().reset()

    while True:
        line = input()
        words = line.rstrip().split(" ", 1)
        if words[0] == "uci":
            print("id name ChessZero")
            print("id author ChessZero")
            print("uciok")
        elif words[0] == "isready":
            if not me_player:
                me_player = get_player(config)
            print("readyok")
        elif words[0] == "ucinewgame":
            env.reset()
        elif words[0] == "position":
            words = words[1].split(" ", 1)
            if words[0] == "startpos":
                env.reset()
            else:
                if words[0] == "fen":  # skip extraneous word
                    words = words[1].split(' ', 1)
                fen = words[0]
                for _ in range(5):
                    words = words[1].split(' ', 1)
                    fen += " " + words[0]
                env.update(fen)
            if len(words) > 1:
                words = words[1].split(" ", 1)
                if words[0] == "moves":
                    for w in words[1].split(" "):
                        env.step(w, False)
        elif words[0] == "go":
            if not me_player:
                me_player = get_player(config)
            action = me_player.action(env, False)
            print(f"bestmove {action}")
        elif words[0] == "stop":
            pass
        elif words[0] == "quit":
            break
Exemplo n.º 5
0
def start(config: Config):

    PlayWithHumanConfig().update_play_config(config.play)
    config.play.thinking_loop = 1

    chess_model = None
    env = ChessEnv().reset()

    while True:
        line = input()
        words = line.rstrip().split(" ", 1)
        if words[0] == "uci":
            print("id name ChessZero")
            print("id author ChessZero")
            print("uciok")
        elif words[0] == "isready":
            if chess_model is None:
                chess_model = PlayWithHuman(config)
            print("readyok")
        elif words[0] == "ucinewgame":
            env.reset()
        elif words[0] == "position":
            words = words[1].split(" ", 1)
            if words[0] == "startpos":
                env.reset()
            else:
                fen = words[0]
                for _ in range(5):
                    words = words[1].split(' ', 1)
                    fen += " "+words[0]
                env.update(fen)
            if len(words) > 1:
                words = words[1].split(" ", 1)
                if words[0] == "moves":
                    for w in words[1].split(" "):
                        env.step(w, False)
        elif words[0] == "go":
            action = chess_model.move_by_ai(env)
            print(f"bestmove {action}")
        elif words[0] == "stop":
            pass
        elif words[0] == "quit":
            break
Exemplo n.º 6
0
def start(config: Config):
    PlayWithHumanConfig().update_play_config(config.play)
    chess_model = PlayWithEngine(config)

    env = ChessEnv().reset()
    human_is_black = random() < 0.5
    chess_model.start_game(human_is_black)

    while not env.done:
        if (env.board.turn == chess.BLACK) == human_is_black:
            action = chess_model.move_by_opponent(env)
            print("You move to: " + action)
        else:
            action = chess_model.move_by_ai(env)
            print("AI moves to: " + action)
        board, info = env.step(action)
        env.render()
        print("Board FEN = " + board.fen())

    print("\nEnd of the game.")  #spaces after this?
    print("Game result:")  #and this?
    print(env.board.result())
Exemplo n.º 7
0
 def create(self):
     # Initial Alpha Zero setup
     default_config = Config()
     PlayWithHumanConfig().update_play_config(default_config.play)
     return self.get_player_from_model(default_config)

# The gRPC serve function.
#
# Params:
# max_workers: pool of threads to execute calls asynchronously
# port: gRPC server port
#
# Add all your classes to the server here.
# (from generated .py files by protobuf compiler)
def serve(max_workers=10, port=7777):
	server = grpc.server(futures.ThreadPoolExecutor(max_workers=max_workers))
	grpc_bt_grpc.add_AlphaZeroServicer_to_server(AlphaZeroServicer(), server)
	server.add_insecure_port("[::]:{}".format(port))
	return server


if __name__ == "__main__":
	"""
	Runs the gRPC server to communicate with the Snet Daemon.
	"""

	# Initial Alpha Zero setup
	default_config = Config()
	PlayWithHumanConfig().update_play_config(default_config.play)
	ALPHA_ZERO_PLAYER = get_player_from_model(default_config)

	parser = service.common.common_parser(__file__)
	args = parser.parse_args(sys.argv[1:])
	service.common.main_loop(serve, args)