我试图用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
这是我初始化并从类调用时的代码:
^{pr2}$
它从IDLE返回以下错误:TypeError: mutate() takes exactly 1 argument (2 given)
我怎么能解决这个问题?在
本文介绍了一个使用Python实现的遗传算法遇到的问题及错误提示。主要问题是mutate方法被错误地传递了两个参数而不是一个,文章提供了相关代码示例并探讨了解决方案。

被折叠的 条评论
为什么被折叠?



