oemof / oemof-solph

A model generator for energy system modelling and optimisation (LP/MILP).
https://oemof.org
MIT License
282 stars 124 forks source link

Cannot solve a restored model #781

Open Fernando3161 opened 2 years ago

Fernando3161 commented 2 years ago
  1. I create a simple energy system (based on the example).
  2. I dump the energy system (without solving it).
  3. I restore the energy system
  4. I attempt to create and solve a model-- Model(energy_system)
  5. I receive an error "KeyError: "<oemof.solph.network.Bus: 'natural_gas'>"

When I solve the model directly after creating the energy system (skipping the dumping and restoring steps), I can get results without problem.

Is this a general or known issue or is there a problem with pickle?

oemof-solph 0.4.2

uvchik commented 2 years ago

@Fernando3161, it is an unnoticed bug, because most people dump the energy system after(!) they solved it and not before.

It seems that the structure of the object is somehow damaged by pickling and unpickling. Is it urgent for you or do have a workaround? I think it is a little more complicated to fix it.

I could reproduce the bug using the basic_example. I just dumped and restored the energysystem before building the model in line 183 and got the same error:

Traceback (most recent call last):
  File ".../oemof/oemof-examples/oemof_examples/oemof.solph/v0.4.x/basic_example/basic_example.py", line 188, in <module>
    model = solph.Model(energysystem)
  File ".../my_python/lib/python3.7/site-packages/oemof/solph/models.py", line 271, in __init__
    super().__init__(energysystem, **kwargs)
  File ".../my_python/lib/python3.7/site-packages/oemof/solph/models.py", line 91, in __init__
    self._constraint_groups += [i for i in self.es.groups
  File ".../my_python/lib/python3.7/site-packages/oemof/network/energy_system.py", line 163, in groups
    maxlen=0,
  File ".../my_python/lib/python3.7/site-packages/oemof/network/energy_system.py", line 161, in <genexpr>
    for n in self.nodes[self._first_ungrouped_node_index_:]
  File ".../my_python/lib/python3.7/site-packages/oemof/network/groupings.py", line 301, in __call__
    ((s, n, f) for (s, f) in n.inputs.items()),
  File ".../my_python/lib/python3.7/site-packages/oemof/network/groupings.py", line 301, in <genexpr>
    ((s, n, f) for (s, f) in n.inputs.items()),
  File ".../my_python/lib/python3.7/_collections_abc.py", line 744, in __iter__
    yield (key, self._mapping[key])
  File ".../my_python/lib/python3.7/site-packages/oemof/network/network.py", line 46, in __getitem__
    return key.outputs.__getitem__(self.target)
  File ".../my_python/lib/python3.7/collections/__init__.py", line 1025, in __getitem__
    raise KeyError(key)
KeyError: "<oemof.solph.network.Bus: 'natural_gas'>"
Fernando3161 commented 2 years ago

I found this since I wanted to build bigger interconnected Energy Systems based on small energy systems, but I found a way around (I had to skip my original Dump-Restore idea). I wanted to submit the issue since it seemed to go unnoticed.

gnn commented 2 years ago

I found this since I wanted to build bigger interconnected Energy Systems based on small energy systems, but I found a way around (I had to skip my original Dump-Restore idea).

That's pretty interesting. It's beside the point of this issue, but I'm interested in how you would do this. Because there are ways of doing this, but they might not be immediately obvious. So if you want to, you can shoot me an email on how you did this to "gnn -dot- code -at- gmail -dot- com".

I wanted to submit the issue since it seemed to go unnoticed.

That's very much appreciated. :+1: :)

jnnr commented 2 years ago

This or at least a similar problem with dump/restore has shown up already for some users, me as well. We tried to track the error down further and learned something, but not enough to fix it unfortunately.

See this related issue: https://github.com/oemof/oemof.network/issues/10 and #742.

FabianBenz48 commented 2 years ago

I found this since I wanted to build bigger interconnected Energy Systems based on small energy systems, but I found a way around (I had to skip my original Dump-Restore idea). I wanted to submit the issue since it seemed to go unnoticed.

Hey, can u do me a favor and post your work around here?