【问题标题】:Python Pulp does not add all variables constraints and forgets objective functionPython Pulp 没有添加所有变量约束并且忘记了目标函数
【发布时间】:2015-10-08 22:26:38
【问题描述】:

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

问题是当我添加第二个约束时,目标函数变为假(添加了虚拟)并且只添加了第一个约束,只有决策变量 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)

【问题讨论】:

  • 你能给我一个关于航班和登机口外观的样本或样本值吗?
  • 航班和登机口只是创建航班和登机口索引的范围。例如,当有 100 个航班时,航班 = 0,1,2 ... 99 或 xrange(100) 同样适用于登机口。我有强烈的感觉,问题出在 lp 文件的编写上。当我将它写入 MPS 时,它可以完美运行。

标签: python pulp


【解决方案1】:

我认为列表推导 [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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多