TypeError:'Var'对象不可迭代

时间:2017-11-22 12:47:12

标签: python gurobi

from gurobipy import *
# Two kinds of products to be transported
product = ['1', '2 ']
# 12 nodes = 12 stations
node = ['1', '2 ', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']    

# Different parameters corresponding to different orbits
# Capacity,Number of tracks,Fixed cost of transportation
link, capacity, maxFrequency, fixcharge = multidict({
    ('1', '2'): [11, 25, [25, 30, 32]],
    ('1', '3'): [10, 30, [20, 24, 26]],
    ('1', '4'): [5, 10, [9, 15, 19]],
    ('2', '4'): [5, 15, [9, 14, 16]],
    ('2', '6'): [4, 40, [11, 15, 17]],
    ('2', '8'): [6, 30, [13, 15, 18]],
    ('3', '5'): [3, 35, [27, 31, 33]],
    ('3', '4'): [7, 30, [15, 20, 23]],
    ('3', '7'): [6, 20, [27, 30, 31]],
    ('4', '7'): [10, 30, [25, 30, 32]],
    ('5', '10'): [5, 25, [15, 20, 22]],
    ('6', '9'): [5, 20, [10, 15, 17]],
    ('7', '9'): [6, 35, [15, 18, 21]],
    ('7', '10'): [9, 35, [20, 27, 30]],
    ('8', '9'): [4, 25, [15, 18, 20]],
    ('8', '11'): [4, 20, [8, 10, 12]],
    ('9', '12'): [10, 15, [36, 41, 43]],
    ('10', '12'): [13, 20, [40, 45, 55]],
    ('11', '12'): [8, 30, [36, 45, 50]]
 })

# Different products corresponding to different nodes
# demand = inflow-outflow
demand = {
    ('1', '1'): 0,
    ('2', '1'): 0,
    ('1', '2'): -265,
    ('2', '2'): 0,
    ('1', '3'): 0,
    ('2', '3'): -195,
    ('1', '4'): 0,
    ('2', '4'): 0,
    ('1', '5'): 0,
    ('2', '5'): 0,
    ('1', '6'): 0,
    ('2', '6'): 0,
    ('1', '7'): 0,
    ('2', '7'): 0,
    ('1', '8'): 0,
    ('2', '8'): 0,
    ('1', '9'): 0,
    ('2', '9'): 0,
    ('1', '10'): 0,
    ('2', '10'): 0,
    ('1', '11'): 0,
    ('2', '11'): 195,
    ('1', '12'): 265,
    ('2', '12'): 0,
}
# Different tracks, corresponding to different nodes, have different product 
cost
cost = {
    ('1', '2', '1'): [20, 25, 27],
    ('1', '2', '2'): [50, 52, 58],
    ('1', '3', '1'): [28, 30, 34],
    ('1', '3', '2'): [52, 55, 60],
    ('1', '4', '1'): [18, 20, 24],
    ('1', '4', '2'): [38, 42, 48],
    ('2', '4', '1'): [45, 48, 56],
    ('2', '4', '2'): [90, 100, 115],
    ('2', '6', '1'): [90, 100, 105],
    ('2', '6', '2'): [135, 150, 170],
    ('2', '8', '1'): [90, 110, 125],
    ('2', '8', '2'): [200, 210, 230],
    ('3', '4', '1'): [20, 23, 28],
    ('3', '4', '2'): [50, 52, 57],
    ('3', '5', '1'): [58, 61, 69],
    ('3', '5', '2'): [100, 112, 120],
    ('3', '7', '1'): [55, 60, 70],
    ('3', '7', '2'): [105, 110, 125],
    ('4', '7', '1'): [69, 70, 75],
    ('4', '7', '2'): [145, 150, 165],
    ('5', '10', '1'): [210, 220, 230],
    ('6', '9', '1'): [113, 120, 130],
    ('6', '9', '2'): [240, 255, 275],
    ('7', '9', '1'): [67, 70, 75],
    ('7', '9', '2'): [130, 135, 145],
    ('7', '10', '1'): [66, 70, 80],
    ('7', '10', '2'): [150, 160, 180],
    ('8', '9', '1'): [96, 100, 110],
    ('8', '9', '2'): [145, 150, 165],
    ('8', '11', '1'): [77, 80, 90],
    ('8', '11', '2'): [150, 160, 175],
    ('9', '12', '1'): [110, 120, 135],
    ('9', '12', '2'): [190, 200, 215],
    ('10', '12', '1'): [115, 120, 135],
    ('10', '12', '2'): [185, 200, 225],
    ('11', '12', '1'): [83, 90, 100],
}
# Model
m = Model("transportation")

x = m.addVars(product, link, lb=0, name="x")
y = m.addVars(link, lb=0, ub=maxFrequency, name="y")


m.addConstrs(quicksum(x[k, i, j]) + demand[k, i] == quicksum(x[k, j, i])for 
k in product for i, j in link)
m.addConstrs(quicksum(x[k, i, j]) <= y[i, j]*capacity[i, j] for k in product 
for i, j in link)

当我运行程序时,会出现这样的错误:

TypeError: 'Var' object is not iterable

排队:

m.addConstrs(quicksum(x[k, i, j]) + demand[k, i] == quicksum(x[k, j, i])for k in product for i, j in link)

我的错误是什么?在我的字典中出错?

我尝试构建一个字典来存储“链接”信息,并通过约束条件获得最优解。

gurobi python示例中的设施和netflow问题的组合问题。

0 个答案:

没有答案