I'm trying to create a very simple 'evolutionary' algorithm in python.
I initially want to create a population of ~100 individuals with four numerical attributes (a1-4), use a function to get a score from these attributes, then remove the worst scoring 20 individuals.
This is what I have so far
import random
population = 100
class Individual(object):
def __init__(self, a1, a2, a3, a4):
self.a1 = a1
self.a2 = a2
self.a3 = a3
self.a4 = a4
starting_population = list()
for i in range (population):
a1 = random.randint(1,10)
a2 = random.randint(1,10)
a3 = random.randint(1,10)
a4 = random.randint(1,10)
starting_population.append(Individual(a1,a2,a3,a4))
def fitness(x):
fitness = a1*a2/a3*a4
return fitness
I'm stuck on how to apply a function to the members of the population list?
Also, I'm very new to Python and I'm sure I've done some things badly, so any tips are very appreciated!
Thank you
random.seed(somenumber)is usually called before random calls. Also, do you want a new list offitness, or to add an attribute?