Ejemplo n.º 1
0
def get_counterfactual_value_by_perspective(perspective, hidden_states, player_values_in_state, probs):
    _, player, _ = perspective
    indices = []
    for h, hidden_state in enumerate(hidden_states):
        if get_python_perspective(hidden_state, player) == perspective:
            indices.append(h)
    return get_counterfactual_value(player, indices, player_values_in_state, probs)
Ejemplo n.º 2
0
def get_player_values_for_state(hidden_states, probs, state, strats):
    player_values_in_state = np.zeros((len(hidden_states), len(hidden_states[0])))

    for h, hidden_state in enumerate(hidden_states):
        print "player values", h
        if probs[h] == 0:
            continue
        perspectives = [
            get_python_perspective(hidden_state, player) for player in range(5)
        ]
        player_values_in_state[h] = get_player_values(hidden_state, state, perspectives, [], strats, 1.0)
    return player_values_in_state
Ejemplo n.º 3
0
def read_data_load_counts():
    global counts
    if counts is not None:
        return counts

    if counts is None and os.path.isfile(STRATEGY_DATAFILE):
        with open(STRATEGY_DATAFILE, 'r') as f:
            counts = pickle.load(f)
        return counts

    print "Creating counts data..."
    counts = {
        'propose': defaultdict(zeros_10),
        'vote': defaultdict(zeros_2),
        'run': defaultdict(zeros_2),
        'merlin': defaultdict(zeros_5)
    }

    human_games = load_human_json()
    for game in human_games:
        hidden_state = reconstruct_hidden_state(game)
        if len(hidden_state) != 5 or frozenset(hidden_state) != frozenset(
            ['merlin', 'assassin', 'minion', 'servant']):
            continue
        print game['id']
        start_state = AvalonState.start_state(len(hidden_state))
        fails = []
        perspectives = [
            get_python_perspective(hidden_state, player)
            for player in range(len(hidden_state))
        ]
        for state, moves in game_state_generator(start_state, game,
                                                 hidden_state):
            moving_players = state.moving_players()
            for player, move in zip(moving_players, moves):
                legal_actions = state.legal_actions(player, hidden_state)
                perspective_bucket = get_hidden_states_bucket(
                    perspectives[player], fails)
                index = legal_actions.index(move)
                counts[state.status][(state.as_key(), perspectives[player],
                                      perspective_bucket)][index] += 1

            if state.status == 'run' and any([move.fail for move in moves]):
                fails.append((state.proposal,
                              len([move for move in moves if move.fail])))

    print "Writing counts data..."
    with open(STRATEGY_DATAFILE, 'w') as f:
        pickle.dump(counts, f)

    return counts
Ejemplo n.º 4
0
def solve_subgame(hidden_states, lls, state, iterations=1000):
    assert state.succeeds == 2 and state.fails == 2 and state.propose_count > 2
    lls = lls - np.max(lls)
    probs = np.exp(lls)
    probs /= np.sum(probs)

    regrets = {
        'propose': defaultdict(lambda: np.zeros(10)),
        'vote': defaultdict(lambda: np.zeros(2)),
        'run': defaultdict(lambda: np.zeros(2)),
        'merlin': defaultdict(lambda: np.zeros(5)),
    }

    strats = {
        'propose': defaultdict(lambda: np.zeros(10)),
        'vote': defaultdict(lambda: np.zeros(2)),
        'run': defaultdict(lambda: np.zeros(2)),
        'merlin': defaultdict(lambda: np.zeros(5)),
    }

    for t in range(iterations):
        if t % 100 == 0:
            print t
        h = np.random.choice(len(hidden_states), p=probs)
        hidden_state = hidden_states[h]
        perspectives = [
            get_python_perspective(hidden_state, player) for player in range(5)
        ]
        for player in range(5):
            subgame_cfr(state, hidden_state, perspectives, player, regrets, strats, [], 1.0, t + 1.0)

    print "Retreiving player values..."
    player_values_in_state = get_player_values_for_state(hidden_states, probs, state, strats)
    interesting_perspective = ('player', 3, None)
    print "Player 3's perspective (if servant): {}".format(
        get_counterfactual_value_by_perspective(
            interesting_perspective,
            hidden_states,
            player_values_in_state,
            probs
        )
    )
Ejemplo n.º 5
0
 def reset(self, game, player, role, hidden_states):
     self.player = player
     self.fails = []
     self.my_perspective = get_python_perspective(hidden_states[0], player)