我正在寫一小段python作爲家庭作業,並且我沒有讓它運行!我沒有那麼多的Python體驗,但我知道很多Java。 我試圖實現粒子羣算法,這裏就是我:Python編譯器錯誤,x不需要參數(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)
現在,我看不出有任何理由爲什麼這不應該工作。 然而,當我運行它,我得到這個錯誤:
「類型錯誤:updateVelocity()函數沒有(給定1)參數」
我不明白!我沒有給出任何論點!
感謝您的幫助,
萊納斯
請突出顯示您的代碼並點擊「010101」按鈕進行正確格式化。 – 2010-12-14 23:40:36
我的源代碼中沒有空白行,這只是本網站格式化它的方式。 – Linus 2010-12-15 22:26:14
質量差的問題:許多不相關的代碼,由於混合的空格和製表符而導致許多語法錯誤。重複更好的問題http://stackoverflow.com/q/6614123/448474 – hynekcer 2012-12-13 12:28:46