MCPcopy Index your code
hub / github.com/algorithmicsuperintelligence/openevolve / evolve_algorithm

Function evolve_algorithm

openevolve/api.py:436–539  ·  view source on GitHub ↗

Evolve an algorithm class based on a benchmark Args: algorithm_class: Initial algorithm class to evolve benchmark: Function that takes an instance and returns metrics iterations: Number of evolution iterations **kwargs: Additional arguments for run_evolution

(
    algorithm_class: type, benchmark: Callable, iterations: int = 100, **kwargs
)

Source from the content-addressed store, hash-verified

434
435
436def evolve_algorithm(
437 algorithm_class: type, benchmark: Callable, iterations: int = 100, **kwargs
438) -> EvolutionResult:
439 """
440 Evolve an algorithm class based on a benchmark
441
442 Args:
443 algorithm_class: Initial algorithm class to evolve
444 benchmark: Function that takes an instance and returns metrics
445 iterations: Number of evolution iterations
446 **kwargs: Additional arguments for run_evolution
447
448 Returns:
449 EvolutionResult with optimized algorithm
450
451 Example:
452 class SortAlgorithm:
453 def sort(self, arr):
454 # Simple bubble sort
455 return sorted(arr) # placeholder
456
457 def benchmark_sort(instance):
458 import time
459 test_data = [list(range(100, 0, -1))] # Reverse sorted
460
461 start = time.time()
462 for data in test_data:
463 result = instance.sort(data.copy())
464 if result != sorted(data):
465 return {"score": 0.0}
466
467 duration = time.time() - start
468 return {
469 "score": 1.0,
470 "runtime": duration,
471 "performance": 1.0 / (duration + 0.001)
472 }
473
474 result = evolve_algorithm(SortAlgorithm, benchmark_sort, iterations=50)
475 """
476
477 # Get class source code
478 class_source = inspect.getsource(algorithm_class)
479
480 # Ensure the class has evolution markers
481 if "EVOLVE-BLOCK-START" not in class_source:
482 lines = class_source.split("\n")
483 # Find class definition
484 class_def_line = next(
485 i for i, line in enumerate(lines) if line.strip().startswith("class ")
486 )
487
488 # Add evolution markers around the class body
489 indent = len(lines[class_def_line]) - len(lines[class_def_line].lstrip())
490 lines.insert(class_def_line + 1, " " * (indent + 4) + "# EVOLVE-BLOCK-START")
491 lines.append(" " * (indent + 4) + "# EVOLVE-BLOCK-END")
492 class_source = "\n".join(lines)
493

Callers 1

Calls 1

run_evolutionFunction · 0.70

Tested by 1