OpenEvolve:Google DeepMind AlphaEvolve 的开源实现
利用大型语言模型(LLM)发现演化算法

引言
Google DeepMind 最近宣布推出 AlphaEvolve,这是一种进化型编码代理,它利用大型语言模型通过迭代过程优化代码。该系统已被用于在数学算法方面取得突破,并优化关键计算基础设施。
今天,我很高兴介绍 OpenEvolve,这是 AlphaEvolve 系统的开源实现,它将这些功能带给更广泛的研发社区。本文将介绍 OpenEvolve 的架构,展示我们成功复制 AlphaEvolve 结果的两个示例,并提供关于如何将其用于您自己的项目的见解。
背景:演化编码代理
代码演化的想法并不新鲜——遗传编程已经存在了几十年。然而,现代 LLM 与演化技术的结合创造了一种强大的新范式。
AlphaEvolve 在此领域取得了重大进展,具体体现在:
- 使用 LLM 生成复杂的代码修改
- 使用自动化指标评估这些修改
- 使用演化框架改进有前途的解决方案
- 演化整个代码库,而不仅仅是单个函数
OpenEvolve 以灵活、可配置的开源软件包实现了这些原则。
技术架构
OpenEvolve 遵循演化方法,包含四个关键组件:
- Prompt 采样器:创建包含历史程序、其分数和问题描述的上下文丰富 Prompt
- LLM 集成:通过语言模型集成生成代码修改
- 评估器池:测试生成的程序并分配分数
- 程序数据库:存储程序及其评估指标,指导未来的演化
控制器以异步管道的方式协调这些组件之间的交互,最大化吞吐量以评估尽可能多的候选解决方案。
主要特点
- 整个代码文件的演化(而不仅仅是单个函数)
- 支持多种编程语言
- 多目标优化
- 灵活的 Prompt 工程
- 分布式评估
- LLM 集成方法
- 检查点系统,用于恢复运行
示例 1:圆填充问题
AlphaEvolve 论文中解决的问题之一是将 26 个不同大小的圆填充到一个单位正方形中,以最大化它们的半径之和。Google 报告称达到了 2.635 的总和。
我们使用 OpenEvolve 复制了这项实验,目标相同。我们的实现经历了多个阶段的演化:
初始解决方案
# 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 代
到第 10 代,OpenEvolve 发现了一种更复杂的六边形排列方式
# 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。
第 100 代:基于网格的方法
到第 100 代,OpenEvolve 已转向基于网格的策略
# 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。
最终解决方案:数学优化
OpenEvolve 发现数学优化力量时,取得了突破
# 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%!
最终填充排列,半径总和 = 2.634
两阶段方法
我们将演化分为两个阶段:
阶段 1:初始探索
- 侧重于不同的基本方法
- 基于构造器的模式探索
- 种群规模:60
- 岛屿数量:4
- 利用率:0.7
阶段 2:突破瓶颈
- 侧重于根本性创新
- 种群规模增加到 70
- 岛屿数量:5
- 利用率降低到 0.6
- 更新系统提示,建议优化技术
这种两阶段策略对于突破解决方案空间中的瓶颈至关重要。
示例 2:函数最小化
我们的第二个示例侧重于最小化一个复杂的非凸函数
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
演化算法:模拟退火
通过演化,OpenEvolve 发现了一种模拟退火算法
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 性能分析
OpenEvolve 成功的关键因素是 LLM 集成。我们发现:
- 低延迟至关重要 - 我们需要大量的生成,因此快速推理至关重要
- 模型多样性有帮助 - 不同的模型有不同的优势
- 最佳组合:对于大多数任务,Gemini-Flash-2.0-lite + Gemini-Flash-2.0
- Cerebras AI 的 API 提供了最快的推理速度
- 对于圆填充问题:Gemini-Flash-2.0 + Claude-Sonnet-3.7 效果最佳
关键见解:对于演化代码优化,您需要平衡速度和质量。对于大多数生成使用更快模型,偶尔调用更强大的模型,可以提供最佳结果。
如何使用 OpenEvolve
开始使用 OpenEvolve 非常简单:
安装
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
准备您自己的问题
- 使用
# EVOLVE-BLOCK-START
和# EVOLVE-BLOCK-END
注释标记要演化的代码段 - 创建一个评估函数,返回一个指标字典
- 配置 OpenEvolve,设置适当的参数
- 运行演化过程
未来方向
尽管 OpenEvolve 已经展示出令人印象深刻的能力,但未来的发展方向还有很多令人兴奋的方面:
- 通过标准化接口支持更多 LLM 提供商
- 改进 Prompt 工程,实现自动适应
- 更复杂的评估级联,以更快地筛选掉劣质解决方案
- 基于 GUI 的演化过程监控和控制
- 扩展示例库,涵盖更多领域
结论
OpenEvolve 将演化算法发现的力量带入开源社区。通过将 LLM 与演化框架中的自动化评估相结合,它能够跨领域发现新颖的算法。
我们对 AlphaEvolve 的圆填充和函数最小化示例的复现证明了该系统的有效性。将简单的实现转化为复杂的算法的能力——例如从同心环演化到数学优化,或从随机搜索演化到模拟退火——展示了引导式演化搜索的强大功能。
我们邀请您尝试 OpenEvolve,为其开发做出贡献,并将其应用于您自己的算法挑战。代码可在 GitHub 上获取:https://github.com/codelion/openevolve。
资源
- GitHub 仓库:https://github.com/codelion/openevolve
- 圆填充示例:https://github.com/codelion/openevolve/tree/main/examples/circle_packing
- 函数最小化示例:https://github.com/codelion/openevolve/tree/main/examples/function_minimization
- AlphaEvolve 论文:Google DeepMind 博客
如果您喜欢这篇文章,请考虑给仓库点赞并为项目做出贡献!