def get_hidden_states_bucket(player_perspective, fails): global perspective_fails_to_bucket global bucket_to_hidden_states key = (player_perspective, tuple(fails)) if key in perspective_fails_to_bucket: return perspective_fails_to_bucket[key] print "Calculating big lookup table..." all_hidden_states = possible_hidden_states( ['merlin', 'minion', 'servant', 'assassin'], 5) possible_beliefs_to_key = defaultdict(lambda: []) for roles in all_hidden_states: beliefs = [ starting_hidden_states(player, roles, all_hidden_states) for player in range(5) ] perspectives = [ get_python_perspective(roles, player) for player in range(5) ] for perspective, b in zip(perspectives, beliefs): possible_beliefs_to_key[frozenset(b)].append((perspective, ())) for p1size in [2, 3]: for p1 in itertools.combinations(range(5), p1size): num_bad = len([p for p in p1 if roles[p] in EVIL_ROLES]) if num_bad == 0: continue for num_fail in range(1, num_bad + 1): new_beliefs = [ filter_hidden_states(b, p1, num_fail) for b in beliefs ] for perspective, b in zip(perspectives, new_beliefs): possible_beliefs_to_key[frozenset(b)].append( (perspective, ((p1, num_fail), ))) for p2size in [2, 3]: for p2 in itertools.combinations(range(5), p2size): num_bad2 = len( [p for p in p2 if roles[p] in EVIL_ROLES]) if num_bad2 == 0: continue for num_fail2 in range(1, num_bad2 + 1): for perspective, b in zip( perspectives, new_beliefs): b_prime = filter_hidden_states( b, p2, num_fail2) possible_beliefs_to_key[frozenset( b_prime)].append( (perspective, ((p1, num_fail), (p2, num_fail2)))) for i, (hidden_states, keys) in enumerate(possible_beliefs_to_key.iteritems()): assert len(bucket_to_hidden_states) == i bucket_to_hidden_states.append(list(hidden_states)) for key in keys: perspective_fails_to_bucket[key] = i return get_hidden_states_bucket(player_perspective, fails)
def handle_transition(self, old_state, new_state, observation, move=None): if old_state.status == 'vote': self.propose_nn_input.append( np.concatenate([ self.perception.flat, onehot(old_state.proposer), onehot(old_state.proposal), np.array( [1.0 if vote.up else -1.0 for vote in observation]) ])) self.vote_nn_input.append( np.concatenate([ self.perception.flat, onehot(self.player), onehot(old_state.proposal), np.array( [1.0 if vote.up else -1.0 for vote in observation]) ])) if old_state.status == 'run': # if move is not None and self.role in EVIL_ROLES and not move.fail: # observation += 1 self.hidden_states = filter_hidden_states(self.hidden_states, old_state.proposal, observation) self.perception = create_perception(self.hidden_states) if new_state.status == 'propose': self.propose_nn_input.append( np.concatenate([ self.perception.flat, onehot(new_state.proposer), np.zeros(5), np.zeros(5) ]))
def handle_transition(self, old_state, new_state, observation, move=None): if old_state.status == 'run': if move is not None and self.role in EVIL_ROLES and not move.fail: observation += 1 self.hidden_states = filter_hidden_states(self.hidden_states, old_state.proposal, observation)
def handle_transition(self, old_state, new_state, observation, move=None): self.history.append((observation, new_state)) if old_state.status == 'run': p_on = self.player in old_state.proposal if observation > 0: for p in old_state.proposal: self.player_status[p] = max(self.player_status[p], 2 if p_on else 1) self.fails.append((old_state.proposal, observation)) # filter hidden states if move is not None and self.role in EVIL_ROLES and not move.fail: observation += 1 self.hidden_states = filter_hidden_states(self.hidden_states, old_state.proposal, observation)
def process_game(game): try: hidden_state = reconstruct_hidden_state(game) if len(hidden_state) != 5: return print game['id'] 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) ] perceptions = [ create_perception(perspective) for perspective in perspectives ] vote_inputs = [[] for _ in hidden_state] propose_inputs = [[] for _ in hidden_state] for round_ in game['log']: last_proposal_dict = None last_proposal = np.zeros(5) last_votes = np.zeros(5) for proposal_num in ['1', '2', '3', '4', '5']: if proposal_num not in round_: break proposal_dict = last_proposal_dict = round_[proposal_num] proposer = proposal_dict['proposer'] for player, perception in enumerate(perceptions): propose_inputs[player].append( np.concatenate([ perception.flat, onehot(proposer), last_proposal, last_votes ])) proposal = onehot(proposal_dict['team']) PROPOSE_INPUTS.append(propose_inputs[proposer][:]) PROPOSE_OUTPUTS.append(proposal) votes = np.array([ -1.0 if vote == 'Reject' else 1.0 for vote in proposal_dict['votes'] ]) for player, vote in enumerate(proposal_dict['votes']): vote_inputs[player].append( np.concatenate([ perceptions[player].flat, onehot(player), proposal, np.zeros(5) ])) VOTE_INPUTS.append(vote_inputs[player][:]) VOTE_OUTPUTS.append(1.0 if vote == 'Approve' else 0.0) vote_inputs[player][-1] = np.concatenate([ perceptions[player].flat, onehot(player), proposal, votes ]) last_proposal = proposal last_votes = votes num_fails = len([ mission_vote for mission_vote in round_['mission'] if mission_vote == 'Fail' ]) for player, perspective in enumerate(perspectives): perspectives[player] = filter_hidden_states( perspectives[player], set(last_proposal_dict['team']), num_fails) perceptions[player] = create_perception(perspectives[player]) except AssertionError: print game['id'], 'is bad'