在尝试制作蛇 AI 时,我遇到了 NEAT 的 TypeError:
node_inputs.append(self.values[i] * w)
TypeError: can't multiply sequence by non-int of type 'float'
代码
class SnakeGame(object):
def __init__(self, genomes, config):
self.genomes = genomes
self.nets = []
for id, g in self.genomes:
net = neat.nn.FeedForwardNetwork.create(g, config)
self.nets.append(net)
g.fitness = 0
另一个函数但同一个类中的代码
def game(self):
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
data = self.nets[0].activate(self.getData())
output = data.index(max(data))
函数 getData 的样子
def getData(self):
data = [self.x_position, self.y_position, self.food_x, self.food_y, self.snakeLength]
return data
config-feedforward.txt 的部分代码
[NEAT]
fitness_criterion = max
fitness_threshold = 1000
pop_size = 2
reset_on_extinction = True