使用PULP进行二进制整数编程,使用变量的矢量语法?

时间:2015-07-14 15:29:48

标签: python pulp

python库PULP的新手,我发现文档有点无用,因为它不包含使用变量列表的示例。我试图在下面创建一个绝对简约的例子来说明我的困惑。

import pulp
IDENTIFIERS = ['A','B','C','D','E']
PRICES      = dict( zip( IDENTIFIERS, [100.0, 99.0, 100.5, 101.5, 200.0 ] ) )
n           = len( IDENTIFIERS )

x     = pulp.LpVariable.dicts( "x", indexs = IDENTIFIERS, lowBound=0, upBound=1, cat='Integer', indexStart=[] )
prob  = pulp.LpProblem( "Minimalist example", pulp.LpMaximize )
prob += pulp.lpSum( [ x[i]*PRICES[i] for i in IDENTIFIERS ]  ), " Objective is sum of prices of selected items "
prob += pulp.lpSum( [ x[i] for i in IDENTIFIERS ] )==2, " Constraint is that we choose two items "
prob.solve()
for ident in IDENTIFIERS:
    if x[ident]==1:
        print ident + " is in the basket "

输出结果为:

A is in the basket 
B is in the basket 
C is in the basket 
D is in the basket 
E is in the basket

优化器无法识别我们只添加两个值的约束。

0 个答案:

没有答案