Beispiel #1
0
 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
Beispiel #2
0
 def collapseProbabilistic(self):
     """
     Utility method that combines any consecutive probabilistic branches at this node into a single distribution
     """
     if self.isProbabilistic():
         collapse = False
         distribution = Distribution(self.children)
         for child in self.children.domain():
             if child.isProbabilistic():
                 # Probabilistic branch to merge
                 collapse = True
                 child.collapseProbabilistic()
                 del distribution[child]
                 for grandchild in child.children.domain():
                     try:
                         distribution[grandchild] += self.children[child]*child.children[grandchild]
                     except KeyError:
                         distribution[grandchild] = self.children[child]*child.children[grandchild]
         if collapse:
             assert sum(distribution.values()) == 1.
             self.makeProbabilistic(distribution)