为什么我会收到一个 TypeError,提示“不带任何参数(给定 1 个)”?
我有这段代码来实现粒子群优化算法:
class Particle:
def __init__(self,domain,ID):
self.ID = ID
self.gbest = None
self.velocity = []
self.current = []
self.pbest = []
for x in range(len(domain)):
self.current.append(random.randint(domain[x][0],domain[x][1]))
self.velocity.append(random.randint(domain[x][0],domain[x][1]))
self.pbestx = self.current
def updateVelocity():
for x in range(0,len(self.velocity)):
self.velocity[x] = 2*random.random()*(self.pbestx[x]-self.current[x]) + 2 * random.random()*(self.gbest[x]-self.current[x])
def updatePosition():
for x in range(0,len(self.current)):
self.current[x] = self.current[x] + self.velocity[x]
def updatePbest():
if costf(self.current) < costf(self.best):
self.best = self.current
def psoOptimize(domain,costf,noOfParticles=20, noOfRuns=30):
particles = []
for i in range(noOfParticles):
particle = Particle(domain,i)
particles.append(particle)
for i in range(noOfRuns):
Globalgbest = []
cost = 9999999999999999999
for i in particles:
if costf(i.pbest) < cost:
cost = costf(i.pbest)
Globalgbest = i.pbest
for particle in particles:
particle.updateVelocity()
particle.updatePosition()
particle.updatePbest(costf)
particle.gbest = Globalgbest
return determineGbest(particles,costf)
当我运行它时,我收到此错误:
TypeError:updateVelocity()不接受任何参数(给定1个)
但它清楚地表明粒子。 updateVelocity()
,()
之间没有任何内容。 “1给定”论点从何而来?代码有什么问题,如何修复?
I have this code to implement a Particle Swarm Optimization algorithm:
class Particle:
def __init__(self,domain,ID):
self.ID = ID
self.gbest = None
self.velocity = []
self.current = []
self.pbest = []
for x in range(len(domain)):
self.current.append(random.randint(domain[x][0],domain[x][1]))
self.velocity.append(random.randint(domain[x][0],domain[x][1]))
self.pbestx = self.current
def updateVelocity():
for x in range(0,len(self.velocity)):
self.velocity[x] = 2*random.random()*(self.pbestx[x]-self.current[x]) + 2 * random.random()*(self.gbest[x]-self.current[x])
def updatePosition():
for x in range(0,len(self.current)):
self.current[x] = self.current[x] + self.velocity[x]
def updatePbest():
if costf(self.current) < costf(self.best):
self.best = self.current
def psoOptimize(domain,costf,noOfParticles=20, noOfRuns=30):
particles = []
for i in range(noOfParticles):
particle = Particle(domain,i)
particles.append(particle)
for i in range(noOfRuns):
Globalgbest = []
cost = 9999999999999999999
for i in particles:
if costf(i.pbest) < cost:
cost = costf(i.pbest)
Globalgbest = i.pbest
for particle in particles:
particle.updateVelocity()
particle.updatePosition()
particle.updatePbest(costf)
particle.gbest = Globalgbest
return determineGbest(particles,costf)
When I run it, I get this error:
TypeError: updateVelocity() takes no arguments (1 given)
But it clearly says particle.updateVelocity()
, with nothing between the ()
. Where is the "1 given" argument coming from? What is wrong with the code, and how do I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Python 隐式地将对象传递给方法调用,但您需要显式声明它的参数。通常将其命名为
self
:Python implicitly passes the object to method calls, but you need to explicitly declare the parameter for it. This is customarily named
self
:确保所有类方法(
updateVelocity
、updatePosition
...)至少采用一个位置参数,该参数的规范名称为self
并引用该类的当前实例。当您调用粒子.updateVelocity() 时,被调用的方法隐式获取一个参数:实例,此处粒子作为第一个参数。
Make sure, that all of your class methods (
updateVelocity
,updatePosition
, ...) take at least one positional argument, which is canonically namedself
and refers to the current instance of the class.When you call
particle.updateVelocity()
, the called method implicitly gets an argument: the instance, hereparticle
as first parameter.您的 updateVelocity() 方法在其定义中缺少显式的 self 参数。
应该是这样的:
您的其他方法(
__init__
除外)也有同样的问题。Your
updateVelocity()
method is missing the explicitself
parameter in its definition.Should be something like this:
Your other methods (except for
__init__
) have the same problem.我对这个问题很困惑,因为我对 Python 还算陌生。我无法将解决方案应用于提问者给出的代码,因为它不是自我可执行的。所以我带来了一个非常简单的代码:
如您所见,解决方案包括使用两个(虚拟)参数,即使函数本身或调用它时都没有使用它们!这听起来很疯狂,但我相信其中一定有一个原因(对新手来说是隐藏的!)。
我尝试了很多其他方法(包括“自我”)。这是唯一有效的(至少对我来说)。
I have been puzzled a lot with this problem, since I am relively new in Python. I cannot apply the solution to the code given by the questioned, since it's not self executable. So I bring a very simple code:
As you can see, the solution consists in using two (dummy) arguments, even if they are not used either by the function itself or in calling it! It sounds crazy, but I believe there must be a reason for it (hidden from the novice!).
I have tried a lot of other ways ('self' included). It's the only one that works (for me, at least).