openevolve icon indicating copy to clipboard operation
openevolve copied to clipboard

KeyError in `_sample_exploration_parent`

Open benjaminy opened this issue 1 year ago • 4 comments

I have been experimenting with openevolve and the two times I let it run to around 100 iterations (once a bit more, once a bit less) I get the following error:

File "/openevolve/quick_start.py", line 16, in main best_program = await evolve.run(iterations=1000) File "/openevolve/openevolve/controller.py", line 231, in run parent, inspirations = self.database.sample() File "/openevolve/openevolve/database.py", line 188, in sample parent = self._sample_parent() File "/openevolve/openevolve/database.py", line 562, in _sample_parent return self._sample_exploration_parent() File "/openevolve/openevolve/database.py", line 591, in _sample_exploration_parent return self.programs[parent_id] KeyError: 'd5c8621b-bd6b-48d7-822d-9cf0a1c4b520'

This is after the run has gotten past the population size limit, so my guess is that _enforce_population_limit isn't cleaning up everything it needs to clean up. I am happy to investigate further if this appears to be a real problem.

benjaminy avatar Jun 07 '25 21:06 benjaminy

Got the same error in my experiments with circle packing

KhrulkovV avatar Jun 10 '25 22:06 KhrulkovV

yea, @benjaminy I think your conclusion is right, this happens after a program is removed. I had the same issue.

2025-06-12 18:58:12,925 - INFO - Evaluated program 97c539c4-6cd2-4d52-9e70-e34f8e8a082c in 0.49s: value_score=0.0000, distance_score=0.0000, speed_score=0.0000, combined_score=0.0000 2025-06-12 18:58:12,925 - INFO - Population size (51) exceeds limit (50), removing 1 programs 2025-06-12 18:58:12,925 - INFO - Population size after cleanup: 50 2025-06-12 18:58:12,926 - INFO - Iteration 53: Child 97c539c4-6cd2-4d52-9e70-e34f8e8a082c from parent bbd11945-3ba5-4f7f-8724-b63017df3751 in 60.77s. Metrics: value_score=0.0000, distance_score=0.0000, speed_score=0.0000, combined_score=0.0000 (Δ: value_score=-0.6525, speed_score=-0.3329, combined_score=-0.7025) Error: 'ec2741c6-7b85-4be4-a17d-8bd4473a7685' Traceback (most recent call last): File "/home/p/openevolve/openevolve/cli.py", line 126, in main_async best_program = await openevolve.run( ^^^^^^^^^^^^^^^^^^^^^ File "/home/p/openevolve/openevolve/controller.py", line 231, in run parent, inspirations = self.database.sample() ^^^^^^^^^^^^^^^^^^^^^^ File "/home/p/openevolve/openevolve/database.py", line 196, in sample inspirations = self._sample_inspirations(parent, n=5) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/p/openevolve/openevolve/database.py", line 681, in _sample_inspirations nearby_programs.append(self.programs[program_id]) ~~~~~~~~~~~~~^^^^^^^^^^^^ KeyError: 'ec2741c6-7b85-4be4-a17d-8bd4473a7685'

elviswind avatar Jun 13 '25 03:06 elviswind

I worked around this issue with this patch, use it if you're desperate, @benjaminy 's proposal, a proper clean up is the right solution.

diff --git a/openevolve/database.py b/openevolve/database.py
index 48527c3..9d8aecb 100644
--- a/openevolve/database.py
+++ b/openevolve/database.py
@@ -576,9 +576,18 @@ class ProgramDatabase:
         """
         Sample a parent for exploration (from current island)
         """
-        current_island_programs = self.islands[self.current_island]
+        current_island_program_ids = self.islands[self.current_island]

-        if not current_island_programs:
+        # --- FIX STARTS HERE ---
+        # Filter for programs that actually exist in the main dictionary.
+        # This prevents KeyErrors if the state is inconsistent (e.g., after a faulty load).
+        valid_programs_in_island = [
+            pid for pid in current_island_program_ids if pid in self.programs
+        ]
+
+        # If, after filtering, the island is empty, fall back.
+        if not valid_programs_in_island:
+        # --- FIX ENDS HERE ---
             # If current island is empty, initialize with best program or random program
             if self.best_program_id and self.best_program_id in self.programs:
                 # Clone best program to current island
@@ -587,12 +596,15 @@ class ProgramDatabase:
                 best_program.metadata["island"] = self.current_island
                 logger.debug(f"Initialized empty island {self.current_island} with best program")
                 return best_program
-            else:
+            elif self.programs: # Check if there are any programs at all
                 # Use any available program
                 return next(iter(self.programs.values()))
+            else:
+                # This should only happen if the DB is completely empty.
+                raise ValueError("No programs available for sampling in the entire database.")

-        # Sample from current island
-        parent_id = random.choice(list(current_island_programs))
+        # Sample from the validated list of current island programs
+        parent_id = random.choice(valid_programs_in_island)
         return self.programs[parent_id]

     def _sample_exploitation_parent(self) -> Program:

elviswind avatar Jun 13 '25 03:06 elviswind

This should be fixed now.

codelion avatar Jun 19 '25 06:06 codelion