我正在尝试在 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)
我怎样才能解决这个问题?