Commit 1530f101 authored by Grégoire Grzeczkowicz's avatar Grégoire Grzeczkowicz
Browse files

SoftMax for genetic Selection

parent 28784f40
......@@ -68,15 +68,24 @@ def genetic(func, init, crossover, neighb, population_size, again):
best_val = P_val[np.argmax(P_val)]
i = 1
while again(i, best_val, best_sol):
# Crossover and mutation
new_sol = []
for j in range(5*population_size):
e1,e2 = np.random.randint(0,population_size,2)
sol = neighb(crossover(P_sol[e1],P_sol[e2]))
new_sol.append(sol)
# Selection
new_val = [func(sol) for sol in new_sol]
P_i = np.random.choice(range(len(new_sol)), size = population_size, replace = False, p=[v/np.sum(new_val) for v in new_val])
# Probability folowing value order
order_val = np.argsort(new_val)
proba = np.zeros(len(new_val))
for j in range(len(new_val)):
proba[order_val[j]] = j
proba = np.exp(proba)/sum(np.exp(proba))
P_i = np.random.choice(range(len(new_sol)), size = population_size, replace = False, p=proba)
P_sol = [new_sol[i] for i in P_i]
P_val = [new_val[i] for i in P_i]
# Keeping best value
if P_val[np.argmax(P_val)] > best_val:
best_sol = P_sol[np.argmax(P_val)]
best_val = P_val[np.argmax(P_val)]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment