def get_agent_data(config, trial_num): # loads config from agent dir agent_dir = join(pardir, get_agent_output_dir(config, agent_t, trial_num)) config_file = join(agent_dir, 'config.json') if not exists(config_file): return None, None config = EnvironmentConfiguration.load_json(config_file) # creates env helper helper = create_helper(config) # tries to load full analysis analyses_dir = get_analysis_output_dir(agent_dir) file_name = join(analyses_dir, 'full-analysis.json') analysis = None if exists(file_name): analysis = FullAnalysis.load_json(file_name) analysis.set_helper(helper) # tries to load all data stats_dir = join(agent_dir, 'results') if exists(stats_dir): helper.load_stats(stats_dir) else: helper = None return analysis, helper
def run_trial(args): # tries to get agent type agent_t = args.agent if agent_t == AgentType.Testing: # tries to load a pre-trained agent configuration file config, results_dir = load_agent_config(args.results, args.trial) else: # tries to load env config from provided file path config_file = args.config_file_path config = args.default_frogger_config if config_file is None or not exists(config_file) \ else EnvironmentConfiguration.load_json(config_file) # creates env helper helper = create_helper(config) # checks for provided output dir output_dir = args.output if args.output is not None else \ get_agent_output_dir(config, agent_t, args.trial) if not exists(output_dir): makedirs(output_dir) # saves / copies configs to file config.save_json(join(output_dir, 'config.json')) helper.save_state_features(join(output_dir, 'state_features.csv')) # register environment in Gym according to env config env_id = '{}-{}-v0'.format(config.gym_env_id, args.trial) helper.register_gym_environment(env_id, False, args.fps, args.show_score_bar) # create environment and monitor env = gym.make(env_id) config.num_episodes = args.num_episodes video_callable = video_schedule(config, args.record) env = Monitor(env, directory=output_dir, force=True, video_callable=video_callable) # adds reference to monitor to allow for gym environments to update video frames if video_callable(0): env.env.monitor = env # initialize seeds (one for the environment, another for the agent) env.seed(config.seed + args.trial) agent_rng = np.random.RandomState(config.seed + args.trial) # creates the agent agent, exploration_strategy = create_agent(helper, agent_t, agent_rng) # if testing, loads tables from file (some will be filled by the agent during the interaction) if agent_t == AgentType.Testing: agent.load(results_dir) # runs episodes behavior_tracker = BehaviorTracker(config.num_episodes) recorded_episodes = [] for e in range(config.num_episodes): # checks whether to activate video monitoring env.env.monitor = env if video_callable(e) else None # reset environment old_obs = env.reset() old_s = helper.get_state_from_observation(old_obs, 0, False) if args.verbose: print(f'Episode: {e}') # helper.update_stats_episode(e) exploration_strategy.update(e) # update for learning agent t = 0 done = False while not done: # select action a = agent.act(old_s) # observe transition obs, r, done, _ = env.step(a) s = helper.get_state_from_observation(obs, r, done) r = helper.get_reward(old_s, a, r, s, done) # update agent and stats agent.update(old_s, a, r, s) behavior_tracker.add_sample(old_s, a) helper.update_stats(e, t, old_obs, obs, old_s, a, r, s) old_s = s old_obs = obs t += 1 # adds to recorded episodes list if video_callable(e): recorded_episodes.append(e) # signals new episode to tracker behavior_tracker.new_episode() # writes results to files agent.save(output_dir) behavior_tracker.save(output_dir) write_table_csv(recorded_episodes, join(output_dir, 'rec_episodes.csv')) helper.save_stats(join(output_dir, 'results'), args.clear_results) print('\nResults of trial {} written to:\n\t\'{}\''.format( args.trial, output_dir)) env.close()
if event.type == pygame.QUIT: exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: advance_time = True elif event.key == pygame.K_s: advance_episode = True if __name__ == '__main__': # tries to get agent type agent_t = int(sys.argv[1]) if len(sys.argv) > 1 else DEF_AGENT_TYPE # tries to load agent from results dir agent_dir = sys.argv[2] if len(sys.argv) > 2 else get_agent_output_dir( DEFAULT_CONFIG, agent_t) if not exists(agent_dir): raise ValueError('Could not load agent from: {}'.format(agent_dir)) config_file = join(agent_dir, 'config.json') if not exists(config_file): raise ValueError('Configuration not found: {}'.format(config_file)) config = EnvironmentConfiguration.load_json(config_file) action_names = config.get_action_names() # creates env helper helper = create_helper(config, SOUND) feats_nbins = helper.get_features_bins() # loads the agent's behavior behavior_tracker = BehaviorTracker(0)
pressed = process_keys() if REAL_TIME: pressed.append(ACTION_NO_MOVE_KEY) for _a, action in enumerate(actions_keys): if action in pressed: return _a return -1 if __name__ == '__main__': # tries to get agent type agent_t = int(sys.argv[1]) if len(sys.argv) > 1 else AgentType.Manual # tries to load agent from results dir agent_dir = get_agent_output_dir(DEFAULT_CONFIG, agent_t) config_file = join(agent_dir, 'config.json') if agent_t == AgentType.Manual: # tries to load env config from given file config_file = sys.argv[1] if len(sys.argv) > 1 else None config = DEFAULT_CONFIG if config_file is None or not exists(config_file) \ else EnvironmentConfiguration.load_json(config_file) elif not exists(agent_dir): raise ValueError('Could not load agent from: {}'.format(agent_dir)) elif not exists(config_file): raise ValueError('Configuration not found: {}'.format(config_file)) else: config = EnvironmentConfiguration.load_json(config_file) # adds no-op if REAL_TIME and config.num_actions == 4:
def run_trial(args): # tries to get agent type agent_t = args.agent results_dir = '' if agent_t == AgentType.Testing: # tries to load config from provided results dir path results_dir = args.results if args.results is not None else \ get_agent_output_dir(DEFAULT_CONFIG, AgentType.Learning) config_file = join(results_dir, 'config.json') if not exists(results_dir) or not exists(config_file): raise ValueError('Could not load configuration from: {}.'.format(config_file)) config = EnvironmentConfiguration.load_json(config_file) # if testing, we want to force a seed different than training (diff. test environments) config.seed += 1 else: # tries to load env config from provided file path config_file = args.config config = DEFAULT_CONFIG if config_file is None or not exists(config_file) \ else EnvironmentConfiguration.load_json(config_file) # creates env helper helper = create_helper(config) # checks for provided output dir output_dir = args.output if args.output is not None else get_agent_output_dir(config, agent_t, args.trial) if not exists(output_dir): makedirs(output_dir) # saves / copies configs to file config.save_json(join(output_dir, 'config.json')) helper.save_state_features(join(output_dir, 'state_features.csv')) # register environment in Gym according to env config env_id = '{}-{}-v0'.format(config.gym_env_id, args.trial) helper.register_gym_environment(env_id, False, FPS, SHOW_SCORE_BAR) # create environment and monitor env = gym.make(env_id) # todo config.num_episodes = 100 video_callable = video_schedule(config, args.record) env = Monitor(env, directory=output_dir, force=True, video_callable=video_callable) # adds reference to monitor to allow for gym environments to update video frames if video_callable(0): env.env.monitor = env # initialize seeds (one for the environment, another for the agent) env.seed(config.seed + args.trial) agent_rng = np.random.RandomState(config.seed + args.trial) # creates the agent agent, exploration_strategy = create_agent(helper, agent_t, agent_rng) # if testing, loads tables from file (some will be filled by the agent during the interaction) if agent_t == AgentType.Testing: agent.load(results_dir, ) # runs episodes behavior_tracker = BehaviorTracker(config.num_episodes) recorded_episodes = [] for e in range(config.num_episodes): # checks whether to activate video monitoring env.env.monitor = env if video_callable(e) else None # reset environment old_obs = env.reset() old_s = helper.get_state_from_observation(old_obs, 0, False) if args.verbose: helper.update_stats_episode(e) exploration_strategy.update(e) t = 0 done = False while not done: # select action a = agent.act(old_s) # observe transition obs, r, done, _ = env.step(a) s = helper.get_state_from_observation(obs, r, done) r = helper.get_reward(old_s, a, r, s, done) # update agent and stats agent.update(old_s, a, r, s) behavior_tracker.add_sample(old_s, a) helper.update_stats(e, t, old_obs, obs, old_s, a, r, s) old_s = s old_obs = obs t += 1 # adds to recorded episodes list if video_callable(e): recorded_episodes.append(e) # signals new episode to tracker behavior_tracker.new_episode() # writes results to files agent.save(output_dir) behavior_tracker.save(output_dir) write_table_csv(recorded_episodes, join(output_dir, 'rec_episodes.csv')) helper.save_stats(join(output_dir, 'results'), CLEAR_RESULTS) print('\nResults of trial {} written to:\n\t\'{}\''.format(args.trial, output_dir)) env.close()
'--agent', help='agent type', type=int, default=DEF_AGENT_TYPE) parser.add_argument('-r', '--report', help='report type', type=int, default=DEF_REPORT_TYPE) args = parser.parse_args() agent_t = args.agent explanation_t = args.report # tries to load agent from results dir agent_dir = get_agent_output_dir(DEFAULT_CONFIG, agent_t) if not exists(agent_dir): raise ValueError('Could not load agent from: {}'.format(agent_dir)) config_file = join(agent_dir, 'config.json') if not exists(config_file): raise ValueError('Configuration not found: {}'.format(config_file)) config = EnvironmentConfiguration.load_json(config_file) # creates env helper helper = create_helper(config) # tries to load full analysis analyses_dir = get_analysis_output_dir(agent_dir) file_name = join(analyses_dir, 'full-analysis.json') if exists(file_name):
from os import makedirs from os.path import exists, join from interestingness_xrl.learning import get_features_from_index, read_table_csv from interestingness_xrl.learning.behavior_tracker import BehaviorTracker from interestingness_xrl.scenarios.configurations import EnvironmentConfiguration from interestingness_xrl.scenarios import DEFAULT_CONFIG, AgentType, get_agent_output_dir, create_helper, \ get_observations_output_dir AGENT_TYPE = AgentType.Learning # AGENT_TYPE = AgentType.Testing FPS = 5 if __name__ == '__main__': # tries to load agent from results dir agent_dir = sys.argv[1] if len(sys.argv) > 1 else get_agent_output_dir( DEFAULT_CONFIG, AGENT_TYPE) if not exists(agent_dir): raise ValueError('Could not load agent from: {}'.format(agent_dir)) config_file = join(agent_dir, 'config.json') if not exists(config_file): raise ValueError('Configuration not found: {}'.format(config_file)) config = EnvironmentConfiguration.load_json(config_file) # creates env helper helper = create_helper(config) # loads the agent's behavior behavior_tracker = BehaviorTracker(0) behavior_tracker.load(agent_dir)
parser.add_argument('-o', '--output', help='directory in which to store results') parser.add_argument('-r', '--results', help='directory from which to load results') parser.add_argument('-c', '--config', help='path to config file') parser.add_argument('-t', '--trial', help='trial number to run', type=int, default=DEF_TRIAL) parser.add_argument('-v', '--videos', help='record videos according to linear schedule', action='store_true') parser.add_argument('-p', '--print', help='print information to the console', action='store_true') args = parser.parse_args() # tries to get agent type agent_t = args.agent results_dir = '' if agent_t == AgentType.Testing: # tries to load config from provided results dir path results_dir = args.results if args.results is not None else \ get_agent_output_dir(DEFAULT_CONFIG, AgentType.Learning) config_file = join(results_dir, 'config.json') if not exists(results_dir) or not exists(config_file): raise ValueError('Could not load configuration from: {}.'.format(config_file)) config = EnvironmentConfiguration.load_json(config_file) # if testing, we want to force a seed different than training (diff. test environments) config.seed += 1 else: # tries to load env config from provided file path config_file = args.config config = DEFAULT_CONFIG if config_file is None or not exists(config_file) \ else EnvironmentConfiguration.load_json(config_file) # runs trial
if __name__ == '__main__': aspect_group = {} # tries to get agent type agent_t = int(sys.argv[1]) if len(sys.argv) > 1 else DEF_AGENT_TYPE # tries to load each analysis from results dir analyses = [] highlights_dirs = [] sequences_dirs = [] ag_names = [] for ag_name, config in DATA_ORGANIZER_CONFIGS.items(): agent_dir = join(pardir, get_agent_output_dir(config, agent_t)) config_file = join(agent_dir, 'config.json') if not exists(config_file): raise ValueError('Configuration not found: {}'.format(config_file)) config = EnvironmentConfiguration.load_json(config_file) # creates env helper helper = create_helper(config) # tries to load full analysis analyses_dir = get_analysis_output_dir(agent_dir) file_name = join(analyses_dir, 'full-analysis.json') if exists(file_name): analysis = FullAnalysis.load_json(file_name) else: raise ValueError(
analysis.save_json(join(results_dir, '{}.json'.format(name))) analysis.save_report(join(results_dir, '{}.txt'.format(name)), True) print('\n') if __name__ == '__main__': parser = argparse.ArgumentParser(description='RL interestingness analyzer') parser.add_argument('-a', '--agent', help='agent type', type=int, default=DEF_AGENT_TYPE) parser.add_argument('-o', '--output', help='directory in which to store results') parser.add_argument('-c', '--config', help='path to config file') args = parser.parse_args() # loads environment config from results dir agent_dir = get_agent_output_dir(DEFAULT_CONFIG, args.agent) if not exists(agent_dir): raise ValueError('Could not load agent from: {}'.format(agent_dir)) config_file = join(agent_dir, 'config.json') if not exists(config_file): raise ValueError('Configuration not found: {}'.format(config_file)) env_config = EnvironmentConfiguration.load_json(config_file) # creates env helper helper = create_helper(env_config) # tries to load analysis config from given file an_config = get_analysis_config(env_config) if args.config is not None and exists(args.config): an_config = AnalysisConfiguration.load_json(args.config)