2

我有这个 LP 问题,我正在尝试使用 Python-3 中的 PuLP 来解决它。我能想到的一种选择是显式编写所有变量,但我想避免它。有没有办法可以在这个问题中使用列表/字典?(我确实参考了使用 dicts 的https://pythonhosted.org/PuLP/CaseStudies/a_sudoku_problem.html,但不太了解整个解决方案)

假设wt{i,j,type}表示person[i]person[j]类型的交易商品的数量。

LP问题: 目标函数图像

(这里,cost{i,j}是所有(i,j)对的已知配对成本。

受制于:

约束方程图像

我会非常感谢任何帮助,因为我是优化和 python/pulp 的初学者。

4

1 回答 1

4

'lists/dicts' 是一种在域上定义变量(索引变量)的方法。的indexs参数LpVariable.dicts()定义了所提供集合的域 - 笛卡尔积。另请参阅 PuLP- LpVariable的文档。

下面的代码示例不包含您的所有约束,但我相信您可以轻松填写​​剩余的约束。约束 1(带有const1和)改为通过变量const2的下限和上限处理。wt

from pulp import LpProblem, LpVariable, LpMaximize, LpInteger, lpSum, value

prob = LpProblem("problem", LpMaximize)

# define 'index sets'
I = range(10) # [0, 1, ..., 9]
J = range(10)
T = range(3)

# define parameter cost[i,j]
cost = {}
for i in I:
    for j in J:
        cost[i,j] = i + j # whatever

# define wt[i,j,t]
const1 = 0 # lower bound for w[i,j,t]
const2 = 100 # upper bound for w[i,j,t]
wt = LpVariable.dicts(name="wt", indexs=(I, J, T), lowBound=const1, upBound=const2, cat=LpInteger)

# define assign[i,j]
assign = LpVariable.dicts(name="assign", indexs=(I, J))

# contraint 
for i in I:
    for j in J:
        prob += assign[i][j] == lpSum(wt[i][j][t] for t in T), ""

# objective
prob += lpSum(cost[i,j] * assign[i][j] for i in I for j in J)

prob.solve()

for i in I:
    for j in J:
        for t in T:
            print "wt(%s, %s, %s) = %s" % (i, j, t, value(wt[i][j][t]))

for i in I:
    for j in J:
        print "assign(%s, %s) = %s" % (i, j, value(assign[i][j]))
于 2015-05-23T09:52:18.600 回答