我是Pyomo的新手并使用它来实施传输扩展规划中的一些优化问题。我试着解决下面的模型:
from pyutilib.misc import import_file
from pyomo.environ import *
import networkx as nx
model = ConcreteModel()
model.name = "DTEPM_trial_concrete"
#Sets
#Epoch
model.E = Set(initialize = [0, 1, 2, 3])
model.E_n = Set(model.E, initialize = {0:[1,2,3,4,5], 1:[6,7,8,9,10], 2:[11,12,13,14,15], 3:[16,17,18,19,20]})
#System nodes
model.N = ['N1', 'N2', 'N3']
#model.n_name= Param(model.N)
#T = Set()
model.G = ['G1', 'G2', 'G3']
model.LI = ['L1', 'L2', 'L3']
#Scalar Parameters
model.int_rate = 0.05
model.vll = 3000
model.tau_period = 8760
model.base = 100
model.ref = ['N3']
model.vadegree = 0
def R_discount_inv_init(model, i):
return sum(1 / (1 + model.int_rate)**(i - 1) for i in model.E)
model.cum_disc_inv_cost = Param(model.E, initialize = R_discount_inv_init)
def R_discount_op_init(model, i):
for index in model.E_n:
return sum(1 / (1 + model.int_rate)**(i - 1) for i in model.E_n[index])
model.cum_disc_op_cost = Param(model.E, initialize = R_discount_op_init)
#Demand Periods
model.t_demand = {'N1': 105, 'N2': 210, 'N3': 735}
model.demand_curtailed = Var(model.E, model.N, within = NonNegativeReals)
#Generation Units
model.ge_max = {'G1': 200, 'G2': 200, 'G3': 1000}
model.ge_marginal_cost = {'G1': 30, 'G2': 35, 'G3': 40}
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}
model.li_max_f = 150
model.li_f = {'L1': 100, 'L2': 100, 'L3': 100}
model.li_sending_bus = {'L1': 'N1', 'L2': 'N1', 'L3': 'N2'}
model.li_receiving_bus = {'L1': 'N2', 'L2': 'N3', 'L3': 'N3'}
model.li_length = {'L1': 100, 'L2': 100, 'L3': 100}
#Expansion Options
model.inv_cost_var = 4000000
nodes = ['N1', 'N2', 'N3']
edges = [['N1', 'N2'], ['N1', 'N3'], ['N2', 'N3']]
I = nx.DiGraph()
I.add_nodes_from(nodes)
I.add_edges_from(edges)
model.I = -nx.incidence_matrix(I, oriented=True) # this returns a scipy sparse matrix
#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 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[g] for g in model.G) \
+ sum(model.I[n, l] * model.f[l, e] for l in model.LI) \
- sum(model.t_demand[n] - model.demand_curtailed[n]) == 0
model.SystemBalance = Constraint(model.E, model.N, rule=system_balance)
#OBJECTIVE FUNCTION
def objective_mincost(model):
return sum( model.cum_disc_inv_cost[e] for e in model.E * sum (model.li_f_inv[e, l] * model.inv_cost_var[l] * model.li_length[l]) + model.cum_disc_op_cost[e] * (model.tau_period * (sum(model.ge_max[g] * (model.ge_marginal_cost[g])) + sum(model.demand_curtailed[n] * model.vll))))
model.objective = Objective(rule = objective_mincost, sense = minimize)
opt = SolverFactory('gurobi')
results = opt.solve(model) # solves and updates instance
model.display()
我从运行代码中收到以下错误消息:
错误:为索引(&#39; L2&#39;,0)生成约束line_equation的表达式时规则失败: TypeError:无法转换类型&#39;生成器&#39; (值=。在0x000001B6F840E360&gt;)到数值。 错误:构建组件&line -equation&#39;来自data =无失败: TypeError:无法转换类型&#39;生成器&#39; (值=。在0x000001B6F840E360&gt;)到数值。
请问您如何解决?
谢谢。
答案 0 :(得分:0)
您的线方程定义中似乎缺少sum()
。
答案 1 :(得分:0)
'(model.LI中的l的model.base / model.li_x(l))'是生成器而不是数值,它是语法错误。
您可以仔细检查直流功率流方程。