|
| 1 | +""" |
| 2 | +Evaluator for circle packing example |
| 3 | +""" |
| 4 | + |
| 5 | +import importlib.util |
| 6 | +import numpy as np |
| 7 | +import time |
| 8 | +import concurrent.futures |
| 9 | +import threading |
| 10 | +import traceback |
| 11 | +import sys |
| 12 | + |
| 13 | + |
| 14 | +def run_with_timeout(func, args=(), kwargs={}, timeout_seconds=30): |
| 15 | + """ |
| 16 | + Run a function with a timeout using concurrent.futures |
| 17 | +
|
| 18 | + Args: |
| 19 | + func: Function to run |
| 20 | + args: Arguments to pass to the function |
| 21 | + kwargs: Keyword arguments to pass to the function |
| 22 | + timeout_seconds: Timeout in seconds |
| 23 | +
|
| 24 | + Returns: |
| 25 | + Result of the function or raises TimeoutError |
| 26 | + """ |
| 27 | + with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: |
| 28 | + future = executor.submit(func, *args, **kwargs) |
| 29 | + try: |
| 30 | + return future.result(timeout=timeout_seconds) |
| 31 | + except concurrent.futures.TimeoutError: |
| 32 | + raise TimeoutError( |
| 33 | + f"Function {func.__name__} timed out after {timeout_seconds} seconds" |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def validate_packing(centers, radii): |
| 38 | + """ |
| 39 | + Validate that circles don't overlap and are inside the unit square |
| 40 | +
|
| 41 | + Args: |
| 42 | + centers: np.array of shape (n, 2) with (x, y) coordinates |
| 43 | + radii: np.array of shape (n) with radius of each circle |
| 44 | +
|
| 45 | + Returns: |
| 46 | + True if valid, False otherwise |
| 47 | + """ |
| 48 | + n = centers.shape[0] |
| 49 | + |
| 50 | + # Check if circles are inside the unit square |
| 51 | + for i in range(n): |
| 52 | + x, y = centers[i] |
| 53 | + r = radii[i] |
| 54 | + if x - r < -1e-6 or x + r > 1 + 1e-6 or y - r < -1e-6 or y + r > 1 + 1e-6: |
| 55 | + print(f"Circle {i} at ({x}, {y}) with radius {r} is outside the unit square") |
| 56 | + return False |
| 57 | + |
| 58 | + # Check for overlaps |
| 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 | + if dist < radii[i] + radii[j] - 1e-6: # Allow for tiny numerical errors |
| 63 | + print(f"Circles {i} and {j} overlap: dist={dist}, r1+r2={radii[i]+radii[j]}") |
| 64 | + return False |
| 65 | + |
| 66 | + return True |
| 67 | + |
| 68 | + |
| 69 | +def evaluate(program_path): |
| 70 | + """ |
| 71 | + Evaluate the program by running it for n=26 and n=32 and checking the sum of radii |
| 72 | +
|
| 73 | + Args: |
| 74 | + program_path: Path to the program file |
| 75 | +
|
| 76 | + Returns: |
| 77 | + Dictionary of metrics |
| 78 | + """ |
| 79 | + # Target values from the paper |
| 80 | + TARGETS = {26: 2.635, 32: 2.937} # AlphaEvolve result for n=26 # AlphaEvolve result for n=32 |
| 81 | + |
| 82 | + try: |
| 83 | + # Load the program |
| 84 | + spec = importlib.util.spec_from_file_location("program", program_path) |
| 85 | + program = importlib.util.module_from_spec(spec) |
| 86 | + spec.loader.exec_module(program) |
| 87 | + |
| 88 | + # Check if the required function exists |
| 89 | + if not hasattr(program, "run_packing"): |
| 90 | + print(f"Error: program does not have 'run_packing' function") |
| 91 | + return {"sum_radii": 0.0, "validity": 0.0, "combined_score": 0.0} |
| 92 | + |
| 93 | + # Run for two different n values |
| 94 | + results = {} |
| 95 | + |
| 96 | + for n in [26, 32]: |
| 97 | + try: |
| 98 | + start_time = time.time() |
| 99 | + |
| 100 | + # Run packing with timeout |
| 101 | + centers, radii, sum_radii = run_with_timeout( |
| 102 | + program.run_packing, args=(n,), timeout_seconds=30 |
| 103 | + ) |
| 104 | + |
| 105 | + end_time = time.time() |
| 106 | + |
| 107 | + # Ensure centers and radii are numpy arrays |
| 108 | + if not isinstance(centers, np.ndarray): |
| 109 | + centers = np.array(centers) |
| 110 | + if not isinstance(radii, np.ndarray): |
| 111 | + radii = np.array(radii) |
| 112 | + |
| 113 | + # Validate solution |
| 114 | + valid = validate_packing(centers, radii) |
| 115 | + |
| 116 | + # Check shape and size |
| 117 | + shape_valid = centers.shape == (n, 2) and radii.shape == (n,) |
| 118 | + if not shape_valid: |
| 119 | + print( |
| 120 | + f"Invalid shapes: centers={centers.shape}, radii={radii.shape}, expected ({n}, 2) and ({n},)" |
| 121 | + ) |
| 122 | + valid = False |
| 123 | + |
| 124 | + # Recalculate sum to verify |
| 125 | + actual_sum = np.sum(radii) if valid else 0.0 |
| 126 | + |
| 127 | + # Make sure sum_radii matches the actual sum |
| 128 | + if abs(actual_sum - sum_radii) > 1e-6: |
| 129 | + print( |
| 130 | + f"Warning: Reported sum {sum_radii} doesn't match calculated sum {actual_sum}" |
| 131 | + ) |
| 132 | + |
| 133 | + target = TARGETS[n] |
| 134 | + |
| 135 | + # Store results |
| 136 | + results[n] = { |
| 137 | + "valid": valid, |
| 138 | + "sum_radii": actual_sum, |
| 139 | + "time": end_time - start_time, |
| 140 | + "target_ratio": actual_sum / target if valid else 0.0, |
| 141 | + } |
| 142 | + |
| 143 | + print( |
| 144 | + f"n={n}: valid={valid}, sum_radii={actual_sum:.6f}, target={target}, ratio={actual_sum/target if valid else 0:.6f}" |
| 145 | + ) |
| 146 | + |
| 147 | + except TimeoutError as e: |
| 148 | + print(f"Timeout running for n={n}: {str(e)}") |
| 149 | + results[n] = { |
| 150 | + "valid": False, |
| 151 | + "sum_radii": 0.0, |
| 152 | + "time": 30.0, # timeout value |
| 153 | + "target_ratio": 0.0, |
| 154 | + } |
| 155 | + except Exception as e: |
| 156 | + print(f"Error running for n={n}: {str(e)}") |
| 157 | + traceback.print_exc() |
| 158 | + results[n] = {"valid": False, "sum_radii": 0.0, "time": 0.0, "target_ratio": 0.0} |
| 159 | + |
| 160 | + # Calculate combined metrics |
| 161 | + avg_ratio = (results[26]["target_ratio"] + results[32]["target_ratio"]) / 2 |
| 162 | + validity = 1.0 if results[26]["valid"] and results[32]["valid"] else 0.0 |
| 163 | + |
| 164 | + # Return metrics - higher values are better |
| 165 | + return { |
| 166 | + "sum_radii_26": float(results[26]["sum_radii"]), |
| 167 | + "sum_radii_32": float(results[32]["sum_radii"]), |
| 168 | + "target_ratio_26": float(results[26]["target_ratio"]), |
| 169 | + "target_ratio_32": float(results[32]["target_ratio"]), |
| 170 | + "validity": float(validity), |
| 171 | + "avg_target_ratio": float(avg_ratio), |
| 172 | + "combined_score": float(avg_ratio * validity), |
| 173 | + } |
| 174 | + |
| 175 | + except Exception as e: |
| 176 | + print(f"Evaluation failed completely: {str(e)}") |
| 177 | + traceback.print_exc() |
| 178 | + return { |
| 179 | + "sum_radii_26": 0.0, |
| 180 | + "sum_radii_32": 0.0, |
| 181 | + "target_ratio_26": 0.0, |
| 182 | + "target_ratio_32": 0.0, |
| 183 | + "validity": 0.0, |
| 184 | + "avg_target_ratio": 0.0, |
| 185 | + "combined_score": 0.0, |
| 186 | + } |
| 187 | + |
| 188 | + |
| 189 | +# Stage-based evaluation for cascade evaluation |
| 190 | +def evaluate_stage1(program_path): |
| 191 | + """ |
| 192 | + First stage evaluation - quick validation check with only n=26 |
| 193 | + """ |
| 194 | + try: |
| 195 | + # Load the program |
| 196 | + spec = importlib.util.spec_from_file_location("program", program_path) |
| 197 | + program = importlib.util.module_from_spec(spec) |
| 198 | + spec.loader.exec_module(program) |
| 199 | + |
| 200 | + # Check if the required function exists |
| 201 | + if not hasattr(program, "run_packing"): |
| 202 | + print(f"Error: program does not have 'run_packing' function") |
| 203 | + return {"validity": 0.0, "error": "Missing run_packing function"} |
| 204 | + |
| 205 | + try: |
| 206 | + # Run with a lower iteration count for quicker checking |
| 207 | + centers, radii, sum_radii = run_with_timeout( |
| 208 | + program.run_packing, args=(26,), timeout_seconds=10 |
| 209 | + ) |
| 210 | + |
| 211 | + # Ensure centers and radii are numpy arrays |
| 212 | + if not isinstance(centers, np.ndarray): |
| 213 | + centers = np.array(centers) |
| 214 | + if not isinstance(radii, np.ndarray): |
| 215 | + radii = np.array(radii) |
| 216 | + |
| 217 | + # Validate solution (shapes and constraints) |
| 218 | + shape_valid = centers.shape == (26, 2) and radii.shape == (26,) |
| 219 | + if not shape_valid: |
| 220 | + print(f"Invalid shapes: centers={centers.shape}, radii={radii.shape}") |
| 221 | + return {"validity": 0.0, "error": "Invalid shapes"} |
| 222 | + |
| 223 | + valid = validate_packing(centers, radii) |
| 224 | + |
| 225 | + # Calculate sum |
| 226 | + actual_sum = np.sum(radii) if valid else 0.0 |
| 227 | + |
| 228 | + # Target from paper |
| 229 | + target = 2.635 |
| 230 | + |
| 231 | + # Return evaluation metrics |
| 232 | + return { |
| 233 | + "validity": 1.0 if valid else 0.0, |
| 234 | + "sum_radii": float(actual_sum), |
| 235 | + "target_ratio": float(actual_sum / target if valid else 0.0), |
| 236 | + } |
| 237 | + |
| 238 | + except TimeoutError as e: |
| 239 | + print(f"Stage 1 evaluation timed out: {e}") |
| 240 | + return {"validity": 0.0, "error": "Timeout"} |
| 241 | + except Exception as e: |
| 242 | + print(f"Stage 1 evaluation failed: {e}") |
| 243 | + print(traceback.format_exc()) |
| 244 | + return {"validity": 0.0, "error": str(e)} |
| 245 | + |
| 246 | + except Exception as e: |
| 247 | + print(f"Stage 1 evaluation failed completely: {e}") |
| 248 | + print(traceback.format_exc()) |
| 249 | + return {"validity": 0.0, "error": str(e)} |
| 250 | + |
| 251 | + |
| 252 | +def evaluate_stage2(program_path): |
| 253 | + """ |
| 254 | + Second stage evaluation - full evaluation with n=26 and n=32 |
| 255 | + """ |
| 256 | + # Full evaluation as in the main evaluate function |
| 257 | + return evaluate(program_path) |
0 commit comments