Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support constraining final market volume #945

Merged
merged 1 commit into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions db/csvs_test_examples/markets/volume/1_base_volume.csv
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
market,stage_id,timepoint,max_market_sales,max_market_purchases
Market_Hub,1,20200101,10,5
Market_Hub,1,20200102,5,10
Market_Hub,1,20200201,10,5
Market_Hub,1,20200202,5,10
Market_Hub,1,20200301,10,5
Market_Hub,1,20200302,5,10
Market_Hub2,1,20200101,10,5
Market_Hub2,1,20200102,5,10
Market_Hub2,1,20200201,10,5
Market_Hub2,1,20200202,5,10
Market_Hub2,1,20200301,10,5
Market_Hub2,1,20200302,5,10
Market_Hub2,2,20200101,5,5
Market_Hub2,2,20200102,2,2
Market_Hub2,2,20200201,5,5
Market_Hub2,2,20200202,2,2
Market_Hub2,2,20200301,5,5
Market_Hub2,2,20200302,2,2
market,stage_id,timepoint,max_market_sales,max_market_purchases,max_final_market_sales,max_final_market_purchases
Market_Hub,1,20200101,10,5,,
Market_Hub,1,20200102,5,10,,
Market_Hub,1,20200201,10,5,,
Market_Hub,1,20200202,5,10,,
Market_Hub,1,20200301,10,5,,
Market_Hub,1,20200302,5,10,,
Market_Hub2,1,20200101,10,5,,
Market_Hub2,1,20200102,5,10,,
Market_Hub2,1,20200201,10,5,,
Market_Hub2,1,20200202,5,10,,
Market_Hub2,1,20200301,10,5,,
Market_Hub2,1,20200302,5,10,,
Market_Hub2,2,20200101,5,5,,
Market_Hub2,2,20200102,2,2,,
Market_Hub2,2,20200201,5,5,,
Market_Hub2,2,20200202,2,2,,
Market_Hub2,2,20200301,5,5,,
Market_Hub2,2,20200302,2,2,,
104 changes: 104 additions & 0 deletions db/csvs_test_examples/scenarios copy.csv

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions db/db_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,8 @@ stage_id INTEGER,
timepoint INTEGER,
max_market_sales FLOAT,
max_market_purchases FLOAT,
max_final_market_sales FLOAT,
max_final_market_purchases FLOAT,
PRIMARY KEY (market_volume_scenario_id, market, stage_id, timepoint),
FOREIGN KEY (market_volume_scenario_id) REFERENCES
subscenarios_market_volume (market_volume_scenario_id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
market timepoint max_market_sales max_market_purchases
Market_Hub 20200101 10.0 5.0
Market_Hub 20200102 5.0 10.0
Market_Hub2 20200101 10.0 5.0
Market_Hub2 20200102 5.0 10.0
market timepoint max_market_sales max_market_purchases max_final_market_sales max_final_market_purchases
Market_Hub 20200101 10.0 5.0 . .
Market_Hub 20200102 5.0 10.0 . .
Market_Hub2 20200101 10.0 5.0 . .
Market_Hub2 20200102 5.0 10.0 . .
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
market timepoint max_market_sales max_market_purchases
Market_Hub 20200101 . .
Market_Hub 20200102 . .
Market_Hub2 20200101 5.0 5.0
Market_Hub2 20200102 2.0 2.0
market timepoint max_market_sales max_market_purchases max_final_market_sales max_final_market_purchases
Market_Hub 20200101 . . . .
Market_Hub 20200102 . . . .
Market_Hub2 20200101 5.0 5.0 . .
Market_Hub2 20200102 2.0 2.0 . .
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
market timepoint max_market_sales max_market_purchases
Market_Hub 20200101 . .
Market_Hub 20200102 . .
Market_Hub2 20200101 . .
Market_Hub2 20200102 . .
market timepoint max_market_sales max_market_purchases max_final_market_sales max_final_market_purchases
Market_Hub 20200101 . . . .
Market_Hub 20200102 . . . .
Market_Hub2 20200101 . . . .
Market_Hub2 20200102 . . . .
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
market timepoint max_market_sales max_market_purchases
Market_Hub 20200201 10.0 5.0
Market_Hub 20200202 5.0 10.0
Market_Hub2 20200201 10.0 5.0
Market_Hub2 20200202 5.0 10.0
market timepoint max_market_sales max_market_purchases max_final_market_sales max_final_market_purchases
Market_Hub 20200201 10.0 5.0 . .
Market_Hub 20200202 5.0 10.0 . .
Market_Hub2 20200201 10.0 5.0 . .
Market_Hub2 20200202 5.0 10.0 . .
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
market timepoint max_market_sales max_market_purchases
Market_Hub 20200201 . .
Market_Hub 20200202 . .
Market_Hub2 20200201 5.0 5.0
Market_Hub2 20200202 2.0 2.0
market timepoint max_market_sales max_market_purchases max_final_market_sales max_final_market_purchases
Market_Hub 20200201 . . . .
Market_Hub 20200202 . . . .
Market_Hub2 20200201 5.0 5.0 . .
Market_Hub2 20200202 2.0 2.0 . .
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
market timepoint max_market_sales max_market_purchases
Market_Hub 20200201 . .
Market_Hub 20200202 . .
Market_Hub2 20200201 . .
Market_Hub2 20200202 . .
market timepoint max_market_sales max_market_purchases max_final_market_sales max_final_market_purchases
Market_Hub 20200201 . . . .
Market_Hub 20200202 . . . .
Market_Hub2 20200201 . . . .
Market_Hub2 20200202 . . . .
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
market timepoint max_market_sales max_market_purchases
Market_Hub 20200301 10.0 5.0
Market_Hub 20200302 5.0 10.0
Market_Hub2 20200301 10.0 5.0
Market_Hub2 20200302 5.0 10.0
market timepoint max_market_sales max_market_purchases max_final_market_sales max_final_market_purchases
Market_Hub 20200301 10.0 5.0 . .
Market_Hub 20200302 5.0 10.0 . .
Market_Hub2 20200301 10.0 5.0 . .
Market_Hub2 20200302 5.0 10.0 . .
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
market timepoint max_market_sales max_market_purchases
Market_Hub 20200301 . .
Market_Hub 20200302 . .
Market_Hub2 20200301 5.0 5.0
Market_Hub2 20200302 2.0 2.0
market timepoint max_market_sales max_market_purchases max_final_market_sales max_final_market_purchases
Market_Hub 20200301 . . . .
Market_Hub 20200302 . . . .
Market_Hub2 20200301 5.0 5.0 . .
Market_Hub2 20200302 2.0 2.0 . .
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
market timepoint max_market_sales max_market_purchases
Market_Hub 20200301 . .
Market_Hub 20200302 . .
Market_Hub2 20200301 . .
Market_Hub2 20200302 . .
market timepoint max_market_sales max_market_purchases max_final_market_sales max_final_market_purchases
Market_Hub 20200301 . . . .
Market_Hub 20200302 . . . .
Market_Hub2 20200301 . . . .
Market_Hub2 20200302 . . . .
6 changes: 3 additions & 3 deletions examples/test_markets/inputs/market_volume.tab
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
market timepoint max_market_sales max_market_purchases
Market_Hub 20200101 10.0 5.0
Market_Hub 20200102 5.0 10.0
market timepoint max_market_sales max_market_purchases max_final_market_sales max_final_market_purchases
Market_Hub 20200101 10.0 5.0 . .
Market_Hub 20200102 5.0 10.0 . .
67 changes: 62 additions & 5 deletions gridpath/system/markets/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
def add_model_components(m, d, scenario_directory, subproblem, stage):
""" """
m.max_market_sales = Param(m.MARKETS, m.TMPS, default=Infinity)

m.max_market_purchases = Param(m.MARKETS, m.TMPS, default=Infinity)
m.max_final_market_sales = Param(m.MARKETS, m.TMPS, default=Infinity)
m.max_final_market_purchases = Param(m.MARKETS, m.TMPS, default=Infinity)

def total_market_sales_rule(mod, market, tmp):
return sum(
Expand All @@ -45,6 +46,29 @@ def total_market_purchases_rule(mod, market, tmp):
m.MARKETS, m.TMPS, rule=total_market_purchases_rule
)

def total_net_market_sales_rule(mod, market, tmp):
return sum(
mod.Final_Sell_Power_Position[lz, mrkt, tmp]
for (lz, mrkt) in mod.LZ_MARKETS
if mrkt == market
)

m.Total_Net_Market_Sales = Expression(
m.MARKETS, m.TMPS, rule=total_net_market_sales_rule
)

def total_net_market_purchases_rule(mod, market, tmp):
return sum(
mod.Final_Buy_Power_Position[lz, mrkt, tmp]
for (lz, mrkt) in mod.LZ_MARKETS
if mrkt == market
)

m.Total_Net_Market_Purchases = Expression(
m.MARKETS, m.TMPS, rule=total_net_market_purchases_rule
)

# Constraints
def max_market_sales_rule(mod, hub, tmp):
return mod.Total_Market_Sales[hub, tmp] <= mod.max_market_sales[hub, tmp]

Expand All @@ -61,6 +85,25 @@ def max_market_purchases_rule(mod, hub, tmp):
m.MARKETS, m.TMPS, rule=max_market_purchases_rule
)

def max_final_market_sales_rule(mod, hub, tmp):
return (
mod.Total_Net_Market_Sales[hub, tmp] <= mod.max_final_market_sales[hub, tmp]
)

m.Max_Net_Market_Sales_Constraint = Constraint(
m.MARKETS, m.TMPS, rule=max_final_market_sales_rule
)

def max_final_market_purchases_rule(mod, hub, tmp):
return (
mod.Total_Net_Market_Purchases[hub, tmp]
<= mod.max_final_market_purchases[hub, tmp]
)

m.Max_Net_Market_Purchases_Constraint = Constraint(
m.MARKETS, m.TMPS, rule=max_final_market_purchases_rule
)


def load_model_data(m, d, data_portal, scenario_directory, subproblem, stage):
data_portal.load(
Expand All @@ -71,7 +114,12 @@ def load_model_data(m, d, data_portal, scenario_directory, subproblem, stage):
"inputs",
"market_volume.tab",
),
param=(m.max_market_sales, m.max_market_purchases),
param=(
m.max_market_sales,
m.max_market_purchases,
m.max_final_market_sales,
m.max_final_market_purchases,
),
)


Expand All @@ -92,7 +140,8 @@ def get_inputs_from_database(scenario_id, subscenarios, subproblem, stage, conn)
# market_scenario_id
market_limits = c.execute(
"""
SELECT market, timepoint, max_market_sales, max_market_purchases
SELECT market, timepoint, max_market_sales, max_market_purchases,
max_final_market_sales, max_final_market_purchases
-- Get prices for included markets only
FROM (
SELECT market
Expand All @@ -107,7 +156,8 @@ def get_inputs_from_database(scenario_id, subscenarios, subproblem, stage, conn)
AND stage_id = ?
) as tmp_tbl
LEFT OUTER JOIN (
SELECT market, stage_id, timepoint, max_market_sales, max_market_purchases
SELECT market, stage_id, timepoint, max_market_sales, max_market_purchases,
max_final_market_sales, max_final_market_purchases
FROM inputs_market_volume
WHERE market_volume_scenario_id = ?
) as price_tbl
Expand Down Expand Up @@ -158,7 +208,14 @@ def write_model_inputs(
writer = csv.writer(f, delimiter="\t", lineterminator="\n")

writer.writerow(
["market", "timepoint", "max_market_sales", "max_market_purchases"]
[
"market",
"timepoint",
"max_market_sales",
"max_market_purchases",
"max_final_market_sales",
"max_final_market_purchases",
]
)
for row in market_limits:
replace_nulls = ["." if i is None else i for i in row]
Expand Down
41 changes: 41 additions & 0 deletions tests/system/markets/test_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,47 @@ def test_initialized_components(self):
)
self.assertDictEqual(expected_max_purchases, actual_max_purchases)

# Param: max_final_market_sales
expected_max_final_sales = OrderedDict(
sorted(
market_volume_df.set_index(["market", "timepoint"])
.to_dict()["max_final_market_sales"]
.items()
)
)
for key in expected_max_final_sales.keys():
expected_max_final_sales[key] = float("inf")

actual_max_final_sales = OrderedDict(
sorted(
{
(mrkt, tmp): instance.max_final_market_sales[mrkt, tmp]
for mrkt in instance.MARKETS
for tmp in instance.TMPS
}.items()
)
)
self.assertDictEqual(expected_max_final_sales, actual_max_final_sales)

# Param: max_final_market_purchases
expected_max_final_purchases = OrderedDict(
sorted(
market_volume_df.set_index(["market", "timepoint"])
.to_dict()["max_final_market_purchases"]
.items()
)
)
actual_max_final_purchases = OrderedDict(
sorted(
{
(mrkt, tmp): instance.max_final_market_purchases[mrkt, tmp]
for mrkt in instance.MARKETS
for tmp in instance.TMPS
}.items()
)
)
self.assertDictEqual(expected_max_final_purchases, actual_max_final_purchases)


if __name__ == "__main__":
unittest.main()
Loading