oemof / oemof-examples

A collection of oemof examples and notebooks.
https://oemof.org/
MIT License
43 stars 68 forks source link

Example for working with dual variables #70

Open jnnr opened 3 years ago

jnnr commented 3 years ago

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:

jnnr commented 3 years ago

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(optimizationmodel.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 commented 3 years ago

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(optimizationmodel.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 commented 2 years ago

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

jnnr commented 2 years ago

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
MaGering commented 2 years ago

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 commented 2 years ago

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?