Skip to content

Commit 5fbe58f

Browse files
update experiments
1 parent d8a78de commit 5fbe58f

53 files changed

Lines changed: 4682 additions & 13 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
# Experiments with CodeEvolve
2-
This repository contains benchmark implementations, experimental configurations, and reproducibility code for the CodeEvolve paper:
2+
This repository contains benchmark implementations, experimental configurations, and reproducibility code for the CodeEvolve paper (versions 1 and 2):
33

44
> **CodeEvolve: an open source evolutionary coding agent for algorithm discovery and optimization**
55
> Henrique Assumpção, Diego Ferreira, Leandro Campos, Fabricio Murai
66
> [arXiv:2510.14150](https://arxiv.org/abs/2510.14150)
77
8-
## Overview
9-
10-
TODO
11-
12-
## Repository Structure
13-
14-
TODO
158

169
## Prerequisites
17-
Install CodeEvolve and dependencies:
10+
Install CodeEvolve v0.1.0 and dependencies:
1811

1912
```bash
2013
# Clone and install CodeEvolve framework
@@ -33,10 +26,6 @@ export API_KEY=your_api_key
3326
export API_BASE=your_api_base_url
3427
```
3528

36-
## Reproducing results
37-
38-
TODO
39-
4029
## Citation
4130

4231
If you use CodeEvolve in your research, please cite our paper:
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
SYS_MSG: |
2+
SETTING:
3+
You are an expert computational geometer and optimization specialist with deep expertise in circle packing problems, geometric optimization algorithms, and constraint satisfaction.
4+
Your mission is to evolve and optimize a constructor function that generates an optimal arrangement of exactly 21 non-overlapping circles within a rectangle, maximizing the sum of their radii.
5+
6+
PROBLEM CONTEXT:
7+
- **Objective**: Create a function that returns optimal (x, y, radius) coordinates for 21 circles
8+
- **Benchmark**: Beat the AlphaEvolve state-of-the-art result of sum_radii = 2.3658321334167627
9+
- **Container**: Rectangle with perimeter = 4 (width + height = 2). You may choose optimal width/height ratio
10+
- **Constraints**:
11+
* All circles must be fully contained within rectangle boundaries
12+
* No circle overlaps (distance between centers ≥ sum of their radii)
13+
* Exactly 21 circles required
14+
* All radii must be positive
15+
16+
COMPUTATIONAL RESOURCES & IMPLEMENTATION GUIDELINES:
17+
**Core packages**: numpy, scipy, sympy, pandas, networkx, jax, torch, numba, scikit-learn
18+
19+
**Additional useful packages**:
20+
- **Global optimization**: `deap` (evolutionary computation), `platypus` (NSGA-II)
21+
- **Metaheuristics**: `scikit-opt` (PSO, GA, SA), `nevergrad` (gradient-free optimization), `optuna` (hyperparameter optimization)
22+
- **Geometric computing**: `shapely` (geometric operations), `rtree` (spatial indexing), `scipy.spatial` (distance matrices, Voronoi)
23+
- **Constraint programming**: `python-constraint`, `ortools` (Google OR-Tools), `cvxpy` (convex optimization)
24+
- **Physics engines**: `pymunk` (2D rigid body physics), `Box2D` (collision detection)
25+
- **Parallel computing**: `joblib` (embarrassingly parallel), `multiprocessing`, `concurrent.futures`
26+
- **Performance**: `cython` (C extensions), `numexpr` (fast numerical expressions)
27+
28+
PERFORMANCE METRICS:
29+
1. **sum_radii**: Total sum of all 21 circle radii (PRIMARY OBJECTIVE - maximize)
30+
2. **benchmark_ratio**: sum_radii / 2.3658321334167627 (progress toward beating benchmark)
31+
3. **eval_time**: Execution time in seconds (keep reasonable, prefer accuracy over speed)
32+
33+
TECHNICAL REQUIREMENTS:
34+
- **Determinism**: Use fixed random seeds if employing stochastic methods for reproducibility
35+
- **Error handling**: Graceful handling of optimization failures or infeasible configurations
36+
- **Memory efficiency**: Avoid excessive memory allocation for distance matrix computations
37+
- **Scalability**: Design with potential extension to different circle counts in mind
38+
39+
# PROMPT-BLOCK-START
40+
**Recommended implementation patterns**:
41+
- **Hierarchical optimization**: First optimize rectangle aspect ratio, then circle placement within optimal container
42+
- **Multi-stage approach**: Start with uniform radii placement, then allow radius variation in second optimization phase
43+
- **Constraint violation handling**: Use penalty methods or constraint satisfaction to ensure feasibility
44+
- **Parallel evaluation**: Leverage multiple CPU cores for population-based algorithms (GA, PSO, differential evolution)
45+
- **Adaptive discretization**: Begin with coarse grid-based positioning, progressively refine to continuous coordinates
46+
- **Hybrid algorithms**: Combine global metaheuristics with local gradient-based refinement (scipy.optimize.minimize)
47+
- **Restart mechanisms**: Multiple independent runs with different initializations, select best result
48+
- **Progressive complexity**: Start with simpler sub-problems (fewer circles) and incrementally add complexity
49+
50+
MATHEMATICAL CONSIDERATIONS:
51+
- **Aspect ratio optimization**: Golden ratio (~1.618) often emerges as optimal for packing problems, but verify empirically
52+
- **Circle size distribution**: Consider both uniform radii and variable radii strategies - sometimes heterogeneous sizes pack more efficiently
53+
- **Geometric bounds**: Maximum possible radius for any circle is min(width, height)/2; total area constraint: π∑r² ≤ width×height
54+
- **Distance constraints**: For circles i,j: √((xi-xj)² + (yi-yj)²) ≥ ri + rj (non-overlap)
55+
- **Boundary constraints**: ri ≤ xi ≤ width-ri and ri ≤ yi ≤ height-ri (containment)
56+
- **Symmetry exploitation**: Look for symmetric arrangements (reflection, rotation) that might be optimal
57+
- **Packing density**: Theoretical maximum density for infinite plane is π/(2√3) ≈ 0.9069; rectangle constraints reduce this significantly
58+
- **Contact graph theory**: Model circle tangencies as graph structures to identify promising geometric configurations
59+
60+
ALGORITHMIC STRATEGIES TO CONSIDER:
61+
- **Physics-based simulation**: Use pymunk/Box2D with repulsive forces to naturally separate overlapping circles
62+
- **Evolutionary algorithms**: NSGA-II or CMA-ES with population size 50-200, run for 1000+ generations
63+
- **Simulated annealing**: Temperature scheduling from high (accept worse solutions) to low (hill-climbing)
64+
- **Particle swarm optimization**: Velocity-based updates with social/cognitive parameters, include constraint handling
65+
- **Sequential placement**: Greedy algorithms that place circles one-by-one in locally optimal positions
66+
- **Space partitioning**: Quadtree or spatial hashing for efficient collision detection during optimization
67+
- **Gradient-free methods**: Nelder-Mead, Powell, or COBYLA for local refinement of promising configurations
68+
- **Multi-objective optimization**: Balance sum_radii maximization with constraint violation minimization
69+
- **Clustering approaches**: Group circles into regions, optimize within regions, then optimize region arrangement
70+
- **Machine learning guidance**: Train surrogate models to predict promising initial configurations
71+
72+
VALIDATION FRAMEWORK:
73+
- **Constraint verification**: Automated checking of all geometric constraints (containment, non-overlap)
74+
- **Floating-point precision**: Use tolerance ε ≈ 1e-10 for numerical comparisons to handle rounding errors
75+
- **Visual validation**: Generate matplotlib plots showing circle arrangements for manual inspection
76+
- **Statistical analysis**: Run multiple seeds (≥10) and report mean, std, min, max of sum_radii across runs
77+
- **Benchmark comparison**: Calculate improvement percentage over AlphaEvolve baseline
78+
- **Regression testing**: Ensure new approaches don't degrade performance on simpler test cases
79+
- **Performance profiling**: Use cProfile to identify computational bottlenecks and optimize critical paths
80+
- **Edge case testing**: Verify behavior with extreme aspect ratios, very small/large rectangles
81+
- **Incremental validation**: Test with fewer circles (5, 10, 15) to build confidence in algorithmic approach
82+
- **Cross-validation**: Compare results across different optimization algorithms to identify consistent solutions
83+
# PROMPT-BLOCK-END
84+
85+
CODEBASE_PATH: 'src/'
86+
INIT_FILE_DATA: {filename: 'init_program.py', language: 'python'}
87+
EVAL_FILE_NAME: 'evaluate.py'
88+
EVAL_TIMEOUT: 360
89+
90+
MAX_MEM_BYTES: 1000000000
91+
MEM_CHECK_INTERVAL_S: 0.1
92+
93+
EVOLVE_CONFIG: {fitness_key: 'benchmark_ratio',
94+
num_epochs: 200,ckpt: 5,max_size: 40,init_pop: 6,
95+
exploration_rate: 0.3,
96+
selection_policy: 'roulette', selection_kwargs: {roulette_by_rank: True},
97+
early_stopping_rounds: 100,
98+
num_islands: 5, migration_topology: 'ring', migration_interval: 40, migration_rate: 0.1,
99+
meta_prompting: True, num_inspirations: 3,
100+
max_chat_depth: 3}
101+
102+
ENSEMBLE: [{model_name: 'GOOGLE_GEMINI-2.5-FLASH', temp: 0.7, top_p: 0.95, retries: 3, weight: 0.8, verify_ssl: False},
103+
{model_name: 'GOOGLE_GEMINI-2.5-PRO', temp: 0.7, top_p: 0.95, retries: 3, weight: 0.2, verify_ssl: False}]
104+
105+
SAMPLER_AUX_LM : {model_name: 'GOOGLE_GEMINI-2.5-FLASH', temp: 0.7, top_p: 0.95, retries: 3, weight: 0.8, verify_ssl: False}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
SYS_MSG: |
2+
SETTING:
3+
You are an expert computational geometer and optimization specialist with deep expertise in circle packing problems, geometric optimization algorithms, and constraint satisfaction.
4+
Your mission is to evolve and optimize a constructor function that generates an optimal arrangement of exactly 21 non-overlapping circles within a rectangle, maximizing the sum of their radii.
5+
6+
PROBLEM CONTEXT:
7+
- **Objective**: Create a function that returns optimal (x, y, radius) coordinates for 21 circles
8+
- **Benchmark**: Beat the AlphaEvolve state-of-the-art result of sum_radii = 2.3658321334167627
9+
- **Container**: Rectangle with perimeter = 4 (width + height = 2). You may choose optimal width/height ratio
10+
- **Constraints**:
11+
* All circles must be fully contained within rectangle boundaries
12+
* No circle overlaps (distance between centers ≥ sum of their radii)
13+
* Exactly 21 circles required
14+
* All radii must be positive
15+
16+
COMPUTATIONAL RESOURCES & IMPLEMENTATION GUIDELINES:
17+
**Core packages**: numpy, scipy, sympy, pandas, networkx, jax, torch, numba, scikit-learn
18+
19+
**Additional useful packages**:
20+
- **Global optimization**: `dea[]` (evolutionary computation), `platypus` (NSGA-II)
21+
- **Metaheuristics**: `scikit-opt` (PSO, GA, SA), `nevergrad` (gradient-free optimization), `optuna` (hyperparameter optimization)
22+
- **Geometric computing**: `shapely` (geometric operations), `rtree` (spatial indexing), `scipy.spatial` (distance matrices, Voronoi)
23+
- **Constraint programming**: `python-constraint`, `ortools` (Google OR-Tools), `cvxpy` (convex optimization)
24+
- **Physics engines**: `pymunk` (2D rigid body physics), `Box2D` (collision detection)
25+
- **Parallel computing**: `joblib` (embarrassingly parallel), `multiprocessing`, `concurrent.futures`
26+
- **Performance**: `cython` (C extensions), `numexpr` (fast numerical expressions)
27+
28+
PERFORMANCE METRICS:
29+
1. **sum_radii**: Total sum of all 21 circle radii (PRIMARY OBJECTIVE - maximize)
30+
2. **benchmark_ratio**: sum_radii / 2.3658321334167627 (progress toward beating benchmark)
31+
3. **eval_time**: Execution time in seconds (keep reasonable, prefer accuracy over speed)
32+
33+
TECHNICAL REQUIREMENTS:
34+
- **Determinism**: Use fixed random seeds if employing stochastic methods for reproducibility
35+
- **Error handling**: Graceful handling of optimization failures or infeasible configurations
36+
- **Memory efficiency**: Avoid excessive memory allocation for distance matrix computations
37+
- **Scalability**: Design with potential extension to different circle counts in mind
38+
39+
# PROMPT-BLOCK-START
40+
41+
**Recommended implementation patterns**:
42+
TODO
43+
44+
MATHEMATICAL CONSIDERATIONS:
45+
TODO
46+
47+
ALGORITHMIC STRATEGIES TO CONSIDER:
48+
TODO
49+
50+
VALIDATION FRAMEWORK:
51+
TODO
52+
53+
# PROMPT-BLOCK-END
54+
55+
CODEBASE_PATH: 'src/'
56+
INIT_FILE_DATA: {filename: 'init_program.py', language: 'python'}
57+
EVAL_FILE_NAME: 'evaluate.py'
58+
EVAL_TIMEOUT: 360
59+
60+
MAX_MEM_BYTES: 1000000000
61+
MEM_CHECK_INTERVAL_S: 0.1
62+
63+
EVOLVE_CONFIG: {fitness_key: 'benchmark_ratio',
64+
num_epochs: 150,ckpt: 5,max_size: 40,init_pop: 6,
65+
exploration_rate: 0.3,
66+
selection_policy: 'roulette', selection_kwargs: {roulette_by_rank: True},
67+
early_stopping_rounds: 100,
68+
num_islands: 5, migration_topology: 'ring', migration_interval: 40, migration_rate: 0.1,
69+
meta_prompting: True, num_inspirations: 3,
70+
max_chat_depth: 3}
71+
72+
ENSEMBLE: [{model_name: 'GOOGLE_GEMINI-2.5-FLASH', temp: 0.7, top_p: 0.95, retries: 3, weight: 0.8, verify_ssl: False},
73+
{model_name: 'GOOGLE_GEMINI-2.5-PRO', temp: 0.7, top_p: 0.95, retries: 3, weight: 0.2, verify_ssl: False}]
74+
75+
SAMPLER_AUX_LM : {model_name: 'GOOGLE_GEMINI-2.5-FLASH', temp: 0.7, top_p: 0.95, retries: 3, weight: 0.8, verify_ssl: False}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# ===--------------------------------------------------------------------------------------===#
2+
#
3+
# Part of the CodeEvolve Project, under the Apache License v2.0.
4+
# See https://github.com/inter-co/science-codeevolve/blob/main/LICENSE for license information.
5+
# SPDX-License-Identifier: Apache-2.0
6+
#
7+
# ===--------------------------------------------------------------------------------------===#
8+
#
9+
# This file implements the evaluator for the circle packing problem on a rectangle
10+
# of perimeter 4.
11+
#
12+
# ===--------------------------------------------------------------------------------------===#
13+
#
14+
# Some of the code in this file is adapted from:
15+
#
16+
# google-deepmind/alphaevolve_results:
17+
# Licensed under the Apache License v2.0.
18+
#
19+
# ===--------------------------------------------------------------------------------------===#
20+
21+
import time
22+
import numpy as np
23+
import sys
24+
import json
25+
import os
26+
from importlib import __import__
27+
28+
BENCHMARK = 2.3658321334167627
29+
NUM_CIRCLES = 21
30+
TOL = 1e-6
31+
32+
33+
def minimum_circumscribing_rectangle(circles: np.ndarray):
34+
"""Returns the width and height of the minimum circumscribing rectangle.
35+
36+
Args:
37+
circles: A numpy array of shape (num_circles, 3), where each row is of the
38+
form (x, y, radius), specifying a circle.
39+
40+
Returns:
41+
A tuple (width, height) of the minimum circumscribing rectangle.
42+
"""
43+
min_x = np.min(circles[:, 0] - circles[:, 2])
44+
max_x = np.max(circles[:, 0] + circles[:, 2])
45+
min_y = np.min(circles[:, 1] - circles[:, 2])
46+
max_y = np.max(circles[:, 1] + circles[:, 2])
47+
return max_x - min_x, max_y - min_y
48+
49+
50+
def validate_packing_radii(radii: np.ndarray) -> None:
51+
n = len(radii)
52+
for i in range(n):
53+
if radii[i] < 0:
54+
raise ValueError(f"Circle {i} has negative radius {radii[i]}")
55+
elif np.isnan(radii[i]):
56+
raise ValueError(f"Circle {i} has nan radius")
57+
58+
59+
def validate_packing_overlap_wtol(circles: np.ndarray, tol: float = 1e-6) -> None:
60+
n = len(circles)
61+
for i in range(n):
62+
for j in range(i + 1, n):
63+
dist = np.sqrt(np.sum((circles[i, :2] - circles[j, :2]) ** 2))
64+
if dist < circles[i, 2] + circles[j, 2] - tol:
65+
raise ValueError(
66+
f"Circles {i} and {j} overlap: dist={dist}, r1+r2={circles[i,2]+circles[j,2]}"
67+
)
68+
69+
70+
def validate_packing_inside_rect_wtol(circles: np.array, tol: float = 1e-6) -> None:
71+
width, height = minimum_circumscribing_rectangle(circles)
72+
if width + height > (2 + tol):
73+
raise ValueError("Circles are not contained inside a rectangle of perimeter 4.")
74+
75+
76+
def evaluate(program_path: str, results_path: str = None) -> None:
77+
abs_program_path = os.path.abspath(program_path)
78+
program_dir = os.path.dirname(abs_program_path)
79+
module_name = os.path.splitext(os.path.basename(program_path))[0]
80+
81+
circles = None
82+
eval_time = 0
83+
try:
84+
sys.path.insert(0, program_dir)
85+
program = __import__(module_name)
86+
87+
start_time = time.time()
88+
circles = program.circle_packing21()
89+
end_time = time.time()
90+
eval_time = end_time - start_time
91+
except Exception as err:
92+
raise err
93+
finally:
94+
if program_dir in sys.path:
95+
sys.path.remove(program_dir)
96+
97+
if not isinstance(circles, np.ndarray):
98+
circles = np.array(circles)
99+
100+
if circles.shape != (NUM_CIRCLES, 3):
101+
raise ValueError(
102+
f"Invalid shapes: circles = {circles.shape}, expected {(NUM_CIRCLES,3)}"
103+
)
104+
105+
validate_packing_radii(circles[:, -1])
106+
validate_packing_overlap_wtol(circles, TOL)
107+
validate_packing_inside_rect_wtol(circles, TOL)
108+
109+
radii_sum = np.sum(circles[:, -1])
110+
with open(results_path, "w") as f:
111+
json.dump(
112+
{
113+
"radii_sum": float(radii_sum),
114+
"benchmark_ratio": float(radii_sum / BENCHMARK),
115+
"eval_time": float(eval_time),
116+
},
117+
f,
118+
indent=4,
119+
)
120+
121+
122+
if __name__ == "__main__":
123+
program_path = sys.argv[1]
124+
results_path = sys.argv[2]
125+
126+
evaluate(program_path, results_path)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# EVOLVE-BLOCK-START
2+
import numpy as np
3+
4+
5+
def circle_packing21() -> np.ndarray:
6+
"""
7+
Places 21 non-overlapping circles inside a rectangle of perimeter 4 in order to maximize the sum of their radii.
8+
9+
Returns:
10+
circles: np.array of shape (21,3), where the i-th row (x,y,r) stores the (x,y) coordinates of the i-th circle of radius r.
11+
"""
12+
n = 21
13+
circles = np.zeros((n, 3))
14+
15+
return circles
16+
17+
18+
# EVOLVE-BLOCK-END
19+
20+
if __name__ == "__main__":
21+
circles = circle_packing21()
22+
print(f"Radii sum: {np.sum(circles[:,-1])}")

0 commit comments

Comments
 (0)