繁体   English   中英

在Pulp Python中制定LP的约束

[英]Formulating constraints of a LP in Pulp Python

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

假定重量{I,J,类型}表示成交商品的人[i]类型人[J]之间的数量。

LP问题: 目标函数图

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

受:

约束方程图

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

“列表/字典”是一种定义域中变量(索引变量)的方法。 indexs的参数LpVariable.dicts()定义了域-所提供的集笛卡儿积。 另请参见PuLP- LpVariable的文档。

下面的代码示例并未包含您的所有约束,但是我相信您可以轻松地填写其余约束。 约束1(带有const1const2 )通过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]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM