def survivors_steady_state(population, offspring, t_size):
"""The winner of a offspring tournamente replaces the loser of a population tournament."""
for i in range(len(population)):
# get indexes
winner = positive_tournament(offspring,t_size)
loser = negative_selection(population, t_size)
# replace
population[loser] = offspring[winner]
return population
def positive_tournament(population, t_size):
"""Deterministic. Return the index of the winner of the tournament."""
pool_index = [choice(range(len(population))) for i in range(t_size)]
best_index = pool_index[0]
for j in range(1, t_size):
if population[pool_index[j]][1] > population[best_index][1]:
best_index = pool_index[j]
return best_index
def negative_tournament(population, t_size):
"""Deterministic. Return the index of the worst individual in the tournament."""
pool_index = [choice(range(len(population))) for i in range(t_size)]
worst_index = pool_index[0]
for j in range(1, t_size):
if population[pool_index[j]][1] < population[worst_index][1]:
worst_index = pool_index[j]
return worst_index
Ernesto Costa

Sem comentários:
Enviar um comentário