blacked all
This commit is contained in:
51
genetics.py
51
genetics.py
@@ -3,67 +3,60 @@ import random
|
||||
from brain import Neural_Network
|
||||
from params import MUTATION_RATE, SELECTION_ALG, KWAY_TOURNAMENT_PLAYERS
|
||||
|
||||
|
||||
def kway_selection(brains, exclude=None):
|
||||
tourn_pool = []
|
||||
best_play = None
|
||||
if exclude :
|
||||
if exclude:
|
||||
brains = [x for x in brains if x != exclude]
|
||||
for x in range(KWAY_TOURNAMENT_PLAYERS):
|
||||
new_play = random.choice(brains)
|
||||
while new_play in tourn_pool :
|
||||
while new_play in tourn_pool:
|
||||
new_play = random.choice(brains)
|
||||
if not best_play or best_play.fitness < new_play.fitness :
|
||||
if not best_play or best_play.fitness < new_play.fitness:
|
||||
best_play = new_play
|
||||
return best_play
|
||||
|
||||
|
||||
def genetic_selection(brains):
|
||||
parents_pool = []
|
||||
half_pop = int(len(brains)/2)
|
||||
half_pop = int(len(brains) / 2)
|
||||
|
||||
if SELECTION_ALG == "kway":
|
||||
for x in range(half_pop) :
|
||||
for x in range(half_pop):
|
||||
p1 = kway_selection(brains)
|
||||
p2 = kway_selection(brains, exclude=p1)
|
||||
parents_pool.append([
|
||||
p1,
|
||||
p2
|
||||
])
|
||||
parents_pool.append([p1, p2])
|
||||
|
||||
|
||||
|
||||
elif SELECTION_ALG == "roulette" :
|
||||
elif SELECTION_ALG == "roulette":
|
||||
# does not seem very optimized... TBR
|
||||
# constitute a list where every brain is represented
|
||||
# proportionnally to its relative fitness
|
||||
wheel = []
|
||||
for b in brains :
|
||||
for b in brains:
|
||||
wheel += [b] * b.fitness
|
||||
|
||||
|
||||
tot_fitness = len(wheel)
|
||||
|
||||
# selection of pool/2 pair of parents to reproduce
|
||||
# selection of pool/2 pair of parents to reproduce
|
||||
for _ in range(half_pop):
|
||||
idx1 = round(random.random()*tot_fitness - 1)
|
||||
idx2 = round(random.random()*tot_fitness - 1)
|
||||
parents_pool.append([
|
||||
wheel[idx1],
|
||||
wheel[idx2]
|
||||
])
|
||||
idx1 = round(random.random() * tot_fitness - 1)
|
||||
idx2 = round(random.random() * tot_fitness - 1)
|
||||
parents_pool.append([wheel[idx1], wheel[idx2]])
|
||||
return parents_pool
|
||||
|
||||
|
||||
|
||||
def cross_mutate_genes(p1_gene, p2_gene):
|
||||
child = []
|
||||
p1_gene = list(p1_gene)
|
||||
p2_gene = list(p1_gene)
|
||||
for idx,x in enumerate(p2_gene):
|
||||
if random.random() > 0.5 :
|
||||
for idx, x in enumerate(p2_gene):
|
||||
if random.random() > 0.5:
|
||||
choice = p1_gene[idx]
|
||||
else :
|
||||
else:
|
||||
choice = p2_gene[idx]
|
||||
# Mutation
|
||||
if random.random() < MUTATION_RATE :
|
||||
if random.random() < MUTATION_RATE:
|
||||
choice[random.randint(0, len(choice) - 1)] = random.random()
|
||||
print("Mutation !")
|
||||
child.append(choice)
|
||||
@@ -73,7 +66,7 @@ def cross_mutate_genes(p1_gene, p2_gene):
|
||||
def genetic_reproduction(parents_pool):
|
||||
# every pair of parents will produce a mixed child
|
||||
new_pop = []
|
||||
for [p1,p2] in parents_pool:
|
||||
for [p1, p2] in parents_pool:
|
||||
W1_kid = cross_mutate_genes(p1.W1, p2.W1)
|
||||
W2_kid = cross_mutate_genes(p1.W2, p2.W2)
|
||||
c_brain1 = Neural_Network(W1=W1_kid, W2=W2_kid)
|
||||
@@ -81,7 +74,3 @@ def genetic_reproduction(parents_pool):
|
||||
new_pop.append(c_brain1)
|
||||
new_pop.append(c_brain2)
|
||||
return new_pop
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user