def reproduce_from_exp_file(exp_name, results_dir="results", open_plot=True): ''' Args: exp_name (str) results_dir (str) open_plot (bool) Summary: Extracts the agents, MDP, and parameters from the file and runs the experiment. Stores data in "results_dir/exp_name/reproduce_i/*", where "i" is determined based on the existence of earlier "reproduce" files. ''' # Get dir and file. exp_dir = os.path.join(results_dir, exp_name) exp_file = Experiment.FULL_EXP_FILE_NAME full_exp_file = os.path.join(exp_dir, exp_file) # Check to make sure the file exists. if not os.path.exists(full_exp_file): raise ImportError("(simple_rl): no such experiment: " + str(full_exp_file) + ".") # Open the file. import json from simple_rl.utils.additional_datastructures import TupleEncoder json_data_file = open(full_exp_file, "r") all_exp_info = json.load(json_data_file, object_hook=TupleEncoder.hinted_tuple_hook) json_data_file.close() # Placeholders. experiment_param_dict = {} actions = [] experiment_func = None # Make MDP. full_mdp_class_str = all_exp_info["MDP"]["name"] mdp_class_str = full_mdp_class_str[full_mdp_class_str.find("'") + 1:full_mdp_class_str.rfind("'")].split( ".")[-1] mdp_param_dict = all_exp_info["MDP"]["params"] MDPClass = globals()[mdp_class_str] mdp = MDPClass(**mdp_param_dict) # Make Agents. agents = [] for full_agent_class_str in all_exp_info["AGENTS"].keys(): # Convert full str into class name. agent_class_str = full_agent_class_str[full_agent_class_str.find("'") + 1:full_agent_class_str. rfind("'")].split(".")[-1] # Get class and make agent. agent_param_dict = all_exp_info["AGENTS"][full_agent_class_str][ "params"] agent_index = all_exp_info["AGENTS"][full_agent_class_str]["index"] AgentClass = globals()[agent_class_str] # Create agent. agent_param_dict["actions"] = mdp.get_actions() agent = AgentClass(**agent_param_dict) agents.insert(agent_index, agent) # Experiment parameters. experiment_param_dict = all_exp_info["MISC"] # Function. experiment_func = eval(all_exp_info["FUNC"]) # Prints. print("\n" + "%" * 17) print("%" * 2, "Reproducing", "%" * 2) print("%" * 17, "\n") print("MDP:", "\n " + str(mdp) + "\n") print("Agents:") for a in agents: print(" ", a) print("\n" + "%" * 17) print("%" * 17, "\n") # Reproduce. chart_utils.CUSTOM_TITLE = "Reproduction: " + chart_utils._format_title( str(mdp)) experiment_func(agents, mdp, dir_for_plot=results_dir, experiment_name_prefix="reproduce_", open_plot=open_plot, **experiment_param_dict) print("\n" + "%" * 22) print("%" * 2, "Done Reproducing", "%" * 2) print("%" * 22, "\n")
def reproduce_from_exp_file(exp_name, results_dir="results", open_plot=True): ''' Args: exp_name (str) results_dir (str) open_plot (bool) Summary: Extracts the agents, MDP, and parameters from the file and runs the experiment. Stores data in "results_dir/exp_name/reproduce_i/*", where "i" is determined based on the existence of earlier "reproduce" files. ''' # Get dir and file. exp_dir = os.path.join(results_dir, exp_name) exp_file = Experiment.FULL_EXP_FILE_NAME full_exp_file = os.path.join(exp_dir, exp_file) # Check to make sure the file exists. if not os.path.exists(full_exp_file): raise ImportError("(simple_rl): no such experiment: " + str(full_exp_file) + ".") # Open the file. import json from simple_rl.utils.additional_datastructures import TupleEncoder json_data_file = open(full_exp_file, "r") all_exp_info = json.load(json_data_file, object_hook=TupleEncoder.hinted_tuple_hook) json_data_file.close() # Placeholders. experiment_param_dict = {} actions = [] experiment_func = None # Make MDP. full_mdp_class_str = all_exp_info["MDP"]["name"] mdp_class_str = full_mdp_class_str[full_mdp_class_str.find("'") + 1 : full_mdp_class_str.rfind("'")].split(".")[-1] mdp_param_dict = all_exp_info["MDP"]["params"] MDPClass = globals()[mdp_class_str] mdp = MDPClass(**mdp_param_dict) # Make Agents. agents = [] for full_agent_class_str in all_exp_info["AGENTS"].keys(): # Convert full str into class name. agent_class_str = full_agent_class_str[full_agent_class_str.find("'") + 1 : full_agent_class_str.rfind("'")].split(".")[-1] # Get class and make agent. agent_param_dict = all_exp_info["AGENTS"][full_agent_class_str]["params"] agent_index = all_exp_info["AGENTS"][full_agent_class_str]["index"] AgentClass = globals()[agent_class_str] # Create agent. agent_param_dict["actions"] = mdp.get_actions() agent = AgentClass(**agent_param_dict) agents.insert(agent_index, agent) # Experiment parameters. experiment_param_dict = all_exp_info["MISC"] # Function. experiment_func = eval(all_exp_info["FUNC"]) # Prints. print("\n" + "%"*17) print("%"*2, "Reproducing", "%"*2) print("%"*17, "\n") print("MDP:", "\n " + str(mdp) + "\n") print("Agents:") for a in agents: print(" ", a) print("\n" + "%"*17) print("%"*17, "\n") # Reproduce. chart_utils.CUSTOM_TITLE = "Reproduction: " + chart_utils._format_title(str(mdp)) experiment_func(agents, mdp, dir_for_plot=results_dir, experiment_name_prefix="reproduce_", open_plot=open_plot, **experiment_param_dict) print("\n" + "%"*22) print("%"*2, "Done Reproducing", "%"*2) print("%"*22, "\n")