OpenEvolve 入门:同心环 1.87 到 2.634,只差 AlphaEvolve 0.04%

同心环 1.87 → 六边形 2.18 → 网格 2.32 → 2.634。一个开源复刻版,离 DeepMind 的数字只差 0.04%。

分享
OpenEvolve 入门:同心环 1.87 到 2.634,只差 AlphaEvolve 0.04%

圆填充这个题目,Google 在 AlphaEvolve 论文里报的分数是 2.635。一个开源复刻版跑出来的最好结果 2.634,差 0.04%。

中间的过程更值得看:它从「一个圆放中间、八个小圆围一圈、十六个围外圈」这种小学生画法(约 1.87)出发,第 10 代自己换成了六边形排布(2.18),第 100 代改成网格(2.32),最后一步干脆把整个问题交给 scipy 的 SLSQP 去做数学优化——那是它自己想到的,没人提示。

这篇是 OpenEvolve 作者 Asankhaya Sharma 写在 Hugging Face 上的入门文,2025 年 5 月 20 日发布。原文把架构、两个完整实验、LLM 怎么配、怎么上手全讲了一遍,下面是全文编译。

背景:什么叫「演化编码 agent」

演化代码这件事本身不新——遗传编程已经存在几十年。新的地方在于,把现代 LLM 和演化技术拼在一起。

AlphaEvolve 在这个方向上往前推了一步:

  • 用 LLM 生成复杂的代码改动,而不是随机翻转比特;
  • 用自动指标评测这些改动;
  • 用演化框架筛选、放大有希望的解;
  • 演化的对象是整段代码库,不只是一两个函数。

OpenEvolve 把这套原则做成一个可配置的开源包。

架构:四个部件加一个调度器

OpenEvolve 架构:LLM Ensemble、Evaluator Pool、Program Database、Prompt Sampler 由 Controller 串成演化回路
  • Prompt Sampler:组织上下文,把历史程序、它们的分数、问题描述一起塞进提示词;
  • LLM Ensemble:一组语言模型生成代码改动;
  • Evaluator Pool:跑生成的程序并打分;
  • Program Database:存程序和指标,决定下一轮往哪演化。

Controller 以异步流水线的方式调度这四者,目标是把吞吐拉满——同一时间尽可能多地在评测候选。

七个关键特性

  • 演化整份代码文件,不是单个函数
  • 支持多种编程语言
  • 多目标优化
  • 提示词工程可自由定制
  • 评测可分布式
  • LLM 集成(ensemble)思路
  • 检查点系统,中断可续跑

实验一:圆填充

把 26 个大小不一的圆塞进一个单位正方形,让半径之和最大。四个阶段:

起点:同心环,约 1.87

# Initial attempt
# Place a large circle in the center
centers[0] = [0.5, 0.5]
# Place 8 circles around it in a ring
for i in range(8):
    angle = 2 * np.pi * i / 8
    centers[i + 1] = [0.5 + 0.3 * np.cos(angle), 0.5 + 0.3 * np.sin(angle)]
# Place 16 more circles in an outer ring
for i in range(16):
    angle = 2 * np.pi * i / 16
    centers[i + 9] = [0.5 + 0.7 * np.cos(angle), 0.5 + 0.7 * np.sin(angle)]

这个朴素写法给出约 1.87 的半径和。

第 10 代:六边形排布,约 2.18

# Generation 10
# Parameters for the arrangement (fine-tuned)
r_center = 0.1675  # Central circle radius
# 1. Place central circle
centers[0] = [0.5, 0.5]
radii[0] = r_center
# 2. First ring: 6 circles in hexagonal arrangement
r_ring1 = 0.1035
ring1_distance = r_center + r_ring1 + 0.0005  # Small gap for stability
for i in range(6):
    angle = 2 * np.pi * i / 6
    centers[i+1] = [
        0.5 + ring1_distance * np.cos(angle),
        0.5 + ring1_distance * np.sin(angle)
    ]
    radii[i+1] = r_ring1

分数到约 2.18。注意 r_center、r_ring1 这些常数都是被"调"出来的——它已经在做一个真正的数值优化,只是还没系统化。

第 100 代:换策略,网格,约 2.32

# Generation 100
# Row 1: 5 circles
centers[0] = [0.166, 0.166]
centers[1] = [0.333, 0.166]
centers[2] = [0.500, 0.166]
centers[3] = [0.667, 0.166]
centers[4] = [0.834, 0.166]
# Row 2: 6 circles (staggered)
centers[5] = [0.100, 0.333]
centers[6] = [0.266, 0.333]
# ... additional circles

交错排列给出约 2.32。

终局:把问题交给数学优化,2.634

突破发生在它自己想到"这件事可以当成数学规划来解":

# Final solution with scipy.optimize
def construct_packing():
    # ... initialization code ...

    # Objective function: Negative sum of radii (to maximize)
    def objective(x):
        centers = x[:2*n].reshape(n, 2)
        radii = x[2*n:]
        return -np.sum(radii)

    # Constraint: No overlaps and circles stay within the unit square
    def constraint(x):
        centers = x[:2*n].reshape(n, 2)
        radii = x[2*n:]

        # Overlap constraint
        overlap_constraints = []
        for i in range(n):
            for j in range(i + 1, n):
                dist = np.sqrt(np.sum((centers[i] - centers[j])**2))
                overlap_constraints.append(dist - (radii[i] + radii[j]))
        # ... boundary constraints ...

    # Optimization using SLSQP
    result = minimize(objective, x0, method='SLSQP', bounds=bounds,
                      constraints=constraints)

最终半径和 2.634,与 AlphaEvolve 论文的 2.635 相比,误差在 0.04% 以内。

n=26 的最终排布,半径和 2.634292

两阶段策略:怎么突破平台期

他们把演化拆成两段,这点比数字本身更实用:

  • 第一阶段,广撒网:重点尝试不同的基本思路(各种构造式排布)。种群 60、岛屿 4、利用比 0.7。
  • 第二阶段,突破平台:重点押注激进创新。种群升到 70、岛屿 5、利用比降到 0.6,并改系统提示词,明确建议它使用最优化技术。

作者的评价是:这个两段式安排,是它能不能捅破平台期的关键。

实验二:函数最小化

目标是这个非凸函数的最小值:

f(x, y) = sin(x) * cos(y) + sin(x*y) + (x^2 + y^2)/20

全局最小大约在 (-1.704, 0.678),函数值 -1.519。

起点:随机搜索

def search_algorithm(iterations=1000, bounds=(-5, 5)):
    # Initialize with a random point
    best_x = np.random.uniform(bounds[0], bounds[1])
    best_y = np.random.uniform(bounds[0], bounds[1])
    best_value = evaluate_function(best_x, best_y)

    for _ in range(iterations):
        # Simple random search
        x = np.random.uniform(bounds[0], bounds[1])
        y = np.random.uniform(bounds[0], bounds[1])
        value = evaluate_function(x, y)

        if value < best_value:
            best_value = value
            best_x, best_y = x, y

    return best_x, best_y, best_value

演化结果:模拟退火

def simulated_annealing(bounds=(-5, 5), iterations=1000, step_size=0.1,
                       initial_temperature=100, cooling_rate=0.99):
    # Initialize with a random point
    best_x = np.random.uniform(bounds[0], bounds[1])
    best_y = np.random.uniform(bounds[0], bounds[1])
    best_value = evaluate_function(best_x, best_y)

    current_x, current_y = best_x, best_y
    current_value = best_value
    temperature = initial_temperature

    for _ in range(iterations):
        # Perturb the current solution
        new_x = current_x + np.random.uniform(-step_size, step_size)
        new_y = current_y + np.random.uniform(-step_size, step_size)

        # Ensure the new solution is within bounds
        new_x = max(bounds[0], min(new_x, bounds[1]))
        new_y = max(bounds[0], min(new_y, bounds[1]))
        new_value = evaluate_function(new_x, new_y)

        # Calculate the acceptance probability
        if new_value < current_value:
            current_x, current_y = new_x, new_y
            current_value = new_value
            if new_value < best_value:
                best_x, best_y = new_x, new_y
                best_value = new_value
        else:
            probability = np.exp((current_value - new_value) / temperature)
            if np.random.rand() < probability:
                current_x, current_y = new_x, new_y
                current_value = new_value

        # Cool down the temperature
        temperature *= cooling_rate

    return best_x, best_y, best_value

它自己把四个概念凑齐了:局部搜索(小步扰动)、按温度决定要不要接受更差的解(这是跳出局部最优的关键)、降温日程(探索与利用的平衡)、参数整定。

LLM 怎么配:作者的四条实测经验

  • 延迟是命门。演化要跑很多代,推理慢就直接卡死;Cerebras 的 API 在测试里最快。
  • 模型多样性有用,不同模型擅长的地方不一样。
  • 通用最佳组合:Gemini-Flash-2.0-lite + Gemini-Flash-2.0。
  • 圆填充这个题:Gemini-Flash-2.0 + Claude-Sonnet-3.7 效果最好。

核心结论一句话:演化式代码优化要的是速度与质量的平衡——大多数代用快模型跑,偶尔叫一次强模型,结果最好。

上手四步

安装

git clone https://github.com/codelion/openevolve.git
cd openevolve
pip install -e .

用 Python API

from openevolve import OpenEvolve

# Initialize the system
evolve = OpenEvolve(
    initial_program_path="path/to/initial_program.py",
    evaluation_file="path/to/evaluator.py",
    config_path="path/to/config.yaml"
)

# Run the evolution
best_program = await evolve.run(iterations=1000)

print(f"Best program metrics:")
for name, value in best_program.metrics.items():
    print(f"  {name}: {value:.4f}")

用命令行

python openevolve-run.py path/to/initial_program.py path/to/evaluator.py \
  --config path/to/config.yaml --iterations 1000

关键配置项

# Example configuration
max_iterations: 1000
llm:
  primary_model: "gemini-2.0-flash-lite"
  secondary_model: "gemini-2.0-flash"
  temperature: 0.7
database:
  population_size: 500
  num_islands: 5
  exploitation_ratio: 0.7

准备自己的问题

  1. 用 # EVOLVE-BLOCK-START 和 # EVOLVE-BLOCK-END 标出允许被改的代码段;
  2. 写一个评测函数,返回一个指标字典;
  3. 配好参数;
  4. 跑。

作者列的未来方向

  • 通过标准接口支持更多 LLM 提供方
  • 提示词工程自动适配
  • 更成熟的评测级联(更早剪掉差解)
  • 图形界面的监控与控制
  • 覆盖更多领域的示例库

我们的整理(2026-11 更新)

原文写于 2025 年 5 月,仓库和接口都动过。要把这篇当教程用,先对齐三处:

项 原文(2025-05) 现在(2026-11)
仓库地址 codelion/openevolve 已迁到 algorithmicsuperintelligence/openevolve,Apache-2.0,约 7.8k star,仍在活跃提交
入口 OpenEvolve(...) + await evolve.run() README 现在主推两条:命令行 openevolve-run.py,和库式 evolve_function(func, test_cases=..., iterations=50) / run_evolution(...);另有 Docker 镜像
模型名 gemini-2.0-flash-lite 这类 已换代(gemini-2.5-flash-lite、gemini-3.5-flash 一类),配置里换 primary_model 与 api_base 即可

概念一件都没变:EVOLVE-BLOCK 圈范围、evaluator 打分、岛屿+MAP-Elites 保多样性、分数决定谁当父本。所以原文的实验仍然是最好的入门读物——它展示的不是 API,是这条循环会长出什么。

三个给新手的提醒:

  1. 先跑 function_minimization,别先跑圆填充。后者的评测要检查重叠与越界,条件多;前者只有"函数值多大"一个数,10 分钟能看懂全流程。
  2. 从 50 轮起步,别一上来 1000。原文配置里 max_iterations: 1000 是最终跑法,前三五十轮就该看出分数在不在动;不动就是 evaluator 或 EVOLVE-BLOCK 圈错了,不是模型不行。
  3. 写 20 行的 evaluator,别抄 500 行的。官方示例里的 evaluator 带超时、并发、多指标级联,那是工程件;你自己的第一版只需要「跑一次、算一个数、返回字典」。

文中所有数字都是作者 2025 年的实测,不是我们跑的,温度和模型都换过一轮,复现时数值会不同——但"同心环 → 六边形 → 网格 → 数学优化"这条演化路径是这套方法该有的样子。

这两个实验其实在演示同一件事的两面:搜索会先逼近、再换类。圆填充的前 100 代都在"调参数、微调排布",收益从 1.87 爬到 2.32,然后卡住;真正的跃升来自它把一个连续优化问题识别成了约束优化问题,直接叫 scipy。函数最小化也一样:从随机采点变成沿当前点小步走,再学会用温度接受更差的解。所以判断一次演化有没有"真进展",看两件事就够了——分数是否还在动,以及代码的类别有没有变(还是同一个思路在磨参数,还是换了算法家族)。只动参数不换类,说明它已经到顶了,该改系统提示词或换种群参数了,作者的第二阶段策略干的就是这件事。

想先看更省事的一条路:RSI 入门:圈两个标记,让 AI 替你迭代程序(Google 官方 codelab,托管版,60 分钟);想把这种循环用到真实项目上并锁住收益:RSI 优化程序能快多少。

更多 AI 编程与工程化实践,见 Vibe Coding 技术栈。


来源

  • Asankhaya Sharma (codelion). OpenEvolve: An Open Source Implementation of Google DeepMind's AlphaEvolve. Hugging Face Blog,2025-05-20。https://huggingface.co/blog/codelion/openevolve
  • 仓库与当前入口:https://github.com/algorithmicsuperintelligence/openevolve
  • 原部署仓库(原文引用):https://github.com/codelion/openevolve

本文由 hubwiz 编译整理,转载请注明出处。

这是「AI 编程 / Vibe Coding」专题的一部分。完整地图见:AI 编程 / Vibe Coding 完全指南