2

I am making a Genetic Algorithm(GA) in python with pygad, but I keep getting this error:

TypeError: can't multiply sequence by non-int of type 'GA'

here is my whole code:

import pygad
import numpy

print(pygad.__version__)

inputs = [0.4,1,0,7,8,1,2,3,4,5,6,7,8,9,0,12,23,34,45,5667,67,78,95678,456,23,234,456,9,0,9,54,213,]
desired_output = 32

def fitness_func(solution, solution_idx, ga_instance):
    output = numpy.sum(solution*inputs) # error here
    fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)

    return fitness

ga_instance = pygad.GA(num_generations=100,
                       sol_per_pop=10,
                       num_genes=len(inputs),
                       num_parents_mating=2,
                       fitness_func=fitness_func,
                       mutation_type="random",
                       mutation_probability=0.6)

ga_instance.run()

ga_instance.plot_fitness()

*the inputs are just a bunch of random numbers

I am using pygad 3.0.0 and python 3.11.3

I have tried to do this:

output = numpy.sum(int(solution)*inputs)

And also just replacing the solution parameter with the expected output(32) but when I do that the AI doesn't do anything.

R1B07008
  • 33
  • 5
  • 1
    Ok what I think is happening is `pygad.GA` is passing either `solution` incorrectly. So check your `fitness_func` function, make sure it's parameters are correct. – Anony Mous Apr 15 '23 at 00:38
  • 1
    You were right, I passed the parameters incorrectly, thanks! – R1B07008 Apr 15 '23 at 12:22
  • PyGAD 3.0.0 has this order for the fitness function parameters: 1) ga_instance 2) solution and 3) solution_idx – Ahmed Gad Apr 18 '23 at 18:42

2 Answers2

2

you are trying to multiply a Python list (inputs) with a numpy array (solution). Just convert the inputs list to a numpy array before .

def fitness_func(solution, solution_idx, ga_instance):
    inputs_array = numpy.array(inputs)
    output = numpy.sum(solution * inputs_array)
    fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)

    return fitness
Saxtheowl
  • 4,136
  • 5
  • 23
  • 32
1

Like Anony Mous said in the comments, here is the answer:

"Ok what I think is happening is pygad.GA is passing either solution incorrectly. So check your fitness_func function, make sure it's parameters are correct."(Anony Mous)

here is the working bit of code:

import pygad
import numpy

print(pygad.__version__)

inputs = [0.4,1,0,7,8,1,2,3,4,5,6,7,8,9,0,12,23,34,45,5667,67,78,95678,456,23,234,456,9,0,9,54,213,]
desired_output = 32

def fitness_func(ga_instance, solution, solution_idx): # error was here
    output = numpy.sum(solution*inputs)
    fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)

    return fitness

ga_instance = pygad.GA(num_generations=100,
                       sol_per_pop=10,
                       num_genes=len(inputs),
                       num_parents_mating=2,
                       fitness_func=fitness_func,
                       mutation_type="random",
                       mutation_probability=0.6)

ga_instance.run()

ga_instance.plot_fitness()

Thanks so much!

R1B07008
  • 33
  • 5