|
| 1 | +""" |
| 2 | +Evaluator for the function minimization example |
| 3 | +""" |
| 4 | +import importlib.util |
| 5 | +import numpy as np |
| 6 | +import time |
| 7 | + |
| 8 | +def evaluate(program_path): |
| 9 | + """ |
| 10 | + Evaluate the program by running it multiple times and checking how close |
| 11 | + it gets to the known global minimum. |
| 12 | + |
| 13 | + Args: |
| 14 | + program_path: Path to the program file |
| 15 | + |
| 16 | + Returns: |
| 17 | + Dictionary of metrics |
| 18 | + """ |
| 19 | + # Known global minimum (approximate) |
| 20 | + GLOBAL_MIN_X = -1.76 |
| 21 | + GLOBAL_MIN_Y = -1.03 |
| 22 | + GLOBAL_MIN_VALUE = -2.104 |
| 23 | + |
| 24 | + # Load the program |
| 25 | + spec = importlib.util.spec_from_file_location("program", program_path) |
| 26 | + program = importlib.util.module_from_spec(spec) |
| 27 | + spec.loader.exec_module(program) |
| 28 | + |
| 29 | + # Run multiple trials |
| 30 | + num_trials = 10 |
| 31 | + values = [] |
| 32 | + distances = [] |
| 33 | + times = [] |
| 34 | + |
| 35 | + for _ in range(num_trials): |
| 36 | + start_time = time.time() |
| 37 | + x, y, value = program.run_search() |
| 38 | + end_time = time.time() |
| 39 | + |
| 40 | + # Calculate metrics |
| 41 | + distance_to_global = np.sqrt((x - GLOBAL_MIN_X)**2 + (y - GLOBAL_MIN_Y)**2) |
| 42 | + value_difference = abs(value - GLOBAL_MIN_VALUE) |
| 43 | + |
| 44 | + values.append(value) |
| 45 | + distances.append(distance_to_global) |
| 46 | + times.append(end_time - start_time) |
| 47 | + |
| 48 | + # Calculate metrics |
| 49 | + avg_value = np.mean(values) |
| 50 | + avg_distance = np.mean(distances) |
| 51 | + avg_time = np.mean(times) |
| 52 | + |
| 53 | + # Convert to scores (higher is better) |
| 54 | + value_score = 1.0 / (1.0 + abs(avg_value - GLOBAL_MIN_VALUE)) # Normalize and invert |
| 55 | + distance_score = 1.0 / (1.0 + avg_distance) |
| 56 | + speed_score = 1.0 / avg_time |
| 57 | + |
| 58 | + # Normalize speed score (so it doesn't dominate) |
| 59 | + speed_score = min(speed_score, 10.0) / 10.0 |
| 60 | + |
| 61 | + return { |
| 62 | + "value_score": value_score, |
| 63 | + "distance_score": distance_score, |
| 64 | + "speed_score": speed_score, |
| 65 | + "combined_score": 0.6 * value_score + 0.3 * distance_score + 0.1 * speed_score |
| 66 | + } |
| 67 | + |
| 68 | +# Stage-based evaluation for cascade evaluation |
| 69 | +def evaluate_stage1(program_path): |
| 70 | + """First stage evaluation with fewer trials""" |
| 71 | + # Quick check to see if the program runs without errors |
| 72 | + try: |
| 73 | + # Load the program |
| 74 | + spec = importlib.util.spec_from_file_location("program", program_path) |
| 75 | + program = importlib.util.module_from_spec(spec) |
| 76 | + spec.loader.exec_module(program) |
| 77 | + |
| 78 | + # Run a single trial |
| 79 | + x, y, value = program.run_search() |
| 80 | + |
| 81 | + # Basic metrics |
| 82 | + return { |
| 83 | + "runs_successfully": 1.0, |
| 84 | + "value": float(value) |
| 85 | + } |
| 86 | + except Exception as e: |
| 87 | + print(f"Stage 1 evaluation failed: {e}") |
| 88 | + return {"runs_successfully": 0.0} |
| 89 | + |
| 90 | +def evaluate_stage2(program_path): |
| 91 | + """Second stage evaluation with more thorough testing""" |
| 92 | + # Full evaluation as in the main evaluate function |
| 93 | + return evaluate(program_path) |
0 commit comments