-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store a table name inside TulipaVariable and TulipaConstraint
With the table_name, we use a different constructor, based only on the connection and table name.
- Loading branch information
1 parent
f6b1613
commit 8a8e51f
Showing
5 changed files
with
393 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,174 +1,182 @@ | ||
export compute_constraints_indices | ||
|
||
function compute_constraints_indices(connection) | ||
constraints = Dict{Symbol,TulipaConstraint}() | ||
# TODO: Format SQL queries consistently (is there a way to add a linter/formatter?) | ||
_create_constraints_tables(connection) | ||
|
||
constraints[:lowest] = TulipaConstraint( | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TEMP SEQUENCE id START 1; | ||
SELECT | ||
nextval('id') AS index, | ||
asset.asset, | ||
t_low.year, | ||
t_low.rep_period, | ||
t_low.time_block_start, | ||
t_low.time_block_end, | ||
FROM t_lowest_all_flows AS t_low | ||
LEFT JOIN asset | ||
ON t_low.asset = asset.asset | ||
WHERE | ||
asset.type in ('conversion', 'producer') | ||
ORDER BY | ||
asset.asset, | ||
t_low.year, | ||
t_low.rep_period, | ||
t_low.time_block_start | ||
", | ||
) |> DataFrame, | ||
constraints = Dict{Symbol,TulipaConstraint}( | ||
key => TulipaConstraint(connection, "cons_$key") for key in ( | ||
:lowest, | ||
:highest_in_out, | ||
:highest_in, | ||
:highest_out, | ||
:units_on_and_outflows, | ||
:storage_level_intra_rp, | ||
:storage_level_inter_rp, | ||
:min_energy_inter_rp, | ||
:max_energy_inter_rp, | ||
) | ||
) | ||
|
||
constraints[:highest_in_out] = TulipaConstraint( | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TEMP SEQUENCE id START 1; | ||
SELECT | ||
nextval('id') AS index, | ||
t_high.* | ||
FROM t_highest_all_flows AS t_high | ||
LEFT JOIN asset | ||
ON t_high.asset = asset.asset | ||
LEFT JOIN asset_both | ||
ON t_high.asset = asset_both.asset | ||
AND t_high.year = asset_both.milestone_year | ||
AND t_high.year = asset_both.commission_year | ||
WHERE | ||
asset_both.active = true | ||
AND asset.type in ('hub', 'consumer')", | ||
) |> DataFrame, | ||
return constraints | ||
end | ||
|
||
function _create_constraints_tables(connection) | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TEMP SEQUENCE id START 1; | ||
CREATE OR REPLACE TABLE cons_lowest AS | ||
SELECT | ||
nextval('id') AS index, | ||
asset.asset, | ||
t_low.year, | ||
t_low.rep_period, | ||
t_low.time_block_start, | ||
t_low.time_block_end, | ||
FROM t_lowest_all_flows AS t_low | ||
LEFT JOIN asset | ||
ON t_low.asset = asset.asset | ||
WHERE | ||
asset.type in ('conversion', 'producer') | ||
ORDER BY | ||
asset.asset, | ||
t_low.year, | ||
t_low.rep_period, | ||
t_low.time_block_start | ||
", | ||
) | ||
|
||
constraints[:highest_in] = TulipaConstraint( | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TEMP SEQUENCE id START 1; | ||
SELECT | ||
nextval('id') AS index, | ||
t_high.* | ||
FROM t_highest_in_flows AS t_high | ||
LEFT JOIN asset | ||
ON t_high.asset = asset.asset | ||
LEFT JOIN asset_both | ||
ON t_high.asset = asset_both.asset | ||
AND t_high.year = asset_both.milestone_year | ||
AND t_high.year = asset_both.commission_year | ||
WHERE | ||
asset_both.active = true | ||
AND asset.type in ('storage')", | ||
) |> DataFrame, | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TEMP SEQUENCE id START 1; | ||
CREATE OR REPLACE TABLE cons_highest_in_out AS | ||
SELECT | ||
nextval('id') AS index, | ||
t_high.* | ||
FROM t_highest_all_flows AS t_high | ||
LEFT JOIN asset | ||
ON t_high.asset = asset.asset | ||
LEFT JOIN asset_both | ||
ON t_high.asset = asset_both.asset | ||
AND t_high.year = asset_both.milestone_year | ||
AND t_high.year = asset_both.commission_year | ||
WHERE | ||
asset_both.active = true | ||
AND asset.type in ('hub', 'consumer')", | ||
) | ||
|
||
constraints[:highest_out] = TulipaConstraint( | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TEMP SEQUENCE id START 1; | ||
SELECT | ||
nextval('id') AS index, | ||
t_high.* | ||
FROM t_highest_out_flows AS t_high | ||
LEFT JOIN asset | ||
ON t_high.asset = asset.asset | ||
LEFT JOIN asset_both | ||
ON t_high.asset = asset_both.asset | ||
AND t_high.year = asset_both.milestone_year | ||
AND t_high.year = asset_both.commission_year | ||
WHERE | ||
asset_both.active = true | ||
AND asset.type in ('producer', 'storage', 'conversion')", | ||
) |> DataFrame, | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TEMP SEQUENCE id START 1; | ||
CREATE OR REPLACE TABLE cons_highest_in AS | ||
SELECT | ||
nextval('id') AS index, | ||
t_high.* | ||
FROM t_highest_in_flows AS t_high | ||
LEFT JOIN asset | ||
ON t_high.asset = asset.asset | ||
LEFT JOIN asset_both | ||
ON t_high.asset = asset_both.asset | ||
AND t_high.year = asset_both.milestone_year | ||
AND t_high.year = asset_both.commission_year | ||
WHERE | ||
asset_both.active = true | ||
AND asset.type in ('storage')", | ||
) | ||
|
||
constraints[:units_on_and_outflows] = TulipaConstraint( | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TEMP SEQUENCE id START 1; | ||
CREATE OR REPLACE TABLE units_on_and_outflows AS | ||
SELECT | ||
nextval('id') AS index, | ||
t_high.* | ||
FROM t_highest_assets_and_out_flows AS t_high | ||
LEFT JOIN asset | ||
ON t_high.asset = asset.asset | ||
LEFT JOIN asset_both | ||
ON t_high.asset = asset_both.asset | ||
AND t_high.year = asset_both.milestone_year | ||
AND t_high.year = asset_both.commission_year | ||
WHERE | ||
asset_both.active = true | ||
AND asset.type in ('producer', 'conversion') | ||
AND asset.unit_commitment = true; | ||
SELECT * FROM units_on_and_outflows | ||
", | ||
) |> DataFrame, | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TEMP SEQUENCE id START 1; | ||
CREATE OR REPLACE TABLE cons_highest_out AS | ||
SELECT | ||
nextval('id') AS index, | ||
t_high.* | ||
FROM t_highest_out_flows AS t_high | ||
LEFT JOIN asset | ||
ON t_high.asset = asset.asset | ||
LEFT JOIN asset_both | ||
ON t_high.asset = asset_both.asset | ||
AND t_high.year = asset_both.milestone_year | ||
AND t_high.year = asset_both.commission_year | ||
WHERE | ||
asset_both.active = true | ||
AND asset.type in ('producer', 'storage', 'conversion')", | ||
) | ||
|
||
constraints[:storage_level_intra_rp] = TulipaConstraint( | ||
DuckDB.query( | ||
connection, | ||
"SELECT * FROM storage_level_intra_rp | ||
", | ||
) |> DataFrame, | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TEMP SEQUENCE id START 1; | ||
CREATE OR REPLACE TABLE cons_units_on_and_outflows AS | ||
SELECT | ||
nextval('id') AS index, | ||
t_high.* | ||
FROM t_highest_assets_and_out_flows AS t_high | ||
LEFT JOIN asset | ||
ON t_high.asset = asset.asset | ||
LEFT JOIN asset_both | ||
ON t_high.asset = asset_both.asset | ||
AND t_high.year = asset_both.milestone_year | ||
AND t_high.year = asset_both.commission_year | ||
WHERE | ||
asset_both.active = true | ||
AND asset.type in ('producer', 'conversion') | ||
AND asset.unit_commitment = true; | ||
", | ||
) | ||
|
||
constraints[:storage_level_inter_rp] = TulipaConstraint( | ||
DuckDB.query( | ||
connection, | ||
"SELECT * FROM storage_level_inter_rp | ||
", | ||
) |> DataFrame, | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TABLE cons_storage_level_intra_rp AS | ||
SELECT * FROM var_storage_level_intra_rp | ||
", | ||
) | ||
|
||
constraints[:min_energy_inter_rp] = TulipaConstraint( | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TEMP SEQUENCE id START 1; | ||
SELECT | ||
nextval('id') AS index, | ||
attr.asset, | ||
attr.year, | ||
attr.period_block_start, | ||
attr.period_block_end, | ||
FROM asset_timeframe_time_resolution AS attr | ||
LEFT JOIN asset_milestone | ||
ON attr.asset = asset_milestone.asset | ||
AND attr.year = asset_milestone.milestone_year | ||
WHERE | ||
asset_milestone.min_energy_timeframe_partition IS NOT NULL | ||
", | ||
) |> DataFrame, | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TABLE cons_storage_level_inter_rp AS | ||
SELECT * FROM var_storage_level_inter_rp | ||
", | ||
) | ||
|
||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TEMP SEQUENCE id START 1; | ||
CREATE OR REPLACE TABLE cons_min_energy_inter_rp AS | ||
SELECT | ||
nextval('id') AS index, | ||
attr.asset, | ||
attr.year, | ||
attr.period_block_start, | ||
attr.period_block_end, | ||
FROM asset_timeframe_time_resolution AS attr | ||
LEFT JOIN asset_milestone | ||
ON attr.asset = asset_milestone.asset | ||
AND attr.year = asset_milestone.milestone_year | ||
WHERE | ||
asset_milestone.min_energy_timeframe_partition IS NOT NULL | ||
", | ||
) | ||
|
||
# a -> any(!ismissing, values(a.max_energy_timeframe_partition)), | ||
|
||
constraints[:max_energy_inter_rp] = TulipaConstraint( | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TEMP SEQUENCE id START 1; | ||
SELECT | ||
nextval('id') AS index, | ||
attr.asset, | ||
attr.year, | ||
attr.period_block_start, | ||
attr.period_block_end, | ||
FROM asset_timeframe_time_resolution AS attr | ||
LEFT JOIN asset_milestone | ||
ON attr.asset = asset_milestone.asset | ||
AND attr.year = asset_milestone.milestone_year | ||
WHERE | ||
asset_milestone.max_energy_timeframe_partition IS NOT NULL | ||
", | ||
) |> DataFrame, | ||
DuckDB.query( | ||
connection, | ||
"CREATE OR REPLACE TEMP SEQUENCE id START 1; | ||
CREATE OR REPLACE TABLE cons_max_energy_inter_rp AS | ||
SELECT | ||
nextval('id') AS index, | ||
attr.asset, | ||
attr.year, | ||
attr.period_block_start, | ||
attr.period_block_end, | ||
FROM asset_timeframe_time_resolution AS attr | ||
LEFT JOIN asset_milestone | ||
ON attr.asset = asset_milestone.asset | ||
AND attr.year = asset_milestone.milestone_year | ||
WHERE | ||
asset_milestone.max_energy_timeframe_partition IS NOT NULL | ||
", | ||
) | ||
|
||
return constraints | ||
return | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.