- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习 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,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()
将解决模型。
您可能会发现以下代码更具可读性,可以用来解决 pulp 中的这个问题,例如使用像 LpVariable.dicts
这样的 pulp 特性。我一直喜欢 pulp 的一点是模型创建代码实际上非常可读。
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 - 使用 PuLP 代码时出现 "int object is not callable"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59674360/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我想知道如果我们有一个引用 const 的函数参数会发生什么功能如下图。 版本 1 int anotherFunc() { std::cout<<"inside anotherFunc"<
我正在尝试运行YOLOv8重新训练的对象检测测试,但我一直收到此错误。。我已经试过安装gnu-cobol和更新西芹了,还有其他想法吗?谢谢
我做了一个静态类方法(这里称为“函数”)f需要一个列表 Callable小号: import java.util.concurrent.*; import java.util.*; class A {
以下是我的类定义: class logline: def __init__(self,t,cmp,msg): self.t = t self.cmp = cmp
我正在用 dart 做一个小实验,我找不到一种方法来确定一个变量是否是“可调用的”,而无需显式检查每种类型(字符串、整数、 bool 等),并猜测它是否是可调用的,如果没有的话那些。我还尝试了一个 t
我大量使用来自 python 3 的 python 类型支持。 最近我试图将一个函数作为参数传递,但我没有找到使用 kwargs 的任何帮助。在 typing.Callable签名。 请检查下面的代码
每当我运行实现可调用的程序时,我都会以顺序形式获得输出。 就像,这是我的程序: package com.handson; import java.util.concurrent.ArrayBlocki
我编写了一个注释(在Spring Boot应用程序中)并尝试将其应用到Callable的call()方法上,但它不起作用,但另一方面,当应用于普通方法时(请参阅下面的代码),它有效,这一直困扰着我,你
我试图将对象列表拆分为较小的子列表,并在不同的线程上单独处理它们。所以我有以下代码: List instances = xmlInstance.readInstancesFromXml
我想我可以用 std.traits.functionAttributes 来做到这一点,但它不支持 static。对于任何类型的可调用对象(包含 opCall 的结构),我如何判断该可调用对象是否使用
我是新来的,如果我把这个放到错误的主题中,很抱歉? 我的问题: class TaskEol implements Callable> { ArrayList coordinates =
我有一些代码可以从目录中的多个 JAR 动态加载类。因为 JAR 中的代码是不受信任的,所以我希望通过对它们进行“沙盒处理”来保护主应用程序免受编码不良或恶意动态类的影响。我想做的一件事是“时间盒”不
这个问题在这里已经有了答案: Overriding special methods on an instance (5 个答案) 关闭 9 年前。 我有以下代码 import numpy as np
我正在尝试制作一个类似于 https://github.com/ElgarL/TownyChat/blob/master/src/com/palmergames/bukkit/TownyChat/To
有人可以解释为什么要继承未参数化和参数化的Callable: from typing import Callable from typing import NoReturn from typing i
我有一个任务要以固定速率运行。但是,我还需要每次执行后的任务结果。这是我尝试过的: 任务 class ScheduledWork implements Callable { public St
给定一个实例 x的 Callable , 我怎样才能运行 x在一个单独的进程中,以便我可以重定向进程的标准输入和输出?例如,有没有办法构建一个 Process来自Callable ?有没有标准Exec
我有以下可调用对象: public class Worker implements Callable{ @Override public Boolean call(){ boo
现在很清楚了what a metaclass is ,有一个相关的概念,我一直在使用,但不知道它的真正含义。 我想每个人都犯了一次括号错误,导致“对象不可调用”异常。更何况,使用 __init__ 和
我是一名优秀的程序员,十分优秀!