calliope-project / calliope

A multi-scale energy systems modelling framework
https://www.callio.pe
Apache License 2.0
277 stars 89 forks source link

Add shadow prices (duals) #540

Closed brynpickering closed 5 months ago

brynpickering commented 5 months ago

Fixes issue(s) #283

Summary of changes in this pull request:

Example:

[In]:
import calliope
m = calliope.examples.national_scale()
m.build()
m.backend.shadow_prices.activate()
m.solve()
m.backend.shadow_prices.get("system_balance").to_series().head()

[Out]:
nodes    carriers  timesteps          
region1  power     2005-01-01 00:00:00    0.04
                   2005-01-01 01:00:00    0.04
                   2005-01-01 02:00:00    0.04
                   2005-01-01 03:00:00    0.04
                   2005-01-01 04:00:00    0.04
Name: system_balance, dtype: object

TODO:

Reviewer checklist:

codecov[bot] commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (e2bfb1a) 95.66% compared to head (67f6919) 95.70%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #540 +/- ## ========================================== + Coverage 95.66% 95.70% +0.04% ========================================== Files 24 24 Lines 3526 3562 +36 Branches 765 710 -55 ========================================== + Hits 3373 3409 +36 Misses 87 87 Partials 66 66 ``` | [Files](https://app.codecov.io/gh/calliope-project/calliope/pull/540?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=calliope-project) | Coverage Δ | | |---|---|---| | [src/calliope/backend/backend\_model.py](https://app.codecov.io/gh/calliope-project/calliope/pull/540?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=calliope-project#diff-c3JjL2NhbGxpb3BlL2JhY2tlbmQvYmFja2VuZF9tb2RlbC5weQ==) | `97.84% <100.00%> (+0.10%)` | :arrow_up: | | [src/calliope/backend/pyomo\_backend\_model.py](https://app.codecov.io/gh/calliope-project/calliope/pull/540?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=calliope-project#diff-c3JjL2NhbGxpb3BlL2JhY2tlbmQvcHlvbW9fYmFja2VuZF9tb2RlbC5weQ==) | `94.94% <100.00%> (+0.35%)` | :arrow_up: |
brynpickering commented 5 months ago

Quite considerable memory footprint created by having dual tracking activated in pyomo:

without dual tracking:

image

with dual tracking:

image

Heap size peak changes from 204MB to 370MB for this example case (urban scale, 2 months).

Have updated the implementation to be deactivated by default and to allow user activatation. It also deactivates automatically if trying to run using CBC solver.

brynpickering commented 5 months ago

I wouldn't do anything special for dealing with duals in MILP models. The default in Pyomo is just to return None instead of a value, so I think that's fine?

brynpickering commented 5 months ago

My implementation of shadow prices as a class attribute of the BackendModel is in the directio of how I'd imagine implementing #510.

sjpfenninger commented 5 months ago

Will update changelog and docs later this week