|
| 1 | +# EVOLVE-BLOCK-START |
| 2 | +"""Advanced circle packing for n=26 circles in a unit square""" |
| 3 | +import numpy as np |
| 4 | +from scipy.optimize import minimize |
| 5 | + |
| 6 | + |
| 7 | +def construct_packing(): |
| 8 | + """ |
| 9 | + Construct an optimized arrangement of 26 circles in a unit square |
| 10 | + using mathematical principles and optimization techniques. |
| 11 | +
|
| 12 | + Returns: |
| 13 | + Tuple of (centers, radii, sum_of_radii) |
| 14 | + centers: np.array of shape (26, 2) with (x, y) coordinates |
| 15 | + radii: np.array of shape (26) with radius of each circle |
| 16 | + sum_of_radii: Sum of all radii |
| 17 | + """ |
| 18 | + n = 26 |
| 19 | + |
| 20 | + # Initial guess: Strategic placement with some randomness |
| 21 | + centers = np.zeros((n, 2)) |
| 22 | + radii = np.zeros(n) |
| 23 | + |
| 24 | + # Heuristic placement for better initial guess: place larger circles in center |
| 25 | + radii[:] = np.linspace(0.12, 0.05, n) # Linear distribution of radii |
| 26 | + |
| 27 | + # Initial placement: approximate hexagonal grid |
| 28 | + grid_x = int(np.sqrt(n)) |
| 29 | + grid_y = int(n / grid_x) |
| 30 | + |
| 31 | + x_coords = np.linspace(0.15, 0.85, grid_x) |
| 32 | + y_coords = np.linspace(0.15, 0.85, grid_y) |
| 33 | + |
| 34 | + count = 0 |
| 35 | + for i in range(grid_x): |
| 36 | + for j in range(grid_y): |
| 37 | + if count < n: |
| 38 | + centers[count] = [x_coords[i] + 0.05 * (j % 2), y_coords[j]] |
| 39 | + count += 1 |
| 40 | + |
| 41 | + # Place remaining circles randomly |
| 42 | + while count < n: |
| 43 | + centers[count] = np.random.rand(2) * 0.7 + 0.15 |
| 44 | + count += 1 |
| 45 | + |
| 46 | + # Objective function: Negative sum of radii (to maximize) |
| 47 | + def objective(x): |
| 48 | + centers = x[: 2 * n].reshape(n, 2) |
| 49 | + radii = x[2 * n :] |
| 50 | + return -np.sum(radii) |
| 51 | + |
| 52 | + # Constraint: No overlaps and circles stay within the unit square |
| 53 | + def constraint(x): |
| 54 | + centers = x[: 2 * n].reshape(n, 2) |
| 55 | + radii = x[2 * n :] |
| 56 | + |
| 57 | + # Overlap constraint |
| 58 | + overlap_constraints = [] |
| 59 | + for i in range(n): |
| 60 | + for j in range(i + 1, n): |
| 61 | + dist = np.sqrt(np.sum((centers[i] - centers[j]) ** 2)) |
| 62 | + overlap_constraints.append(dist - (radii[i] + radii[j])) |
| 63 | + |
| 64 | + # Boundary constraints |
| 65 | + boundary_constraints = [] |
| 66 | + for i in range(n): |
| 67 | + boundary_constraints.append(centers[i, 0] - radii[i]) # x >= radius |
| 68 | + boundary_constraints.append(1 - centers[i, 0] - radii[i]) # x <= 1 - radius |
| 69 | + boundary_constraints.append(centers[i, 1] - radii[i]) # y >= radius |
| 70 | + boundary_constraints.append(1 - centers[i, 1] - radii[i]) # y <= 1 - radius |
| 71 | + |
| 72 | + return np.array(overlap_constraints + boundary_constraints) |
| 73 | + |
| 74 | + # Initial guess vector |
| 75 | + x0 = np.concatenate([centers.flatten(), radii]) |
| 76 | + |
| 77 | + # Bounds: Circles stay within the unit square and radii are positive |
| 78 | + bounds = [(0, 1)] * (2 * n) + [(0.03, 0.2)] * n # radii are positive, up to 0.2 |
| 79 | + |
| 80 | + # Constraints dictionary |
| 81 | + constraints = {"type": "ineq", "fun": constraint} |
| 82 | + |
| 83 | + # Optimization using SLSQP |
| 84 | + result = minimize( |
| 85 | + objective, |
| 86 | + x0, |
| 87 | + method="SLSQP", |
| 88 | + bounds=bounds, |
| 89 | + constraints=constraints, |
| 90 | + options={"maxiter": 1000, "ftol": 1e-8}, |
| 91 | + ) |
| 92 | + |
| 93 | + # Extract optimized centers and radii |
| 94 | + optimized_centers = result.x[: 2 * n].reshape(n, 2) |
| 95 | + optimized_radii = result.x[2 * n :] |
| 96 | + |
| 97 | + # Ensure radii are not negative (numerical stability) |
| 98 | + optimized_radii = np.maximum(optimized_radii, 0.001) |
| 99 | + |
| 100 | + # Calculate the sum of radii |
| 101 | + sum_radii = np.sum(optimized_radii) |
| 102 | + |
| 103 | + return optimized_centers, optimized_radii, sum_radii |
| 104 | + |
| 105 | + |
| 106 | +# EVOLVE-BLOCK-END |
| 107 | + |
| 108 | + |
| 109 | +# This part remains fixed (not evolved) |
| 110 | +def run_packing(): |
| 111 | + """Run the circle packing constructor for n=26""" |
| 112 | + centers, radii, sum_radii = construct_packing() |
| 113 | + return centers, radii, sum_radii |
| 114 | + |
| 115 | + |
| 116 | +def visualize(centers, radii): |
| 117 | + """ |
| 118 | + Visualize the circle packing |
| 119 | +
|
| 120 | + Args: |
| 121 | + centers: np.array of shape (n, 2) with (x, y) coordinates |
| 122 | + radii: np.array of shape (n) with radius of each circle |
| 123 | + """ |
| 124 | + import matplotlib.pyplot as plt |
| 125 | + from matplotlib.patches import Circle |
| 126 | + |
| 127 | + fig, ax = plt.subplots(figsize=(8, 8)) |
| 128 | + |
| 129 | + # Draw unit square |
| 130 | + ax.set_xlim(0, 1) |
| 131 | + ax.set_ylim(0, 1) |
| 132 | + ax.set_aspect("equal") |
| 133 | + ax.grid(True) |
| 134 | + |
| 135 | + # Draw circles |
| 136 | + for i, (center, radius) in enumerate(zip(centers, radii)): |
| 137 | + circle = Circle(center, radius, alpha=0.5) |
| 138 | + ax.add_patch(circle) |
| 139 | + ax.text(center[0], center[1], str(i), ha="center", va="center") |
| 140 | + |
| 141 | + plt.title(f"Circle Packing (n={len(centers)}, sum={sum(radii):.6f})") |
| 142 | + plt.show() |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + centers, radii, sum_radii = run_packing() |
| 147 | + print(f"Sum of radii: {sum_radii}") |
| 148 | + # AlphaEvolve improved this to 2.635 |
| 149 | + |
| 150 | + # Uncomment to visualize: |
| 151 | + # visualize(centers, radii) |
0 commit comments