Пример #1
0
    def __init__(self, files=[], maps=None, processor=None, rddl_file=None, action_file=None, feature_output=None, logger=logging):
        # Extract files to process
        self.files = accumulate_files(files)
        self.processor = processor
        self.logger = logger
        self.rddl_file = rddl_file
        self.action_file = action_file
        if self.action_file:
            Msg2ActionEntry.read_psysim_msg_conversion(self.action_file)
            self.msg_types = Msg2ActionEntry.get_msg_types()
        else:
            self.msg_types = None
        self.rddl_converter = None

        # Feature count bookkeeping
        self.derived_features = []
        self.feature_output = feature_output
        self.feature_data = []
        self.condition_fields = None

        # information for each log file # TODO maybe encapsulate in an object and send as arg in post_replay()?
        self.world = None
        self.triage_agent = None
        self.observer = None
        self.victims = None
        self.world_map = None
        self.map_table = None
        self.parser = None
        self.conditions = None
        self.file_name = None

        # Extract maps
        self.maps = get_default_maps(logger) if maps is None else maps
Пример #2
0
    def __init__(self,
                 files=[],
                 maps=None,
                 models=None,
                 ignore_models=None,
                 create_observer=True,
                 processor=None,
                 logger=logging):
        # Extract files to process
        self.files = accumulate_files(files)
        self.create_observer = create_observer
        self.processor = processor
        self.logger = logger

        # information for each log file # TODO maybe encapsulate in an object and send as arg in post_replay()?
        self.world = None
        self.triage_agent = None
        self.observer = None
        self.victims = None
        self.world_map = None
        self.map_table = None
        self.parser = None
        self.conditions = None
        self.file_name = None

        # Extract maps
        self.maps = get_default_maps(logger) if maps is None else maps

        # Set player models for observer agent
        if models is None:
            models = DEFAULT_MODELS
        if ignore_models is None:
            ignore_models = DEFAULT_IGNORE
        for dimension, entries in models.items():
            if dimension in ignore_models:
                first = True
                for key in list(entries.keys()):
                    if first:
                        first = False
                    else:
                        del entries[key]
        self.model_list = [{
            dimension: value[index]
            for index, dimension in enumerate(models)
        } for value in itertools.product(*models.values()) if len(value) > 0]
        self.models = models
Пример #3
0
    def __init__(self,
                 replays,
                 output=OUTPUT_DIR,
                 maps=None,
                 clear=True,
                 num_trajectories=NUM_TRAJECTORIES,
                 length=TRAJ_LENGTH,
                 normalize=NORM_THETA,
                 learn_rate=LEARNING_RATE,
                 epochs=MAX_EPOCHS,
                 diff=DIFF_THRESHOLD,
                 prune=PRUNE_THRESHOLD,
                 horizon=HORIZON,
                 seed=0,
                 verbosity=0,
                 processes=PROCESSES,
                 img_format=IMG_FORMAT):
        """
        Creates a new reward model learning replayer.
        :param list[str] replays: list of replay log files to process containing the player data.
        :param str output: path to the directory in which to save results.
        :param dict[str,dict[str,str]] maps: the map configuration dictionary.
        :param bool clear: whether to clear the output sub-directories.
        :param int num_trajectories: number of trajectories to use for reward learning.
        :param int length: length of the sub-trajectories used for reward learning.
        :param bool normalize: whether to normalize reward weights at each step of the algorithm.
        :param float learn_rate: the gradient descent learning/update rate.
        :param int epochs: the maximum number of gradient descent steps.
        :param float diff: the termination threshold for the weight vector difference.
        :param float prune: the likelihood below which stochastic outcomes are pruned.
        :param int horizon: planning horizon of the "learner" agent.
        :param int seed: seed for random number generation.
        :param int verbosity: verbosity level.
        :param int processes: the number of processes/cores to use. If `None`, all available cores will be used.
        :param str img_format: the format/extension of result images to be saved.
        """
        if maps is None:
            maps = get_default_maps()
        super().__init__(replays,
                         maps, {},
                         create_observer=False,
                         processor=TrajectoryParseProcessor())

        self._all_replays = replays
        self.output = output
        self.clear = clear
        self.num_trajectories = num_trajectories
        self.length = length
        self.normalize = normalize
        self.learn_rate = learn_rate
        self.epochs = epochs
        self.diff = diff
        self.prune = prune
        self.horizon = horizon
        self.seed = seed
        self.verbosity = verbosity
        self.processes = processes
        self.img_format = img_format

        self.results = {}
        self.trajectories = {}
        self.agent_names = {}
        self.map_tables = {}
        self.trial_conditions = {}

        self._output_dir = None
Пример #4
0
        raise ValueError('Invalid debug level: {}'.format(args['debug']))
    logging.basicConfig(level=level)
    # Look for reward file
    ignore = [dimension for dimension in DEFAULT_MODELS if args['ignore_{}'.format(dimension)]]
    mission_times = {}
    if args['metadata']:
        with open(args['metadata'], 'r') as log_file:
            for line in log_file:
                entry = json.loads(line)
                if 'mission_timer' in entry['data'] and entry['data']['mission_timer'] != 'Mission Timer not initialized.':
                    minutes, seconds = [int(value) for value in entry['data']['mission_timer'].split(':')]
                    timestamp = pytz.utc.localize(datetime.fromisoformat(entry['@timestamp'][:-1]))
                    mission_times[timestamp] = (minutes, seconds)
    if args['clusters']:
        import atomic.model_learning.linear.post_process.clustering as clustering

        reward_weights, cluster_map, condition_map = load_clusters(args['clusters'])
        AnalysisParser.condition_dist = condition_map
        apply_cluster_rewards({0: reward_weights[0]})
    elif args['reward_file']:
        import atomic.model_learning.linear.post_process.clustering as clustering

        apply_cluster_rewards(clustering.load_cluster_reward_weights(args['reward_file']))
    replayer = Analyzer(args['fname'], get_default_maps(logging), DEFAULT_MODELS, ignore, mission_times, logging)
    if args['profile']:
        cProfile.run('replayer.process_files(args["number"])', sort=1)
    elif args['1']:
        replayer.process_files(args['number'], replayer.files[0])
    else:
        replayer.process_files(args['number'])
Пример #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_STR: 1,
                                        GOLD_STR: 3
                                    }
                                }])
    world.save('world.psy' if len(sys.argv) == 1 else sys.argv[1])
Пример #6
0
              logging.FileHandler('hala.log', 'w')],
    format='%(message)s', level=logging.DEBUG)

#### Parse the data file into a sequence of actions and events
maxDist = 5
try:
    fname = os.path.join(os.path.dirname(__file__), '../data', sys.argv[1])
    parser = ProcessCSV(fname, maxDist, logging)
except IndexError:
    fname = os.path.join(os.path.dirname(__file__), '../data',
                         'processed_20200728_Participant3_Cond1.csv')
    parser = ProcessCSV(fname, maxDist, logging)

######### Get Map Data
mapName = 'FalconEasy'
DEFAULT_MAPS = get_default_maps()
SandRLocs = DEFAULT_MAPS[mapName].adjacency
SandRVics = DEFAULT_MAPS[mapName].victims

## Fabricate a light switch map that maps each room with a switch to a list of rooms affected by the switch
shared = {'lh':8, 'rh':9, 'mb':5, 'wb':5}
lightMap = {k:[k] for k in SandRLocs.keys() if sum([k.startswith(shr) for shr in shared.keys()]) == 0}
for shr,num in shared.items():
    lightMap[shr + '1'] = []
    for i in range(1,num+1):
        lightMap[shr + '1'].append(shr + str(i))


#use_unobserved=True, full_obs=False, logger=logging):
world, triageAgent, agent, victimsObj, world_map = make_single_player_world(
    parser.player_name(), None, SandRLocs, SandRVics, False, True)
Пример #7
0
        logging.info(
            'Parsing of {} files took a total of {:.3f}s (mean: {:.3f}s per file, {:.3f}s per step)'
            .format(len(replayer.timings), np.sum(times), np.mean(times),
                    np.mean(times / lengths)))

        # prints results
        files = sorted(replayer.timings.keys())
        times = [replayer.timings[filename] for filename in files]
        subject_ids = [replayer.subject_ids[filename] for filename in files]
        plot_bar(OrderedDict(zip(subject_ids, times)), 'Parsing Times',
                 os.path.join(args.output, 'parse-times.pdf'))
        plot_bar(OrderedDict(zip(subject_ids, lengths)), 'Trajectory Lengths',
                 os.path.join(args.output, 'parse-lengths.pdf'))

    # generate trajectories
    default_maps = get_default_maps()
    if args.trajectories == 0 or args.map_name not in default_maps:
        msg = 'Skipping generation benchmark. '
        if args.map_name not in default_maps:
            msg += 'Map name {} not in default maps.'.format(args.map_name)
        logging.info(msg)

    else:
        # create world, agent and observer
        map_table = default_maps[args.map_name]
        world, agent, observer, victims, world_map = make_single_player_world(
            PLAYER_NAME, map_table.init_loc, map_table.adjacency,
            map_table.victims, False, FULL_OBS, False)

        # agent params
        agent.setAttribute('rationality', args.rationality)
Пример #8
0
    mission_times = {}
    if args['metadata']:
        with open(args['metadata'], 'r') as log_file:
            for line in log_file:
                entry = json.loads(line)
                if 'mission_timer' in entry['data'] and entry['data'][
                        'mission_timer'] != 'Mission Timer not initialized.':
                    minutes, seconds = [
                        int(value)
                        for value in entry['data']['mission_timer'].split(':')
                    ]
                    timestamp = pytz.utc.localize(
                        datetime.fromisoformat(entry['@timestamp'][:-1]))
                    mission_times[timestamp] = (minutes, seconds)
    if args['clusters']:
        import atomic.model_learning.linear.post_process.clustering as clustering

        reward_weights, cluster_map, condition_map = load_clusters(
            args['clusters'])
        AnalysisParseProcessor.condition_dist = condition_map
        apply_cluster_rewards(reward_weights)
    elif args['reward_file']:
        import atomic.model_learning.linear.post_process.clustering as clustering

        apply_cluster_rewards(
            clustering.load_cluster_reward_weights(args['reward_file']))
    replayer = Analyzer(args['fname'], get_default_maps(logging),
                        DEFAULT_MODELS, ignore, mission_times, args['rddl'],
                        args['actions'], args['feature_file'], logging)
    replayer.parameterized_replay(args)
Пример #9
0
        raise ValueError('Unable to find a file for log {} in {}'.format(trial, log_dir))

def replay_parser():
    parser = ArgumentParser()
    parser.add_argument('fname', nargs='+',
                        help='Log file(s) (or directory of log files) to process')
    parser.add_argument('-1', '--1', action='store_true', help='Exit after the first run-through')
    parser.add_argument('-n', '--number', type=int, default=0,
                        help='Number of steps to replay (default is 0, meaning all)')
    parser.add_argument('-d', '--debug', default='WARNING', help='Level of logging detail')
    parser.add_argument('--profile', action='store_true', help='Run profiler')
    parser.add_argument('--rddl', help='Name of RDDL file containing domain specification')
    parser.add_argument('--actions', help='Name of CSV file containing JSON to PsychSim action mapping')
    parser.add_argument('--feature_file', help='Destination of feature count output')
    return parser

def parse_replay_args(parser):
    args = vars(parser.parse_args())
    # Extract logging level from command-line argument
    level = getattr(logging, args['debug'].upper(), None)
    if not isinstance(level, int):
        raise ValueError('Invalid debug level: {}'.format(args['debug']))
    logging.basicConfig(level=level)
    return args

if __name__ == '__main__':
    # Process command-line arguments
    args = parse_replay_args(replay_parser())
    replayer = Replayer(args['fname'], get_default_maps(logging), None, args['rddl'], args['actions'], args['feature_file'], logging)
    replayer.parameterized_replay(args)
Пример #10
0
from model_learning.trajectory import generate_trajectory
from model_learning.util.io import create_clear_dir, save_object, change_log_handler
from atomic.definitions.plotting import plot_trajectories, plot_agent_location_frequencies, \
    plot_agent_action_frequencies, plot_environment
from atomic.model_learning.linear.post_process.clustering import load_cluster_reward_weights
from atomic.model_learning.linear.rewards import create_reward_vector
from atomic.scenarios.single_player import make_single_player_world
from atomic.definitions.map_utils import get_default_maps

__author__ = 'Pedro Sequeira'
__email__ = '*****@*****.**'
__description__ = 'Loads the results of linear reward vector clustering and sets the agent with the weights of the ' \
                  'first cluster.'

CLUSTERS_FILE = 'data/rewards/linear/hackathon_cluster_weights.csv'
MAP_TABLE = get_default_maps()['sparky']
OUTPUT_DIR = 'output/linear_rwd_from_clusters'
DEBUG = False
FULL_OBS = True

# agents properties
AGENT_NAME = 'Player'
SELECTION = 'distribution'
RATIONALITY = 1 / 0.1  # inverse temperature
HORIZON = 2

NUM_STEPS = 100

if __name__ == '__main__':
    np.set_printoptions(precision=2, suppress=True)