我正在学习PuLP库的工作原理,这是一个线性编程求解器。 我正在使用的代码位于LINK。它解决了以下优化问题(在这种情况下,使用二进制变量x_ {ij}):
这是教程中的代码:
import random
import pulp as plp
#Creating random constants for the model's input
n = 10
m = 5
set_I = range(1,n+1)
set_J = range(1,m+1)
c = {(i,j): random.normalvariate(0,1) for i in set_I for j in set_J}
a = {(i,5) for i in set_I for j in set_J}
l = {(i,j): random.randint(0,10) for i in set_I for j in set_J}
u = {(i,j): random.randint(10,20) for i in set_I for j in set_J}
b = {j: random.randint(0,30) for j in set_J}
opt_model = plp.LpProblem(name="Binary Model")
# if x is Binary
x_vars = {(i,j):
plp.LpVariable(cat=plp.LpBinary,name="x_{0}_{1}".format(i,j))
for i in set_I for j in set_J}
# Less than equal constraints
constraints = {j : opt_model.addConstraint(
plp.LpConstraint(
e=m(a[i,j] * x_vars[i,j] for i in set_I),sense=plp.plp.LpConstraintLE,rhs=b[j],name="constraint_{0}".format(j)))
for j in set_J}
objective = plp.lpSum(x_vars[i,j] * c[i,j]
for i in set_I
for j in set_J)
# for minimization
opt_model.sense = plp.LpMinimize
opt_model.setObjective(objective)
我收到了以下我不理解的错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-08e33a7305cc> in <module>
28 rhs=b[j],29 name="constraint_{0}"))
---> 30 for j in set_J}
31
32 objective = plp.lpSum(x_vars[i,j]
<ipython-input-20-08e33a7305cc> in <dictcomp>(.0)
28 rhs=b[j],j]
TypeError: 'int' object is not callable