|
| 1 | +""" |
| 2 | +Evaluator for the function minimization example |
| 3 | +""" |
| 4 | + |
| 5 | +import re |
| 6 | +import subprocess |
| 7 | +import time |
| 8 | +import traceback |
| 9 | + |
| 10 | + |
| 11 | +def run_with_timeout(program_path, timeout_seconds=60): |
| 12 | + """ |
| 13 | + Run a function with a timeout using subprocess. |
| 14 | +
|
| 15 | + Args: |
| 16 | + func: Function to run |
| 17 | + args: Arguments to pass to the function |
| 18 | + kwargs: Keyword arguments to pass to the function |
| 19 | + timeout_seconds: Timeout in seconds |
| 20 | +
|
| 21 | + Returns: |
| 22 | + Result of the function or raises TimeoutError |
| 23 | + """ |
| 24 | + cmd = ["python", "submit.py", program_path, "-p", "alphabet", "-l", "Python 3", "-f"] |
| 25 | + |
| 26 | + try: |
| 27 | + # Run the command and grab its output using subprocess.Popen |
| 28 | + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
| 29 | + stdout, stderr = proc.communicate(timeout=timeout_seconds) |
| 30 | + exit_code = proc.returncode |
| 31 | + if exit_code != 0: |
| 32 | + print(stderr) # Print the error output if the command failed |
| 33 | + raise RuntimeError(f"Process exited with code {exit_code}") |
| 34 | + except subprocess.TimeoutExpired: |
| 35 | + # Kill the process if it times out |
| 36 | + proc.kill() |
| 37 | + raise TimeoutError(f"Process timed out after {timeout_seconds} seconds") |
| 38 | + |
| 39 | + pattern = ( |
| 40 | + r"Score:\s*(\d+)\s*" |
| 41 | + r"Test cases done:\s*(\d+)\s*" |
| 42 | + r"Test cases correct:\s*(\d+)\s*" |
| 43 | + r"Test cases total:\s*(\d+)" |
| 44 | + ) |
| 45 | + match = re.search(pattern, stdout) |
| 46 | + if not match: |
| 47 | + raise ValueError("Expected summary lines not found") |
| 48 | + |
| 49 | + score, done, correct, total = map(int, match.groups()) |
| 50 | + return score, done, correct, total |
| 51 | + |
| 52 | + |
| 53 | +def evaluate(program_path): |
| 54 | + """ |
| 55 | + Evaluate the program by submitting it to OJ and fetching metrics based on how well it performs. |
| 56 | +
|
| 57 | + Args: |
| 58 | + program_path: Path to the program file |
| 59 | +
|
| 60 | + Returns: |
| 61 | + Dictionary of metrics |
| 62 | + """ |
| 63 | + try: |
| 64 | + # For constructor-based approaches, a single evaluation is sufficient |
| 65 | + # since the result is deterministic |
| 66 | + start_time = time.time() |
| 67 | + |
| 68 | + # Use subprocess to run with timeout |
| 69 | + score, done, correct, total = run_with_timeout( |
| 70 | + program_path, timeout_seconds=60 # Single timeout |
| 71 | + ) |
| 72 | + |
| 73 | + end_time = time.time() |
| 74 | + eval_time = end_time - start_time |
| 75 | + |
| 76 | + # Combined score - higher is better |
| 77 | + combined_score = correct / total if total > 0 else 0.0 |
| 78 | + |
| 79 | + print( |
| 80 | + f"Evaluation: Score={score}, Done={done}, Correct={correct}, Total={total}, Combined={combined_score:.2f}" |
| 81 | + ) |
| 82 | + |
| 83 | + return { |
| 84 | + "score": score, |
| 85 | + "done": done, |
| 86 | + "correct": correct, |
| 87 | + "total": total, |
| 88 | + "eval_time": eval_time, |
| 89 | + "combined_score": float(combined_score), |
| 90 | + } |
| 91 | + |
| 92 | + except Exception as e: |
| 93 | + print(f"Evaluation failed completely: {str(e)}") |
| 94 | + traceback.print_exc() |
| 95 | + return { |
| 96 | + "score": 0, |
| 97 | + "done": 0, |
| 98 | + "correct": 0, |
| 99 | + "total": 0, |
| 100 | + "eval_time": 0.0, |
| 101 | + "combined_score": 0.0, |
| 102 | + } |
0 commit comments