-
Notifications
You must be signed in to change notification settings - Fork 20
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
Create constraints with new TulipaConstraint object #928
Conversation
Benchmark Results
Benchmark PlotsA plot of the benchmark results have been uploaded as an artifact to the workflow run for this PR. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #928 +/- ##
==========================================
- Coverage 96.17% 95.76% -0.41%
==========================================
Files 30 30
Lines 1202 1110 -92
==========================================
- Hits 1156 1063 -93
- Misses 46 47 +1 ☔ View full report in Codecov by Sentry. |
Update highest resolution table computation to use union tables and move highest_... out of dataframes Create TulipaConstraint structure. Instead of incoming and outgoing expressions, create a dictionary of expressions inside TulipaConstraint. This is necessary at the moment because some additional expressions were being created in addition to incoming_flow and outgoing_flow. Create TulipaConstraint objects for all other constraints, including the ones that are copies of variables. Remove the dataframes variable completely. Clean up add_..._constraints signatures and related places
56f019f
to
17eab49
Compare
incoming_flow_storage_inter_rp_balance, | ||
outgoing_flow_storage_inter_rp_balance, | ||
) | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we be explicit about the return? I mean like this? I am unsure if it makes a difference
return | |
return nothing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just a coding style choice, it doesn't make a difference (https://docs.julialang.org/en/v1/manual/functions/#Returning-nothing). I don't think there is a JuliaFormatter option to force one or the other. There is an option to force writing return
(https://domluna.github.io/JuliaFormatter.jl/dev/#always_use_return, #796 ), but it's something else.
If we do decide on anything, we should add it to https://github.com/TulipaEnergy/TulipaEnergyModel.jl/blob/main/docs/src/91-developer.md#code-format-and-guidelines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I like enforcing the return, but I don't have a strong opinion on the nothing
. So, I created #940 for this.
# Outgoing flows | ||
DuckDB.execute( | ||
connection, | ||
"CREATE OR REPLACE TEMP TABLE t_union_out_flows AS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These new tables are available in the connection object. Having an overview of all tables available in the connection will be nice, including which ones are read from the files/DB/csv and the ones that are created internally in the model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the Miro description. When finished it can be rewritten into an official SQL description if desired, to have something in code format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, I have created #941 to keep it in our minds.
constraints[:storage_level_intra_rp] = TulipaConstraint( | ||
DuckDB.query( | ||
connection, | ||
"SELECT * FROM storage_level_intra_rp | ||
", | ||
) |> DataFrame, | ||
) | ||
|
||
constraints[:storage_level_inter_rp] = TulipaConstraint( | ||
DuckDB.query( | ||
connection, | ||
"SELECT * FROM storage_level_inter_rp | ||
", | ||
) |> DataFrame, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is because the both the constraint and the variable use the same created table in the connection. Is that correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's correct
# Incoming flows | ||
DuckDB.execute( | ||
connection, | ||
"CREATE OR REPLACE TEMP TABLE t_union_in_flows AS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it okay to just use CREATE
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be, in the final version we can change to that, if preferred. However, for development it makes it easier to rerun some functions without rerunning everything. If only CREATE
is used and the table exists, it will fail and then you have to manually delete the table or create a new connection
object.
DuckDB.execute( | ||
connection, | ||
"CREATE OR REPLACE TEMP TABLE t_union_in_flows AS | ||
SELECT DISTINCT to_asset as asset, year, rep_period, time_block_start, time_block_end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here, okay to use only "SELECT"? Do we have duplicates?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this table it is ok to write only SELECT
because they are filtered later in the resolution computation, but might as well do it now.
There are probably duplicates because this ignores from_asset
.
SELECT DISTINCT asset, year, rep_period, time_block_start, time_block_end | ||
FROM asset_time_resolution | ||
UNION | ||
FROM t_union_out_flows |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I'm also learning SQL a bit in the process: is it fine to neglect SELECT
after UNION
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not for all SQL languages, but DuckDB allows writing FROM table
without the SELECT
. It means SELECT * FROM table
. We can be explicit, if preferred
function compute_constraints_indices(connection) | ||
constraints = Dict{Symbol,TulipaConstraint}() | ||
|
||
constraints[:lowest] = TulipaConstraint( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm checking miro at the same time, there we have lowest_allflows, lowest_all? Which one is this, and better naming (or for later)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, this seems to be an in-between step that is not shown on miro...so it takes t_lowest_all_flows (line 17) and then filter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still uses the old table names. Miro shows the tables after renaming and some splitting, which hasn't happened yet to avoid further complications. There are some purple stickers in the bottom of the constraint tables showing where they come from.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the replies and updates. I created a couple of issues to work on after we have all the tables on MIRO in the code and a final overview of them.
Update highest resolution table computation to use union tables and move highest_... out of dataframes.
Create
TulipaConstraint
structure.Instead of incoming and outgoing expressions, create a dictionary of expressions inside TulipaConstraint.
This is necessary at the moment because some additional expressions were being created in addition to incoming_flow and outgoing_flow.
Create
TulipaConstraint
objects for all other constraints, including the ones that are copies of variables.Remove the
dataframes
variable completely.Clean up add_..._constraints signatures and related places
Closes #927, #918, #895, #611