Reputation: 145
I am quite new to Pyomo but not to Python.
I have the following problem:
import pyomo.environ as pyo
model = pyo.ConcreteModel()
model.A = pyo.Set(initialize=range(7))
model.B = pyo.Set(initialize=range(7))
model.AB = pyo.Set(initilize= list((a,b) for a in model.A for b in model.B)
model.X = pyo.Var(model.A, model.B, within=pyo.Binary)
model.objective = pyo.Objective(... maximize)
solver = pyo.SolverFactory('glpk')
result = solver.solve(model)
Next I want to update the model.B Set() and as aconsequence modify de model.AB Set and the model.X Var so I can solve it again. For example, I want to set:
model.B = pyo.Set(initialize=range(4)
Then update the model.AB, model.X and solve again. If done that manually:
model.B = pyo.Set(initialize=range(4)
model.AB = pyo.Set(initilize= list((a,b) for a in model.A for b in model.B)
model.X = pyo.Var(model.A, model.B, within=pyo.Binary)
result = solver.solve(model)
Thats not working, it gives the following error:
RuntimeError: Cannot add component 'X_index' (type <class 'pyomo.core.base.sets._SetProduct'>) to block 'unknown': a component by that name (type <class 'pyomo.core.base.sets._SetProduct'>) is already defined.
Upvotes: 1
Views: 2815
Reputation: 440
Basically, you need to delete the variable and its index before you create it again. Same goes for the sets.
Thus, essentially, you need to add the following lines before you redefine the new components:
model.del_component(model.B)
model.del_component(model.AB)
model.del_component(model.X)
model.del_component(model.X_index)
Then you can proceed to redefine them:
model.B = pyo.Set(initialize=range(4)
model.AB = pyo.Set(initialize= list((a,b) for a in model.A for b in model.B))
model.X = pyo.Var(model.A, model.B, within=pyo.Binary)
Upvotes: 2