Please I am new to Pyomo. I have tried to run the following codes:
from pyutilib.misc import import_file
from pyomo.environ import *
model = ConcreteModel()
model.name = "Transmission Investment planning problem_"
#Sets
#Epoch
model.E = RangeSet(0,3) #No. of Epochs
#System nodes
model.N = ['N1', 'N2', 'N3'] #Names of Bus nodes
#model.n_name= Param(model.N)
model.G = ['G1', 'G2', 'G3'] #Names of generators
model.LI = ['L1', 'L2', 'L3'] #Nnames of Transmission lines
#Scalar Parameters
model.int_rate = 0.05 #interest rate
model.vll = 3000 #value of loss load(£/MWh)
model.tau_period = 8760 #Time duration of demand period (hours)
model.base = 100 #MVA base
model.ref = {'N3'} #reference node
model.vadegree = 0 #phase angle of reference node
#Discount factors
L = 5
Y= len(model.E)*L
irate = range(0,(Y-1))
def disc_factor(i):
disc = [1/((1 + model.int_rate)**i) for i in irate]
return disc
model.cum_disc_inv_cost = [sum(disc_factor(i)[(i*L):]) for i in model.E] #investment discount factor
model.cum_disc_op_cost = [sum(disc_factor(i)[(i*L):((i+1)*L)]) for i in model.E] #operation discount factor
#Demand Periods
model.t_demand = {'N1': 105, 'N2': 210, 'N3': 735} #demand at nodes (MW)
model.demand_curtailed = Var(model.E, model.N, initialize = 0) #curtailed demand (MW)
#Generation Units
model.ge_max = {'G1': 200, 'G2': 200, 'G3': 1000} #maximum stable power generation(MW)
model.ge_marginal_cost = {'G1': 30, 'G2': 35, 'G3': 40} #marginal cost of generation units (£/MWh)
#Bus to generation incidence matrix
model.B = {('N1','G1'): 1, ('N1','G2'): 0, ('N1','G3'): 0, ('N2','G1'): 0, ('N2','G2'): 1, ('N2','G3'): 0, ('N3','G1'): 0, ('N3','G2'): 0, ('N3','G3'): 1,}
#Transmission lines
model.li_x = {'L1': 0.2, 'L2': 0.2, 'L3': 0.2} #reactance of transmission line(p.u)
model.li_max_f = 150 #maximum capaciy provided for line expansion (MW)
model.li_f = {'L1': 100, 'L2': 100, 'L3': 100} #initial capacity for line l (MW)
model.li_sending_bus = {'L1': 'N1', 'L2': 'N1', 'L3': 'N2'} #sending bus for line l
model.li_receiving_bus = {'L1': 'N2', 'L2': 'N3', 'L3': 'N3'} #receiving bus for line l
model.li_length = {'L1': 100, 'L2': 100, 'L3': 100} #length of line l (km)
#Expansion Options
model.inv_cost_var = 4000000 #Annuitized variable investment cost for line l (£/MW.km.yr)
#Bus to line incidence matrix
model.I = {('N1','L1'): 1, ('N1','L2'): 1, ('N1','L3'): 0, ('N2','L1'): -1, ('N2','L2'): 0, ('N2','L3'): 1, ('N3','L1'): 0, ('N3','L2'): -1, ('N3','L3'): -1,}
#Variables
#Transmission line power flow limits
def fl_inv(model, i, l):
return (0, model.li_max_f)
model.li_f_inv = Var(model.E, model.LI, bounds = fl_inv) #transmission capacity to be built for line l (MW)
#Transmission line investment and operation contraints
model.f = Var(model.LI, model.E, initialize=0)
def fl_rule(model, l, j, i):
if i:
return model.f[l,j] >= -(model.li_f_inv[j,l] + model.li_f[l])
else:
return model.f[l,j] <= (model.li_f_inv[j,l] + model.li_f[l])
model.bound_f = Constraint(model.LI, model.E, [0,1], rule=fl_rule)
##generation limit
def fg(model, i, g):
return (0, model.ge_max[g])
model.ge_output = Var(model.E, model.G, initialize = 0, bounds = fg)
#phase angles for the nodes
def theta(model, e, n):
for n in model.N:
if n == model.ref:
model.theta[e, n].fixed = True
return model.vadegree
else: return 0
model.theta = Var(model.E, model.N, initialize = theta)
def line_equation(model, l, e):
return model.f[l, e] == model.base/model.li_x[l] *(sum(model.theta[e, n] for n in model.N if n == model.li_sending_bus[l]) - sum(model.theta[e, n] for n in model.N if n == model.li_receiving_bus[l]))
model.line_equation = Constraint(model.LI, model.E, rule = line_equation)
def system_balance(model, e, n):
return sum(model.B[n, g] * model.ge_output[e, g] for g in model.G) \
+ sum(model.I[n, l] * model.f[l, e] for l in model.LI) \
- model.t_demand[n] + model.demand_curtailed[e, n] == 0
model.SystemBalance = Constraint(model.E, model.N, rule=system_balance)
#OBJECTIVE FUNCTION
def objective_mincost(model):
for i in model.E:
return sum(model.cum_disc_inv_cost[i] * sum(model.li_f_inv[i, l] * model.inv_cost_var * model.li_length[l] for l in model.LI) + model.cum_disc_op_cost[i] * (model.tau_period * (sum(model.ge_max[g] * (model.ge_marginal_cost[g]) for g in model.G) + sum(model.demand_curtailed[i, n] for n in model.N * model.vll))))
model.objective = Objective(rule = objective_mincost, sense = minimize)
opt = SolverFactory('gurobi')
results = opt.solve(model) # solves and updates instance
model.display()
I got the following errorcodes:
ERROR: Rule failed when generating expression for objective objective: TypeError: '_SumExpression' object is not iterable ERROR: Constructing component 'objective' from data=None failed: TypeError: '_SumExpression' object is not iterable
Please any suggestions on where the problem might be and possible solution?
Thank you.
You have two places where model.E is multiplied by something that is not a set. You probably meant to parenthesize your sums differently (i.e., this is mostly a problem with how your sums are organized)