我正在尝试用 python 编写遗传算法的实现。它说,当只允许一个参数时,我会用两个参数来调用它,但我确信我不允许。
这是相关代码:
class GA:
def __init__(self, best, pops=100, mchance=.07, ps=-1):
import random as r
self.pop = [[] for _ in range(pops)]
if ps == -1:
ps = len(best)
for x in range(len(self.pop)): #Creates array of random characters
for a in range(ps):
self.pop[x].append(str(unichr(r.randint(65,122))))
def mutate(array):
if r.random() <= mchance:
if r.randint(0,1) == 0:
self.pop[r.randint(0, pops)][r.randint(0, ps)] +=1
else:
self.pop[r.randint(0, pops)][r.randint(0, ps)] -=1
这是我初始化并从类中调用时的代码:
a = GA("Hello",10,5)
a.mutate(a.pop)
它从 IDLE 返回以下错误:
TypeError: mutate() takes exactly 1 argument (2 given)
我怎样才能解决这个问题?
类的方法会自动传递该类的实例作为其第一个参数(其名称为self
按照惯例):
def mutate(self, array):
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)