0

i am trying to write a code which takes input as below and then find the highest and average where f(x)= number of ones in x .

['00111110011001010011', '01111101001101110010', '01100110111110000000', '01101101100111001001']
def fitness(genome):
    return reduce((lambda x, y: int(x) + int(y)), list(genome))

# reference : https://www.geeksforgeeks.org/reduce-in-python/

def evaluateFitness(population):
    sums = list(map(lambda x: fitness(x), population))
    return [max(sums), sum(sums) / len(population)]

the above function works well. i want to know if there is any library in numpy or scipy which can do the same or pygad.basically trying to find alternate option to achieve the same result.

Shariq
  • 1
  • 1
  • 2
    Your `fitness` could also be `return genome.count('1')`. And, if you're just calling a function that takes one parameter, you can do `list(map(fitness, population))`. You don't need a lambda. – Tim Roberts Nov 19 '22 at 04:54
  • thats a good advice Tim, that simplifies the implementation to a great extent – Shariq Nov 21 '22 at 02:23

0 Answers0