Exemplo n.º 1
0
def general_test():
    ray.init()

    # Network loading
    config = TablutConfig()

    # Test network loading
    heuristic_name = "oldschool"

    heuristic_test = NeuralHeuristicFunction(config)
    if heuristic_test.init_tflite():
        print("Netowrk loaded successfully")
        heuristic_name = "neural"
    else:
        print("Netowrk loading error")

    search = MultiThreadSearch(config, heuristic_name)

    state = AshtonTablut.get_initial()

    best_next_state, best_action, best_score, max_depth, nodes_explored, search_time = search.iterative_deepening_search(
        state=state, initial_cutoff_depth=2, cutoff_time=10.0)

    best_action = AshtonTablut.num_to_coords(best_action)
    print(
        "Game move ({0}): {1} -> {2}, Search time: {3}, Max Depth: {4}, Nodes explored: {5}, Score: {6}"
        .format(state.to_move(), (best_action[0], best_action[1]),
                (best_action[2], best_action[3]), search_time, max_depth,
                nodes_explored, best_score))
Exemplo n.º 2
0
def test1():
    config = TablutConfig()
    folder = config.folder
    filename = config.tflite_model
    model_path = os.path.join(folder, filename)

    interpreter = tflite.Interpreter(model_path, num_threads=4)
    interpreter.allocate_tensors()

    # Get input and output tensors.
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    # Test the model on random input data.
    input_shape = input_details[0]['shape']
    input_data = np.array(np.random.random_sample(input_shape),
                          dtype=np.float32)
    interpreter.set_tensor(input_details[0]['index'], input_data)

    n = 100
    sum = 0

    for i in range(n):

        startTime = time.time()
        interpreter.invoke()
        deltaTime = time.time() - startTime
        sum += deltaTime

    print(
        "Inference time: {0} ms ({3} invokes), Input details: {1}, Output details: {2}"
        .format((sum / n) * 1000, input_details, output_details, n))
Exemplo n.º 3
0
def self_play_worker(priority, heuristic_alpha, random=False):
    logging.basicConfig(format='%(levelname)s:%(asctime)s: %(message)s',
                        level=logging.INFO)
    config = TablutConfig()

    folder = config.folder
    filename = config.tflite_model
    filepath = os.path.join(folder, filename)

    heuristic = OldSchoolHeuristicFunction()

    if not random:
        if os.path.exists(filepath) and os.path.isfile(filepath):
            heuristic = MixedHeuristicFunction(config, heuristic_alpha)
            heuristic.init_tflite()
            if heuristic.initialized():
                logging.info("Tflite model loaded")
            else:
                logging.info(
                    "Tflite model not loaded... Using OldSchoolHeuristic")
                heuristic = OldSchoolHeuristicFunction()
        else:
            logging.info("Tflite model not found... Using OldSchoolHeuristic")
            heuristic = OldSchoolHeuristicFunction()

    time_per_move = config.max_time / (2 * priority + 1)

    logging.info("Starting SelfPlay. Priority: {0}".format(priority))

    winner = 'D'
    selfplay = SelfPlay(config, heuristic, priority, time_per_move)

    while winner == 'D':
        winner, utility, history = selfplay.play(random)

    logging.info("Done. Result: {0}".format(winner))

    return SelfPlayResult(
        priority * (10 * config.random_workers if random else 1), winner,
        utility, history)
Exemplo n.º 4
0
def test_h():
    ray.init()

    # Network loading
    config = TablutConfig()

    # Test network loading
    heuristic_name = "oldschool"

    heuristic_test = NeuralHeuristicFunction(config)
    if heuristic_test.init_tflite():
        print("Netowrk loaded successfully")
        heuristic_name = "neural"
    else:
        print("Netowrk loading error")

    actor = BrachActor.remote(config, heuristic_name)
    print(ray.get(actor.heuristic_initialized.remote()))

    state = AshtonTablut.get_initial()
    print(
        ray.get(
            actor.evalutate.remote(state.board(), state.to_move(),
                                   state.turn())))
Exemplo n.º 5
0
        if not have_draw:
            if current_state.utility(player) == 1:
                winner = player
                result = "WON"
            elif current_state.utility(player) == -1:
                winner = 'W' if player == 'B' else 'B'
                result = "LOST"

        logging.info(
            "Game ended: Player {0} {1}, Moves: {2}, Time: {3} s, Utility: {4}"
            .format(player, result, current_state.turn(), end - start,
                    current_state.utility(player)))

        return winner, current_state.utility(player), self.game_history


if __name__ == '__main__':
    # test()
    init_rand()

    logging.basicConfig(format='%(levelname)s:%(asctime)s: %(message)s',
                        level=logging.DEBUG)

    heuristic = NeuralHeuristicFunction(TablutConfig())
    heuristic.init_tflite()

    #heuristic = OldSchoolHeuristicFunction()

    self_play = SelfPlay(TablutConfig(), heuristic, 1, 10)
    self_play.play(False)
Exemplo n.º 6
0
 def __init__(self, config=TablutConfig()):
     super().__init__()
     self.sample_weight = config.value_loss_weight
Exemplo n.º 7
0
    def tflite_optimization(self):
        folder = self.config.folder
        filename = self.config.tflite_model
        filepath = os.path.join(folder, filename)

        converter = tf.lite.TFLiteConverter.from_keras_model(self.nnet)
        converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_LATENCY]
        converter.target_spec.supported_types = [tf.float16]
        quantized_tflite_model = converter.convert()

        with open(filepath, "wb") as f:
            f.write(quantized_tflite_model)


if __name__ == '__main__':
    config = TablutConfig()
    buf = ActionBuffer(config)
    buf.load_buffer()
    batch_size = config.batch_size
    batch_size = min(batch_size, buf.size())
    dataset = buf.generate_dataset(batch_size)

    net = ResNNet(config)
    net.train(dataset)

    board = np.zeros((1, 9, 9, 4), dtype=np.int8)

    board[0, :, :, 0] = np.array(
        [[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0],
         [0, 0, 1, 1, 0, 1, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0],
Exemplo n.º 8
0
def game_loop(args):
    # Args
    player = args.player.upper()[0]
    playing_player = 'W'
    timeout = args.timeout
    host = args.host
    cores = args.cores

    port = 5800 if player == 'W' else 5801

    ray.init()

    # Network loading
    config = TablutConfig()

    #Test network loading
    heuristic_name = "oldschool"

    heuristic_test = NeuralHeuristicFunction(config)
    if heuristic_test.init_tflite():
        logging.info("Netowrk loaded successfully")
        heuristic_name = "neural"
    else:
        logging.info("Netowrk loading error")

    search = MultiThreadSearch(config, heuristic_name)

    turn = 0

    # Start connection
    connHandle = ConnectionHandler(host, port)
    connHandle.send('AlphaTablut')

    # Game loop
    while True:
        try:
            length, message = connHandle.recv()

            logging.debug("Received message of length {}".format(length))

            if message:
                # Sync local state with server state
                data = json.loads(message)
                state, playing_player = JSON_to_local_state(data, turn)

                logging.info("Turn {0}: {1} is playing.".format(
                    turn, playing_player))

                logging.info("\n" + state.display())

                if playing_player == 'WHITEWIN':
                    logging.info("We {} GG WP!".format(
                        'WON' if playing_player[0] == player else 'LOST'))
                    break
                elif playing_player == 'BLACKWIN':
                    logging.info("We {} GG WP!".format(
                        'WON' if playing_player[0] == player else 'LOST'))
                    break
                elif playing_player == 'DRAW':
                    logging.info("We {} GG WP!".format('DREW'))
                    break
                elif playing_player[0] == player:
                    logging.info("Computing and sending action.")

                    best_next_state, best_action, best_score, max_depth, nodes_explored, search_time = search.iterative_deepening_search(
                        state=state,
                        initial_cutoff_depth=2,
                        cutoff_time=timeout)

                    send_move(connHandle, best_action, player)
                    logging.debug("Action sent!")

                    best_action = AshtonTablut.num_to_coords(best_action)
                    logging.info(
                        "Game move ({0}): {1} -> {2}, Search time: {3}, Max Depth: {4}, Nodes explored: {5}, Score: {6}"
                        .format(player, (best_action[0], best_action[1]),
                                (best_action[2], best_action[3]), search_time,
                                max_depth, nodes_explored, best_score))
                else:
                    logging.info("Waiting...")

                turn += 1
                #heuristic.set_alpha(2 / turn)

        except ConnectionException as e:
            logging.debug(e)
            logging.info("Coonection lost: {}".format(playing_player))
            break

    connHandle.close()
Exemplo n.º 9
0
 def __init__(self):
     self.config = TablutConfig()
     self.action_buffer = ActionBuffer(self.config)
     self.nnet = ResNNet(self.config)
     self.sigInterrupt = False