Skip to content

Commit 84b5845

Browse files
committed
linter errors
1 parent 5782888 commit 84b5845

File tree

3 files changed

+50
-35
lines changed

3 files changed

+50
-35
lines changed

openevolve/database.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

openevolve/evaluator.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ async def evaluate_program(
9090
"""
9191
start_time = time.time()
9292
program_id_str = f" {program_id}" if program_id else ""
93-
93+
9494
# Retry logic for evaluation
9595
last_exception = None
9696
for attempt in range(self.config.max_retries + 1):
@@ -126,19 +126,23 @@ async def evaluate_program(
126126

127127
except Exception as e:
128128
last_exception = e
129-
logger.warning(f"Evaluation attempt {attempt + 1}/{self.config.max_retries + 1} failed for program{program_id_str}: {str(e)}")
130-
129+
logger.warning(
130+
f"Evaluation attempt {attempt + 1}/{self.config.max_retries + 1} failed for program{program_id_str}: {str(e)}"
131+
)
132+
131133
# If this is not the last attempt, wait a bit before retrying
132134
if attempt < self.config.max_retries:
133135
await asyncio.sleep(1.0) # Wait 1 second before retry
134-
136+
135137
finally:
136138
# Clean up temporary file
137139
if os.path.exists(temp_file_path):
138140
os.unlink(temp_file_path)
139-
141+
140142
# All retries failed
141-
logger.error(f"All evaluation attempts failed for program{program_id_str}. Last error: {str(last_exception)}")
143+
logger.error(
144+
f"All evaluation attempts failed for program{program_id_str}. Last error: {str(last_exception)}"
145+
)
142146
return {"error": 0.0}
143147

144148
@run_in_executor

openevolve/prompt/sampler.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,21 +275,24 @@ def _format_evolution_history(
275275
)
276276
+ "\n\n"
277277
)
278-
278+
279279
# Format diverse programs using num_diverse_programs config
280280
diverse_programs_str = ""
281-
if self.config.num_diverse_programs > 0 and len(top_programs) > self.config.num_top_programs:
281+
if (
282+
self.config.num_diverse_programs > 0
283+
and len(top_programs) > self.config.num_top_programs
284+
):
282285
# Skip the top programs we already included
283-
remaining_programs = top_programs[self.config.num_top_programs:]
284-
286+
remaining_programs = top_programs[self.config.num_top_programs :]
287+
285288
# Sample diverse programs from the remaining
286289
num_diverse = min(self.config.num_diverse_programs, len(remaining_programs))
287290
if num_diverse > 0:
288291
# Use random sampling to get diverse programs
289292
diverse_programs = random.sample(remaining_programs, num_diverse)
290-
293+
291294
diverse_programs_str += "\n\n## Diverse Programs\n\n"
292-
295+
293296
for i, program in enumerate(diverse_programs):
294297
# Extract a snippet (first 5 lines for diversity)
295298
program_code = program.get("code", "")
@@ -307,7 +310,9 @@ def _format_evolution_history(
307310
if not key_features:
308311
key_features = [
309312
f"Alternative approach to {name}"
310-
for name in list(program.get("metrics", {}).keys())[:2] # Just first 2 metrics
313+
for name in list(program.get("metrics", {}).keys())[
314+
:2
315+
] # Just first 2 metrics
311316
]
312317

313318
key_features_str = ", ".join(key_features)
@@ -322,7 +327,7 @@ def _format_evolution_history(
322327
)
323328
+ "\n\n"
324329
)
325-
330+
326331
# Combine top and diverse programs
327332
combined_programs_str = top_programs_str + diverse_programs_str
328333

0 commit comments

Comments
 (0)