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

Example for working with dual variables #70

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft

Conversation

jnnr
Copy link
Member

@jnnr jnnr commented Feb 18, 2021

This PR gives a draft of an example showing how to work with dual variables in oemof.solph. Final aim is to transparently show how to work with duals to e.g. get electricity or CO2 prices from a model.

At the moment, it also serves to check to see how model.receive_duals() fits in the rest of the common optimization workflow.

This is how the plot looks like currently:
Dispatch_with_duals

TODO:

  • Check pyomo warning (see below)
  • Add a plot of the some duals to make the example more illustrative.
  • Polish script and plot
    • Change order of stacked dispatch according to merit order (renewables first, ascending variable costs)
    • Colors
    • Fix axis for time

@jnnr
Copy link
Member Author

jnnr commented Feb 18, 2021

The results processing does not work properly after calling receive_duals

Traceback (most recent call last):
File "/.virtualenvs/oemof-solph/lib/python3.6/site-packages/pyomo/common/collections/component_map.py", line 100, in getitem
return self._dict[id(obj)][1]
KeyError: 139633195285288

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "simple_dispatch_with_duals.py", line 195, in
data = views.node(optimization_model.results(), "bel")
File "oemof-solph/src/oemof/solph/models.py", line 171, in results
return processing.results(self)
File "oemof-solph/src/oemof/solph/processing.py", line 162, in results
duals = [om.dual[om.Bus.balance[bus, t]] for _, t in timesteps]
File "oemof-solph/src/oemof/solph/processing.py", line 162, in
duals = [om.dual[om.Bus.balance[bus, t]] for _, t in timesteps]
File "/.virtualenvs/oemof-solph/lib/python3.6/site-packages/pyomo/common/collections/component_map.py", line 103, in getitem
% (id(obj), str(obj)))
KeyError: "Component with id '139633195285288': Bus.balance[b_heat_source,0]"

@jnnr
Copy link
Member Author

jnnr commented Feb 18, 2021

Before that fails, pyomo gives some warnings

Traceback (most recent call last):
File "/.virtualenvs/oemof-solph/lib/python3.6/site-packages/pyomo/common/collections/component_map.py", line 100, in getitem
return self._dict[id(obj)][1]
KeyError: 139633195285288

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "simple_dispatch_with_duals.py", line 195, in
data = views.node(optimization_model.results(), "bel")
File "oemof-solph/src/oemof/solph/models.py", line 171, in results
return processing.results(self)
File "oemof-solph/src/oemof/solph/processing.py", line 162, in results
duals = [om.dual[om.Bus.balance[bus, t]] for _, t in timesteps]
File "oemof-solph/src/oemof/solph/processing.py", line 162, in
duals = [om.dual[om.Bus.balance[bus, t]] for _, t in timesteps]
File "/.virtualenvs/oemof-solph/lib/python3.6/site-packages/pyomo/common/collections/component_map.py", line 103, in getitem
% (id(obj), str(obj)))
KeyError: "Component with id '139633195285288': Bus.balance[b_heat_source,0]"

@jnnr jnnr removed the bug label Jan 6, 2022
@jnnr
Copy link
Member Author

jnnr commented Jan 6, 2022

The errors described above were no bug, but caused by me using it wrongly - Model.receive_duals must be called before Model.solve.

@jnnr
Copy link
Member Author

jnnr commented Jan 6, 2022

However, I get a warning - not sure if this is ok or not, @oemof/oemof-solph ?

WARNING: Reassigning the non-component attribute rc on block (model).Model
    with a new Component with type <class 'pyomo.core.base.suffix.Suffix'>.
    This is usually indicative of a modelling error. To avoid this warning,
    explicitly delete the attribute:
        del Model.rc

@jnnr jnnr self-assigned this Jan 6, 2022
@p-snft p-snft marked this pull request as draft January 10, 2022 13:14
@MaGering
Copy link
Collaborator

However, I get a warning - not sure if this is ok or not, @oemof/oemof-solph ?

WARNING: Reassigning the non-component attribute rc on block (model).Model
    with a new Component with type <class 'pyomo.core.base.suffix.Suffix'>.
    This is usually indicative of a modelling error. To avoid this warning,
    explicitly delete the attribute:
        del Model.rc

Addition: The warning is printed within these lines in pyomo's block.py

@MaGering
Copy link
Collaborator

MaGering commented Feb 2, 2022

I have fixed the timeline. In the stacked plot, the merit order is now taken into account and the colors are assigned more intuitively. Can you review the changes @jnnr?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants