Skip to content

Commit

Permalink
-remove index from unit initialization
Browse files Browse the repository at this point in the history
-clean up index everywhere
  • Loading branch information
nick-harder committed Nov 20, 2024
1 parent fdf2902 commit 9ea05d6
Show file tree
Hide file tree
Showing 23 changed files with 76 additions and 81 deletions.
25 changes: 10 additions & 15 deletions assume/common/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,24 @@ def __init__(
unit_operator: str,
technology: str,
bidding_strategies: dict[str, BaseStrategy],
index: pd.DatetimeIndex,
forecaster: Forecaster,
node: str = "node0",
forecaster: Forecaster = None,
location: tuple[float, float] = (0.0, 0.0),
**kwargs,
):
self.id = id
self.unit_operator = unit_operator
self.technology = technology
self.bidding_strategies: dict[str, BaseStrategy] = bidding_strategies
self.forecaster = forecaster
self.index = forecaster.index

self.node = node
self.location = location
self.bidding_strategies: dict[str, BaseStrategy] = bidding_strategies
self.index = index

self.outputs = defaultdict(lambda: FastSeries(value=0.0, index=self.index))
# series does not like to convert from tensor to float otherwise

# RL data stored as lists to simplify storing to the buffer
self.outputs["rl_observations"] = []
self.outputs["rl_actions"] = []
self.outputs["rl_rewards"] = []

# some data is stored as series to allow to store it in the outputs
# check if any bidding strategy is using the RL strategy
if any(
Expand All @@ -85,12 +82,10 @@ def __init__(
)
self.outputs["reward"] = FastSeries(value=0.0, index=self.index)

if forecaster:
self.forecaster = forecaster
else:
self.forecaster = defaultdict(
lambda: FastSeries(value=0.0, index=self.index)
)
# RL data stored as lists to simplify storing to the buffer
self.outputs["rl_observations"] = []
self.outputs["rl_actions"] = []
self.outputs["rl_rewards"] = []

def calculate_bids(
self,
Expand Down
21 changes: 11 additions & 10 deletions assume/scenario/loader_amiris.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def add_agent_to_world(
base_path: str,
markups: dict = {},
supports: dict = {},
index: pd.DatetimeIndex = None,
):
"""
Adds an agent from a amiris agent definition to the ASSUME world.
Expand Down Expand Up @@ -175,7 +176,7 @@ def add_agent_to_world(
"technology": "demand",
"price": value,
},
NaiveForecast(world.index, demand=100000),
NaiveForecast(index, demand=100000),
)
case "EnergyExchange" | "DayAheadMarketSingleZone":
clearing_section = agent["Attributes"].get("Clearing", agent["Attributes"])
Expand Down Expand Up @@ -223,7 +224,7 @@ def add_agent_to_world(
co2_price = agent["Attributes"]["Co2Prices"]
if isinstance(co2_price, str):
price_series = read_csv(base_path, co2_price)
co2_price = price_series.reindex(world.index).ffill().fillna(0)
co2_price = price_series.reindex(index).ffill().fillna(0)
prices["co2"] = co2_price
case "FuelsMarket":
# fill prices for forecaster
Expand All @@ -235,7 +236,7 @@ def add_agent_to_world(
price_series.index = price_series.index.round("h")
if not price_series.index.is_unique:
price_series = price_series.groupby(level=0).last()
price = price_series.reindex(world.index).ffill()
price = price_series.reindex(index).ffill()
prices[fuel_type] = price * fuel["ConversionFactor"]
case "DemandTrader":
world.add_unit_operator(agent["Id"])
Expand All @@ -253,7 +254,7 @@ def add_agent_to_world(
"technology": "demand",
"price": load["ValueOfLostLoad"],
},
NaiveForecast(world.index, demand=demand_series),
NaiveForecast(index, demand=demand_series),
)

case "StorageTrader":
Expand All @@ -270,7 +271,7 @@ def add_agent_to_world(
forecast_price = prices.get("co2", 20)
# TODO forecast should be calculated using calculate_EOM_price_forecast
forecast = NaiveForecast(
world.index,
index,
availability=1,
co2_price=prices.get("co2", 2),
# price_forecast is used for price_EOM
Expand Down Expand Up @@ -336,11 +337,11 @@ def add_agent_to_world(
availability = prototype["PlannedAvailability"]
if isinstance(availability, str):
availability = read_csv(base_path, availability)
availability = availability.reindex(world.index).ffill()
availability = availability.reindex(index).ffill()
availability *= prototype.get("UnplannedAvailabilityFactor", 1)

forecast = NaiveForecast(
world.index,
index,
availability=availability,
fuel_price=fuel_price,
co2_price=prices.get("co2", 2),
Expand Down Expand Up @@ -387,7 +388,7 @@ def add_agent_to_world(
max_power = attr["InstalledPowerInMW"]
if isinstance(availability, str):
dispatch_profile = read_csv(base_path, availability)
availability = dispatch_profile.reindex(world.index).ffill().fillna(0)
availability = dispatch_profile.reindex(index).ffill().fillna(0)

if availability.max() > 1:
scale_value = availability.max()
Expand All @@ -398,7 +399,7 @@ def add_agent_to_world(
fuel_price = prices.get(translate_fuel_type[attr["EnergyCarrier"]], 0)
fuel_price += attr.get("OpexVarInEURperMWH", 0)
forecast = NaiveForecast(
world.index,
index,
availability=availability,
fuel_price=fuel_price,
co2_price=prices.get("co2", 0),
Expand Down Expand Up @@ -488,7 +489,6 @@ def load_amiris(
end=end,
save_frequency_hours=save_interval,
simulation_id=sim_id,
index=index,
)
# helper dict to map trader markups/markdowns to powerplants
markups = {}
Expand Down Expand Up @@ -523,6 +523,7 @@ def load_amiris(
base_path,
markups,
supports,
index,
)
# calculate market price before simulation
world
Expand Down
3 changes: 0 additions & 3 deletions assume/scenario/loader_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,6 @@ def load_config_and_create_forecaster(
"path": path,
"start": start,
"end": end,
"index": forecaster.index,
"powerplant_units": powerplant_units,
"storage_units": storage_units,
"demand_units": demand_units,
Expand Down Expand Up @@ -565,7 +564,6 @@ def setup_world(
config = scenario_data["config"]
start = scenario_data["start"]
end = scenario_data["end"]
index = scenario_data["index"]
powerplant_units = scenario_data["powerplant_units"]
storage_units = scenario_data["storage_units"]
demand_units = scenario_data["demand_units"]
Expand Down Expand Up @@ -627,7 +625,6 @@ def setup_world(
simulation_id=sim_id,
learning_config=learning_config,
bidding_params=bidding_strategy_params,
index=index,
forecaster=forecaster,
)

Expand Down
1 change: 0 additions & 1 deletion assume/scenario/loader_oeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def load_oeds(
end=end,
save_frequency_hours=48,
simulation_id=sim_id,
index=index,
)
# setup eom market

Expand Down
1 change: 0 additions & 1 deletion assume/scenario/loader_pypsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def load_pypsa(
end=end,
save_frequency_hours=save_frequency_hours,
simulation_id=sim_id,
index=index,
)
# setup eom market

Expand Down
7 changes: 4 additions & 3 deletions assume/units/demand.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from assume.common.base import SupportsMinMax
from assume.common.fast_pandas import FastSeries
from assume.common.forecasts import Forecaster


class Demand(SupportsMinMax):
Expand Down Expand Up @@ -36,9 +37,9 @@ def __init__(
unit_operator: str,
technology: str,
bidding_strategies: dict,
index: pd.DatetimeIndex,
max_power: float,
min_power: float,
forecaster: Forecaster,
node: str = "node0",
price: float | pd.Series = 3000.0,
location: tuple[float, float] = (0.0, 0.0),
Expand All @@ -49,7 +50,7 @@ def __init__(
unit_operator=unit_operator,
technology=technology,
bidding_strategies=bidding_strategies,
index=index,
forecaster=forecaster,
node=node,
location=location,
**kwargs,
Expand All @@ -65,7 +66,7 @@ def __init__(
volume = self.forecaster[self.id]
self.volume = -volume # demand is negative
if isinstance(price, numbers.Real):
price = FastSeries(index=index, value=price)
price = FastSeries(index=self.index, value=price)
self.price = price

def execute_current_dispatch(
Expand Down
5 changes: 3 additions & 2 deletions assume/units/powerplant.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pandas as pd

from assume.common.base import SupportsMinMax
from assume.common.forecasts import Forecaster
from assume.common.market_objects import MarketConfig, Orderbook
from assume.common.utils import get_products_index

Expand Down Expand Up @@ -54,7 +55,7 @@ def __init__(
unit_operator: str,
technology: str,
bidding_strategies: dict,
index: pd.DatetimeIndex,
forecaster: Forecaster,
max_power: float,
min_power: float = 0.0,
efficiency: float = 1.0,
Expand Down Expand Up @@ -82,7 +83,7 @@ def __init__(
unit_operator=unit_operator,
technology=technology,
bidding_strategies=bidding_strategies,
index=index,
forecaster=forecaster,
node=node,
location=location,
**kwargs,
Expand Down
5 changes: 3 additions & 2 deletions assume/units/steel_plant.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)

from assume.common.base import SupportsMinMax
from assume.common.forecasts import Forecaster
from assume.common.market_objects import MarketConfig, Orderbook
from assume.common.utils import get_products_index
from assume.units.dsm_load_shift import DSMFlex
Expand Down Expand Up @@ -53,9 +54,9 @@ def __init__(
id: str,
unit_operator: str,
bidding_strategies: dict,
forecaster: Forecaster,
technology: str = "steel_plant",
node: str = "node0",
index: pd.DatetimeIndex = None,
location: tuple[float, float] = (0.0, 0.0),
components: dict[str, dict] = None,
objective: str = None,
Expand All @@ -70,7 +71,7 @@ def __init__(
technology=technology,
components=components,
bidding_strategies=bidding_strategies,
index=index,
forecaster=forecaster,
node=node,
location=location,
**kwargs,
Expand Down
9 changes: 5 additions & 4 deletions assume/units/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from assume.common.base import SupportsMinMaxCharge
from assume.common.fast_pandas import FastSeries
from assume.common.forecasts import Forecaster
from assume.common.market_objects import MarketConfig, Orderbook
from assume.common.utils import get_products_index

Expand Down Expand Up @@ -59,6 +60,7 @@ def __init__(
unit_operator: str,
technology: str,
bidding_strategies: dict,
forecaster: Forecaster,
max_power_charge: float | pd.Series,
max_power_discharge: float | pd.Series,
max_soc: float,
Expand All @@ -82,19 +84,18 @@ def __init__(
min_down_time: float = 0,
downtime_hot_start: int = 8, # hours
downtime_warm_start: int = 48, # hours
index: pd.DatetimeIndex = None,
location: tuple[float, float] = (0, 0),
node: str = "node0",
**kwargs,
):
super().__init__(
id=id,
unit_operator=unit_operator,
technology=technology,
bidding_strategies=bidding_strategies,
forecaster=forecaster,
node=node,
location=location,
bidding_strategies=bidding_strategies,
index=index,
unit_operator=unit_operator,
**kwargs,
)

Expand Down
4 changes: 0 additions & 4 deletions assume/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from datetime import datetime
from pathlib import Path

import pandas as pd
from mango import (
RoleAgent,
activate,
Expand Down Expand Up @@ -157,7 +156,6 @@ def setup(
start: datetime,
end: datetime,
simulation_id: str,
index: pd.Series,
save_frequency_hours: int = 24,
bidding_params: dict = {},
learning_config: LearningConfig = {},
Expand Down Expand Up @@ -195,7 +193,6 @@ def setup(
self.forecaster = forecaster

self.bidding_params = bidding_params
self.index = index

# create new container
container_kwargs = {}
Expand Down Expand Up @@ -474,7 +471,6 @@ def create_unit(
return unit_class(
id=id,
unit_operator=unit_operator_id,
index=self.index,
forecaster=forecaster,
**unit_params,
)
Expand Down
1 change: 0 additions & 1 deletion examples/world_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def init(world, n=1):
end=end,
save_frequency_hours=48,
simulation_id=sim_id,
index=index,
)

marketdesign = [
Expand Down
1 change: 0 additions & 1 deletion examples/world_script_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def init(world: World):
end=end,
save_frequency_hours=48,
simulation_id=sim_id,
index=index,
)

marketdesign = [
Expand Down
Loading

0 comments on commit 9ea05d6

Please sign in to comment.