Skip to content

Commit 7cbac30

Browse files
authored
Merge pull request #7 from codelion/feature/replicate-circle-packing
Feature/replicate circle packing
2 parents 10a4ad3 + 2696912 commit 7cbac30

File tree

11 files changed

+801
-1
lines changed

11 files changed

+801
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ ENV/
3434

3535
# Output files
3636
examples/*/output/
37-
openevolve_output/
37+
openevolve_output*/
3838
*.log
3939

4040
# Test cache

examples/circle_packing/README.md

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
# Circle Packing Example
2+
3+
This example demonstrates how OpenEvolve can be used to tackle the challenging mathematical problem of circle packing, a classic problem in computational geometry. Specifically, we focus on packing 26 circles of varying sizes into a unit square to maximize the sum of their radii, replicating one of the tasks from the AlphaEvolve paper.
4+
5+
## Problem Overview
6+
7+
The circle packing problem involves placing n non-overlapping circles inside a container (in this case, a unit square) to optimize a specific metric. For this example:
8+
9+
- We pack exactly 26 circles
10+
- Each circle must lie entirely within the unit square
11+
- No circles may overlap
12+
- We aim to maximize the sum of all circle radii
13+
14+
According to the AlphaEvolve paper, a solution with a sum of radii of approximately 2.635 is achievable for n=26. Our goal was to match or exceed this result.
15+
16+
## Our Approach
17+
18+
We structured our evolution in two phases, each with a different configuration to encourage exploration and exploitation at different stages:
19+
20+
### Phase 1: Initial Exploration
21+
22+
In the first phase, we focused on exploring different fundamental approaches to the packing problem:
23+
24+
- Used a constructor-based approach that places circles in strategic positions
25+
- Explored various geometric patterns (concentric rings, grid-based arrangements, etc.)
26+
- Developed simple optimization routines to maximize circle sizes without overlaps
27+
28+
Configuration highlights:
29+
```yaml
30+
max_iterations: 100
31+
population_size: 60
32+
num_islands: 4
33+
exploitation_ratio: 0.7
34+
```
35+
36+
### Phase 2: Breaking Through the Plateau
37+
38+
After the initial exploration phase, we observed our solutions plateauing around 2.377. For the second phase, we reconfigured OpenEvolve to encourage more radical innovations:
39+
40+
- Increased the population size to promote diversity
41+
- Lowered the exploitation ratio to favor exploration
42+
- Updated the system prompt to suggest different optimization techniques
43+
- Allowed for longer and more complex code solutions
44+
45+
Configuration highlights:
46+
```yaml
47+
max_iterations: 100
48+
population_size: 70
49+
num_islands: 5
50+
exploitation_ratio: 0.6
51+
```
52+
53+
## Evolution Progress
54+
55+
We tracked the evolution over 470 generations, capturing visualizations at each checkpoint. The progression shows dramatic improvements in the packing strategy:
56+
57+
### Initial Solution (Generation 0)
58+
59+
The initial program used a simple constructive approach with a central circle and two concentric rings:
60+
61+
```python
62+
# Initial attempt
63+
# Place a large circle in the center
64+
centers[0] = [0.5, 0.5]
65+
66+
# Place 8 circles around it in a ring
67+
for i in range(8):
68+
angle = 2 * np.pi * i / 8
69+
centers[i + 1] = [0.5 + 0.3 * np.cos(angle), 0.5 + 0.3 * np.sin(angle)]
70+
71+
# Place 16 more circles in an outer ring
72+
for i in range(16):
73+
angle = 2 * np.pi * i / 16
74+
centers[i + 9] = [0.5 + 0.7 * np.cos(angle), 0.5 + 0.7 * np.sin(angle)]
75+
```
76+
77+
This approach yielded a sum of radii of approximately 0.959.
78+
79+
![Initial Circle Packing](circle_packing_1.png)
80+
81+
### Generation 10 Breakthrough
82+
83+
By generation 10, OpenEvolve had already discovered a more sophisticated approach:
84+
85+
```python
86+
# Generation 10
87+
# Parameters for the arrangement (fine-tuned)
88+
r_center = 0.1675 # Central circle radius
89+
90+
# 1. Place central circle
91+
centers[0] = [0.5, 0.5]
92+
radii[0] = r_center
93+
94+
# 2. First ring: 6 circles in hexagonal arrangement
95+
r_ring1 = 0.1035
96+
ring1_distance = r_center + r_ring1 + 0.0005 # Small gap for stability
97+
for i in range(6):
98+
angle = 2 * np.pi * i / 6
99+
centers[i+1] = [
100+
0.5 + ring1_distance * np.cos(angle),
101+
0.5 + ring1_distance * np.sin(angle)
102+
]
103+
radii[i+1] = r_ring1
104+
```
105+
106+
The key innovations at this stage included:
107+
- A carefully tuned hexagonal arrangement for the first ring
108+
- Strategic placement of corner circles
109+
- An additional optimization step to maximize each circle's radius
110+
111+
This approach achieved a sum of radii of approximately 1.795.
112+
113+
![Generation 10 Packing](circle_packing_10.png)
114+
115+
### Generation 100: Grid-Based Approach
116+
117+
By generation 100, OpenEvolve had pivoted to a grid-based approach with variable sized circles:
118+
119+
```python
120+
# Generation 100
121+
# Row 1: 5 circles
122+
centers[0] = [0.166, 0.166]
123+
centers[1] = [0.333, 0.166]
124+
centers[2] = [0.500, 0.166]
125+
centers[3] = [0.667, 0.166]
126+
centers[4] = [0.834, 0.166]
127+
128+
# Row 2: 6 circles (staggered)
129+
centers[5] = [0.100, 0.333]
130+
centers[6] = [0.266, 0.333]
131+
# ... additional circles
132+
```
133+
134+
Key innovations:
135+
- Grid-like pattern with staggered rows
136+
- Variable circle sizes based on position (larger in the center)
137+
- More aggressive optimization routine with 50 iterations
138+
139+
This approach achieved a sum of radii of approximately 2.201.
140+
141+
![Generation 100 Packing](circle_packing_190.png)
142+
143+
### Final Solution: Mathematical Optimization
144+
145+
The breakthrough came when OpenEvolve discovered the power of mathematical optimization techniques. The final solution uses:
146+
147+
```python
148+
# Final solution with scipy.optimize
149+
def construct_packing():
150+
# ... initialization code ...
151+
152+
# Objective function: Negative sum of radii (to maximize)
153+
def objective(x):
154+
centers = x[:2*n].reshape(n, 2)
155+
radii = x[2*n:]
156+
return -np.sum(radii)
157+
158+
# Constraint: No overlaps and circles stay within the unit square
159+
def constraint(x):
160+
centers = x[:2*n].reshape(n, 2)
161+
radii = x[2*n:]
162+
163+
# Overlap constraint
164+
overlap_constraints = []
165+
for i in range(n):
166+
for j in range(i + 1, n):
167+
dist = np.sqrt(np.sum((centers[i] - centers[j])**2))
168+
overlap_constraints.append(dist - (radii[i] + radii[j]))
169+
# ... boundary constraints ...
170+
171+
# Optimization using SLSQP
172+
result = minimize(objective, x0, method='SLSQP', bounds=bounds, constraints=constraints)
173+
```
174+
175+
The key innovation in the final solution:
176+
- Using `scipy.optimize.minimize` with SLSQP method to find the optimal configuration
177+
- Formulating circle packing as a constrained optimization problem
178+
- Representing both circle positions and radii as optimization variables
179+
- Carefully crafted constraints to enforce non-overlap and boundary conditions
180+
181+
This approach achieved a sum of radii of 2.634, matching the AlphaEvolve paper's result of 2.635 to within 0.04%!
182+
183+
![Final Packing Solution](circle_packing_460.png)
184+
185+
## Results
186+
187+
Our final solution achieves:
188+
189+
```
190+
Sum of radii: 2.634292402141039
191+
Target ratio: 0.9997314619131079 (99.97% of AlphaEvolve's result)
192+
```
193+
194+
This demonstrates that OpenEvolve can successfully reproduce the results from the AlphaEvolve paper on this mathematical optimization problem.
195+
196+
## Key Observations
197+
198+
The evolution process demonstrated several interesting patterns:
199+
200+
1. **Algorithm Transition**: OpenEvolve discovered increasingly sophisticated algorithms, from basic geometric constructions to advanced mathematical optimization techniques.
201+
202+
2. **Exploration-Exploitation Balance**: The two-phase approach was crucial - initial exploration of different patterns followed by exploitation and refinement of the most promising approaches.
203+
204+
3. **Breakthrough Discoveries**: The most significant improvements came from fundamental changes in approach (e.g., switching from manual construction to mathematical optimization), not just parameter tuning.
205+
206+
4. **Code Complexity Evolution**: As the solutions improved, the code grew in complexity, adopting more sophisticated mathematical techniques.
207+
208+
## Running the Example
209+
210+
To reproduce our results:
211+
212+
```bash
213+
# Phase 1: Initial exploration
214+
python openevolve-run.py examples/circle_packing/initial_program.py \
215+
examples/circle_packing/evaluator.py \
216+
--config examples/circle_packing/config_phase_1.yaml \
217+
--iterations 100
218+
219+
# Phase 2: Breaking through the plateau
220+
python openevolve-run.py examples/circle_packing/openevolve_output/checkpoints/checkpoint_100/best_program.py \
221+
examples/circle_packing/evaluator.py \
222+
--config examples/circle_packing/config_phase_2.yaml \
223+
--iterations 100
224+
```
225+
226+
To visualize the best solution:
227+
228+
```python
229+
from examples.circle_packing.openevolve_output.best.best_program import run_packing, visualize
230+
231+
centers, radii, sum_radii = run_packing()
232+
print(f"Sum of radii: {sum_radii}")
233+
visualize(centers, radii)
234+
```
235+
236+
## Conclusion
237+
238+
This example demonstrates OpenEvolve's ability to discover sophisticated algorithms for mathematical optimization problems. By evolving from simple constructive approaches to advanced numerical optimization techniques, OpenEvolve was able to match the results reported in the AlphaEvolve paper.
239+
240+
The circle packing problem shows how OpenEvolve can discover not just improvements to existing algorithms, but entirely new algorithmic approaches, transitioning from manual geometric construction to principled mathematical optimization.
42.1 KB
Loading
60.3 KB
Loading
68.1 KB
Loading
73.2 KB
Loading
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Configuration for circle packing constructor evolution (n=26)
2+
max_iterations: 100 # Increased iterations
3+
checkpoint_interval: 10
4+
log_level: "INFO"
5+
6+
# LLM configuration
7+
llm:
8+
primary_model: "google/gemini-2.0-flash-001"
9+
# primary_model: "llama3.1-8b"
10+
primary_model_weight: 0.8
11+
secondary_model: "anthropic/claude-3.7-sonnet"
12+
# secondary_model: "llama-4-scout-17b-16e-instruct"
13+
secondary_model_weight: 0.2
14+
api_base: "https://openrouter.ai/api/v1"
15+
# api_base: "https://api.cerebras.ai/v1"
16+
temperature: 0.7
17+
top_p: 0.95
18+
max_tokens: 8192
19+
timeout: 600
20+
21+
# Prompt configuration
22+
prompt:
23+
system_message: |
24+
You are an expert mathematician specializing in circle packing problems and computational geometry. Your task is to improve a constructor function that directly produces a specific arrangement of 26 circles in a unit square, maximizing the sum of their radii. The AlphaEvolve paper achieved a sum of 2.635 for n=26.
25+
26+
Key geometric insights:
27+
- Circle packings often follow hexagonal patterns in the densest regions
28+
- Maximum density for infinite circle packing is pi/(2*sqrt(3)) ≈ 0.9069
29+
- Edge effects make square container packing harder than infinite packing
30+
- Circles can be placed in layers or shells when confined to a square
31+
- Similar radius circles often form regular patterns, while varied radii allow better space utilization
32+
- Perfect symmetry may not yield the optimal packing due to edge effects
33+
34+
Focus on designing an explicit constructor that places each circle in a specific position, rather than an iterative search algorithm.
35+
num_top_programs: 3
36+
use_template_stochasticity: true
37+
38+
# Database configuration
39+
database:
40+
population_size: 60 # Increased population for more diversity
41+
archive_size: 25
42+
num_islands: 4
43+
elite_selection_ratio: 0.3
44+
exploitation_ratio: 0.7
45+
46+
# Evaluator configuration
47+
evaluator:
48+
timeout: 60
49+
cascade_evaluation: true
50+
cascade_thresholds: [0.5, 0.75]
51+
parallel_evaluations: 4
52+
use_llm_feedback: false
53+
54+
# Evolution settings
55+
diff_based_evolution: false # Use full rewrites instead of diffs
56+
allow_full_rewrites: true # Allow full rewrites for constructor functions
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Configuration for breaking through the circle packing plateau
2+
max_iterations: 100
3+
checkpoint_interval: 10
4+
log_level: "INFO"
5+
6+
# LLM configuration
7+
llm:
8+
primary_model: "google/gemini-2.0-flash-001"
9+
# primary_model: "llama3.1-8b"
10+
primary_model_weight: 0.8
11+
secondary_model: "anthropic/claude-3.7-sonnet"
12+
# secondary_model: "llama-4-scout-17b-16e-instruct"
13+
secondary_model_weight: 0.2
14+
api_base: "https://openrouter.ai/api/v1"
15+
# api_base: "https://api.cerebras.ai/v1"
16+
temperature: 0.7
17+
top_p: 0.95
18+
max_tokens: 8192
19+
timeout: 600
20+
21+
# Prompt configuration
22+
prompt:
23+
system_message: |
24+
You are an expert mathematician specializing in circle packing problems and computational geometry. We're trying to reach the AlphaEvolve target of 2.635 for the sum of radii when packing 26 circles in a unit square. The current implementation has plateaued at 2.377, so we need significant improvements.
25+
26+
Key insights to explore:
27+
1. The optimal arrangement likely involves variable-sized circles
28+
2. A pure hexagonal arrangement may not be optimal due to edge effects
29+
3. The densest known circle packings often use a hybrid approach
30+
4. The optimization routine is critically important - simple physics-based models with carefully tuned parameters
31+
5. Consider strategic placement of circles at square corners and edges
32+
6. Adjusting the pattern to place larger circles at the center and smaller at the edges
33+
7. The math literature suggests special arrangements for specific values of n
34+
35+
Focus on breaking through the plateau by trying fundamentally different approaches - don't just tweak parameters.
36+
num_top_programs: 4
37+
use_template_stochasticity: true
38+
39+
# Database configuration
40+
database:
41+
population_size: 70 # Larger population for more diversity
42+
archive_size: 30
43+
num_islands: 5
44+
elite_selection_ratio: 0.3
45+
exploitation_ratio: 0.6 # Slightly lower to encourage exploration
46+
47+
# Evaluator configuration
48+
evaluator:
49+
timeout: 90 # Extended timeout to allow for more complex optimization
50+
cascade_evaluation: true
51+
cascade_thresholds: [0.5, 0.8]
52+
parallel_evaluations: 4
53+
use_llm_feedback: false
54+
55+
# Evolution settings
56+
diff_based_evolution: false
57+
allow_full_rewrites: true # Definitely allow full rewrites
58+
max_code_length: 100000

0 commit comments

Comments
 (0)