【发布时间】:2017-08-17 07:50:43
【问题描述】:
我必须用纸浆解决整数线性优化问题。我解决了这个问题并得到了等于 42 的优化值。但是当我编写更通用的代码时,比如在循环内声明变量、在循环内定义约束以及使用 lpSum 函数定义优化,我没有解决方案。 我认为我的问题在于定义下一个约束。
for a in itemset_dict.keys():
for b in itemset_dict[a][0]:
my_lp_program +=b >= a, "2Constraint"
我收到了下一个警告:
C:\Program Files\Python36\lib\site-packages\pulp\pulp.py:1353: UserWarning: Overwriting previously set objective.
warnings.warn("Overwriting previously set objective.")
Status: Optimal
Total Optimum= None
__dummy = None
products_beer = 0.0
products_cheese = 0.0
products_cola = 0.0
products_peanuts = 0.0
The thread 'MainThread' (0xb30) has exited with code 0 (0x0).
The program '[10140] python.exe' has exited with code 0 (0x0).
谢谢。
from pulp import *
# defining list of products
products = ['cola','peanuts', 'cheese', 'beer']
itemsets = ['x1','x2', 'x3']
#disctionary of the costs of each of the products is created
costs = {'cola' : 5, 'peanuts' : 3, 'cheese' : 1, 'beer' : 4 }
# dictionary of frequent itemsets
itemset_dict = { "x1" : (("cola", "peanuts"),10),
"x2" : (("peanuts","cheese"),20),
"x3" : (("peanuts","beer"),30)
}
products_var=LpVariable.dicts("Products", products, 0)
itemsets_var=LpVariable.dicts("Itemsets", itemsets, 0)
# defining itemsets variables
'''
for x in itemsets:
x = LpVariable('x', lowBound=0, upBound=1, cat='Binary')
'''
#x = LpVariable.dicts("x", itemsets, lowBound=0, upBound=1, cat='Binary')
#option2
i1=LpVariable.dict("itemsets", itemsets, lowBound=0, upBound=1, cat='Binary')
#print(i1);
#print(i1['x1'])
#print(type(i1['x1']))
'''
x1 = LpVariable('x1', lowBound=0, upBound=1, cat='Binary')
x2 = LpVariable('x2', lowBound=0, upBound=1, cat='Binary')
x3 = LpVariable('x3', lowBound=0, upBound=1, cat='Binary')
'''
# defining products variables
'''
for p in products:
p = LpVariable(p, lowBound=0, upBound=1, cat='Binary')
'''
#p = [LpVariable.dicts("p", i, lowBound=0, upBound=1, cat='Binary') for i in products]
'''
option1
p=[LpVariable(i, lowBound=0, upBound=1, cat='Binary') for i in products]
print(p[2])
print(type(p[2]))
'''
#option2
p1=LpVariable.dict("products", products, lowBound=0, upBound=1, cat='Binary')
#print(p1);
#print(p1['cola'])
#print(type(p1['cola']))
'''
cola = LpVariable('cola', lowBound=0, upBound=1, cat='Binary')
peanuts = LpVariable('peanuts', lowBound=0, upBound=1, cat='Binary')
cheese = LpVariable('cheese', lowBound=0, upBound=1, cat='Binary')
beer = LpVariable('beer', lowBound=0, upBound=1, cat='Binary')
'''
my_lp_program = LpProblem('My LP Problem', LpMaximize)
#my_lp_program += 10*x1+20*x2+30*x3-5*p1-3*p2-1*p3-4*p4 , "Maximization"
#my_lp_program += 10*x1+20*x2+30*x3 - lpSum([costs[i] * p1[i] for i in products]) , "Maximization"
my_lp_program += lpSum([itemset_dict[i][1] * i1[i] for i in itemsets]) - lpSum([costs[i] * p1[i] for i in products]) , "Maximization"
#my_lp_program += lpSum([itemset_dict[i][1] * itemsets_var[i] for i in itemsets]) - lpSum([costs[i] * products_var[i] for i in products]) , "Maximization"
#my_lp_program +=cola+peanuts+cheese+beer<=3, "1Constained"
my_lp_program +=lpSum([p1[i] for i in products]) <= 3, "1Constaint"
'''
my_lp_program +=cola>=x1, "2Constained"
my_lp_program +=peanuts>=x1, "3Constained"
my_lp_program +=peanuts>=x2, "4Constained"
my_lp_program +=cheese>=x2, "5Constained"
my_lp_program +=peanuts>=x3, "6Constained"
my_lp_program +=beer>=x3, "7Constained"
'''
for a in itemset_dict.keys():
for b in itemset_dict[a][0]:
my_lp_program +=b >= a, "2Constraint"
my_lp_program.writeLP("CheckLpProgram.lp")
my_lp_program.solve()
print("Status:", LpStatus[my_lp_program.status])
print("Total Optimum=", value(my_lp_program.objective))
for v in my_lp_program.variables():
print(v.name, "=", v.varValue)
【问题讨论】:
-
很明显,您的方法中至少存在两个一般性错误(可能一个取决于我现在无法测试的纸浆行为),但我很困惑为什么人们只会说喜欢
I got no solution而不是详细描述正在发生的事情。我认为它给出了一个错误(而且我现在没有使用纸浆的权限)。还是没有?请编辑您的问题。 (顺便说一句:我将编辑您的 qestion-title,因为有问题的库的名称应该正确书写) -
感谢您的回答。我更新了代码。我想我取得了进步。我仍然无法定义约束。
标签: python linear-programming pulp