IT Share you

파이썬 인터프리터 오류, x는 인수를받지 않습니다 (주어진 1).

shareyou 2020. 12. 3. 20:51
반응형

파이썬 인터프리터 오류, x는 인수를받지 않습니다 (주어진 1).


나는 숙제 과제로 작은 파이썬 조각을 쓰고 있는데 그것을 실행하지 못하고 있습니다! 파이썬에 대한 경험은 많지 않지만 Java는 많이 알고 있습니다. Particle Swarm Optimization 알고리즘을 구현하려고하는데 여기에 제가 가지고있는 것이 있습니다.

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 개 제공됨)"

이해가 안 돼요! 나는 그것에 어떤 논쟁도주지 않는다!

도와 주셔서 감사합니다,

리누스


Python은 객체를 메서드 호출에 암시 적으로 전달하지만 이에 대한 매개 변수를 명시 적으로 선언해야합니다. 이것은 일반적으로 다음과 같이 명명됩니다 self.

def updateVelocity(self):

Make sure, that all of your class methods (updateVelocity, updatePosition, ...) take at least one positional argument, which is canonically named self and refers to the current instance of the class.

When you call particle.updateVelocity(), the called method implicitly gets an argument: the instance, here particle as first parameter.


Your updateVelocity() method is missing the explicit self parameter in its definition.

Should be something like this:

def updateVelocity(self):    
    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])

Your other methods (except for __init__) have the same problem.


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:

from turtle import *

ts = Screen(); tu = Turtle()

def move(x,y):
  print "move()"
  tu.goto(100,100)

ts.listen();
ts.onclick(move)

done()

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).

참고URL : https://stackoverflow.com/questions/4445405/python-interpreter-error-x-takes-no-arguments-1-given

반응형