Python的编译器错误,X不带任何参数(1提供) [英] Python compiler error, x takes no arguments (1 given)
问题描述
我正在写一小片蟒蛇作为家庭作业,而我没有得到它运行!我没有那么多的Python的经验,但我知道相当多的Java。
我试图实现粒子群算法,这里就是我有:
I'm writing a small piece of python as a homework assignment, and I'm not getting it to run! I don't have that much Python-experience, but I know quite a lot of Java. I'm trying to implement a Particle Swarm Optimization algorithm, and here's what I have:
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)
现在,我看不出为什么这不应该工作。
然而,当我运行它,我得到这个错误:
Now, I see no reason why this shouldn't work. However, when I run it, I get this error:
类型错误:updateVelocity()不带任何参数(1给出)
我不明白!我不会放弃的任何参数!
I don't understand! I'm not giving it any arguments!
感谢您的帮助,
莱纳斯
推荐答案
Python的隐含传递对象方法调用,但是你需要显式声明的参数吧。这是习惯上命名为自
:
Python implicitly passes the object to method calls, but you need to explicitly declare the parameter for it. This is customarily named self
:
def updateVelocity(self):
这篇关于Python的编译器错误,X不带任何参数(1提供)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!