Ich versuche, die folgende Integer Programming Modell mit Python PulpWie lege ich die Einschränkungen in PuLP Python für die Multiplikation zweier Variablen fest?
ich den folgenden Code geschrieben haben, zu modellieren:
y1 + y2 + y3 + y4 + y5
y1 + y2 + y3 + y4 + y5 >= 1
Traceback (most recent call last):
File "C:\Python34\BinPacking6.py", line 58, in <module>
knapsolve(item)
File "C:\Python34\BinPacking6.py", line 27, in knapsolve
con1 = sum(xs[(i + j*item.bins)]* ys[j] for j in range(item.bins))
File "C:\Python34\BinPacking6.py", line 27, in <genexpr>
con1 = sum(xs[(i + j*item.bins)]* ys[j] for j in range(item.bins))
File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\pulp.py", line 209, in __mul__
return LpAffineExpression(self) * other
File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\pulp.py", line 786, in __mul__
return self * LpAffineExpression(other)
File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\pulp.py", line 774, in __mul__
raise TypeError("Non-constant expressions cannot be multiplied")
TypeError: Non-constant expressions cannot be multiplied
:
dies den Fehler liefert
from pulp import *
#knapsack problem
def knapsolve(item):
prob = LpProblem('BinPacking', LpMinimize)
ys = [LpVariable("y{0}".format(i+1), cat="Binary") for i in range(item.bins)]
xs = [LpVariable("x{0}{1}".format(i+1, j+1), cat="Binary")
for i in range(item.items) for j in range(item.bins)]
#minimize objective
nbins = sum(ys)
prob += nbins
print(nbins)
#constraints
t = nbins >= 1
print(t)
prob += t
for i in range(item.items):
con1 = sum(xs[(i + j*item.bins)]* ys[j] for j in range(item.bins))
t = con1 == 1
prob += t
print(t)
status = prob.solve()
print(LpStatus[status])
print("Objective value:", value(prob.objective))
print ('\nThe values of the variables : \n')
for v in prob.variables():
print(v.name, "=", v.varValue)
return
class Probelm:
#bins, binweight, items, weight, itemheight, binheight
bins = 5
items = 5
binweight = [2,3,2,5,3]
itemweight = [1,2,2,1,3]
itemheight = [2,1,4,5,3]
binheight = [4,9,10,8,10]
item = Problem()
knapsolve(item)
Zur Laufzeit
Soweit ich feststellen kann, liegt das Problem in con1 = sum(xs[(i + j*item.bins)]* ys[j] for j in range(item.bins))
. Wie lege ich die Einschränkungen fest?
Ich versuche, eine zweidimensionale bin Verpackung zu tun, der Wikipedia-Eintrag entspricht einem eindimensionalen –
Sorry, ich nahm an, das war ein Schritt, den Sie machen könnten. Fügen Sie einfach eine weitere Einschränkung hinzu wie 'sum (j, a (i) * x (i, j)) <= V '. –
<= V oder <= V tiefgestellt y [i]? –