Skip to content

Commit

Permalink
-fix tests
Browse files Browse the repository at this point in the history
-avoid division by zero
-relocate prepare_observations function to the base class
  • Loading branch information
nick-harder committed Dec 16, 2024
1 parent 0467347 commit 7e7ec46
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 39 deletions.
8 changes: 4 additions & 4 deletions assume/reinforcement_learning/learning_unit_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ def add_unit(

self.rl_units.append(unit)

#prepare scaled foecasts for the RL staretgy as observations


unit.bidding_strategies[market.market_id].prepare_observations(unit, market.market_id)
# prepare scaled foecasts for the RL staretgy as observations
unit.bidding_strategies[market.market_id].prepare_observations(
unit, market.market_id
)
break

def handle_market_feedback(self, content: ClearingMessage, meta: MetaDict) -> None:
Expand Down
3 changes: 3 additions & 0 deletions assume/reinforcement_learning/learning_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ def min_max_scale(x, min_val: float, max_val: float):
min_val: minimum value of the parameter
max_val: maximum value of the parameter
"""
# Avoid division by zero
if min_val == max_val:
return x
return (x - min_val) / (max_val - min_val)


Expand Down
2 changes: 1 addition & 1 deletion assume/strategies/learning_advanced_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def create_observation(
# residual load forecast
upper_scaling_factor_res_load = self.max_residual
lower_scaling_factor_res_load = self.min_residual

# price forecast
upper_scaling_factor_price = self.max_market_price
lower_scaling_factor_price = self.min_market_price
Expand Down
59 changes: 26 additions & 33 deletions assume/strategies/learning_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ def load_actor_params(self, load_path):
self.actor_target.eval()
self.actor.optimizer.load_state_dict(params["actor_optimizer"])

def prepare_observations(self, unit, market_id):
# scaling factors for the observations
upper_scaling_factor_res_load = max(unit.forecaster[f"price_{market_id}"])
lower_scaling_factor_res_load = min(unit.forecaster[f"price_{market_id}"])
upper_scaling_factor_price = max(unit.forecaster[f"residualy_load_{market_id}"])
lower_scaling_factor_price = min(unit.forecaster[f"residual_load_{market_id}"])

self.scaled_res_load_obs = min_max_scale(
unit.forecaster[f"residual_load_{market_id}"],
lower_scaling_factor_res_load,
upper_scaling_factor_res_load,
)

self.scaled_pices_obs = min_max_scale(
unit.forecaster[f"price_{market_id}"],
lower_scaling_factor_price,
upper_scaling_factor_price,
)


class RLStrategy(AbstractLearningStrategy):
"""
Expand Down Expand Up @@ -142,7 +161,6 @@ def __init__(self, *args, **kwargs):
self.algorithm = kwargs.get("algorithm", "matd3")
actor_architecture = kwargs.get("actor_architecture", "mlp")


if actor_architecture in actor_architecture_aliases.keys():
self.actor_architecture_class = actor_architecture_aliases[
actor_architecture
Expand Down Expand Up @@ -187,27 +205,6 @@ def __init__(self, *args, **kwargs):
raise FileNotFoundError(
f"No policies were provided for DRL unit {self.unit_id}!. Please provide a valid path to the trained policies."
)

def prepare_observations(self, unit, market_id):

# scaling factors for the observations
upper_scaling_factor_res_load = max(unit.forecaster[f"price_{market_id}"])
lower_scaling_factor_res_load = min(unit.forecaster[f"price_{market_id}"])
upper_scaling_factor_price = max(unit.forecaster[f"residualy_load_{market_id}"])
lower_scaling_factor_price = min(unit.forecaster[f"residual_load_{market_id}"])


self.scaled_res_load_obs = min_max_scale(
unit.forecaster[f"residual_load_{market_id}"],
lower_scaling_factor_res_load,
upper_scaling_factor_res_load,
)

self.scaled_pices_obs = min_max_scale(
unit.forecaster[f"price_{market_id}"],
lower_scaling_factor_price,
upper_scaling_factor_price,
)

def calculate_bids(
self,
Expand Down Expand Up @@ -424,12 +421,9 @@ def create_observation(

# checks if we are at end of simulation horizon, since we need to change the forecast then
# for residual load and price forecast and scale them
if (
end_excl + forecast_len
> self.scaled_res_load_obs.index[-1]
):
if end_excl + forecast_len > self.scaled_res_load_obs.index[-1]:
scaled_res_load_forecast = self.scaled_res_load_obs.loc[start:]

scaled_res_load_forecast = np.concatenate(
[
scaled_res_load_forecast,
Expand All @@ -441,9 +435,9 @@ def create_observation(

else:
scaled_res_load_forecast = self.scaled_res_load_obs.loc[
start : end_excl + forecast_len
]
start : end_excl + forecast_len
]

if end_excl + forecast_len > self.scaled_pices_obs.index[-1]:
scaled_price_forecast = self.scaled_pices_obs.loc[start:]
scaled_price_forecast = np.concatenate(
Expand All @@ -457,9 +451,8 @@ def create_observation(

else:
scaled_price_forecast = self.scaled_pices_obs.loc[
start : end_excl + forecast_len
]

start : end_excl + forecast_len
]

# get last accepted bid volume and the current marginal costs of the unit
current_volume = unit.get_output_before(start)
Expand Down
2 changes: 1 addition & 1 deletion docs/source/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ v0.5.0 - (10th December 2024)
- **Overall Performance Optimization:** The overall performance of the framework has been improved by a factor of 5x to 12x
depending on the size of the simulation (number of units, markets, and time steps).
- **Learning Opservation Space Scaling:** Instead of the formerly used max sclaing of the observation space, we added a min-max scaling to the observation space.
This allows for a more robust scaling of the observation space for furture analysis.
This allows for a more robust scaling of the observation space for future analysis.

**Bugfixes:**
- **Tutorials**: General fixes of the tutorials, to align with updated functionalitites of Assume
Expand Down
2 changes: 2 additions & 0 deletions tests/test_rl_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def test_learning_strategies(mock_market_config, power_plant_mcp):
]

strategy = power_plant_mcp.bidding_strategies["EOM"]
strategy.prepare_observations(power_plant_mcp, mc.market_id)
bids = strategy.calculate_bids(power_plant_mcp, mc, product_tuples=product_tuples)

assert len(bids) == 2
Expand Down Expand Up @@ -123,6 +124,7 @@ def test_lstm_learning_strategies(mock_market_config, power_plant_lstm):
]

strategy = power_plant_lstm.bidding_strategies["EOM"]
strategy.prepare_observations(power_plant_lstm, mc.market_id)
bids = strategy.calculate_bids(power_plant_lstm, mc, product_tuples=product_tuples)

assert len(bids) == 2
Expand Down

0 comments on commit 7e7ec46

Please sign in to comment.