Repository navigation
Expand file tree
/
Copy pathtle_controller.py
More file actions
193 lines (147 loc) · 3.9 KB
/
Copy pathtle_controller.py
File metadata and controls
193 lines (147 loc) · 3.9 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
184
185
186
187
188
189
190
191
192
193
from Benchmark.Benchmark_controller import BenchmarkController
from ast_file import analyze_complexity
from TLE_ import TLEPredictor
import re
def normalize_complexity(c):
c = c.strip()
replacements = {
"O(n^2)": "O(n²)",
"O(n^3)": "O(n³)",
}
return replacements.get(c, c)
COMPLEXITY_RANK = {
"O(1)": 1,
"O(log n)": 2,
"O(sqrt(n))": 3,
"O(n)": 4,
"O(n log n)": 5,
"O(n sqrt(n))": 6,
"O(n²)": 7,
"O(n² log n)": 8,
"O(n³)": 9,
"O(2^n)": 10
}
def extract_complexity(text):
matches = re.findall(
r'O\([^)]+\)',
text,
flags=re.IGNORECASE
)
if matches:
return matches[0]
return "UNKNOWN"
class TLEController:
def __init__(
self,
code,
constraint,
ast_result=None,
benchmark_result=None
):
self.code = code
self.constraint = constraint
self.ast_result = ast_result
self.benchmark_result = benchmark_result
def get_ast_complexity(self):
if self.ast_result is not None:
return self.ast_result
summary, analysis = analyze_complexity(
self.code
)
return extract_complexity(
analysis
)
def get_benchmark_result(self):
if self.benchmark_result is not None:
return self.benchmark_result
controller = BenchmarkController(
code=self.code,
max_n=5000
)
return controller.run()
def choose_complexity(
self,
ast_complexity,
benchmark_complexity
):
ast_complexity = normalize_complexity(
ast_complexity
)
benchmark_complexity = normalize_complexity(
benchmark_complexity
)
if ast_complexity == benchmark_complexity:
return ast_complexity, "HIGH"
ast_rank = COMPLEXITY_RANK.get(
ast_complexity,
0
)
bench_rank = COMPLEXITY_RANK.get(
benchmark_complexity,
0
)
chosen = (
ast_complexity
if ast_rank > bench_rank
else benchmark_complexity
)
return chosen, "MEDIUM"
def parse_constraint(self):
if (
self.constraint is None
or str(self.constraint).strip() == ""
):
return 5000
match = re.search(
r"\d+",
str(self.constraint)
)
if not match:
return 5000
return int(match.group())
def run(self):
ast_complexity = (
self.get_ast_complexity()
)
benchmark_result = (
self.get_benchmark_result()
)
benchmark_complexity = (
benchmark_result["complexity"]
)
final_complexity, confidence = (
self.choose_complexity(
ast_complexity,
benchmark_complexity
)
)
print("AST:", ast_complexity)
print("BENCH:", benchmark_complexity)
print("FINAL:", final_complexity)
target_n = (
self.parse_constraint()
)
last_sample = (
benchmark_result["benchmarks"][-1]
)
predictor = TLEPredictor(
time_limit=1.0
)
prediction = predictor.predict(
measured_time=last_sample[
"avg_time"
],
measured_n=last_sample[
"size"
],
target_n=target_n,
complexity=final_complexity
)
return {
"ast_complexity": ast_complexity,
"benchmark_complexity": benchmark_complexity,
"chosen_complexity": final_complexity,
"confidence": confidence,
"prediction": prediction,
"benchmark_result": benchmark_result
}