DEAP / deap

Distributed Evolutionary Algorithms in Python
http://deap.readthedocs.org/
GNU Lesser General Public License v3.0
5.88k stars 1.13k forks source link

Lambda Function from toolbox.compile is failing? #607

Closed stevenf7 closed 3 years ago

stevenf7 commented 3 years ago

Hello,

I am pretty new to Python and GP in general. But when I was writing my fitness function, the lambda function, "func" kept returning this the unsupported operand error, does anyone know what could have possibly gone wrong? Thank you in advance, any help is greatly appreciated!

Implementation 1

 def evaluation(individual):
        func = toolbox.compile(expr=individual)
        sum = 0
        for i in range(0, len(func_input)):
             val = func(func_input[i][0], func_input[i][1],func_input[i][2],func_input[i][3],func_input[i][4],func_input[i][5])
             if (val == expected_out[i]):
                sum+= 1

        return sum
traceback (most recent call last):
  File "GeneticProgramming.py", line 128, in <module>
    population, logbook = algorithms.eaSimple(population=population_, toolbox=toolbox_, cxpb= crossover_Probability, mutpb = 0, ngen = generations, stats = stats, halloffame=HallofFame)
  File "C:\Python\Python39\site-packages\deap\algorithms.py", line 151, in eaSimple
    for ind, fit in zip(invalid_ind, fitnesses):
  File "GeneticProgramming.py", line 99, in evaluation
    val = func(func_input[i][0], func_input[i][1],func_input[i][2],func_input[i][3],func_input[i][4],func_input[i][5])
  File "<string>", line 1, in <lambda>
TypeError: unsupported operand type(s) for |: 'int' and 'str'

Implementation 2

    def evaluation(individual):
        func = toolbox.compile(expr=individual)
        return sum(func(*in_) == out for in_, out in zip(func_input, expected_out))
Traceback (most recent call last):
  File "GeneticProgramming.py", line 130, in <module>
    population, logbook = algorithms.eaSimple(population=population_, toolbox=toolbox_, cxpb= crossover_Probability, mutpb = 0, ngen = generations, stats = stats, halloffame=HallofFame)
  File "C:\Python\Python39\site-packages\deap\algorithms.py", line 151, in eaSimple
    for ind, fit in zip(invalid_ind, fitnesses):
  File "GeneticProgramming.py", line 106, in evaluation
    return sum(func(*in_) == out for in_, out in zip(func_input, expected_out))
  File "GeneticProgramming.py", line 106, in <genexpr>
    return sum(func(*in_) == out for in_, out in zip(func_input, expected_out))
  File "<string>", line 1, in <lambda>
TypeError: unsupported operand type(s) for &: 'int' and 'str'

Thanks again!