running
This commit is contained in:
18
genetics.py
18
genetics.py
@@ -10,7 +10,7 @@ def genetic_selection(brains):
|
||||
# proportionnally to its relative fitness
|
||||
wheel = []
|
||||
for b in brains :
|
||||
wheel += [b] * b.brains
|
||||
wheel += [b] * b.fitness
|
||||
|
||||
|
||||
tot_fitness = len(wheel)
|
||||
@@ -19,7 +19,11 @@ def genetic_selection(brains):
|
||||
# selection of pool/2 pair of parents to reproduce
|
||||
parents_pool = []
|
||||
for _ in range(half_pop):
|
||||
parents_pool.append([round(random.random()*tot_fitness), round(random.random()*tot_fitness)])
|
||||
parents_pool.append([
|
||||
wheel[round(random.random()*tot_fitness)],
|
||||
wheel[round(random.random()*tot_fitness)]
|
||||
])
|
||||
return parents_pool
|
||||
|
||||
|
||||
def cross_mutate_genes(p1_gene, p2_gene):
|
||||
@@ -28,15 +32,15 @@ def cross_mutate_genes(p1_gene, p2_gene):
|
||||
p2_gene = list(p1_gene)
|
||||
for idx,x in enumerate(p2_gene):
|
||||
if random.random() > 0.5 :
|
||||
choice = p1_gene
|
||||
choice = p1_gene[idx]
|
||||
else :
|
||||
choice = p2_gene
|
||||
choice = p2_gene[idx]
|
||||
# Mutation
|
||||
if random.random() < 0.005 :
|
||||
choice[random.randint(0, len(choice - 1))] = random.random()
|
||||
choice[random.randint(0, len(choice) - 1)] = random.random()
|
||||
print("Mutation !")
|
||||
child.append(np.array(choice))
|
||||
return child
|
||||
child.append(choice)
|
||||
return np.array(child)
|
||||
|
||||
|
||||
def genetic_reproduction(parents_pool):
|
||||
|
||||
Reference in New Issue
Block a user