Pyomo / pyomo

An object-oriented algebraic modeling language in Python for structured optimization problems.
https://www.pyomo.org
Other
1.95k stars 504 forks source link

Component.construct not working? #2536

Closed wenjingsu16 closed 1 year ago

wenjingsu16 commented 1 year ago

Hi, I see that component.reconstruct is removed from pyomo. I followed the instructions to use clear and construct. I tried component.clear() and then component.construct(data=data). The construct does not seem to be working and the component is still the same as the component after clear. May I ask why this happens? I defined my component as mutable and gave it an initial value.

model.a = Param(model.NODES, mutable=True,initialize=a_dict)
model.a.clear()
model.a.construct(data = new_dict)
model.a.pprint()

The parameter is expected to be using new_dict values. However the parameter looks like the following.

a : Size=0, Index=NODES, Domain=Any, Default=None, Mutable=True
    Key : Value
...

Information on your system

Pyomo version: 5.7.1 Python version: 3.7 Operating system: Mac OS How Pyomo was installed (PyPI, conda, source): conda Solver (if applicable): Cplex

Additional information

jsiirola commented 1 year ago

This is expected behavior. clear() removes all modeling components from an indexed component (in this case, _ParamData objects from the Param). You almost certainly do NOT want to do this (we should probably deprecate/remove that API like we did for reconstruct()).

The whole point of a mutable parameter is that you can change it's value without regenerating the Param (and all the constraints that reference it). What you probably want to do is to use store_values():

>>> from pyomo.environ import *
>>> m = ConcreteModel()
>>> m.p = Param([1,2,3], mutable=True)
>>> m.pprint()
1 Set Declarations
    p_index : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {1, 2, 3}

1 Param Declarations
    p : Size=0, Index=p_index, Domain=Any, Default=None, Mutable=True
        Key : Value

2 Declarations: p_index p
>>> m.p.store_values(0)
>>> m.pprint()
1 Set Declarations
    p_index : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {1, 2, 3}

1 Param Declarations
    p : Size=3, Index=p_index, Domain=Any, Default=None, Mutable=True
        Key : Value
          1 :     0
          2 :     0
          3 :     0

2 Declarations: p_index p
>>> m.p.store_values({1:50, 2:25, 3:100})
>>> m.pprint()
1 Set Declarations
    p_index : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {1, 2, 3}

1 Param Declarations
    p : Size=3, Index=p_index, Domain=Any, Default=None, Mutable=True
        Key : Value
          1 :    50
          2 :    25
          3 :   100

2 Declarations: p_index p
wenjingsu16 commented 1 year ago

Thank you for the detailed explanation!