def post_step(self, world, act): t = world.getState(WORLD, 'seconds', unique=True) if len(self.model_data) == 0 or self.model_data[-1]['Timestep'] != t: # Haven't made some inference for this timestep (maybe wait until last one?) player_name = self.player_name() player = world.agents[player_name] agent = world.agents['ATOMIC'] # Store beliefs over player models beliefs = agent.getBelief() if len(beliefs) > 1: raise RuntimeError('Agent {} has {} possible models in true state'.format(agent.name, len(beliefs))) beliefs = next(iter(beliefs.values())) player_model = world.getFeature(modelKey(player_name), beliefs) for model in player_model.domain(): entry = {'Timestep': t, 'Belief': player_model[model]} # Find root model (i.e., remove the auto-generated numbers from the name) while player.models[player.models[model]['parent']]['parent'] is not None: model = player.models[model]['parent'] entry['Model'] = model[len(player_name) + 1:] self.model_data.append(entry) if self.condition_dist: condition_dist = Distribution() for model, model_prob in player_model.items(): for condition, condition_prob in self.condition_dist[model_to_cluster(model)].items(): condition_dist.addProb(condition, model_prob*condition_prob) condition_dist.normalize() for condition, condition_prob in condition_dist.items(): self.condition_data.append({'Timestep': t, 'Belief': condition_prob, 'Condition': condition})
def next_victim(self, world): """ Generate an expectation about what room the player will enter next """ player = world.agents[self.parser.player_name()] action = world.getAction(player.name, unique=True) if action['verb'] == 'triage_Green': # Triaging green as we speak return Distribution({'Green': 1}) elif action['verb'] == 'triage_Gold': # Triaging yellow as we speak return Distribution({'Yellow': 1}) # Not so obvious who will be next agent = world.agents['ATOMIC'] beliefs = agent.getBelief() if len(beliefs) == 1: agent_model, agent_beliefs = next(iter(beliefs.items())) else: raise NotImplementedError( 'Unable to generate predictions unless agent has unique model') location = world.getState(player.name, 'loc', unique=True) prediction = None for player_model, player_model_prob in world.getModel( player.name, agent_beliefs).items(): player_beliefs = player.models[player_model]['beliefs'] fov = world.getState(player.name, 'vicInFOV', player_beliefs, unique=True) if fov in {'Yellow', 'Green'}: # The next victim found is the one the player is looking at now next_seen = Distribution({fov: 1}) else: # The next victim found is one in the player's current location next_seen = { 'Yellow': world.getState(WORLD, 'ctr_{}_Gold'.format(location), player_beliefs).expectation(), 'Green': world.getState(WORLD, 'ctr_{}_Green'.format(location), player_beliefs).expectation() } if sum(next_seen.values()) == 0: # No victim in the current room next_seen = {'Yellow': 1, 'Green': 1} next_seen = Distribution(next_seen) next_seen.normalize() if prediction is None: prediction = next_seen.scale_prob(player_model_prob) else: prediction = prediction.__class__({ color: prob + next_seen[color] * player_model_prob for color, prob in prediction.items() }) return prediction