File size: 6,587 Bytes
5fed0fc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
"""
Runner for algorithmic problems using the judge server.
"""
import time
from pathlib import Path
from typing import Optional
import requests
from .base import Runner, EvaluationResult, EvaluationStatus
class AlgorithmicRunner(Runner):
"""
Runner for algorithmic problems.
Submits solutions to the judge server (go-judge) and polls for results.
"""
DEFAULT_JUDGE_URL = "http://localhost:8081"
DEFAULT_POLL_INTERVAL = 2 # seconds
def __init__(
self,
judge_url: str = DEFAULT_JUDGE_URL,
poll_interval: float = DEFAULT_POLL_INTERVAL,
):
self.judge_url = judge_url.rstrip("/")
self.poll_interval = poll_interval
self.session = requests.Session()
def evaluate(
self,
problem_id: str,
solution_code: str,
*,
timeout: Optional[int] = None,
lang: str = "cpp",
unbounded: bool = False,
) -> EvaluationResult:
"""
Evaluate a solution for an algorithmic problem.
Args:
problem_id: Problem ID (numeric string or int)
solution_code: C++ solution code
timeout: Optional timeout in seconds
lang: Programming language (default: cpp)
unbounded: If True, use unbounded score (without clipping)
Returns:
EvaluationResult with score and status
"""
pid = str(problem_id)
start_time = time.time()
# Check for empty code
if not solution_code or not solution_code.strip():
return EvaluationResult(
problem_id=pid,
status=EvaluationStatus.ERROR,
message="Empty code submission - please provide valid code",
)
# Submit solution
sid = self._submit(pid, solution_code, lang)
if sid is None:
return EvaluationResult(
problem_id=pid,
status=EvaluationStatus.ERROR,
message="Submission failed - judge server may be unavailable",
)
# Poll for result
result = self._poll_result(sid, timeout)
duration = time.time() - start_time
if result is None:
return EvaluationResult(
problem_id=pid,
status=EvaluationStatus.TIMEOUT,
message=f"Evaluation timed out after {timeout}s",
duration_seconds=duration,
)
status = result.get("status", "")
if status == "error":
return EvaluationResult(
problem_id=pid,
status=EvaluationStatus.ERROR,
message=result.get("message", "Unknown error"),
logs=result.get("logs"),
duration_seconds=duration,
)
# Use unbounded score if requested and available
score = result.get("scoreUnbounded" if unbounded else "score", 0.0)
# Create result with both scores if available
eval_result = EvaluationResult(
problem_id=pid,
score=score,
status=EvaluationStatus.SUCCESS,
duration_seconds=duration,
metadata=result,
)
# Add unbounded score as attribute if available
if "scoreUnbounded" in result:
eval_result.score_unbounded = result["scoreUnbounded"]
return eval_result
def evaluate_file(
self,
problem_id: str,
solution_path: Path,
*,
timeout: Optional[int] = None,
) -> EvaluationResult:
"""Evaluate a solution file."""
if not solution_path.exists():
return EvaluationResult(
problem_id=str(problem_id),
status=EvaluationStatus.ERROR,
message=f"Solution file not found: {solution_path}",
)
code = solution_path.read_text(encoding="utf-8")
lang = "cpp" if solution_path.suffix in [".cpp", ".cc", ".cxx"] else "cpp"
return self.evaluate(problem_id, code, timeout=timeout, lang=lang)
def _submit(self, pid: str, code: str, lang: str) -> Optional[str]:
"""Submit solution to judge server."""
ext = ".cpp" if lang == "cpp" else f".{lang}"
files = {"code": (f"solution{ext}", code)}
data = {"pid": pid, "lang": lang}
try:
response = self.session.post(
f"{self.judge_url}/submit",
files=files,
data=data,
timeout=30,
)
response.raise_for_status()
return response.json().get("sid")
except requests.RequestException:
return None
def _poll_result(
self,
sid: str,
timeout: Optional[int] = None,
) -> Optional[dict]:
"""Poll for evaluation result."""
start = time.time()
while True:
if timeout and (time.time() - start) > timeout:
return None
try:
response = self.session.get(
f"{self.judge_url}/result/{sid}",
timeout=10,
)
if response.status_code == 404:
time.sleep(self.poll_interval)
continue
response.raise_for_status()
result = response.json()
if result.get("status") in ["done", "error"]:
return result
time.sleep(self.poll_interval)
except requests.RequestException:
time.sleep(self.poll_interval)
def list_problems(self) -> list:
"""List all available algorithmic problems."""
try:
response = self.session.get(f"{self.judge_url}/problems", timeout=10)
response.raise_for_status()
return response.json()
except requests.RequestException:
return []
def get_problem_statement(self, problem_id: str) -> Optional[str]:
"""Get problem statement."""
try:
response = self.session.get(
f"{self.judge_url}/problem/{problem_id}/statement",
timeout=10,
)
response.raise_for_status()
return response.text
except requests.RequestException:
return None
|