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