Skip to content

Commit

Permalink
fix tournament return duplicate fittest
Browse files Browse the repository at this point in the history
Took 42 minutes
  • Loading branch information
Nikronic committed Oct 14, 2019
1 parent 85e8a92 commit b28a8f7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,13 @@ def test_tournament(supply_population):
assert parents.len() == 2
assert supply_population.contains(parents[0]) == True
assert supply_population.contains(parents[1]) == True
assert parents[0] != parents[1]

parents = F.tournament(supply_population, 0.0) # force to use random parents not fittest ('else' condition)
assert parents.len() == 2
assert supply_population.contains(parents[0]) == True
assert supply_population.contains(parents[1]) == True
assert parents[0] != parents[1]


def test_routes_ending_indices_attr(supply_depot: Depot):
Expand Down
3 changes: 3 additions & 0 deletions utils/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def tournament(population: Population, tournament_probability: float = 0.8, size
3. Select another random unique sample from the population
4. Find fittest chromosomes from each sampled populations and return them as new population.
5. Randomly choose two chromosomes and return them as a new population.
:param population: An instance of `Population` class
:param tournament_probability: The probability of using fittest or random sample (=0.8)
:param size: The size of population to be sampled. By default, we use Binary tournament (size = 2).
Expand All @@ -133,6 +134,8 @@ def tournament(population: Population, tournament_probability: float = 0.8, size
second_sample = extract_population(population, size)
first_fittest = fittest_chromosome(first_sample)
second_fittest = fittest_chromosome(second_sample)
while first_sample == second_fittest:
second_fittest = fittest_chromosome(second_sample)
return Population(0, [first_fittest, second_fittest])
else:
indices = random.sample(range(0, first_sample.len()), 2)
Expand Down

0 comments on commit b28a8f7

Please sign in to comment.