I am looking to set variables in my Pyomo model by using a loop, so that they can be created automatically. However, each variable also contains bounds.
I was hoping that it can loop through a dictionary to assign the bounds to each variable.
Current code
coeffs = [120, 600, 800, 200]
y_target = 165120
rangeStore = []
for index in range(4):
# Define model and variables
model = ConcreteModel()
model.b = Var(bounds=(0,15))
model.c = Var(bounds=(15,60))
model.d = Var(bounds=(60,150))
model.e = Var(bounds=(150,300))
model_x = [model.b,model.c, model.d, model.e]
# Model Constraint
def weight_rule(model):
return sum(coeffs[i] * model_x[i] for i in range(len(coeffs))) == y_target
model.weight = Constraint( rule=weight_rule )
# Model Objective
obj = model_x[index]
model.objective = Objective(expr=obj, sense=minimize)
# Solve the model
solver = SolverFactory('glpk')
solver.solve(model)
# Store value
rangeStore.append([value(model_x[i]) for i in range(len(coeffs))])
Part I want to be created in loop
model.b = Var(bounds=(0,15))
model.c = Var(bounds=(15,60))
model.d = Var(bounds=(60,150))
model.e = Var(bounds=(150,300))