@@ -557,7 +557,7 @@ def _sample_parent(self) -> Program:
557557 """
558558 # Use exploration_ratio and exploitation_ratio to decide sampling strategy
559559 rand_val = random .random ()
560-
560+
561561 if rand_val < self .config .exploration_ratio :
562562 # EXPLORATION: Sample from current island (diverse sampling)
563563 return self ._sample_exploration_parent ()
@@ -590,15 +590,15 @@ def _sample_exploration_parent(self) -> Program:
590590 # Sample from current island
591591 parent_id = random .choice (list (current_island_programs ))
592592 return self .programs [parent_id ]
593-
593+
594594 def _sample_exploitation_parent (self ) -> Program :
595595 """
596596 Sample a parent for exploitation (from archive/elite programs)
597597 """
598598 if not self .archive :
599599 # Fallback to exploration if no archive
600600 return self ._sample_exploration_parent ()
601-
601+
602602 # Prefer programs from current island in archive
603603 archive_programs_in_island = [
604604 pid
@@ -614,14 +614,14 @@ def _sample_exploitation_parent(self) -> Program:
614614 # Fall back to any archive program if current island has none
615615 parent_id = random .choice (list (self .archive ))
616616 return self .programs [parent_id ]
617-
617+
618618 def _sample_random_parent (self ) -> Program :
619619 """
620620 Sample a completely random parent from all programs
621621 """
622622 if not self .programs :
623623 raise ValueError ("No programs available for sampling" )
624-
624+
625625 # Sample randomly from all programs
626626 program_id = random .choice (list (self .programs .keys ()))
627627 return self .programs [program_id ]
@@ -656,7 +656,7 @@ def _sample_inspirations(self, parent: Program, n: int = 5) -> List[Program]:
656656 if len (self .programs ) > n and len (inspirations ) < n :
657657 # Calculate how many diverse programs to add (up to remaining slots)
658658 remaining_slots = n - len (inspirations )
659-
659+
660660 # Sample from different feature cells for diversity
661661 feature_coords = self ._calculate_feature_coords (parent )
662662
@@ -705,18 +705,20 @@ def _enforce_population_limit(self) -> None:
705705
706706 # Calculate how many programs to remove
707707 num_to_remove = len (self .programs ) - self .config .population_size
708-
709- logger .info (f"Population size ({ len (self .programs )} ) exceeds limit ({ self .config .population_size } ), removing { num_to_remove } programs" )
708+
709+ logger .info (
710+ f"Population size ({ len (self .programs )} ) exceeds limit ({ self .config .population_size } ), removing { num_to_remove } programs"
711+ )
710712
711713 # Get programs sorted by fitness (worst first)
712714 all_programs = list (self .programs .values ())
713-
715+
714716 # Sort by average metric (worst first)
715717 sorted_programs = sorted (
716718 all_programs ,
717- key = lambda p : sum (p .metrics .values ()) / max (1 , len (p .metrics )) if p .metrics else 0.0
719+ key = lambda p : sum (p .metrics .values ()) / max (1 , len (p .metrics )) if p .metrics else 0.0 ,
718720 )
719-
721+
720722 # Remove worst programs, but never remove the best program
721723 programs_to_remove = []
722724 for program in sorted_programs :
@@ -725,39 +727,43 @@ def _enforce_population_limit(self) -> None:
725727 # Don't remove the best program
726728 if program .id != self .best_program_id :
727729 programs_to_remove .append (program )
728-
730+
729731 # If we still need to remove more and only have the best program protected,
730732 # remove from the remaining programs anyway (but keep the absolute best)
731733 if len (programs_to_remove ) < num_to_remove :
732- remaining_programs = [p for p in sorted_programs if p not in programs_to_remove and p .id != self .best_program_id ]
733- additional_removals = remaining_programs [:num_to_remove - len (programs_to_remove )]
734+ remaining_programs = [
735+ p
736+ for p in sorted_programs
737+ if p not in programs_to_remove and p .id != self .best_program_id
738+ ]
739+ additional_removals = remaining_programs [: num_to_remove - len (programs_to_remove )]
734740 programs_to_remove .extend (additional_removals )
735-
741+
736742 # Remove the selected programs
737743 for program in programs_to_remove :
738744 program_id = program .id
739-
745+
740746 # Remove from main programs dict
741747 if program_id in self .programs :
742748 del self .programs [program_id ]
743-
749+
744750 # Remove from feature map
745751 keys_to_remove = []
746752 for key , pid in self .feature_map .items ():
747753 if pid == program_id :
748754 keys_to_remove .append (key )
749755 for key in keys_to_remove :
750756 del self .feature_map [key ]
751-
757+
752758 # Remove from islands
753759 for island in self .islands :
754760 island .discard (program_id )
755-
761+
756762 # Remove from archive
757763 self .archive .discard (program_id )
758-
764+
759765 logger .debug (f"Removed program { program_id } due to population limit" )
760-
766+
761767 logger .info (f"Population size after cleanup: { len (self .programs )} " )
762768
763769 # Island management methods
0 commit comments