Get the top N programs based on a metric Args: n: Number of programs to return metric: Metric to use for ranking (uses average if None) island_idx: If specified, only return programs from this island Returns: List of top prog
(
self, n: int = 10, metric: Optional[str] = None, island_idx: Optional[int] = None
)
| 542 | return sorted_programs[0] if sorted_programs else None |
| 543 | |
| 544 | def get_top_programs( |
| 545 | self, n: int = 10, metric: Optional[str] = None, island_idx: Optional[int] = None |
| 546 | ) -> List[Program]: |
| 547 | """ |
| 548 | Get the top N programs based on a metric |
| 549 | |
| 550 | Args: |
| 551 | n: Number of programs to return |
| 552 | metric: Metric to use for ranking (uses average if None) |
| 553 | island_idx: If specified, only return programs from this island |
| 554 | |
| 555 | Returns: |
| 556 | List of top programs |
| 557 | """ |
| 558 | # Validate island_idx parameter |
| 559 | if island_idx is not None and (island_idx < 0 or island_idx >= len(self.islands)): |
| 560 | raise IndexError(f"Island index {island_idx} is out of range (0-{len(self.islands)-1})") |
| 561 | |
| 562 | if not self.programs: |
| 563 | return [] |
| 564 | |
| 565 | # Get candidate programs |
| 566 | if island_idx is not None: |
| 567 | # Island-specific query |
| 568 | island_programs = [ |
| 569 | self.programs[pid] for pid in self.islands[island_idx] if pid in self.programs |
| 570 | ] |
| 571 | candidates = island_programs |
| 572 | else: |
| 573 | # Global query |
| 574 | candidates = list(self.programs.values()) |
| 575 | |
| 576 | if not candidates: |
| 577 | return [] |
| 578 | |
| 579 | if metric: |
| 580 | # Sort by specific metric |
| 581 | sorted_programs = sorted( |
| 582 | [p for p in candidates if metric in p.metrics], |
| 583 | key=lambda p: p.metrics[metric], |
| 584 | reverse=True, |
| 585 | ) |
| 586 | else: |
| 587 | # Sort by combined_score if available, otherwise by average of all numeric metrics |
| 588 | sorted_programs = sorted( |
| 589 | candidates, |
| 590 | key=lambda p: get_fitness_score(p.metrics, self.config.feature_dimensions), |
| 591 | reverse=True, |
| 592 | ) |
| 593 | |
| 594 | return sorted_programs[:n] |
| 595 | |
| 596 | def save(self, path: Optional[str] = None, iteration: int = 0) -> None: |
| 597 | """ |
no test coverage detected