This repository was archived by the owner on Oct 23, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathsolution.py
More file actions
179 lines (160 loc) · 7.1 KB
/
Copy pathsolution.py
File metadata and controls
179 lines (160 loc) · 7.1 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
#!/usr/bin/env python3
from abc import ABC, abstractmethod
from task_maker.args import CacheMode
from task_maker.config import Config
from task_maker.formats import IOITask
from task_maker.source_file import SourceFile
from task_maker.task_maker_frontend import File, Execution, Frontend, \
Resources, Fifo
from typing import Optional, List
def get_checker_execution(frontend: Frontend, config: Config, task: IOITask,
checker: Optional[SourceFile], input: Optional[File],
output: File, correct_output: File,
message: str) -> Execution:
if checker:
checker.prepare(frontend, config)
check = checker.execute(frontend, message,
["input", "output", "contestant_output"])
check.addInput("input", input)
else:
check = frontend.addExecution(message)
check.setExecutablePath("diff")
check.setArgs(["-w", "output", "contestant_output"])
check.addInput("output", correct_output)
check.addInput("contestant_output", output)
if config.cache != CacheMode.ALL:
check.disableCache()
limits = Resources()
limits.cpu_time = task.time_limit * 2
limits.wall_time = task.time_limit * 1.5 * 2
limits.memory = task.memory_limit_kb * 2
check.setLimits(limits)
return check
class Solution(ABC):
def __init__(self, solution: SourceFile, task: IOITask, config: Config):
self.solution = solution
self.task = task
self.config = config
@abstractmethod
def evaluate(
self, frontend: Frontend, testcase: int, subtask: int, input: File,
validation: Optional[File],
correct_output: Optional[File]) -> (List[Execution], Execution):
"""
Evaluate this solution against this input/output.
Assuming self.solution is already prepared
:param frontend: the frontend
:param testcase: number of the testcase
:param subtask: number of the subtask
:param input: input file
:param validation: validation stdout, useful to evaluate only after
valiation
:param correct_output: output file
:return: a list of the "evaluating" executions with the checker
execution
"""
pass
class BatchSolution(Solution):
def __init__(self, solution: SourceFile, task: IOITask, config: Config,
checker: SourceFile):
super().__init__(solution, task, config)
self.checker = checker
def evaluate(
self, frontend: Frontend, testcase: int, subtask: int, input: File,
validation: Optional[File],
correct_output: Optional[File]) -> (List[Execution], Execution):
eval = self.solution.execute(
frontend,
"Evaluation of %s on testcase %d" % (self.solution.name, testcase),
[])
if self.config.cache != CacheMode.ALL:
eval.disableCache()
limits = Resources()
limits.cpu_time = self.task.time_limit
limits.wall_time = self.task.time_limit * 1.5
limits.memory = self.task.memory_limit_kb
eval.setLimits(limits)
if validation:
eval.addInput("tm_wait_validation", validation)
if self.task.input_file:
eval.addInput(self.task.input_file, input)
else:
eval.setStdin(input)
if self.task.output_file:
output = eval.output(self.task.output_file, False)
else:
output = eval.stdout(False)
if self.config.exclusive:
eval.makeExclusive()
if self.config.extra_time:
eval.setExtraTime(self.config.extra_time)
return [eval], get_checker_execution(
frontend, self.config, self.task, self.checker, input, output,
correct_output, "Checking solution %s for testcase %d" %
(self.solution.name, testcase))
class CommunicationSolution(Solution):
def __init__(self, solution: SourceFile, task: IOITask, config: Config,
manager: SourceFile, num_processes: int):
super().__init__(solution, task, config)
self.manager = manager
self.num_processes = num_processes
def evaluate(
self, frontend: Frontend, testcase: int, subtask: int, input: File,
validation: Optional[File],
correct_output: Optional[File]) -> (List[Execution], Execution):
group = frontend.addExecutionGroup(
"Evaluation of %s on testcase %d" % (self.solution.name, testcase))
pipes_m_2_sol = [] # type: List[Fifo]
pipes_sol_2_m = [] # type: List[Fifo]
pipes_m_2_sol_names = [] # type: List[str]
pipes_sol_2_m_names = [] # type: List[str]
for p in range(self.num_processes):
pipes_m_2_sol.append(group.createFifo())
pipes_sol_2_m.append(group.createFifo())
pipes_m_2_sol_names.append("pipe_m_2_sol%d" % p)
pipes_sol_2_m_names.append("pipe_sol%d_2_m" % p)
executions = []
for p, (p_in, p_out, p_in_name, p_out_name) in enumerate(
zip(pipes_m_2_sol, pipes_sol_2_m, pipes_m_2_sol_names,
pipes_sol_2_m_names)):
exec = self.solution.execute(
frontend, "Evaluation of %s (process %d) on testcase %d" %
(self.solution.name, p, testcase),
[p_out_name, p_in_name, str(p)], group)
limits = Resources()
limits.cpu_time = self.task.time_limit
limits.wall_time = self.task.time_limit * 1.5
limits.memory = self.task.memory_limit_kb
exec.setLimits(limits)
if validation:
exec.addInput("tm_wait_validation", validation)
if self.config.extra_time:
exec.setExtraTime(self.config.extra_time)
exec.addFifo(p_in_name, p_in)
exec.addFifo(p_out_name, p_out)
executions.append(exec)
args = []
for p_in_name, p_out_name in zip(pipes_sol_2_m_names,
pipes_m_2_sol_names):
args += [p_out_name, p_in_name]
manager = self.manager.execute(
frontend, "Evaluation of %s (manager process) on testcase %d" %
(self.solution.name, testcase), args, group)
limits = Resources()
limits.cpu_time = self.task.time_limit * self.num_processes
limits.wall_time = limits.cpu_time * 1.5
manager.setLimits(limits)
for p_in, p_out, p_in_name, p_out_name in zip(
pipes_m_2_sol, pipes_sol_2_m, pipes_m_2_sol_names,
pipes_sol_2_m_names):
manager.addFifo(p_in_name, p_in)
manager.addFifo(p_out_name, p_out)
if self.task.input_file:
manager.addInput(self.task.input_file, input)
else:
manager.setStdin(input)
if self.config.exclusive:
manager.makeExclusive() # this will be propagated to the group
if self.config.cache != CacheMode.ALL:
manager.disableCache() # this will be propagated to the group
return executions, manager