Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b2ac512

Browse files
authored
fix: keep original code + fix for multi-objective optimization
1 parent 7e068dd commit b2ac512

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

‎pygad/pygad.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,14 +2231,22 @@ def best_solution(self, pop_fitness=None):
22312231
# break ties using the second objective, then third, etc.
22322232
pop_fitness_arr = numpy.array(pop_fitness)
22332233
# Get the indices that would sort by all objectives in descending order
2234-
sorted_indices = numpy.lexsort([ -pop_fitness_arr[:,i] for i in reversed(range(pop_fitness_arr.shape[1])) ])
2235-
best_match_idx = sorted_indices[0]
2236-
maximum_fitness_value = pop_fitness_arr[best_match_idx]
2237-
2238-
best_match_list = numpy.where(
2239-
pop_fitness == maximum_fitness_value)
2234+
if pop_fitness_arr.ndim == 1:
2235+
# Single-objective optimization.
2236+
best_match_idx = numpy.where(
2237+
pop_fitness == numpy.max(pop_fitness))[0][0]
2238+
elif pop_fitness_arr.ndim == 2:
2239+
# Multi-objective optimization.
2240+
# Sort by all objectives in descending order.
2241+
# The first objective is the most important, then the second, etc.
2242+
sorted_indices = numpy.lexsort([ -pop_fitness_arr[:,i] for i in reversed(range(pop_fitness_arr.shape[1])) ])
2243+
best_match_idx = sorted_indices[0]
2244+
maximum_fitness_value = pop_fitness_arr[best_match_idx]
2245+
2246+
best_match_list = numpy.where(
2247+
pop_fitness == maximum_fitness_value)
22402248

2241-
best_match_idx = best_match_list[0][0] # Get the first index of the best match.
2249+
best_match_idx = best_match_list[0][0] # Get the first index of the best match.
22422250

22432251

22442252
best_solution = self.population[best_match_idx, :].copy()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /