diff --git a/oracle4grid/core/oracle.py b/oracle4grid/core/oracle.py index a55b54d..ba11d03 100644 --- a/oracle4grid/core/oracle.py +++ b/oracle4grid/core/oracle.py @@ -12,7 +12,7 @@ def oracle(atomic_actions, env, debug, config, debug_directory=None,agent_seed=None,env_seed=None, - reward_significant_digit=None, grid_path=None, chronic_id=None, constants=EnvConstants()): + reward_significant_digit=None, grid_path=None, chronic_scenario=None, constants=EnvConstants()): # 0 - Preparation : Get initial topo and line status # init_topo_vect, init_line_status = get_initial_configuration(env) @@ -94,12 +94,12 @@ def oracle(atomic_actions, env, debug, config, debug_directory=None,agent_seed=N # 6 - Replay of best path in real game rules condition replay_results = agent_replay.replay(grid2op_action_path, int(config[MAX_ITER]), - kpis, grid_path, chronic_id, debug=debug, constants=constants, + kpis, grid_path, chronic_scenario, debug=debug, constants=constants, env_seed=env_seed, agent_seed=agent_seed, rel_tol=float(config[REL_TOL]), path_logs=debug_directory,oracle_action_path=best_path) if(len(grid2op_action_path_no_overload)>=1): replay_results_no_overload = agent_replay.replay(grid2op_action_path_no_overload, int(config[MAX_ITER]), - kpis, grid_path, chronic_id, debug=debug, constants=constants, + kpis, grid_path, chronic_scenario, debug=debug, constants=constants, env_seed=env_seed, agent_seed=agent_seed, rel_tol=float(config[REL_TOL]), path_logs=debug_directory,logs_file_name_extension="no_overload",oracle_action_path=best_path_no_overload) diff --git a/oracle4grid/core/replay/agent_replay.py b/oracle4grid/core/replay/agent_replay.py index 77b86c5..82d7c4e 100644 --- a/oracle4grid/core/replay/agent_replay.py +++ b/oracle4grid/core/replay/agent_replay.py @@ -11,7 +11,7 @@ from oracle4grid.core.utils.prepare_environment import prepare_env, get_initial_configuration def replay(action_path: list, max_iter: int, - kpis, grid_path, chronic_id, debug = False, constants=EnvConstants(), env_seed = None, agent_seed = None, + kpis, grid_path, chronic_scenario, debug = False, constants=EnvConstants(), env_seed = None, agent_seed = None, rel_tol = 1e7, path_logs = None, logs_file_name_extension=None,oracle_action_path=None): if debug: print('\n') @@ -19,7 +19,7 @@ def replay(action_path: list, max_iter: int, # Environment settings for replay param = Parameters() param.init_from_dict(constants.DICT_GAME_PARAMETERS_REPLAY) - env = prepare_env(grid_path, chronic_id, param, constants=constants) + env, chronic_id = prepare_env(grid_path, chronic_scenario, param, constants=constants) init_topo_vect, init_line_status = get_initial_configuration(env) env.set_id(chronic_id) @@ -41,12 +41,13 @@ def replay(action_path: list, max_iter: int, if env_seed is not None: env_seed=[env_seed] runner = Runner(**env.get_params_for_runner(), agentClass=None,agentInstance=agent) - name_chron, agent_reward, nb_time_step, episode_data =runner.run_one_episode(path_save=path_logs, - indx=chronic_id, - env_seeds=env_seed, # ENV_SEEDS, - agent_seeds=agent_seed, # AGENT_SEEDS, - max_iter=max_iter, + + name_chron, agent_reward, nb_time_step, episode_data =runner.run_one_episode(indx=chronic_id, + path_save=path_logs, pbar=True, + env_seed=env_seed, # ENV_SEEDS, + max_iter=max_iter, + agent_seed=agent_seed, # AGENT_SEEDS, detailed_output=True) #res = runner.run(nb_episode=1, # nb_process=1, @@ -65,7 +66,7 @@ def replay(action_path: list, max_iter: int, elif nb_time_step==max_iter: # if pas de game over print("Expected reward of "+str(expected_reward)+" has been correctly obtained in replay conditions") else: - warnings.warn("During replay - oracle agent has game over before max iter (timestep " + str(t) + ")") + warnings.warn("During replay - oracle agent has game over before max iter (timestep " + str(nb_time_step) + ")") return nb_time_step def extract_expected_reward(kpis): diff --git a/oracle4grid/core/utils/launch_utils.py b/oracle4grid/core/utils/launch_utils.py index eacc82c..4b4e563 100644 --- a/oracle4grid/core/utils/launch_utils.py +++ b/oracle4grid/core/utils/launch_utils.py @@ -15,7 +15,7 @@ "load":"loads_id_bus"} def load_and_run(env_dir, chronic, action_file, debug,agent_seed,env_seed, config, constants=EnvConstants()): - atomic_actions, env, debug_directory = load(env_dir, chronic, action_file, debug, constants=constants, config = config) + atomic_actions, env, debug_directory, chronic_id = load(env_dir, chronic, action_file, debug, constants=constants, config = config) # Parse atomic_actions format # atomic_actions = parse(atomic_actions,env) parser = OracleParser(atomic_actions, env.action_space) @@ -23,13 +23,13 @@ def load_and_run(env_dir, chronic, action_file, debug,agent_seed,env_seed, confi # Run all steps return oracle(atomic_actions, env, debug, config, debug_directory=debug_directory,agent_seed=agent_seed,env_seed=env_seed, - grid_path=env_dir, chronic_id=chronic, constants=constants) + grid_path=env_dir, chronic_scenario=chronic, constants=constants) def load(env_dir, chronic, action_file, debug, constants=EnvConstants(), config = None, opponent_allowed=True): param = Parameters() param.init_from_dict(constants.DICT_GAME_PARAMETERS_SIMULATION) - env = prepare_env(env_dir, chronic, param, opponent_allowed=opponent_allowed) + env, chronic_id = prepare_env(env_dir, chronic, param, opponent_allowed=opponent_allowed) # Load unitary actions with open(action_file) as f: @@ -44,7 +44,7 @@ def load(env_dir, chronic, action_file, debug, constants=EnvConstants(), config debug_directory = init_debug_directory(env_dir, action_file, chronic, output_path) else: debug_directory = None - return atomic_actions, env, debug_directory + return atomic_actions, env, debug_directory, chronic_id def init_debug_directory(env_dir, action_file, chronic, output_path = None): diff --git a/oracle4grid/core/utils/prepare_environment.py b/oracle4grid/core/utils/prepare_environment.py index c74706e..5e69855 100644 --- a/oracle4grid/core/utils/prepare_environment.py +++ b/oracle4grid/core/utils/prepare_environment.py @@ -64,7 +64,7 @@ def prepare_env(env_path, chronic_scenario, param, constants=EnvConstants(), opp env.reset() else: # if name not found raise ValueError("Chronic scenario name: " + str(chronic_scenario) + " not found in folder") - return env + return env,found_id def search_chronic_name_from_num(num, env): diff --git a/oracle4grid/test/graph_validity_test.py b/oracle4grid/test/graph_validity_test.py index a02caa5..48de818 100644 --- a/oracle4grid/test/graph_validity_test.py +++ b/oracle4grid/test/graph_validity_test.py @@ -57,7 +57,7 @@ def init_test_env(): file = "./oracle4grid/ressources/actions/neurips_track1/ExpertActions_Track1_action_list_score4_reduite.json" chronic = "000" env_dir = "./data/l2rpn_neurips_2020_track1" - atomic_actions, env, debug_directory = load(env_dir, chronic, file, False, constants=EnvConstantsTest()) + atomic_actions, env, debug_directory, chronic_id = load(env_dir, chronic, file, False, constants=EnvConstantsTest()) parser = OracleParser(atomic_actions, env.action_space) atomic_actions = parser.parse() @@ -86,7 +86,7 @@ def test_fast_forward(self): file = "./oracle4grid/ressources/actions/neurips_track1/ExpertActions_Track1_action_list_score4_reduite.json" chronic = "000" env_dir = "./data/l2rpn_neurips_2020_track1" - atomic_actions, env, debug_directory = load(env_dir, chronic, file, False, constants=EnvConstantsTest()) + atomic_actions, env, debug_directory,chronic_id = load(env_dir, chronic, file, False, constants=EnvConstantsTest()) parser = OracleParser(atomic_actions, env.action_space) atomic_actions = parser.parse() env.chronics_handler.tell_id(0) @@ -171,7 +171,7 @@ def test_multiverse_with_initial_computation(self): file = "./oracle4grid/ressources/actions/neurips_track1/ExpertActions_Track1_action_list_score4_reduite.json" chronic = "000" env_dir = "./data/l2rpn_neurips_2020_track1" - atomic_actions, env, debug_directory = load(env_dir, chronic, file, False, constants=EnvConstantsTest()) + atomic_actions, env, debug_directory, chronic_id = load(env_dir, chronic, file, False, constants=EnvConstantsTest()) parser = OracleParser(atomic_actions, env.action_space) atomic_actions = parser.parse() @@ -185,7 +185,7 @@ def test_multiverse_with_initial_computation(self): df_runner = make_df_from_res([run_runner], False) windows = get_windows_from_df(df_runner) # multiverse needs a different environment - atomic_actions, env, debug_directory = load(env_dir, chronic, file, False, constants=EnvConstantsTest(), opponent_allowed=False) + atomic_actions, env, debug_directory, chronic_id = load(env_dir, chronic, file, False, constants=EnvConstantsTest(), opponent_allowed=False) #compute same action with multiverse for window in windows: begin = int(window.split("_")[0]) diff --git a/oracle4grid/test/integration_test.py b/oracle4grid/test/integration_test.py index 86a1d16..6d31253 100644 --- a/oracle4grid/test/integration_test.py +++ b/oracle4grid/test/integration_test.py @@ -105,7 +105,7 @@ def test_agent_rewards(self): constants = EnvConstantsTest() param = Parameters() param.init_from_dict(constants.DICT_GAME_PARAMETERS_GRAPH) - env = prepare_env(env_dir, chronic, param, constants) + env, chronic_id = prepare_env(env_dir, chronic, param, constants) env.set_id(chronic) obs = env.reset() agent = OracleAgent(action_path=grid2op_action_path, action_space=env.action_space, @@ -133,7 +133,7 @@ def test_agent_reco(self): constants = EnvConstantsTest() param = Parameters() param.init_from_dict(constants.DICT_GAME_PARAMETERS_GRAPH) - env = prepare_env(env_dir, chronic, param, constants) + env, chronic_id = prepare_env(env_dir, chronic, param, constants) env.set_id(chronic) obs = env.reset() @@ -174,7 +174,7 @@ def test_actions_combination(self): file = "./oracle4grid/ressources/actions/rte_case14_realistic/test_unitary_actions.json" chronic = "000" env_dir = "./data/rte_case14_realistic" - atomic_actions, env, debug_directory = load(env_dir, chronic, file, False, constants=EnvConstantsTest()) + atomic_actions, env, debug_directory, chronic_id = load(env_dir, chronic, file, False, constants=EnvConstantsTest()) # 1 - Action generation step actions = combinator.generate(atomic_actions, int(CONFIG[MAX_DEPTH]), env, False) @@ -193,7 +193,7 @@ def test_reward_df(self): file = "./oracle4grid/ressources/actions/rte_case14_realistic/test_unitary_actions.json" chronic = "000" env_dir = "./data/rte_case14_realistic" - atomic_actions, env, debug_directory = load(env_dir, chronic, file, False, constants=EnvConstantsTest()) + atomic_actions, env, debug_directory, chronic_id = load(env_dir, chronic, file, False, constants=EnvConstantsTest()) cols_to_check = ['action', 'timestep', 'reward', 'overload_reward', 'attack_id'] @@ -245,7 +245,7 @@ def test_parsing1(self): # Load env param = Parameters() param.init_from_dict(EnvConstantsTest().DICT_GAME_PARAMETERS_SIMULATION) - env = prepare_env(env_dir, chronic, param) + env, chronic_id = prepare_env(env_dir, chronic, param) # Read and convert action with open(file_json) as json_file: @@ -267,7 +267,7 @@ def test_parsing2(self): # Load env param = Parameters() param.init_from_dict(EnvConstantsTest().DICT_GAME_PARAMETERS_SIMULATION) - env = prepare_env(env_dir, chronic, param) + env, chronic_id = prepare_env(env_dir, chronic, param) # Read expected format with open("./oracle4grid/test_resourses/expected_actions_format2.json") as json_file_expected: expected_format = json.load(json_file_expected) @@ -291,7 +291,7 @@ def test_actions_impact(self): chronic = 0 env_dir = "./data/wcci_test" - atomic_actions_original, env, debug_directory = load(env_dir, chronic, file_json, debug=False, constants=EnvConstantsTest()) + atomic_actions_original, env, debug_directory, chronic_id = load(env_dir, chronic, file_json, debug=False, constants=EnvConstantsTest()) parser = OracleParser(atomic_actions_original, env.action_space) atomic_actions = parser.parse() @@ -328,7 +328,7 @@ def test_cancelling_action(self): file_json = "./oracle4grid/test_resourses/test_unitary_actions_cancelling.json" # Compute a fake Oracle action path and Oracle Agent - atomic_actions_original, env, debug_directory = load(env_dir, chronic, file_json, debug=False, + atomic_actions_original, env, debug_directory, chronic_id = load(env_dir, chronic, file_json, debug=False, constants=EnvConstantsTest()) obs = env.reset() init_topo_vect, init_line_status = get_initial_configuration(env) @@ -379,7 +379,7 @@ def test_cancelling_action_sub1(self): # Compute a fake Oracle action path and Oracle Agent - atomic_actions_original, env, debug_directory = load(env_dir, chronic, file_json, debug=False, + atomic_actions_original, env, debug_directory, chronic_id = load(env_dir, chronic, file_json, debug=False, constants=EnvConstantsTest()) init_topo_vect, init_line_status = get_initial_configuration(env) obs = env.reset() @@ -422,7 +422,7 @@ def test_run_oracleagent_randompath(self): max_timestep = 100 # Compute a fake Oracle action path and Oracle Agent - atomic_actions_original, env, debug_directory = load(env_dir, chronic, file_json, debug=False, + atomic_actions_original, env, debug_directory, chronic_id = load(env_dir, chronic, file_json, debug=False, constants=EnvConstantsTest()) init_topo_vect, init_line_status = get_initial_configuration(env) obs = env.reset() diff --git a/oracle4grid/test/performance_test.py b/oracle4grid/test/performance_test.py index cca8619..444a66f 100644 --- a/oracle4grid/test/performance_test.py +++ b/oracle4grid/test/performance_test.py @@ -63,7 +63,7 @@ def test_graph_duration(self): file = "./oracle4grid/ressources/actions/rte_case14_realistic/test_unitary_actions.json" chronic = "000" env_dir = "./data/rte_case14_realistic" - atomic_actions, env, debug_directory = load(env_dir, chronic, file, False, constants=EnvConstantsTest()) + atomic_actions, env, debug_directory, chronic_id = load(env_dir, chronic, file, False, constants=EnvConstantsTest()) parser = OracleParser(atomic_actions, env.action_space) atomic_actions = parser.parse() # 0 - Preparation : Get initial topo and line status