使用PuLP代码的“int对象不可调用”错误 - python

我只是在学习PuLP库的工作原理,它是一个线性编程求解器。
在这里找到了我正在使用的代码:LINK。它解决了以下优化问题(在这种情况下,使用二进制变量x_ {ij}):

使用PuLP代码的“int对象不可调用”错误 - python

这是本教程中的代码:

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,j): random.normalvariate(0,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] * c[i,j] 

<ipython-input-20-08e33a7305cc> in <dictcomp>(.0)
     28              rhs=b[j],
     29              name="constraint_{0}"))
---> 30        for j in set_J}
     31 
     32 objective = plp.lpSum(x_vars[i,j] * c[i,j] 

TypeError: 'int' object is not callable

参考方案

您的代码包含以下内容作为plp.LpConstraint的参数:

e=m(a[i,j] * x_vars[i,j] for i in set_I)

m定义为int,但被称为函数。相反,它应该是:

e=plp.lpSum(a[i,j] * x_vars[i,j] for i in set_I)

这是导致错误的原因,在您的指南中似乎只是一个错字。同样表达中的另一个错字是plp.plp.LpConstraintLE应该只是plp.LpConstraintLE

一旦清除了这两个错误,就应该解除阻止,继续使用opt_model.solve()将解决该模型。

您可能会发现以下代码更具可读性,可以解决纸浆中的此问题,例如,使用像LpVariable.dicts这样的漂亮纸浆功能。我一直很喜欢纸浆的一点是,模型创建代码实际上非常易读。

import pulp

### Create n, m, set_I, set_J, c, a, l, u, and b as before

# Model
opt_mod = pulp.LpProblem(name="Binary_model", sense=pulp.LpMinimize)

# Variables from a dictionary
x_vars = pulp.LpVariable.dicts("x", c.keys(), cat=pulp.LpBinary)

# Constraints
for j in set_J:
    opt_mod += pulp.lpSum(a[i,j] * x_vars[i,j] for i in set_I) <= b[j]

# Objective
opt_mod += pulp.lpSum(c[i,j] * x_vars[i,j] for i,j in c)

# Solve
opt_mod.solve()

# Which variables are set?
print([x for x in x_vars if x_vars[x].varValue > 0.999])

Python GPU资源利用 - python

我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…

Python:图像处理可产生皱纹纸效果 - python

也许很难描述我的问题。我正在寻找Python中的算法,以在带有某些文本的白色图像上创建皱纹纸效果。我的第一个尝试是在带有文字的图像上添加一些真实的皱纹纸图像(具有透明度)。看起来不错,但副作用是文本没有真正起皱。所以我正在寻找更好的解决方案,有什么想法吗?谢谢 参考方案 除了使用透明性之外,假设您有两张相同尺寸的图像,一张在皱纹纸上明亮,一张在白色背景上有深…

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

Python:无法识别Pip命令 - python

这是我拍摄的屏幕截图。当我尝试在命令提示符下使用pip时,出现以下错误消息:pip无法识别为内部或外部命令,可操作程序或批处理文件。我已经检查了这个线程:How do I install pip on Windows?我所能找到的就是我必须将"C:\PythonX\Scripts"添加到我的类路径中,其中X代表python版本。如您在我的…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…