Skip to content

Commit

Permalink
run pre commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kim-mskw committed Dec 3, 2024
1 parent 88da585 commit b4ad505
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 110 deletions.
2 changes: 1 addition & 1 deletion assume/reinforcement_learning/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def to_torch(self, array: np.array, copy=True):
Args:
array (numpy.ndarray): The numpy array to convert.
copy (bool, optional): Whether to copy the data or not
copy (bool, optional): Whether to copy the data or not
(may be useful to avoid changing things by reference). Defaults to True.
Returns:
Expand Down
2 changes: 1 addition & 1 deletion assume/reinforcement_learning/learning_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def polyak_update(params, target_params, tau: float):
th.add(target_param.data, param.data, alpha=tau, out=target_param.data)


def min_max_scale(x, min_val : float, max_val : float):
def min_max_scale(x, min_val: float, max_val: float):
"""
Min-Max scaling of a value x to the range [0, 1]
Expand Down
65 changes: 28 additions & 37 deletions assume/strategies/learning_advanced_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from assume.common.base import SupportsMinMax
from assume.common.market_objects import MarketConfig, Orderbook, Product
from assume.common.utils import get_products_index
from assume.strategies.learning_strategies import RLStrategy
from assume.reinforcement_learning.learning_utils import min_max_scale
from assume.strategies.learning_strategies import RLStrategy


class RLAdvancedOrderStrategy(RLStrategy):
Expand Down Expand Up @@ -274,7 +274,6 @@ def create_observation(
self.min_market_price = min(unit.forecaster[f"price_{market_id}"])
self.max_residual = max(unit.forecaster[f"residualy_load_{market_id}"])
self.min_residual = min(unit.forecaster[f"residual_load_{market_id}"])


# price forecast
upper_scaling_factor_price = self.max_market_price
Expand All @@ -298,47 +297,39 @@ def create_observation(
end_excl + forecast_len
> unit.forecaster[f"residual_load_{market_id}"].index[-1]
):
scaled_res_load_forecast = (
min_max_scale(
scaled_res_load_forecast = min_max_scale(
unit.forecaster[f"residual_load_{market_id}"][
-int(product_len + self.foresight - 1) :
]
, lower_scaling_factor_res_load
, upper_scaling_factor_res_load
)
],
lower_scaling_factor_res_load,
upper_scaling_factor_res_load,
)

else:
scaled_res_load_forecast = (
min_max_scale(
scaled_res_load_forecast = min_max_scale(
unit.forecaster[f"residual_load_{market_id}"].loc[
start : end_excl + forecast_len
]
, lower_scaling_factor_res_load
, upper_scaling_factor_res_load
)
],
lower_scaling_factor_res_load,
upper_scaling_factor_res_load,
)

if end_excl + forecast_len > unit.forecaster[f"price_{market_id}"].index[-1]:
scaled_price_forecast = (
min_max_scale(
scaled_price_forecast = min_max_scale(
unit.forecaster[f"price_{market_id}"][
-int(product_len + self.foresight - 1) :
]
, lower_scaling_factor_price
, upper_scaling_factor_price
)
],
lower_scaling_factor_price,
upper_scaling_factor_price,
)

else:
scaled_price_forecast = (
min_max_scale(
scaled_price_forecast = min_max_scale(
unit.forecaster[f"price_{market_id}"].loc[
start : end_excl + forecast_len
]
, lower_scaling_factor_price
, upper_scaling_factor_price
)
],
lower_scaling_factor_price,
upper_scaling_factor_price,
)

# get last accepted bid volume and the current marginal costs of the unit
Expand All @@ -348,14 +339,14 @@ def create_observation(

# scale unit outputs
scaled_max_power = min_max_scale(
current_volume
, lower_scaling_factor_total_dispatch
, upper_scaling_factor_total_dispatch
current_volume,
lower_scaling_factor_total_dispatch,
upper_scaling_factor_total_dispatch,
)
scaled_marginal_cost = min_max_scale(
current_costs
, lower_scaling_factor_marginal_cost
, upper_scaling_factor_marginal_cost
current_costs,
lower_scaling_factor_marginal_cost,
upper_scaling_factor_marginal_cost,
)

# calculate the time the unit has to continue to run or be down
Expand All @@ -364,12 +355,12 @@ def create_observation(
must_run_time = max(op_time - unit.min_operating_time, 0)
elif op_time < 0:
must_run_time = min(op_time + unit.min_down_time, 0)
upper_scaling_factor_must_run_time = max(unit.min_operating_time, unit.min_down_time)
upper_scaling_factor_must_run_time = max(
unit.min_operating_time, unit.min_down_time
)

scaled_must_run_time = min_max_scale(
must_run_time
, 0
, upper_scaling_factor_must_run_time
scaled_must_run_time = min_max_scale(
must_run_time, 0, upper_scaling_factor_must_run_time
)

# concat all obsverations into one array
Expand Down
121 changes: 51 additions & 70 deletions assume/strategies/learning_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
from assume.common.base import LearningStrategy, SupportsMinMax, SupportsMinMaxCharge
from assume.common.market_objects import MarketConfig, Orderbook, Product
from assume.reinforcement_learning.algorithms import actor_architecture_aliases
from assume.reinforcement_learning.learning_utils import NormalActionNoise, min_max_scale
from assume.reinforcement_learning.learning_utils import (
NormalActionNoise,
min_max_scale,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -405,14 +408,13 @@ def create_observation(
self.min_market_price = min(unit.forecaster[f"price_{market_id}"])
self.max_residual = max(unit.forecaster[f"residualy_load_{market_id}"])
self.min_residual = min(unit.forecaster[f"residual_load_{market_id}"])

# scaling factors for the observations
upper_scaling_factor_res_load = self.max_residual
lower_scaling_factor_res_load = self.min_residual
upper_scaling_factor_price = self.max_market_price
lower_scaling_factor_price = self.min_market_price


# total dispatch and marginal cost
upper_scaling_factor_total_dispatch = unit.max_power
# if unit is not running, total dispatch is 0
Expand All @@ -429,12 +431,10 @@ def create_observation(
end_excl + forecast_len
> unit.forecaster[f"residual_load_{market_id}"].index[-1]
):
scaled_res_load_forecast = (
min_max_scale(
unit.forecaster[f"residual_load_{market_id}"].loc[start:]
, lower_scaling_factor_res_load
, upper_scaling_factor_res_load
)
scaled_res_load_forecast = min_max_scale(
unit.forecaster[f"residual_load_{market_id}"].loc[start:],
lower_scaling_factor_res_load,
upper_scaling_factor_res_load,
)
scaled_res_load_forecast = np.concatenate(
[
Expand All @@ -446,24 +446,19 @@ def create_observation(
)

else:
scaled_res_load_forecast = (
min_max_scale(
scaled_res_load_forecast = min_max_scale(
unit.forecaster[f"residual_load_{market_id}"].loc[
start : end_excl + forecast_len
]
, lower_scaling_factor_res_load
, upper_scaling_factor_res_load
)

],
lower_scaling_factor_res_load,
upper_scaling_factor_res_load,
)

if end_excl + forecast_len > unit.forecaster[f"price_{market_id}"].index[-1]:
scaled_price_forecast = (
min_max_scale(
unit.forecaster[f"price_{market_id}"].loc[start:]
, lower_scaling_factor_price
, upper_scaling_factor_price
)
scaled_price_forecast = min_max_scale(
unit.forecaster[f"price_{market_id}"].loc[start:],
lower_scaling_factor_price,
upper_scaling_factor_price,
)
scaled_price_forecast = np.concatenate(
[
Expand All @@ -475,14 +470,12 @@ def create_observation(
)

else:
scaled_price_forecast = (
min_max_scale(
scaled_price_forecast = min_max_scale(
unit.forecaster[f"price_{market_id}"].loc[
start : end_excl + forecast_len
]
, lower_scaling_factor_price
, upper_scaling_factor_price
)
],
lower_scaling_factor_price,
upper_scaling_factor_price,
)

# get last accepted bid volume and the current marginal costs of the unit
Expand All @@ -491,15 +484,15 @@ def create_observation(

# scale unit outputs
scaled_total_dispatch = min_max_scale(
current_volume
, lower_scaling_factor_total_dispatch
, upper_scaling_factor_total_dispatch
)
current_volume,
lower_scaling_factor_total_dispatch,
upper_scaling_factor_total_dispatch,
)
scaled_marginal_cost = min_max_scale(
current_costs
, lower_scaling_factor_marginal_cost
, upper_scaling_factor_marginal_cost
)
current_costs,
lower_scaling_factor_marginal_cost,
upper_scaling_factor_marginal_cost,
)

# concat all obsverations into one array
observation = np.concatenate(
Expand Down Expand Up @@ -1050,7 +1043,7 @@ def create_observation(
self.min_market_price = min(unit.forecaster[f"price_{market_id}"])
self.max_residual = max(unit.forecaster[f"residualy_load_{market_id}"])
self.min_residual = min(unit.forecaster[f"residual_load_{market_id}"])

upper_scaling_factor_res_load = self.max_residual
lower_scaling_factor_res_load = self.min_residual
upper_scaling_factor_price = self.max_market_price
Expand All @@ -1062,12 +1055,10 @@ def create_observation(
end_excl + forecast_len
> unit.forecaster[f"residual_load_{market_id}"].index[-1]
):
scaled_res_load_forecast = (
min_max_scale(
unit.forecaster[f"residual_load_{market_id}"].loc[start:]
, lower_scaling_factor_res_load
, upper_scaling_factor_res_load
)
scaled_res_load_forecast = min_max_scale(
unit.forecaster[f"residual_load_{market_id}"].loc[start:],
lower_scaling_factor_res_load,
upper_scaling_factor_res_load,
)
scaled_res_load_forecast = np.concatenate(
[
Expand All @@ -1079,23 +1070,19 @@ def create_observation(
)

else:
scaled_res_load_forecast = (
min_max_scale(
scaled_res_load_forecast = min_max_scale(
unit.forecaster[f"residual_load_{market_id}"].loc[
start : end_excl + forecast_len
]
, lower_scaling_factor_res_load
, upper_scaling_factor_res_load
)
],
lower_scaling_factor_res_load,
upper_scaling_factor_res_load,
)

if end_excl + forecast_len > unit.forecaster[f"price_{market_id}"].index[-1]:
scaled_price_forecast = (
min_max_scale(
unit.forecaster[f"price_{market_id}"].loc[start:]
, lower_scaling_factor_price
, upper_scaling_factor_price
)
scaled_price_forecast = min_max_scale(
unit.forecaster[f"price_{market_id}"].loc[start:],
lower_scaling_factor_price,
upper_scaling_factor_price,
)
scaled_price_forecast = np.concatenate(
[
Expand All @@ -1107,27 +1094,21 @@ def create_observation(
)

else:
scaled_price_forecast = (
min_max_scale(
scaled_price_forecast = min_max_scale(
unit.forecaster[f"price_{market_id}"].loc[
start : end_excl + forecast_len
]
, lower_scaling_factor_price
, upper_scaling_factor_price
)
],
lower_scaling_factor_price,
upper_scaling_factor_price,
)

# get the current soc value
soc_scaled = min_max_scale(
unit.outputs["soc"].at[start]
, 0
, unit.max_soc
)
soc_scaled = min_max_scale(unit.outputs["soc"].at[start], 0, unit.max_soc)
energy_cost_scaled = min_max_scale(
unit.outputs["energy_cost"].at[start]
,self.min_bid_price
, self.max_bid_price
)
unit.outputs["energy_cost"].at[start],
self.min_bid_price,
self.max_bid_price,
)

# concat all obsverations into one array
observation = np.concatenate(
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 @@ -36,7 +36,7 @@ Upcoming Release
- **Outputs Role Performance Optimization:** Output role handles dict data directly and only converts to DataFrame on Database write.
- **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.
- **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.

**Bugfixes:**
Expand Down

0 comments on commit b4ad505

Please sign in to comment.