@@ -2231,14 +2231,22 @@ def best_solution(self, pop_fitness=None):
2231
2231
# break ties using the second objective, then third, etc.
2232
2232
pop_fitness_arr = numpy .array (pop_fitness )
2233
2233
# 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 )
2240
2248
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.
2242
2250
2243
2251
2244
2252
best_solution = self .population [best_match_idx , :].copy ()
0 commit comments