1515from grid2op .Agent import DoNothingAgent
1616from grid2op .Chronics import ChangeNothing
1717import re
18-
19- from lightsim2grid import solver
18+ from packaging import version
19+ import pandapower
20+ if version .parse (pandapower .__version__ ) > version .parse ("3.0.0" ):
21+ PP_ORIG_FILE = "pandapower_v3"
22+ else :
23+ PP_ORIG_FILE = "pandapower_v2"
24+
2025try :
2126 from grid2op .Chronics import GridStateFromFileWithForecastsWithoutMaintenance as GridStateFromFile
2227except ImportError :
3338from grid2op .Parameters import Parameters
3439import lightsim2grid
3540from lightsim2grid .lightSimBackend import LightSimBackend
36- from utils_benchmark import print_res , run_env , str2bool , get_env_name_displayed , print_configuration
41+ from utils_benchmark import run_env , str2bool , get_env_name_displayed , print_configuration
3742TABULATE_AVAIL = False
3843try :
3944 from tabulate import tabulate
@@ -107,6 +112,7 @@ def main(max_ts,
107112 save_results = DONT_SAVE ):
108113 param = Parameters ()
109114 param .init_from_dict ({"NO_OVERFLOW_DISCONNECTION" : True })
115+ aor_pp = None # needed in case the user does not want to compute results for pandapower
110116
111117 with warnings .catch_warnings ():
112118 warnings .filterwarnings ("ignore" )
@@ -121,7 +127,7 @@ def main(max_ts,
121127 env_pp_ls_numba = make (env_name_input , param = param , test = test ,
122128 backend = PandaPowerBackend (lightsim2grid = True , with_numba = True ),
123129 data_feeding_kwargs = {"gridvalueClass" : GridStateFromFile })
124- env_lightsim = make (env_name_input , backend = LightSimBackend (), param = param , test = test ,
130+ env_lightsim = make (env_name_input , backend = LightSimBackend (loader_kwargs = { "pp_orig_file" : PP_ORIG_FILE } ), param = param , test = test ,
125131 data_feeding_kwargs = {"gridvalueClass" : GridStateFromFile })
126132 if pypowbk_error is None :
127133 env_pypow = make (env_name_input , param = param , test = test ,
@@ -150,7 +156,7 @@ def main(max_ts,
150156 grid_path = env_name_input ,
151157 backend = PyPowSyBlBackend ())
152158 env_lightsim = make ("blank" , param = param , test = True ,
153- backend = LightSimBackend (),
159+ backend = LightSimBackend (loader_kwargs = { "pp_orig_file" : PP_ORIG_FILE } ),
154160 data_feeding_kwargs = {"gridvalueClass" : ChangeNothing },
155161 grid_path = env_name_input )
156162 _ , env_name_input = os .path .split (env_name_input )
@@ -192,7 +198,6 @@ def main(max_ts,
192198
193199 wst = True # print extra info in the run_env function
194200 solver_types = env_lightsim .backend .available_solvers
195-
196201 for solver_type in solver_types :
197202 if solver_type not in solver_names :
198203 continue
@@ -234,7 +239,7 @@ def main(max_ts,
234239 this_order = [el for el in res_times .keys () if el not in order_solver_print ] + order_solver_print
235240
236241 env_name = get_env_name_displayed (env_name_input )
237- hds = [f"{ env_name } " , f "grid2op speed (it/s)" , f "grid2op 'backend.runpf' time (ms)" , f "time in 'algo' (ms / pf)" ]
242+ hds = [f"{ env_name } " , "grid2op speed (it/s)" , "grid2op 'backend.runpf' time (ms)" , "time in 'algo' (ms / pf)" ]
238243 tab = []
239244 if no_pp is False :
240245 tab .append (["PP" , f"{ nb_ts_pp / time_pp :.2e} " ,
@@ -278,28 +283,30 @@ def main(max_ts,
278283 print (tab )
279284 print ()
280285
281- hds = [f"{ env_name } ({ nb_ts_pp } iter)" , f"Δ aor (amps)" , f"Δ gen_p (MW)" , f"Δ gen_q (MVAr)" ]
282- if no_pp is False :
283- tab = [["PP (ref)" , "0.00" , "0.00" , "0.00" ]]
284-
285- for key in this_order :
286- if key not in res_times :
287- continue
288- solver_name , nb_ts_gs , time_gs , aor_gs , gen_p_gs , gen_q_gs , gs_comp_time , gs_time_pf = res_times [key ]
289- tab .append ([solver_name ,
290- f"{ np .max (np .abs (aor_gs - aor_pp )):.2e} " ,
291- f"{ np .max (np .abs (gen_p_gs - gen_p_pp )):.2e} " ,
292- f"{ np .max (np .abs (gen_q_gs - gen_q_pp )):.2e} " ])
286+ if aor_pp is not None :
287+ nb_ts_this_table = res_times [solver_types [0 ]][1 ]
288+ hds = [f"{ env_name } ({ nb_ts_this_table } iter)" , "Δ aor (amps)" , "Δ gen_p (MW)" , "Δ gen_q (MVAr)" ]
289+ if no_pp is False :
290+ tab = [["PP (ref)" , "0.00" , "0.00" , "0.00" ]]
291+
292+ for key in this_order :
293+ if key not in res_times :
294+ continue
295+ solver_name , nb_ts_gs , time_gs , aor_gs , gen_p_gs , gen_q_gs , gs_comp_time , gs_time_pf = res_times [key ]
296+ tab .append ([solver_name ,
297+ f"{ np .max (np .abs (aor_gs - aor_pp )):.2e} " ,
298+ f"{ np .max (np .abs (gen_p_gs - gen_p_pp )):.2e} " ,
299+ f"{ np .max (np .abs (gen_q_gs - gen_q_pp )):.2e} " ])
293300
294- if TABULATE_AVAIL :
295- res_use_with_grid2op_2 = tabulate (tab , headers = hds , tablefmt = "rst" )
296- print (res_use_with_grid2op_2 )
297- else :
298- print (tab )
299-
300- if save_results != DONT_SAVE :
301- dt = pd .DataFrame (tab , columns = hds )
302- dt .to_csv (save_results + "diff.csv" , index = False , header = True , sep = ";" )
301+ if TABULATE_AVAIL :
302+ res_use_with_grid2op_2 = tabulate (tab , headers = hds , tablefmt = "rst" )
303+ print (res_use_with_grid2op_2 )
304+ else :
305+ print (tab )
306+
307+ if save_results != DONT_SAVE :
308+ dt = pd .DataFrame (tab , columns = hds )
309+ dt .to_csv (save_results + "diff.csv" , index = False , header = True , sep = ";" )
303310 print ()
304311
305312
0 commit comments