Beispiel #1
0
    def scores(self, state: GameState, board: GameBoard,
               actions: List[Action]) -> List[Tuple[float, Action]]:
        # actions = [a for a in actions if a.__class__.__name__ is not "PassFigure"]
        X = [
            vectorState(state) + vectorAction(action) +
            vectorBoard(board, state, action) for action in actions
        ]

        df = pd.DataFrame(data=X,
                          columns=vectorStateInfo() + vectorActionInfo() +
                          vectorBoardInfo()).dropna(axis=1)

        df = df.drop(['meta_seed', 'meta_scenario', 'action_team'], axis=1)
        df['action_obj'] = actions

        df_m = df.loc[(df['action_type_Move']
                       | df['action_type_MoveLoadInto'])].copy()
        df_m_obj = df_m['action_obj']
        df_m.drop('action_obj', 1, inplace=True)

        df_a = df.loc[(df['action_type_Attack']
                       | df['action_type_AttackGround']
                       | df['action_type_AttackRespond'])].copy()
        df_a_obj = df_a['action_obj']
        df_a.drop('action_obj', 1, inplace=True)

        df_p = df.loc[(df['action_type_Pass'] | df['action_type_PassFigure']
                       | df['action_type_PassTeam']
                       | df['action_type_PassRespond'])].copy()
        df_p_obj = df_p['action_obj']
        df_p.drop('action_obj', 1, inplace=True)

        score_m = []
        if not df_m.empty:
            y_m = self.model_m.predict(df_m)
            score_m = list(zip(y_m, df_m_obj))

        score_p = []
        if not df_p.empty:
            y_p = self.model_p.predict(df_m)
            score_p = list(zip(y_p, df_p_obj))

        score_a = []
        if not df_a.empty:
            y_a = self.model_a.predict(df_a)
            score_a = list(zip(y_a, df_a_obj))

        return score_a + score_m + score_p
Beispiel #2
0
    def scores(self, board: GameBoard, state: GameState,
               actions: List[Action]) -> List[Tuple[float, Action]]:
        """
        :param board:       board of the game
        :param state:       state of the game
        :param actions:     list of available actions to score
        :return: list of all actions with their score
        """
        X = [
            vectorState(state) + vectorAction(action) +
            vectorBoard(board, state, action) for action in actions
        ]

        df = pd.DataFrame(data=X,
                          columns=vectorStateInfo() + vectorActionInfo() +
                          vectorBoardInfo()).dropna(axis=1)
        df = df.drop(['meta_seed', 'meta_scenario', 'action_team'], axis=1)

        scores = self.model.predict(df)

        return list(zip(scores, actions))
Beispiel #3
0
    def scores(self, board: GameBoard, state: GameState,
               actions: List[Action]) -> List[Tuple[float, Action]]:
        """
        :param board:       board of the game
        :param state:       state of the game
        :param actions:     list of available actions to score
        :return: list of all actions with their score
        """
        X = [
            vectorState(state) + vectorAction(action) +
            vectorBoard(board, state, action) for action in actions
        ]

        df = pd.DataFrame(data=X,
                          columns=vectorStateInfo() + vectorActionInfo() +
                          vectorBoardInfo()).dropna(axis=1)
        df = df.drop(['meta_seed', 'meta_scenario', 'action_team'], axis=1)

        offset = 1 if self.team == BLUE else 0
        scores = self.model.predict_proba(df)

        return [(abs(offset - scores[i][0]), actions[i])
                for i in range(len(actions))]
Beispiel #4
0
def play(args) -> tuple:
    red: Player = args[0]
    blue: Player = args[1]
    seed: int = args[2]
    epoch: int = args[3]
    dir_data: str = args[4]

    mm = playJunction(seed, red, blue)

    # get data frames
    states_cols = vectorStateInfo()
    states_data = [vectorState(x) for x in mm.states_history]
    df_state = pd.DataFrame(columns=states_cols, data=states_data)

    actions_cols = vectorActionInfo()
    actions_data = [vectorAction(x) for x in mm.actions_history]
    df_action = pd.DataFrame(columns=actions_cols, data=actions_data)

    board_cols = vectorBoardInfo()
    board_data = [
        vectorBoard(mm.board, s, a)
        for s, a in zip(mm.states_history, mm.actions_history)
    ]
    df_board = pd.DataFrame(columns=board_cols, data=board_data)

    df = pd.concat([df_state, df_action, df_board], axis=1)

    df['winner'] = mm.winner
    df['meta_player'] = df['action_team'].apply(lambda x: red.id
                                                if x == RED else blue.id)

    # save to disk
    filename = f'game.{epoch}.{seed}.{red.id}.{blue.id}.pkl.gz'
    df.to_pickle(os.path.join(dir_data, str(epoch), filename),
                 compression='gzip')

    return red.id, blue.id, mm.winner
Beispiel #5
0
    # different agents can have different set of parameters
    red = AlphaBetaFast1Agent(RED, maxDepth=3)
    blue = AlphaBetaFast1Agent(BLUE, maxDepth=3)

    # the MatchManager is the object that is in charge of control the evolution of a game
    mm = MatchManager('', red, blue, board, state, seed=seed)

    # there is a dedicated method to play the full game
    mm.play()

    # at the end it is possible to collect some information from the MatchManager object, like the winner
    logger.info('winner: ', mm.winner)

    # it is also possible to get information on the history of played actions...
    actions_cols = vectorActionInfo()
    actions_data = [vectorAction(x) for x in mm.actions_history]

    df_actions = pd.DataFrame(columns=actions_cols, data=actions_data)

    # ...on the states...
    states_cols = vectorStateInfo()
    states_data = [vectorState(x) for x in mm.states_history]

    df_states = pd.DataFrame(columns=states_cols, data=states_data)

    # ...on the board...
    board_cols = vectorBoardInfo()
    board_data = [vectorBoard(board, s, a) for s, a in zip(mm.states_history, mm.actions_history)]

    df_board = pd.DataFrame(columns=board_cols, data=board_data)