Skip to content

Commit a4e0107

Browse files
committed
linter
1 parent fa5116c commit a4e0107

File tree

7 files changed

+73
-56
lines changed

7 files changed

+73
-56
lines changed

examples/circle_packing/best_program.py

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,88 +3,96 @@
33
import numpy as np
44
from scipy.optimize import minimize
55

6+
67
def construct_packing():
78
"""
89
Construct an optimized arrangement of 26 circles in a unit square
910
using mathematical principles and optimization techniques.
10-
11+
1112
Returns:
1213
Tuple of (centers, radii, sum_of_radii)
1314
centers: np.array of shape (26, 2) with (x, y) coordinates
1415
radii: np.array of shape (26) with radius of each circle
1516
sum_of_radii: Sum of all radii
1617
"""
1718
n = 26
18-
19+
1920
# Initial guess: Strategic placement with some randomness
2021
centers = np.zeros((n, 2))
2122
radii = np.zeros(n)
2223

2324
# Heuristic placement for better initial guess: place larger circles in center
24-
radii[:] = np.linspace(0.12, 0.05, n) # Linear distribution of radii
25-
25+
radii[:] = np.linspace(0.12, 0.05, n) # Linear distribution of radii
26+
2627
# Initial placement: approximate hexagonal grid
2728
grid_x = int(np.sqrt(n))
2829
grid_y = int(n / grid_x)
29-
30+
3031
x_coords = np.linspace(0.15, 0.85, grid_x)
3132
y_coords = np.linspace(0.15, 0.85, grid_y)
32-
33+
3334
count = 0
3435
for i in range(grid_x):
3536
for j in range(grid_y):
3637
if count < n:
3738
centers[count] = [x_coords[i] + 0.05 * (j % 2), y_coords[j]]
3839
count += 1
39-
40+
4041
# Place remaining circles randomly
4142
while count < n:
4243
centers[count] = np.random.rand(2) * 0.7 + 0.15
4344
count += 1
4445

4546
# Objective function: Negative sum of radii (to maximize)
4647
def objective(x):
47-
centers = x[:2*n].reshape(n, 2)
48-
radii = x[2*n:]
48+
centers = x[: 2 * n].reshape(n, 2)
49+
radii = x[2 * n :]
4950
return -np.sum(radii)
5051

5152
# Constraint: No overlaps and circles stay within the unit square
5253
def constraint(x):
53-
centers = x[:2*n].reshape(n, 2)
54-
radii = x[2*n:]
55-
54+
centers = x[: 2 * n].reshape(n, 2)
55+
radii = x[2 * n :]
56+
5657
# Overlap constraint
5758
overlap_constraints = []
5859
for i in range(n):
5960
for j in range(i + 1, n):
60-
dist = np.sqrt(np.sum((centers[i] - centers[j])**2))
61+
dist = np.sqrt(np.sum((centers[i] - centers[j]) ** 2))
6162
overlap_constraints.append(dist - (radii[i] + radii[j]))
62-
63+
6364
# Boundary constraints
6465
boundary_constraints = []
6566
for i in range(n):
6667
boundary_constraints.append(centers[i, 0] - radii[i]) # x >= radius
67-
boundary_constraints.append(1 - centers[i, 0] - radii[i]) # x <= 1 - radius
68+
boundary_constraints.append(1 - centers[i, 0] - radii[i]) # x <= 1 - radius
6869
boundary_constraints.append(centers[i, 1] - radii[i]) # y >= radius
69-
boundary_constraints.append(1 - centers[i, 1] - radii[i]) # y <= 1 - radius
70-
70+
boundary_constraints.append(1 - centers[i, 1] - radii[i]) # y <= 1 - radius
71+
7172
return np.array(overlap_constraints + boundary_constraints)
7273

7374
# Initial guess vector
7475
x0 = np.concatenate([centers.flatten(), radii])
7576

7677
# Bounds: Circles stay within the unit square and radii are positive
77-
bounds = [(0, 1)] * (2*n) + [(0.03, 0.2)] * n # radii are positive, up to 0.2
78+
bounds = [(0, 1)] * (2 * n) + [(0.03, 0.2)] * n # radii are positive, up to 0.2
7879

7980
# Constraints dictionary
80-
constraints = {'type': 'ineq', 'fun': constraint}
81+
constraints = {"type": "ineq", "fun": constraint}
8182

8283
# Optimization using SLSQP
83-
result = minimize(objective, x0, method='SLSQP', bounds=bounds, constraints=constraints, options={'maxiter': 1000, 'ftol': 1e-8})
84+
result = minimize(
85+
objective,
86+
x0,
87+
method="SLSQP",
88+
bounds=bounds,
89+
constraints=constraints,
90+
options={"maxiter": 1000, "ftol": 1e-8},
91+
)
8492

8593
# Extract optimized centers and radii
86-
optimized_centers = result.x[:2*n].reshape(n, 2)
87-
optimized_radii = result.x[2*n:]
94+
optimized_centers = result.x[: 2 * n].reshape(n, 2)
95+
optimized_radii = result.x[2 * n :]
8896

8997
# Ensure radii are not negative (numerical stability)
9098
optimized_radii = np.maximum(optimized_radii, 0.001)
@@ -93,46 +101,51 @@ def constraint(x):
93101
sum_radii = np.sum(optimized_radii)
94102

95103
return optimized_centers, optimized_radii, sum_radii
104+
105+
96106
# EVOLVE-BLOCK-END
97107

108+
98109
# This part remains fixed (not evolved)
99110
def run_packing():
100111
"""Run the circle packing constructor for n=26"""
101112
centers, radii, sum_radii = construct_packing()
102113
return centers, radii, sum_radii
103114

115+
104116
def visualize(centers, radii):
105117
"""
106118
Visualize the circle packing
107-
119+
108120
Args:
109121
centers: np.array of shape (n, 2) with (x, y) coordinates
110122
radii: np.array of shape (n) with radius of each circle
111123
"""
112124
import matplotlib.pyplot as plt
113125
from matplotlib.patches import Circle
114-
126+
115127
fig, ax = plt.subplots(figsize=(8, 8))
116-
128+
117129
# Draw unit square
118130
ax.set_xlim(0, 1)
119131
ax.set_ylim(0, 1)
120-
ax.set_aspect('equal')
132+
ax.set_aspect("equal")
121133
ax.grid(True)
122-
134+
123135
# Draw circles
124136
for i, (center, radius) in enumerate(zip(centers, radii)):
125137
circle = Circle(center, radius, alpha=0.5)
126138
ax.add_patch(circle)
127-
ax.text(center[0], center[1], str(i), ha='center', va='center')
128-
139+
ax.text(center[0], center[1], str(i), ha="center", va="center")
140+
129141
plt.title(f"Circle Packing (n={len(centers)}, sum={sum(radii):.6f})")
130142
plt.show()
131143

144+
132145
if __name__ == "__main__":
133146
centers, radii, sum_radii = run_packing()
134147
print(f"Sum of radii: {sum_radii}")
135148
# AlphaEvolve improved this to 2.635
136-
149+
137150
# Uncomment to visualize:
138-
# visualize(centers, radii)
151+
# visualize(centers, radii)

examples/circle_packing_with_artifacts/evaluator.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ def evaluate(program_path):
295295
# Add successful packing stats for good solutions
296296
if valid and target_ratio > 0.95: # Near-optimal solutions
297297
artifacts["stdout"] = f"Excellent packing! Achieved {target_ratio:.1%} of target value"
298-
artifacts[
299-
"radius_stats"
300-
] = f"Min: {validation_details['min_radius']:.6f}, Max: {validation_details['max_radius']:.6f}, Avg: {validation_details['avg_radius']:.6f}"
298+
artifacts["radius_stats"] = (
299+
f"Min: {validation_details['min_radius']:.6f}, Max: {validation_details['max_radius']:.6f}, Avg: {validation_details['avg_radius']:.6f}"
300+
)
301301

302302
return EvaluationResult(
303303
metrics={
@@ -404,9 +404,9 @@ def evaluate_stage1(program_path):
404404

405405
# Add validation issues if any
406406
if not valid:
407-
artifacts[
408-
"stderr"
409-
] = f"Validation failed: {len(validation_details.get('boundary_violations', []))} boundary violations, {len(validation_details.get('overlaps', []))} overlaps"
407+
artifacts["stderr"] = (
408+
f"Validation failed: {len(validation_details.get('boundary_violations', []))} boundary violations, {len(validation_details.get('overlaps', []))} overlaps"
409+
)
410410
artifacts["failure_stage"] = "stage1_geometric_validation"
411411
if validation_details.get("boundary_violations"):
412412
artifacts["boundary_issues"] = validation_details["boundary_violations"][

examples/mlx_metal_kernel_opt/best_program.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
Target: Qwen3-0.6B with 40 query heads : 8 KV heads
99
Hardware: Apple M-series GPUs with unified memory
10-
Baseline: Standard MLX-LM using mx.fast.scaled_dot_product_attention
10+
Baseline: Standard MLX-LM using mx.fast.scaled_dot_product_attention
1111
Goal: 5-15% performance improvement through custom Metal kernel optimization
1212
1313
Evolution Target: The Metal kernel source code that computes GQA attention

examples/mlx_metal_kernel_opt/evaluator.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,24 +1082,24 @@ def _analyze_performance_with_safety_metrics(
10821082
custom_memories = [r.peak_memory_gb for r in custom_results if r.peak_memory_gb > 0]
10831083

10841084
aggregate_metrics = {
1085-
"avg_decode_speed": float(np.mean(custom_decode_speeds))
1086-
if custom_decode_speeds
1087-
else 0.0,
1088-
"min_decode_speed": float(np.min(custom_decode_speeds))
1089-
if custom_decode_speeds
1090-
else 0.0,
1091-
"max_decode_speed": float(np.max(custom_decode_speeds))
1092-
if custom_decode_speeds
1093-
else 0.0,
1094-
"avg_prefill_speed": float(np.mean(custom_prefill_speeds))
1095-
if custom_prefill_speeds
1096-
else 0.0,
1085+
"avg_decode_speed": (
1086+
float(np.mean(custom_decode_speeds)) if custom_decode_speeds else 0.0
1087+
),
1088+
"min_decode_speed": (
1089+
float(np.min(custom_decode_speeds)) if custom_decode_speeds else 0.0
1090+
),
1091+
"max_decode_speed": (
1092+
float(np.max(custom_decode_speeds)) if custom_decode_speeds else 0.0
1093+
),
1094+
"avg_prefill_speed": (
1095+
float(np.mean(custom_prefill_speeds)) if custom_prefill_speeds else 0.0
1096+
),
10971097
"avg_memory_gb": float(np.mean(custom_memories)) if custom_memories else 0.0,
10981098
"max_memory_gb": float(np.max(custom_memories)) if custom_memories else 0.0,
10991099
"num_successful_tests": len(custom_results),
1100-
"decode_speed_std": float(np.std(custom_decode_speeds))
1101-
if len(custom_decode_speeds) > 1
1102-
else 0.0,
1100+
"decode_speed_std": (
1101+
float(np.std(custom_decode_speeds)) if len(custom_decode_speeds) > 1 else 0.0
1102+
),
11031103
}
11041104

11051105
# Enhanced comparison summary

examples/mlx_metal_kernel_opt/initial_program.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
Target: Qwen3-0.6B with 40 query heads : 8 KV heads
99
Hardware: Apple M-series GPUs with unified memory
10-
Baseline: Standard MLX-LM using mx.fast.scaled_dot_product_attention
10+
Baseline: Standard MLX-LM using mx.fast.scaled_dot_product_attention
1111
Goal: 5-15% performance improvement through custom Metal kernel optimization
1212
1313
Evolution Target: The Metal kernel source code that computes GQA attention

examples/mlx_metal_kernel_opt/run_benchmarks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,9 @@ def print_comparison_summary(comparison_results):
457457
print(f" ⏱️ Average Time Reduction: {summary['avg_time_reduction_pct']:+.2f}%")
458458

459459
print(f"\n📊 ABSOLUTE PERFORMANCE:")
460-
print(f" 🔵 Standard MLX-LM: {summary['avg_standard_decode_speed']:.1f} tokens/sec average")
460+
print(
461+
f" 🔵 Standard MLX-LM: {summary['avg_standard_decode_speed']:.1f} tokens/sec average"
462+
)
461463
print(
462464
f" 🟠 Metal Kernel Optimized: {summary['avg_optimized_decode_speed']:.1f} tokens/sec average"
463465
)

openevolve/controller.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ async def run(
354354

355355
# Specifically check if this is the new best program
356356
if self.database.best_program_id == child_program.id:
357-
logger.info(f"🌟 New best solution found at iteration {i+1}: {child_program.id}")
357+
logger.info(
358+
f"🌟 New best solution found at iteration {i+1}: {child_program.id}"
359+
)
358360
logger.info(f"Metrics: {format_metrics_safe(child_program.metrics)}")
359361

360362
# Save checkpoint

0 commit comments

Comments
 (0)