Skip to content

Commit 101de7f

Browse files
author
Žiga Stupan
committed
fix LOA
1 parent d118384 commit 101de7f

File tree

1 file changed

+16
-6
lines changed
  • src/niapy/algorithms/basic

1 file changed

+16
-6
lines changed

src/niapy/algorithms/basic/loa.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -441,12 +441,15 @@ def move_to_safe_place(self, population, pride_size, task):
441441
territory.append(lion)
442442
num_of_improvements += lion.has_improved
443443

444-
# Tournament selection
445-
tournament_size = max(2, int(np.ceil(num_of_improvements / 2)))
446-
indices = self.rng.choice(len(territory), tournament_size, replace=False)
447-
selected = [territory[i] for i in indices]
448444

449-
winner = min(selected, key=lambda selected_lion: selected_lion.f)
445+
if len(territory) > 1:
446+
tournament_size = max(2, int(np.ceil(num_of_improvements / 2)))
447+
indices = self.rng.choice(len(territory), tournament_size, replace=False)
448+
selected = [territory[i] for i in indices]
449+
450+
winner = min(selected, key=lambda selected_lion: selected_lion.f)
451+
else:
452+
winner = territory[0]
450453
winner_x = winner.x.copy()
451454

452455
# Move female non-hunters
@@ -557,7 +560,14 @@ def roaming(self, population, pride_size, task):
557560

558561
def create_offspring(self, female, males, pride_idx, has_pride, task):
559562
offspring = []
560-
n_mating_males = 1 if not has_pride else self.integers(1, len(males))
563+
# Handle edge case when there are no males
564+
if len(males) == 0:
565+
return offspring
566+
# Handle edge case when there's only 1 male or for nomads
567+
if not has_pride or len(males) == 1:
568+
n_mating_males = 1
569+
else:
570+
n_mating_males = self.integers(1, len(males))
561571
indices = self.rng.choice(len(males), n_mating_males, replace=False)
562572
mating_males = [males[i] for i in indices]
563573

0 commit comments

Comments
 (0)