Skip to content

Commit cf5004e

Browse files
committed
Fixing roulette selection for minimization problems, as pointed by @Martsks.
1 parent 67998d3 commit cf5004e

File tree

1 file changed

+13
-5
lines changed
  • opytimizer/optimizers/evolutionary

1 file changed

+13
-5
lines changed

opytimizer/optimizers/evolutionary/ga.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def _roulette_selection(self, n_agents, fitness):
114114
fitness (list): A fitness list of every agent.
115115
116116
Returns:
117-
The selected indexes of the population
117+
The selected indexes of the population.
118118
119119
"""
120120

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

129-
# Calculates the total fitness
130-
total_fitness = np.sum(fitness)
129+
# Defines the maximum fitness of current generation
130+
max_fitness = np.max(fitness)
131131

132-
# Calculates the probability of each fitness
133-
probs = [fit / total_fitness for fit in fitness]
132+
# Re-arrange the list of fitness by inverting it
133+
# Note that we apply a trick due to it being designed for minimization
134+
# f'(x) = f_max - f(x)
135+
inv_fitness = [max_fitness - fit + c.EPSILON for fit in fitness]
136+
137+
# Calculates the total inverted fitness
138+
total_fitness = np.sum(inv_fitness)
139+
140+
# Calculates the probability of each inverted fitness
141+
probs = [fit / total_fitness for fit in inv_fitness]
134142

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

0 commit comments

Comments
 (0)