Skip to content

Commit

Permalink
Fixing roulette selection for minimization problems, as pointed by @M…
Browse files Browse the repository at this point in the history
  • Loading branch information
gugarosa committed Feb 17, 2021
1 parent 67998d3 commit cf5004e
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions opytimizer/optimizers/evolutionary/ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def _roulette_selection(self, n_agents, fitness):
fitness (list): A fitness list of every agent.
Returns:
The selected indexes of the population
The selected indexes of the population.
"""

Expand All @@ -126,11 +126,19 @@ def _roulette_selection(self, n_agents, fitness):
# If it is, increase it by one
n_individuals += 1

# Calculates the total fitness
total_fitness = np.sum(fitness)
# Defines the maximum fitness of current generation
max_fitness = np.max(fitness)

# Calculates the probability of each fitness
probs = [fit / total_fitness for fit in fitness]
# Re-arrange the list of fitness by inverting it
# Note that we apply a trick due to it being designed for minimization
# f'(x) = f_max - f(x)
inv_fitness = [max_fitness - fit + c.EPSILON for fit in fitness]

# Calculates the total inverted fitness
total_fitness = np.sum(inv_fitness)

# Calculates the probability of each inverted fitness
probs = [fit / total_fitness for fit in inv_fitness]

# Performs the selection process
selected = d.generate_choice_distribution(n_agents, probs, n_individuals)
Expand Down

0 comments on commit cf5004e

Please sign in to comment.