Skip to content

Commit

Permalink
correcting a bug to pass chronic_id and not chhronic name when runnin…
Browse files Browse the repository at this point in the history
…g rune_one_episode
  • Loading branch information
marota committed Dec 21, 2021
1 parent ef4c6df commit c1bd168
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 31 deletions.
6 changes: 3 additions & 3 deletions oracle4grid/core/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
17 changes: 9 additions & 8 deletions oracle4grid/core/replay/agent_replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
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')
print("============== 6 - Replay OracleAgent on best path with real condition game rules ==============")
# 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)

Expand All @@ -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,
Expand All @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions oracle4grid/core/utils/launch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
"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)
atomic_actions = parser.parse()

# 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:
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion oracle4grid/core/utils/prepare_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions oracle4grid/test/graph_validity_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand All @@ -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])
Expand Down
20 changes: 10 additions & 10 deletions oracle4grid/test/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand All @@ -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']

Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion oracle4grid/test/performance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c1bd168

Please sign in to comment.