Python Pulp不会添加所有变量约束并忘记目标函数

时间:2015-06-04 11:52:31

标签: python pulp

我使用模块纸浆遇到了一些问题。我想创建一个混合整数线性编程问题并将其写为LP文件。在此之后我用cplex解决它。

问题在于,当我添加第二个约束时,目标函数变为false(虚拟被添加),只有第一个约束被添加,只有决策变量x。

这是我的代码:我希望你能帮助我!

bay_model = pulp.LpProblem('Bay Problem', pulp.LpMinimize)

y = pulp.LpVariable.dicts(name = "y",indexs = (flight, flight, gates),
                          lowBound = 0, upBound = 1,cat = pulp.LpInteger)

x = pulp.LpVariable.dicts(name = "x",indexs = (flight,gates),lowBound = 0,                             
                          upBound = 1, cat=pulp.LpInteger)




bay_model += pulp.lpSum([x[i][j]*g.distance[j] for i in flight for j in gates])

for i in flight:
     bay_model += pulp.lpSum([x[i][j] for j in gates]) == 1
     print "flight must be assigned" + str(i)

for k in gates:
     bay_model += [y[i][j][k] * f.time_matrix[i][j] for i in flight for j in flight if f.time_matrix[i][j] == 1] <= g.capacity[k]
     bay_model += [(2 * y[i][j][k] - x[i][k] - x[j][k]) for i in flight for j in flight] == 0
     print "time constraint" + str(k)

1 个答案:

答案 0 :(得分:0)

我不认为列表推导[x [i] for i in ...]可以像这样添加到bay_model。 如果希望约束保留列表中的每个元素,则可以预先定义元素:

for i in flights:
  for j in flights:
    if f.time_matrix[i][j] == 1:
      for k in gates:
        bay_model += y[i][j][k] * f.time_matrix[i][j] <= g.capacity[k]