Pulp : What does lpDot() does, How to use it

人盡茶涼 提交于 2021-01-28 20:13:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!