CodeEvolve
█████ █████
██ ███████████████ ███
██ ████████████ ███████████ ██
██ █████████████ ██████████████ ██
██ ██████████████ ███████████████ █
█ ██████████ █████ ████ ███████████ ██
██ ████████ ████ █ ██ ██ ██████ ███████ █
█ █████████ ███ █ █ ██ █ █████████ █
█ ██████████ █ █████ █████ █ ███████████ █
██ █████████████████████ ██ ██ █████████████████████ █
█ ██████ █████ █ █ █ ██ ██ █████ ██████ █
█ ███████ ██ █ ██ ███ ███ ██ █████ ███████ █
█ ███████████████ █ ███ █ █ ███ ████████████████ █
█ ██████████ ██ █ ███ ██ █ ██ ██████████ █
█ ████████████ ███████ ███ ███████████ ███████████ █
█ ███████████████████ █ ██ █████████████████ █
█ ███████████ ██ ██████ ███████ ██ ███████████ █
█ ███████████████ ███████ ██ ██████ ███████████████ █
██ ███████████████████████ ██ ████████████████████████ █
█ ██████████████████████ ████ ██████████████████ ██ ██
██ █████████████████████ ████ ██████████████████████ █
█ ██████████████████████ ██ ██████████████████████ █
█ ██████████████████████ ██ ██████████████████████ ██
█ █████████████████████ ████████████████████ ██
█ ████████████████ █████████████████ ██
██ ████ ██ █ ██ ██ █ ███████ █
██ ███ ██ ███ ██ ██ █ █████ ███ ██ ████ ██
██ ████████████ ██ ██ █████████ ██
██ ████ ████ █████ ████ █████ ██
███ ███ ██████████ ████ ███
█████ █████
███
██████ ██ ██████ ██
██ ██ █████ ██████ █████ ██ ██ ██ ████ ██ ██ ██ ████
██ ██ ██ ██ ██ ██ ██ ██████ ██ █ ██ █ ██ ██ ██ █ ██
██ ██ ██ ██ ██ ██ ██ ██ ████ ██ █ ██ ██ █ ██
█████ █████ ██████ █████ ██████ ██ ████ ██ ███ ████
An open-source framework that combines large language models with evolutionary algorithms to discover and optimize high-performing code solutions.
CodeEvolve democratizes algorithmic discovery by making LLM-driven evolutionary search transparent, reproducible, and accessible. Whether you're tackling combinatorial optimization, discovering novel algorithms, or optimizing computational kernels, CodeEvolve provides a modular foundation for automated code synthesis guided by quantifiable metrics.
Table of Contents
Why CodeEvolve?
State-of-the-art performance with transparency. CodeEvolve matches or exceeds the performance of closed-source systems like Google DeepMind's AlphaEvolve on established algorithm-discovery benchmarks, while remaining fully open and reproducible.
Cost-effective solutions. Open-weight models like Qwen often match or outperform expensive closed-source LLMs at a fraction of the compute cost, making cutting-edge algorithmic discovery accessible to researchers and practitioners with limited budgets.
Designed for real problems. CodeEvolve addresses meta-optimization tasks where you need to discover programs that solve complex optimization problems—from mathematical constructions to scientific discovery.
Key Features
Islands-based Genetic Algorithm
Multiple populations evolve independently and periodically exchange top performers, maintaining diversity while propagating successful solutions across the search space. This parallel architecture enables efficient exploration and scales naturally to concurrent evaluation.
Modular Evolutionary Operators
Inspiration-based Crossover: Contextual recombination that combines successful solution patterns while preserving semantic coherence. Parent solutions are presented to the LLM along with high-performing "inspiration" programs, allowing it to synthesize novel combinations.
Meta-prompting Exploration: Evolves the prompts themselves, enabling the LLM to reflect on and rewrite its own instructions for more diverse search trajectories. The system maintains a population of prompts that co-evolve with solutions.
Depth-based Exploitation: Targeted refinement mechanism that makes precise edits to promising solutions by maintaining conversation history. The LLM sees the full evolutionary lineage, enabling incremental improvements while preserving working components.
Quality-Diversity Optimization
MAP-Elites Integration: Optional quality-diversity archive that maintains behavioral diversity across the solution space. Supports both grid-based and CVT-based (Centroidal Voronoi Tessellation) feature maps.
Flexible LLM Integration
Ensemble Support: Mix and match multiple LLMs with weighted selection. Use different models for exploration vs. exploitation phases, or combine open-weight and proprietary models.
OpenAI-Compatible APIs: Works with any OpenAI-compatible endpoint including vLLM, Ollama, Together AI, and cloud providers.
How It Works
CodeEvolve operates as a distributed evolutionary algorithm where code itself is the evolving entity:
The Evolutionary Loop
Initialization: Start with an initial code template and system prompt that defines the task
Selection: Choose parent and inspiration programs based on fitness
- Exploration mode: Random or uniform selection for broad search
- Exploitation mode: Tournament or roulette selection for refinement
Variation: Generate new candidates through LLM-driven operations
- Exploration: Broad modifications with meta-prompting
- Exploitation: Targeted improvements with conversation history
Code Generation: LLM produces SEARCH/REPLACE diffs
- Only specified code blocks are modified (between markers)
- Preserves working code outside evolution zones
- Applies structured diffs rather than regenerating entire files
Evaluation: Execute in sandboxed environment
- Resource limits (time, memory)
- Capture metrics and errors
- Extract fitness from evaluation results
Migration: Periodically exchange top solutions between islands
- Maintains diversity while spreading innovations
- Prevents premature convergence
Archiving (optional): MAP-Elites maintains diverse solutions
- Preserves behavioral variety
- Increases diversity in a multi-objective setting
Exploration vs Exploitation
The system dynamically balances exploration and exploitation:
| Phase |
Selection |
LLM Context |
Operators |
| Exploration |
Random |
Random Inspirations |
Meta-prompting exploration |
| Exploitation |
Fitness |
Best Inspirations + Full lineage |
Depth exploitation |
The exploration rate is controlled by a scheduler (e.g., exponential decay) and can adapt based on fitness improvements.
Distributed Islands
┌─────────┐ ┌─────────┐ ┌─────────┐
│Island 0 │────▶│Island 1 │────▶│Island 2 │
│Pop: 20 │ │Pop: 20 │ │Pop: 20 │
└────┬────┘ └────┬────┘ └────┬────┘
│ │ │
└───────────────┴───────────────┘
Periodic Migration
Each island maintains:
- Solution population
- Prompt population
- Local fitness rankings
- Migration history
Core Components
CLI Entry Point (cli.py)
- Parses command-line arguments and orchestrates execution flow
- Validates environment, paths, and configuration
- Creates shared memory and synchronization primitives
- Coordinates island spawning, monitoring, and shutdown
Process Runner (runner.py)
- Process Management: Spawns and monitors island processes
- Signal Handling: Handles SIGTERM, SIGTSTP, SIGQUIT
- Failure Recovery: Detects crashed islands and terminates remaining processes
- Log Daemon: Manages centralized logging process
Evolution Engine (evolution.py)
- Modular Design: Main loop decomposed into specialized helper functions for selection, meta-prompting, code generation, evaluation, and migration
- Coordinates exploration/exploitation scheduling with adaptive rates
- Handles checkpoint creation and restoration with state consistency
Program Database (database.py)
- Manages populations with genealogical tracking and fitness rankings
- Implements multiple selection strategies
- Implements the MAP-Elites algorithm for improved diversity, with Classic grid-based archive and Central Voronoi Tesselations variant
Schedulers (scheduler.py)
- ExponentialScheduler: Exponential growth or decay over epochs (configurable via
weight)
- PlateauScheduler: Adapts value based on fitness improvements (decrease on progress, increase on plateau)
- CosineScheduler: Cycles value using cosine annealing
Schedulers are used for both exploration rate and evaluation timeout scheduling.
Evaluator (evaluator.py)
- Sandboxed program execution with resource limits (time, memory)
- Multi-threaded memory monitoring with process tree management
- Isolated execution in temporary directories
- Structured metrics extraction from JSON results
Islands Coordinator (islands/)
- Synchronization (
sync.py): Barriers, locks, and shared state for distributed coordination
- Communication Graph (
graph.py): Flexible topologies (ring, star, complete, etc.)
- Migration Logic (
migration.py): Threaded send/receive with barrier synchronization
LLM Interface (lm/)
- Abstract Base Classes (
base.py): Clean interface for plugging in different LLM providers
- OpenAI Wrapper (
openai.py): Async client with retry logic and ensemble support
- Mock Models: Set model_name to "MOCK" for debugging without API calls
- Ensemble Support: Weighted random selection from multiple models
- Embedding generation support for semantic search (optional)
Prompt Sampler (prompt/)
- Sampler (
sampler.py): Builds conversation histories from program lineages for context-aware generation
- Templates (
template.py): Predefined prompt templates for evolution and meta-prompting
- Incorporates inspiration programs for crossover operations
- Meta-prompting: Evolves the system prompts themselves for adaptive search
- Dynamic depth control for managing context length
Utilities (utils/)
- Constants (
constants.py): Centralized magic constants (markers, file names, defaults)
- Checkpointing (
ckpt.py): Save and load algorithm state
- Configuration (
cli_setup.py): Validation, loading, and island argument setup
- Parsing (
parsing.py): SEARCH/REPLACE diff application
- Logging (
logging.py): Distributed logging with real-time CLI dashboard
- Locking (
lock.py): Directory-level locks to prevent concurrent runs
Performance Highlights
CodeEvolve demonstrates superior performance on several benchmarks previously used to assess AlphaEvolve:
- Competitive or better results across diverse algorithm-discovery tasks including autocorrelation inequalities, packing problems, and Heilbronn problems
- Open-weight models (e.g., Qwen) matching closed-source performance at significantly lower cost