This question already has an answer here:
This is the code:
from random import randint
import time, random
class Individual: # Create a new class for the individual composed of:
fitness = 0 # This measures how evolved is the Individual. The higher the better.
geneLength = 4 # This is the length of the gene that will evolve and mutate over time.
genes = [] # This will be the list of genes of the said individual
def __init__(self): # This operation creates a new instance of the class individual
random.seed(time.time())
print ("New:")
for x in range(self.geneLength): # In the beginning the genes will be random
self.genes.append(randint(0,1))
print(self.genes[x])
def calcFitness(self): # This operation calculates the fitness of the individual
self.fitness = 0 # Put to 0 so last generation doesn't interfere
for x in range(self.geneLength): # The fitness is calculated as the number of 1s in the genes array
if self.genes[x] == 1:
self.fitness += 1
return self.fitness
ind = Individual()
ind2 = Individual()
but the output is repeated instead of two different sequences
Aucun commentaire:
Enregistrer un commentaire