问题
I am trying to generate an equation via lpDot()
, Such as
PulpVar = [x1,x2]
Constants = [5,6]
then doing dot product as:
model += lpDot(PulpVar, Constants)
Form what I understand this should generate an equation as x1*5+x2*6
but I am getting lpAffineExpression
as output and the lp file so generated is empty
回答1:
lpDot() – given two lists of the form [a1, a2, …, an] and [ x1, x2, …, xn] will construct a linear epression to be used as a constraint or variable ref
So, if you use with constants, lpDot() will return dot product, that is a <class 'pulp.pulp.LpAffineExpression'>
:
import pulp
x1 = [1]
x2 = [2]
X = [x1,x2]
Constants = [5, 6]
model = pulp.lpDot(X, Constants)
print(model, type(model))
Output:
17 <class 'pulp.pulp.LpAffineExpression'>
If you quant the equation x1*5+x2*6
you should use LpVariable
like this:
import pulp
PulpVar1 = pulp.LpVariable('x1')
PulpVar2 = pulp.LpVariable('x2')
Constants = [13, 2]
model = pulp.lpDot([PulpVar1, PulpVar2], Constants)
print(model, type(model))
Output:
5*x1 + 6*x2 <class 'pulp.pulp.LpAffineExpression'>
来源:https://stackoverflow.com/questions/57309541/pulp-what-does-lpdot-does-how-to-use-it