I'm working on selection of two parents in GA, and the problem is that when I implemented the ' tournament_selection' function, it always returns two identical parents.
The initial population is:
num_emitter_coefficients = 782
sol_per_pop_1=1
sol_per_pop_2=3
sol_per_pop=sol_per_pop_1+sol_per_pop_2
pop_size_1 = [sol_per_pop_1, num_emitter_coefficients]
pop_size_2 = [sol_per_pop_2, num_emitter_coefficients]
initial_population_1 = np.random.uniform(low=0.0, high=0.0, size=pop_size_1)
initial_population_2 = np.random.uniform(low=0.0, high=0.0, size=pop_size_2)
for row in initial_population_2:
locations_used_in_this_row = 0
while locations_used_in_this_row != 5:
column = np.random.randint(num_emitter_coefficients)
if row[column] == 0.0:
row[column] = np.random.rand()*10
locations_used_in_this_row += 1
population=np.vstack([initial_population_1, initial_population_2])
print('Population: ',population)
Assuming that each individual's score is:
[44,56,63,34]
And in my project, the higher score each individual is , the lower fitness each individual has. The function of selection of parents is shown below:
def tournament_selection(population, scores, k=4):
first_pick = np.random.randint(0,len(population))
for second_pick in np.random.randint(0,len(population),k-1):
if scores[second_pick] < scores[first_pick]:
winner = second_pick
else:
winner = first_pick
return population[winner,:]
When I implemented the function,
for i in range(0, len(population),2):
parent_1 = tournament_selection(population, scores)
parent_2 = tournament_selection(population, scores)
it always returned two identical parents. Could you please tell me how to aviod this problem?
Maybe if I can exclude 'winner' from the population list, the problem of picking two same parents can be fixed, but I don't know how to achieve it with code, could you please give me ideas? Thanks a lot in advance.