0

I'm getting a TypeError with NEAT while trying to make a snake AI:

node_inputs.append(self.values[i] * w)
TypeError: can't multiply sequence by non-int of type 'float'

Code

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
 

code in another function but same class

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

what the function getData looks like

def getData(self):
    data = [self.x_position, self.y_position, self.food_x, self.food_y, self.snakeLength]
    return data

part of code for config-feedforward.txt

[NEAT]
fitness_criterion = max
fitness_threshold = 1000
pop_size = 2
reset_on_extinction = True
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Heidar-An
  • 89
  • 2
  • 11

2 Answers2

0

The problem here is that 'w' is float. Consider the following examples:

"a" * 5 # this makes "aaaaa"
["a"] * 5 # this makes ["a", "a", "a", "a", "a"]

In the same logic, it would make no sense to write something like ["a"] * 5.5. If you do, you get the error that you write above. So figure out why w is a float instead of an int - could be that its value is an integer but represented as a float (e.g. 5.0).

Adam Minas
  • 136
  • 8
  • thanks for the comment, i've been looking for something that is a float, and i've changed all the values in my "config-feedforward.txt" file to make sure that integers don't have a decimal, yet the problem is still here :( – Heidar-An Jul 05 '21 at 17:24
  • Perhaps the library that parses your config returns it as a float? Print the w variable and it's type to make sure it is an integer. – Adam Minas Jul 05 '21 at 18:46
  • I don't know how the print the w variable, it's not part of my code. It's part of a file from the library called "activate" which I don't know how to access – Heidar-An Jul 06 '21 at 18:46
0

The mistake I made was that I wasn't using a variable that wasn't there, along with also passing an array as an input which I guess is not allowed?

Heidar-An
  • 89
  • 2
  • 11