I am new to Python and I cannot seem to solve the problem I am having, even after going through similar problems posted on the forum.
My problem is that I get the ''SumExpression' object is not iterable' error on my objective function.
My code:
import pyomo.environ as pyo
from pyomo.opt import SolverFactory
model=pyo.ConcreteModel()
#C = contract capacity
D = [5048,5144,4616,4296,3696,3392,5048,5144,4616,4296,3696,3392]
R = [166.9,166.9,166.9,166.9,166.9,166.9,166.9,166.9,223.6,223.6,223.6,223.6]
PF=[0.027 for i in range (12)]
E=[1319.25,1319.25,1319.25,1319.25,1319.25,1319.25,1319.25,1319.25,1759,1759,1759,1759]
model.M=pyo.RangeSet(0,11)
model.X = pyo.Var(model.M, domain=pyo.NonNegativeReals)
model.C = pyo.Var(model.M, domain=pyo.NonNegativeReals)
model.Y = pyo.Var(model.M, domain=pyo.NonNegativeReals)
def constraint_1(model,M):
return model.X[M]+model.C[M] >= D[M]
model.constraint_1 = pyo.Constraint(model.M, rule=constraint_1)
def constraint_2(model,M):
return model.Y[M]+1.1*model.C[M] >= D[M]
model.constraint_2 = pyo.Constraint(model.M, rule=constraint_2)
def constraint_3(model,M):
if M == 11:
return model.C[0] >= model.C[M]
else:
return model.C[M+1] >= model.C[M]
model.constraint_3 = pyo.Constraint(model.M, rule=constraint_3)
def mod_obj(model,M):
if M==11:
return sum((1-PF[M])*R[M]*model.C[M]+2*R[M]*model.X[M]+R[M]*model.Y[M]+E[M]*(model.C[0]-model.C[M]))
else:
return sum((1-PF[M])*R[M]*model.C[M]+2*R[M]*model.X[M]+R[M]*model.Y[M]+E[M]*(model.C[M+1]-model.C[M]))
model.mod_obj = pyo.Constraint(model.M, rule=mod_obj)
model.obj=pyo.Objective(rule=mod_obj, sense=pyo.minimize)
opt = SolverFactory('glpk')
opt.solve(model)
model.pprint()
The error I get is from the second line in the objective function:
return sum((1-PF[M])*R[M]*model.C[M]+2*R[M]*model.X[M]+R[M]*model.Y[M]+E[M]*(model.C[M+1]-model.C[M]))
I think it has to do something with the M+1, as the first line does not give out an error but I cannot seem to get through this error and appreciate your help/input.
Thanks in advance!