Exemplo n.º 1
0
 def pre_replay(self, logger=logging):
     # Create PsychSim model
     logger.info('Creating world with "{}" map'.format(self.map_table.name))
     try:
         self.world, self.triage_agent, self.observer, self.victims, self.world_map = \
             make_single_player_world(self.parser.player_name(), self.map_table.init_loc,
                                      self.map_table.adjacency, self.map_table.victims, False, True, {},
                                      self.create_observer, logger.getChild('make_single_player_world'))
     except:
         logger.error(traceback.format_exc())
         logger.error('Unable to create world')
         return False
     # Last-minute filling in of models. Would do it earlier if we extracted triage_agent's name
     features = None
     self.model_list = [{dimension: value[index] for index, dimension in enumerate(self.models)}
                        for value in itertools.product(*self.models.values()) if len(value) > 0]
     for index, model in enumerate(self.model_list):
         if 'name' not in model:
             model['name'] = '{}_{}'.format(self.triage_agent.name,
                                            '_'.join([model[dimension] for dimension in self.models]))
             for dimension in self.models:
                 model[dimension] = self.models[dimension][model[dimension]]
                 if dimension == 'reward':
                     if not isinstance(model[dimension], dict):
                         if features is None:
                             import atomic.model_learning.linear.rewards as rewards
                             features = rewards.create_reward_vector(
                                 self.triage_agent, self.world_map.all_locations,
                                 self.world_map.moveActions[self.triage_agent.name])
                         model[dimension] = {feature: model[dimension][i] for i, feature in enumerate(features)}
     if len(self.model_list) > 0:
         set_player_models(self.world, self.observer.name, self.triage_agent.name, self.victims, self.model_list)
     #        self.parser.victimsObj = self.victims
     return True
def create_mental_models(world, agent, observer, victimsObj):
    model_list = [{'name': PREFER_NONE_MODEL, 'reward': {GREEN_VICTIM: MEAN_VAL, YELLOW_VICTIM: MEAN_VAL},
                   'rationality': MODEL_RATIONALITY, 'selection': MODEL_SELECTION},
                  {'name': PREFER_GREEN_MODEL, 'reward': {GREEN_VICTIM: HIGH_VAL, YELLOW_VICTIM: LOW_VAL},
                   'rationality': MODEL_RATIONALITY, 'selection': MODEL_SELECTION},
                  {'name': PREFER_YELLOW_MODEL, 'reward': {GREEN_VICTIM: LOW_VAL, YELLOW_VICTIM: HIGH_VAL},
                   'rationality': MODEL_RATIONALITY, 'selection': MODEL_SELECTION}]

    if INCLUDE_RANDOM_MODEL:
        model_list.append({'name': RANDOM_MODEL, 'reward': {GREEN_VICTIM: 0, YELLOW_VICTIM: 0,
                                                            'rationality': MODEL_RATIONALITY,
                                                            'selection': MODEL_SELECTION}})

    set_player_models(world, observer.name, agent.name, victimsObj, model_list)
    return [m['name'] for m in model_list]
Exemplo n.º 3
0
    triage_agent.resetBelief()
    triage_agent.omega = [
        key for key in world.state.keys() if not ((key in {
            modelKey(observer.name if observer is not None else ''),
            rewardKey(triage_agent.name)
        }) or (key.find('unobs') > -1))
    ]

    return world, triage_agent, observer, victims, world_map


if __name__ == '__main__':
    # Create a world using the simple map and save the file out (for use in generating a graphical visualization of the model)
    import sys
    import atomic.definitions.map_utils as utils
    import atomic.inference as inference

    map_data = utils.get_default_maps()['simple']
    world, triage_agent, observer, victims, world_map = make_single_player_world(
        'player', map_data.init_loc, map_data.adjacency, map_data.victims,
        False)
    inference.set_player_models(world, observer.name, triage_agent.name,
                                victims, [{
                                    'name': 'player0',
                                    'reward': {
                                        GREEN_STR: 1,
                                        GOLD_STR: 3
                                    }
                                }])
    world.save('world.psy' if len(sys.argv) == 1 else sys.argv[1])
Exemplo n.º 4
0
        'rationality': MODEL_RATIONALITY,
        'selection': MODEL_SELECTION
    }]

    if INCLUDE_RANDOM_MODEL:
        model_list.append({
            'name': RANDOM_MODEL,
            'reward': {
                GREEN_VICTIM: 0,
                YELLOW_VICTIM: 0,
                'rationality': MODEL_RATIONALITY,
                'selection': MODEL_SELECTION
            }
        })

    set_player_models(world, observer.name, agent.name, victims, model_list)

    # generates trajectory
    parser.getActionsAndEvents(victims, world_map, MAX_TRAJ_LENGTH)
    logging.info('Getting trajectory out of {} actions/events...'.format(
        len(parser.actions)))

    parser.runTimeless(world, 0, len(parser.actions), len(parser.actions),
                       PRUNE_THRESHOLD, True)
    logging.info('Recorded {} state-action pairs'.format(len(
        parser.trajectory)))

    # gets evolution of inference over reward models of the agent
    model_names = [m['name'] for m in model_list]
    probs = track_reward_model_inference(parser.trajectory, model_names, agent,
                                         observer,
Exemplo n.º 5
0
    triage_agent.resetBelief()
    triage_agent.omega = [
        key for key in world.state.keys() if not ((key in {
            modelKey(observer.name if observer is not None else ''),
            rewardKey(triage_agent.name)
        }) or (key.find('unobs') > -1))
    ]

    return world, triage_agent, observer, victims, world_map


if __name__ == '__main__':
    # Create a world using the simple map and save the file out (for use in generating a graphical visualization of the model)
    import sys
    import atomic.definitions.map_utils as utils
    import atomic.inference as inference

    map_data = utils.get_default_maps()['simple']
    world, triage_agent, observer, victims, world_map = make_single_player_world(
        'player', map_data.init_loc, map_data.adjacency, map_data.victims,
        False)
    inference.set_player_models(world, observer.name, triage_agent.name,
                                victims, [{
                                    'name': 'player0',
                                    'reward': {
                                        'Green': 1,
                                        'Gold': 3
                                    }
                                }])
    world.save('world.psy' if len(sys.argv) == 1 else sys.argv[1])