Skip to content

Commit

Permalink
Bugfix: Correct sampling in LoadShedding and MaxRenewable env
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaswolgast committed Nov 28, 2024
1 parent 6979cff commit 1a6ba96
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
31 changes: 20 additions & 11 deletions opfgym/envs/load_shedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,31 @@ def __init__(self, simbench_network_name='1-MV-comm--2-sw',

# Define the RL problem
# See all load power values, sgen max active power...
obs_keys = [('sgen', 'p_mw', net.sgen.index),
('load', 'max_p_mw', net.load.index),
('load', 'q_mvar', net.load.index),
('storage', 'p_mw', net.storage.index[~net.storage.controllable]),
('poly_cost', 'cp1_eur_per_mw', net.poly_cost.index),
('pwl_cost', 'cp1_eur_per_mw', net.pwl_cost.index)]
obs_keys = [
('sgen', 'p_mw', net.sgen.index),
('load', 'max_p_mw', net.load.index),
('load', 'q_mvar', net.load.index),
('storage', 'p_mw', net.storage.index[~net.storage.controllable]),
('poly_cost', 'cp1_eur_per_mw', net.poly_cost.index),
('pwl_cost', 'cp1_eur_per_mw', net.pwl_cost.index)
]

# TODO: This is a workaround. Better would be to have identical obs and state keys.
state_keys = [
('sgen', 'p_mw', net.sgen.index),
('load', 'p_mw', net.load.index),
('load', 'q_mvar', net.load.index),
('storage', 'p_mw', net.storage.index[~net.storage.controllable]),
# ('poly_cost', 'cp1_eur_per_mw', net.poly_cost.index), # Separately sampled in _sampling(), see below
# ('pwl_cost', 'cp1_eur_per_mw', net.pwl_cost.index)
]

# Control active power of loads and storages
act_keys = [('load', 'p_mw', net.load.index[net.load.controllable]),
('storage', 'p_mw', net.storage.index[net.storage.controllable])]

super().__init__(net, act_keys, obs_keys, profiles=profiles,
super().__init__(net, act_keys, obs_keys, state_keys=state_keys,
profiles=profiles,
*args, **kwargs)

def _define_opf(self, simbench_network_name, *args, **kwargs):
Expand Down Expand Up @@ -104,10 +117,6 @@ def _define_opf(self, simbench_network_name, *args, **kwargs):

net.ext_grid['vm_pu'] = 1.0

# Required for data sampling
net.load['mean_max_p_mw'] = net.load['mean_p_mw']
net.load['std_dev_max_p_mw'] = net.load['std_dev_p_mw']

return net, profiles

def _sampling(self, *args, **kwargs):
Expand Down
15 changes: 10 additions & 5 deletions opfgym/envs/max_renewable.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,22 @@ def __init__(self, simbench_network_name='1-HV-mixed--1-sw',
('storage', 'p_mw', net.storage.index[~net.storage.controllable])
]

# TODO: This is a workaround. Better would be to have identical obs and state keys.
state_keys = [
('sgen', 'p_mw', net.sgen.index),
('load', 'p_mw', net.load.index),
('load', 'q_mvar', net.load.index),
('storage', 'p_mw', net.storage.index[~net.storage.controllable])
]

# ... and control all sgens' active power values + some storage systems
act_keys = [
('sgen', 'p_mw', net.sgen.index[net.sgen.controllable]),
('storage', 'p_mw', net.storage.index[net.storage.controllable])
]

super().__init__(net, act_keys, obs_keys, profiles=profiles,
super().__init__(net, act_keys, obs_keys, state_keys=state_keys,
profiles=profiles,
*args, **kwargs)

def _define_opf(self, simbench_network_name, *args, **kwargs):
Expand Down Expand Up @@ -81,10 +90,6 @@ def _define_opf(self, simbench_network_name, *args, **kwargs):
net.sgen['max_q_mvar'] = 0.0
net.sgen['min_q_mvar'] = 0.0

# Required for data sampling
net.sgen['mean_max_p_mw'] = net.sgen['mean_p_mw']
net.sgen['std_dev_max_p_mw'] = net.sgen['std_dev_p_mw']

# OPF objective: Maximize active power feed-in to external grid
active_power_costs = 30/1000 # /1000 to achieve smaller scale
for idx in net.sgen.index:
Expand Down
2 changes: 1 addition & 1 deletion opfgym/opf_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def _sample_normal(self, relative_std=None, truncated=False,
assert sample_new, 'Currently only implemented for sample_new=True'
for unit_type, column, idxs in self.state_keys:
if 'res_' in unit_type or 'poly_cost' in unit_type:
continue
continue

df = self.net[unit_type].loc[idxs]
mean = df[f'mean_{column}']
Expand Down

0 comments on commit 1a6ba96

Please sign in to comment.