I am new to Pyomo, and am wanting to know how to change the value of an already existing model parameter that has one or more index.
I have seen some examples for scalar parameters, i.e. no index. For example:
model5 = ConcreteModel()
model5.data2 = Param(initialize=10.0, mutable=True)
print("print data2 before")
model5.data2.pprint()
model5.data2 = 999
print("print data2 after")
model5.data2.pprint()
This produces the output:
print data2 before
data2 : Size=1, Index=None, Domain=Any, Default=None, Mutable=True
Key : Value
None : 10.0
print data2 after
data2 : Size=1, Index=None, Domain=Any, Default=None, Mutable=True
Key : Value
None : 999
But if I try and do it with a parameter that has an index I get an error. The following code fails, but probably no surprise because I am trying to assign a Python object to a Pyomo object. What is the correct way to update a parameter with an index (or more than one index)?
model5 = ConcreteModel()
# Make a small set
myList = ['i1', 'i2', 'i3', 'i4']
model5.i = Set(dimen=1, initialize=myList)
# Make a dict for each element in the set and give it the value 10
dataDict = {}
for element in myList:
dataDict[element] = 10
print("print dataDict")
print(dataDict)
# Make the data into a model Param
model5.data = Param(model5.i, initialize=dataDict, mutable=True)
print("print data parameter")
model5.data.pprint()
# Change a values for each element to 999
for element in myList:
dataDict[element] = 999
# Try and update the Param
model5.data = dataDict # THIS FAILS <-- how do I do this?