This repository was archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathterry.py
More file actions
183 lines (153 loc) · 6.42 KB
/
terry.py
File metadata and controls
183 lines (153 loc) · 6.42 KB
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
#!/usr/bin/env python3
import json
from enum import Enum
from task_maker.formats import TerryTask
from task_maker.remote import Execution
from task_maker.source_file import SourceFile
from task_maker.task_maker_frontend import Result, ResultStatus
from task_maker.uis import UIInterface, result_to_str
from typing import Optional, Dict, List
class SolutionStatus(Enum):
"""
Status of the evaluation of a solution
"""
WAITING = 0 # the evaluation is pending
GENERATING = 1 # the input file is generating
GENERATED = 2 # the input file has been generated
VALIDATING = 3 # the input file is validating
VALIDATED = 4 # the input file has been validated
SOLVING = 5 # the solution has started
SOLVED = 6 # the solution ended well
CHECKING = 7 # the checker is running
DONE = 8 # the checker has done
FAILED = 9 # the evaluation failed
class TestcaseStatus(Enum):
"""
Outcome of a testcase from a solution
"""
MISSING = 0 # the solution didn't answered the testcase
CORRECT = 1 # the solution answered well
WRONG = 2 # the solution didn't answered well
class SolutionInfo:
"""
Information about a solution and it's evaluation
"""
def __init__(self, source_file: SourceFile):
self.status = SolutionStatus.WAITING
# each solution can be evaluated with different seeds
self.seed = None
self.generation = None # type: Optional[Execution]
self.validation = None # type: Optional[Execution]
self.solution = None # type: Optional[Execution]
self.checking = None # type: Optional[Execution]
self.score = 0.0
self.message = ""
self.source_file = source_file
self.testcases_status = [] # type: List[TestcaseStatus]
class TerryUIInterface(UIInterface):
"""
Terry-like task variant of the UI interface
"""
def __init__(self, task: TerryTask, do_print: bool, json: bool):
super().__init__(task, do_print, json)
self.task = task
self.solutions_info = dict() # type: Dict[str, SolutionInfo]
def add_solution(self, source_file: SourceFile):
super().add_solution(source_file)
self.solutions_info[source_file.name] = SolutionInfo(source_file)
def add_generation(self, solution: str, seed: int, generation: Execution):
"""
Start tracking the generation of a testcase
"""
info = self.solutions_info[solution]
info.seed = seed
info.generation = generation
def on_start():
info.status = SolutionStatus.GENERATING
def on_done(result: Result):
if result.status == ResultStatus.SUCCESS:
info.status = SolutionStatus.GENERATED
else:
self.add_error(
"Failed to generate input for {} with seed {}".format(
solution, seed))
info.status = SolutionStatus.FAILED
info.message = "Generator failed: " + result_to_str(result)
generation.bind(on_done, on_start)
def add_validation(self, solution: str, validation: Execution):
"""
Start tracking the validation of a testcase
"""
info = self.solutions_info[solution]
info.validation = validation
def on_start():
info.status = SolutionStatus.VALIDATING
def on_done(result: Result):
if result.status == ResultStatus.SUCCESS:
info.status = SolutionStatus.VALIDATED
else:
self.add_error(
"Failed to validate input for {}".format(solution))
info.status = SolutionStatus.FAILED
info.message = "Validator failed: " + result_to_str(result)
validation.bind(on_done, on_start)
def add_solving(self, solution: str, solving: Execution):
"""
Start tracking the evaluation of a solution
"""
info = self.solutions_info[solution]
info.solution = solving
def on_start():
info.status = SolutionStatus.SOLVING
def on_done(result: Result):
if result.status == ResultStatus.SUCCESS:
info.status = SolutionStatus.SOLVED
else:
self.add_error("Solution {} failed".format(solution))
info.status = SolutionStatus.FAILED
info.message = "Solution failed: " + result_to_str(result)
solving.bind(on_done, on_start)
def add_checking(self, solution: str, checking: Execution):
"""
Start the tracking of a checker
"""
info = self.solutions_info[solution]
info.checking = checking
def on_start():
info.status = SolutionStatus.CHECKING
def on_done(result: Result):
self._compute_score(solution, checking.stdout_content)
self.ui_printer.terry_solution_outcome(solution, info)
if result.status == ResultStatus.SUCCESS:
info.status = SolutionStatus.DONE
else:
self.add_error(
"Checker failed on output of solution {}".format(solution))
info.status = SolutionStatus.FAILED
info.message = "Checker failed: " + result_to_str(result)
checking.bind(on_done, on_start)
def _compute_score(self, solution: str, check_outcome: str):
"""
Process the checker's outcome and compute the score of a solution
"""
info = self.solutions_info[solution]
try:
outcome = json.loads(check_outcome)
except json.JSONDecodeError as ex:
info.message = "Checker's output is not valid json: {}".format(ex)
return
score = outcome.get("score")
if score is None or not 0.0 <= score <= 1.0:
info.message = "Score is not in [0.0, 1.0]: {}".format(score)
return
info.score = score
for val, feedback in zip(outcome["validation"]["cases"],
outcome["feedback"]["cases"]):
# TODO store the checker messages somewhere
# (val["message"], feedback["message"])
if val["status"] == "missing":
info.testcases_status.append(TestcaseStatus.MISSING)
elif feedback["correct"]:
info.testcases_status.append(TestcaseStatus.CORRECT)
else:
info.testcases_status.append(TestcaseStatus.WRONG)