Skip to content

Commit

Permalink
Merge pull request Grid2op#61 from BDonnot/bd-dev
Browse files Browse the repository at this point in the history
Fix an issue when environment got opponent (when generating new data)
  • Loading branch information
marota authored May 31, 2022
2 parents 2c19f7e + 10b7c89 commit b3c27f7
Show file tree
Hide file tree
Showing 3 changed files with 366 additions and 16 deletions.
59 changes: 50 additions & 9 deletions chronix2grid/grid2op_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import grid2op
from grid2op.Parameters import Parameters
from grid2op.Chronics import ChangeNothing, FromNPY
from grid2op.Action import DontAct
from grid2op.Opponent import NeverAttackBudget, BaseOpponent
from lightsim2grid import LightSimBackend
import numpy as np
from numpy.random import default_rng
Expand Down Expand Up @@ -598,13 +600,19 @@ def _adjust_gens(all_loss_orig,
try:
prob.solve()
except cp.error.SolverError as exc_:
error_ = RuntimeError(f"Pypsa failed to find a solution at iteration {iter_num}, error {exc_}")
error_ = RuntimeError(f"cvxpy failed to find a solution at iteration {iter_num}, error {exc_}")
res_gen_p = None
quality_ = None
break

# assign the generators
gen_p_after_optim = real_p.value
if gen_p_after_optim is None:
error_ = RuntimeError(f"cvxpy failed to find a solution at iteration {iter_num}, and returned None.")
res_gen_p = None
quality_ = None
break

id_redisp = 0
for gen_id, gen_nm in enumerate(env_for_loss.name_gen):
if env_for_loss.gen_redispatchable[gen_id]:
Expand All @@ -625,7 +633,12 @@ def _adjust_gens(all_loss_orig,
data_feeding_kwargs={"load_p": load_p,
"load_q": load_q,
"prod_p": 1.0 * res_gen_p,
"prod_v": gen_v}
"prod_v": gen_v},
opponent_budget_per_ts=0.,
opponent_init_budget=0.,
opponent_class=BaseOpponent,
opponent_budget_class=NeverAttackBudget,
opponent_action_class=DontAct,
)
diff_ = np.full((env_fixed.max_episode_duration(), env_fixed.n_gen), fill_value=np.NaN)
all_loss[:] = np.NaN
Expand Down Expand Up @@ -921,7 +934,16 @@ def handle_losses(path_env,

return res_gen_p_df, error_, quality_

def save_generated_data(this_scen_path, load_p, load_p_forecasted, load_q, load_q_forecasted, prod_p, prod_p_forecasted,
def save_generated_data(this_scen_path,
load_p,
load_p_forecasted,
load_q,
load_q_forecasted,
prod_p_generated,
prod_p_after_dispatch,
prod_p, # after everything, the one the agent sees
prod_p_forecasted,
debug,
sep=';',
float_prec=FLOATING_POINT_PRECISION_FORMAT):
"""This function saves the data that have been generated by this script.
Expand All @@ -938,22 +960,30 @@ def save_generated_data(this_scen_path, load_p, load_p_forecasted, load_q, load_
_description_
load_q_forecasted : _type_
_description_
prod_p_generated: _type_
generators just after renewable generation (non renewables gens are Nans here !)
prod_p : _type_
_description_
generators after dispatch and losses !
prod_p_forecasted : _type_
_description_
sep : str, optional
_description_, by default ';'
float_prec : _type_, optional
_description_, by default FLOATING_POINT_PRECISION_FORMAT
"""
for df, nm in zip([load_p, load_p_forecasted, load_q, load_q_forecasted, prod_p, prod_p_forecasted],
["load_p", "load_p_forecasted", "load_q", "load_q_forecasted", "prod_p", "prod_p_forecasted"]):
li_dfs = [load_p, load_p_forecasted, load_q, load_q_forecasted, prod_p, prod_p_forecasted]
li_nms = ["load_p", "load_p_forecasted", "load_q", "load_q_forecasted", "prod_p", "prod_p_forecasted"]
if debug:
li_dfs.append(prod_p_generated)
li_nms.append("prod_p_generated")
li_dfs.append(prod_p_after_dispatch)
li_nms.append("prod_p_after_dispatch")
for df, nm in zip(li_dfs, li_nms):
df.to_csv(os.path.join(this_scen_path, f'{nm}.csv.bz2'),
sep=sep,
float_format=float_prec,
header=True,
index=False)
index=False)


def save_meta_data(this_scen_path,
Expand Down Expand Up @@ -1062,7 +1092,9 @@ def generate_a_scenario(path_env,
PmaxErrorCorrRatio=0.9,
RampErrorCorrRatio=0.95,
threshold_stop=0.05,
max_iter=100):
max_iter=100,
debug=True # TODO more feature !
):
"""This function generates and save the data for a scenario.
Generation includes:
Expand Down Expand Up @@ -1190,7 +1222,16 @@ def generate_a_scenario(path_env,
this_scen_path = os.path.join(output_dir, scenario_id)
if not os.path.exists(this_scen_path):
os.mkdir(this_scen_path)
save_generated_data(this_scen_path, load_p, load_p_forecasted, load_q, load_q_forecasted, res_gen_p_df, res_gen_p_forecasted_df)
save_generated_data(this_scen_path,
load_p,
load_p_forecasted,
load_q,
load_q_forecasted,
final_gen_p, # generated, before economic dispatch
gen_p_after_dispatch, # generated, after economic dispatch (and possibly curtailment)
res_gen_p_df,
res_gen_p_forecasted_df,
debug=debug)
total_load = float(load_p.sum().sum())
total_gen = float(res_gen_p_df.sum().sum())
gen_p_per_step = res_gen_p_df.sum(axis=1)
Expand Down
Loading

0 comments on commit b3c27f7

Please sign in to comment.