Ejemplo n.º 1
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.º 2
0
def filter_data(human_data, num_players, min_game_id, max_game_id, roles):
    result = []
    for game in human_data:
        hidden_state = reconstruct_hidden_state(game)
        if len(hidden_state) != num_players:
            continue
        if not all(role in roles for role in hidden_state):
            continue
        if game['id'] >= max_game_id or game['id'] < min_game_id:
            continue
        if 'findMerlin' not in game['log'][-1]:
            continue
        result.append(game)

    return result
Ejemplo n.º 3
0
def get_bot_merlin_prediction(bot_class, game):
    hidden_state = reconstruct_hidden_state(game)
    state = AvalonState.start_state(len(hidden_state))

    possible = possible_hidden_states(set(hidden_state),
                                      num_players=len(hidden_state))
    perspectives = [
        starting_hidden_states(player, hidden_state, possible)
        for player, _ in enumerate(hidden_state)
    ]

    assassin_player = hidden_state.index('assassin')
    assassin_perspective = perspectives[assassin_player]
    assassin_bot = bot_class.create_and_reset(state, assassin_player,
                                              'assassin', assassin_perspective)

    for round_ in game['log']:
        state = handle_round(game, state, hidden_state, assassin_bot,
                             assassin_player, round_)

    final_round = game['log'][-1]

    assert 'findMerlin' in final_round
    find_merlin = round_['findMerlin']
    assert find_merlin['assassin'] == assassin_player

    legal_moves = state.legal_actions(assassin_player, hidden_state)
    move_probs = assassin_bot.get_move_probabilities(state, legal_moves)

    return {
        'human_guess': find_merlin['merlin_guess'],
        'bot_human_prob': move_probs[find_merlin['merlin_guess']],
        'correct_guess': hidden_state.index('merlin'),
        'bot_correct_prob': move_probs[hidden_state.index('merlin')],
        'top_pick': np.argmax(move_probs),
        'top_pick_prob': np.max(move_probs),
        'game': game['id'],
        'merlin': game['players'][hidden_state.index('merlin')]['player_id'],
        'assassin':
        game['players'][hidden_state.index('assassin')]['player_id']
    }
Ejemplo n.º 4
0
def predict_evil_using_voting_on_game(game, vote_same_if_same_prob, vote_same_if_diff_prob):
    try:
        dataframe_data = []
        hidden_state = reconstruct_hidden_state(game)
        assert len(hidden_state) <= 7
        team = tuple([role in EVIL_ROLES for role in hidden_state])
        avalon_start = AvalonState.start_state(len(hidden_state))
        game_generator = human_game_state_generator(avalon_start, game, hidden_state)

        same_team_prob = np.ones((len(hidden_state), len(hidden_state))) / 2

        vote_count = 0
        mission_num = 0
        who_failed = []
        for old_state, new_state, observation in game_generator:
            if old_state.status == 'run':
                mission_num += 1
                if old_state.fails > new_state.fails:
                    who_failed.append((old_state.proposal, observation))
            if old_state.status == 'vote':
                vote_count += 1
                update_pairings(observation, vote_same_if_same_prob, vote_same_if_diff_prob, same_team_prob)
                (nll_picked, pick), nll_correct = most_likely_team(same_team_prob, len(hidden_state), who_failed, team)
                dataframe_data.append({
                    'game': game['id'],
                    'num_players': len(hidden_state),
                    'vsis_prob': vote_same_if_same_prob,
                    'vsid_prob': vote_same_if_diff_prob,
                    'mission': mission_num,
                    'vote_count': vote_count,
                    'nll_correct': nll_correct,
                    'nll_picked': nll_picked,
                    'correct': pick == team,
                })
        sys.stdout.flush()
        return dataframe_data
    except ValueError:
        return []
    except AssertionError:
        return []
Ejemplo n.º 5
0
def predict_evil_over_human_game(game, as_bot, tremble):
    try:
        print game['id']
        sys.stdout.flush()
        hidden_state = reconstruct_hidden_state(game)
        assert frozenset(hidden_state) == frozenset(['merlin', 'assassin', 'minion', 'servant'])
        avalon_start = AvalonState.start_state(len(hidden_state))
        game_generator = human_game_state_generator(avalon_start, game, hidden_state)
        nll, particles = get_hidden_state_nll_for_game(avalon_start, game_generator, hidden_state, as_bot, tremble)
        data = {
            'bot': as_bot.__name__,
            'game': game['id'],
            'num_players': len(hidden_state),
            'trembling_hand_prob': tremble,
            'nll': nll
        }
        for p in range(len(hidden_state)):
            data['player_{}'.format(p)] = game['players'][p]['player_id']
            data['role_{}'.format(p)] = hidden_state[p]

        return data, particles
    except AssertionError:
        return None, None