def main(max_ts, ENV_NAME, test=True): backend = LightSimBackend() param = Parameters() param.init_from_dict({"NO_OVERFLOW_DISCONNECTION": True}) env_klu = make(ENV_NAME, backend=backend, param=param, test=test, data_feeding_kwargs={"gridvalueClass": GridStateFromFile}) agent = DoNothingAgent(action_space=env_klu.action_space) nb_ts_klu, time_klu, aor_klu, gen_p_klu, gen_q_klu = run_env(env_klu, max_ts, agent, chron_id=0) env_pp = make(ENV_NAME, param=param, test=test, data_feeding_kwargs={"gridvalueClass": GridStateFromFile}) agent = DoNothingAgent(action_space=env_pp.action_space) nb_ts_pp, time_pp, aor_pp, gen_p_pp, gen_q_pp = run_env(env_pp, max_ts, agent, chron_id=0) print_res(env_klu, env_pp, nb_ts_klu, nb_ts_pp, time_klu, time_pp, aor_klu, aor_pp, gen_p_klu, gen_p_pp, gen_q_klu, gen_q_pp)
def main(max_ts, name): backend = LightSimBackend() param = Parameters() param.init_from_dict({"NO_OVERFLOW_DISCONNECTION": True}) env_klu = make( name, backend=backend, param=param, gamerules_class=AlwaysLegal, test=True, data_feeding_kwargs={"gridvalueClass": GridStateFromFileWithForecasts}) agent = PowerLineSwitch(action_space=env_klu.action_space) nb_ts_klu, time_klu, aor_klu, gen_p_klu, gen_q_klu = run_env( env_klu, max_ts, agent, chron_id=0, keep_forecast=True) env_pp = make( name, param=param, gamerules_class=AlwaysLegal, test=True, data_feeding_kwargs={"gridvalueClass": GridStateFromFileWithForecasts}) agent = PowerLineSwitch(action_space=env_pp.action_space) nb_ts_pp, time_pp, aor_pp, gen_p_pp, gen_q_pp = run_env(env_pp, max_ts, agent, chron_id=0, keep_forecast=True) print_res(env_klu, env_pp, nb_ts_klu, nb_ts_pp, time_klu, time_pp, aor_klu, aor_pp, gen_p_klu, gen_p_pp, gen_q_klu, gen_q_pp)
def main(max_ts, name, test=True): backend = LightSimBackend() param = Parameters() param.init_from_dict({"NO_OVERFLOW_DISCONNECTION": True}) env_klu = make(name, backend=backend, test=test, param=param, gamerules_class=AlwaysLegal, data_feeding_kwargs={ "chunk_size": 128, "max_iter": max_ts, "gridvalueClass": GridStateFromFile }) agent = TestAgent(action_space=env_klu.action_space, env_name=name) nb_ts_klu, time_klu, aor_klu, gen_p_klu, gen_q_klu = run_env( env_klu, max_ts, agent, chron_id=0, keep_forecast=False) env_pp = make(name, param=param, gamerules_class=AlwaysLegal, test=test, data_feeding_kwargs={ "chunk_size": 128, "max_iter": max_ts, "gridvalueClass": GridStateFromFile }) agent = TestAgent(action_space=env_pp.action_space, env_name=name) nb_ts_pp, time_pp, aor_pp, gen_p_pp, gen_q_pp = run_env( env_pp, max_ts, agent, chron_id=0, keep_forecast=False) print_res(env_klu, env_pp, nb_ts_klu, nb_ts_pp, time_klu, time_pp, aor_klu, aor_pp, gen_p_klu, gen_p_pp, gen_q_klu, gen_q_pp)
def main(max_ts, name, use_lightsim=False): param = Parameters() if use_lightsim: if light_sim_avail: backend = LightSimBackend() else: raise RuntimeError("LightSimBackend not available") else: backend = PandaPowerBackend() param.init_from_dict({"NO_OVERFLOW_DISCONNECTION": True}) env_klu = make(name, backend=backend, param=param, gamerules_class=AlwaysLegal, test=True) agent = TestAgent(action_space=env_klu.action_space, env_name=name) cp = cProfile.Profile() cp.enable() nb_ts_klu, time_klu, aor_klu, gen_p_klu, gen_q_klu = run_env( env_klu, max_ts, agent) cp.disable() nm_f, ext = os.path.splitext(__file__) nm_out = "{}_{}_{}.prof".format(nm_f, "lightsim" if use_ls else "pp", name) cp.dump_stats(nm_out) print("You can view profiling results with:\n\tsnakeviz {}".format(nm_out))
def main(max_ts, ENV_NAME): backend = LightSimBackend() param = Parameters() param.init_from_dict({"NO_OVERFLOW_DISCONNECTION": True}) env_klu = make(ENV_NAME, backend=backend, param=param, test=True) agent = DoNothingAgent(action_space=env_klu.action_space) nb_ts_klu, time_klu, aor_klu, gen_p_klu, gen_q_klu = run_env(env_klu, max_ts, agent) env_pp = make(ENV_NAME, param=param, test=True) agent = DoNothingAgent(action_space=env_pp.action_space) nb_ts_pp, time_pp, aor_pp, gen_p_pp, gen_q_pp = run_env(env_pp, max_ts, agent) print_res(env_klu, env_pp, nb_ts_klu, nb_ts_pp, time_klu, time_pp, aor_klu, aor_pp, gen_p_klu, gen_p_pp, gen_q_klu, gen_q_pp )
def main(max_ts, name): backend = LightSimBackend() param = Parameters() param.init_from_dict({"NO_OVERFLOW_DISCONNECTION": True}) env_klu = make(name, backend=backend, param=param, gamerules_class=AlwaysLegal, test=True) agent = PowerLineSwitch(action_space=env_klu.action_space) nb_ts_klu, time_klu, aor_klu, gen_p_klu, gen_q_klu = run_env( env_klu, max_ts, agent) env_pp = make(name, param=param, gamerules_class=AlwaysLegal, test=True) agent = PowerLineSwitch(action_space=env_pp.action_space) nb_ts_pp, time_pp, aor_pp, gen_p_pp, gen_q_pp = run_env( env_pp, max_ts, agent) print_res(env_klu, env_pp, nb_ts_klu, nb_ts_pp, time_klu, time_pp, aor_klu, aor_pp, gen_p_klu, gen_p_pp, gen_q_klu, gen_q_pp)
def main(max_ts, ENV_NAME, test=True): param = Parameters() param.init_from_dict({"NO_OVERFLOW_DISCONNECTION": True}) env_pp = make(ENV_NAME, param=param, test=test, data_feeding_kwargs={"gridvalueClass": GridStateFromFile}) agent = DoNothingAgent(action_space=env_pp.action_space) nb_ts_pp, time_pp, aor_pp, gen_p_pp, gen_q_pp = run_env(env_pp, max_ts, agent, chron_id=0, env_seed=0) pp_time_pf = env_pp._time_powerflow wst = False # print extra info in the run_env function env_lightsim = make( ENV_NAME, backend=LightSimBackend(), param=param, test=test, data_feeding_kwargs={"gridvalueClass": GridStateFromFile}) li_tols = [10., 1., 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8, 1e-9] nb_ts = [] time = [] aor = [] gen_p = [] gen_q = [] comp_time = [] time_pf = [] for tol in li_tols: env_lightsim.backend.set_tol(tol) nb_ts_, time_, aor_, gen_p_, gen_q_ = run_env(env_lightsim, max_ts, agent, chron_id=0, with_type_solver=wst, env_seed=0) comp_time_ = env_lightsim.backend.comp_time time_pf_ = env_lightsim._time_powerflow nb_ts.append(nb_ts_) time.append(time_) aor.append(aor_) gen_p.append(gen_p_) gen_q.append(gen_q_) comp_time.append(comp_time_) time_pf.append(time_pf_) # NOW PRINT THE RESULTS env_name = get_env_name_displayed(ENV_NAME) hds = [ f"{env_name} ({nb_ts_pp} iter)", f"speed (it/s)", f"Δ aor (amps)", f"Δ gen_p (MW)", f"Δ gen_q (MVAr)" ] tab = [["PP", int(nb_ts_pp / time_pp), "0.00", "0.00", "0.00"]] for i, tol in enumerate(li_tols): if lightsim2grid.SolverType.GaussSeidel: tab.append([ f"{tol:.2e}", f"{int(nb_ts[i] / time[i])}", f"{np.max(np.abs(aor[i] - aor_pp)):.2e}", f"{np.max(np.abs(gen_p[i] - gen_p_pp)):.2e}", f"{np.max(np.abs(gen_q[i] - gen_q_pp)):.2e}" ]) res_tol = tabulate(tab, headers=hds, tablefmt="rst") print(res_tol)
def main(max_ts, ENV_NAME, test=True): param = Parameters() param.init_from_dict({"NO_OVERFLOW_DISCONNECTION": True}) env_pp = make(ENV_NAME, param=param, test=test, data_feeding_kwargs={"gridvalueClass": GridStateFromFile}) agent = DoNothingAgent(action_space=env_pp.action_space) nb_ts_pp, time_pp, aor_pp, gen_p_pp, gen_q_pp = run_env(env_pp, max_ts, agent, chron_id=0, env_seed=0) pp_time_pf = env_pp._time_powerflow wst = True # print extra info in the run_env function env_lightsim = make( ENV_NAME, backend=LightSimBackend(), param=param, test=test, data_feeding_kwargs={"gridvalueClass": GridStateFromFile}) solver_types = env_lightsim.backend.available_solvers if lightsim2grid.SolverType.KLU in solver_types: env_lightsim.backend.set_solver_type(lightsim2grid.SolverType.KLU) env_lightsim.backend.set_solver_max_iter(10) nb_ts_klu, time_klu, aor_klu, gen_p_klu, gen_q_klu = run_env( env_lightsim, max_ts, agent, chron_id=0, with_type_solver=wst, env_seed=0) klu_comp_time = env_lightsim.backend.comp_time klu_time_pf = env_lightsim._time_powerflow if lightsim2grid.SolverType.SparseLU in solver_types: env_lightsim.backend.set_solver_type(lightsim2grid.SolverType.SparseLU) env_lightsim.backend.set_solver_max_iter(10) nb_ts_slu, time_slu, aor_slu, gen_p_slu, gen_q_slu = run_env( env_lightsim, max_ts, agent, chron_id=0, with_type_solver=wst, env_seed=0) slu_comp_time = env_lightsim.backend.comp_time slu_time_pf = env_lightsim._time_powerflow if lightsim2grid.SolverType.GaussSeidel in solver_types: env_lightsim.backend.set_solver_type( lightsim2grid.SolverType.GaussSeidel) env_lightsim.backend.set_solver_max_iter(10000) nb_ts_gs, time_gs, aor_gs, gen_p_gs, gen_q_gs = run_env( env_lightsim, max_ts, agent, chron_id=0, with_type_solver=wst, env_seed=0) gs_comp_time = env_lightsim.backend.comp_time gs_time_pf = env_lightsim._time_powerflow # NOW PRINT THE RESULTS env_name = get_env_name_displayed(ENV_NAME) hds = [ f"{env_name}", f"grid2op speed (it/s)", f"grid2op powerflow time (ms)", f"solver powerflow time (ms)" ] tab = [[ "PP", int(nb_ts_pp / time_pp), f"{1000.*pp_time_pf/nb_ts_pp:.2e}", f"{1000.*pp_time_pf/nb_ts_pp:.2e}" ]] if lightsim2grid.SolverType.GaussSeidel: tab.append([ "LS+GS", int(nb_ts_gs / time_gs), f"{1000.*gs_time_pf/nb_ts_gs:.2e}", f"{1000.*gs_comp_time/nb_ts_gs:.2e}" ]) if lightsim2grid.SolverType.SparseLU: tab.append([ "LS+SLU", int(nb_ts_slu / time_slu), f"{1000.*slu_time_pf/nb_ts_slu:.2e}", f"{1000.*slu_comp_time/nb_ts_slu:.2e}" ]) if lightsim2grid.SolverType.KLU: tab.append([ "LS+KLU", int(nb_ts_klu / time_klu), f"{1000.*klu_time_pf/nb_ts_klu:.2e}", f"{1000.*klu_comp_time/nb_ts_klu:.2e}" ]) res_use_with_grid2op_1 = tabulate(tab, headers=hds, tablefmt="rst") print(res_use_with_grid2op_1) print() res_github_readme = tabulate(tab, headers=hds, tablefmt="github") print(res_github_readme) print() hds = [ f"{env_name} ({nb_ts_pp} iter)", f"Δ aor (amps)", f"Δ gen_p (MW)", f"Δ gen_q (MVAr)" ] tab = [["PP", "0.00", "0.00", "0.00"]] if lightsim2grid.SolverType.GaussSeidel: tab.append([ "LS+GS", f"{np.max(np.abs(aor_gs - aor_pp)):.2e}", f"{np.max(np.abs(gen_p_gs - gen_p_pp)):.2e}", f"{np.max(np.abs(gen_q_gs - gen_q_pp)):.2e}" ]) if lightsim2grid.SolverType.SparseLU: tab.append([ "LS+SLU", f"{np.max(np.abs(aor_slu - aor_pp)):.2e}", f"{np.max(np.abs(gen_p_slu - gen_p_pp)):.2e}", f"{np.max(np.abs(gen_q_slu - gen_q_pp)):.2e}" ]) if lightsim2grid.SolverType.KLU: tab.append([ "LS+KLU", f"{np.max(np.abs(aor_klu - aor_pp)):.2e}", f"{np.max(np.abs(gen_p_klu - gen_p_pp)):.2e}", f"{np.max(np.abs(gen_q_klu - gen_q_pp)):.2e}" ]) res_use_with_grid2op_2 = tabulate(tab, headers=hds, tablefmt="rst") print(res_use_with_grid2op_2)