为什么我会收到一个 TypeError,提示“不带任何参数(给定 1 个)”?

发布于 2024-10-07 19:14:54 字数 1895 浏览 1 评论 0原文

我有这段代码来实现粒子群优化算法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜宝宝 2024-10-14 19:14:54

Python 隐式地将对象传递给方法调用,但您需要显式声明它的参数。通常将其命名为 self

def updateVelocity(self):

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):
暗恋未遂 2024-10-14 19:14:54

确保所有类方法(updateVelocityupdatePosition...)至少采用一个位置参数,该参数的规范名称为self 并引用该类的当前实例。

当您调用粒子.updateVelocity() 时,被调用的方法隐式获取一个参数:实例,此处粒子作为第一个参数。

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.

你曾走过我的故事 2024-10-14 19:14:54

您的 updateVelocity() 方法在其定义中缺少显式的 self 参数。

应该是这样的:

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

您的其他方法(__init__ 除外)也有同样的问题。

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.

如痴如狂 2024-10-14 19:14:54

我对这个问题很困惑,因为我对 Python 还算陌生。我无法将解决方案应用于提问者给出的代码,因为它不是自我可执行的。所以我带来了一个非常简单的代码:

from turtle import *

ts = Screen(); tu = Turtle()

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

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

done()

如您所见,解决方案包括使用两个(虚拟)参数,即使函数本身或调用它时都没有使用它们!这听起来很疯狂,但我相信其中一定有一个原因(对新手来说是隐藏的!)。

我尝试了很多其他方法(包括“自我”)。这是唯一有效的(至少对我来说)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文