-
Notifications
You must be signed in to change notification settings - Fork 934
Expand file tree
/
Copy pathevaluator.py
More file actions
228 lines (198 loc) · 8 KB
/
evaluator.py
File metadata and controls
228 lines (198 loc) · 8 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
Evaluator for Rust adaptive sorting example
"""
import asyncio
import json
import subprocess
import tempfile
from pathlib import Path
from openevolve.evaluation_result import EvaluationResult
import logging
import os
THIS_FILE_DIR = Path(os.path.dirname(os.path.realpath(__file__)))
logger = logging.getLogger("examples.rust_adaptive_sort.evaluator")
def evaluate(program_path: str) -> EvaluationResult:
result = asyncio.run(_evaluate(program_path))
if "error" in result.artifacts:
logger.error(f"Error evaluating program: {result.artifacts['error']}")
if "stderr" in result.artifacts:
logger.error(f"Stderr: {result.artifacts['stderr']}")
if "stdout" in result.artifacts:
logger.error(f"Stdout: {result.artifacts['stdout']}")
return result
async def _evaluate(program_path: str) -> EvaluationResult:
"""
Evaluate a Rust sorting algorithm implementation.
Tests the algorithm on various data patterns to measure:
- Correctness
- Performance (speed)
- Adaptability to different data patterns
- Memory efficiency
"""
try:
# Create a temporary Rust project
with tempfile.TemporaryDirectory() as temp_dir:
project_dir = Path(temp_dir) / "sort_test"
# Initialize Cargo project
result = subprocess.run(
["cargo", "init", "--name", "sort_test", str(project_dir)],
capture_output=True,
text=True,
)
if result.returncode != 0:
return EvaluationResult(
metrics={"score": 0.0, "compile_success": 0.0},
artifacts={
"error": "Failed to create Cargo project",
"stderr": result.stderr,
},
)
# Copy the program to src/lib.rs
lib_path = project_dir / "src" / "lib.rs"
with open(program_path, "r") as src:
lib_content = src.read()
with open(lib_path, "w") as dst:
dst.write(lib_content)
# Create main.rs with benchmark code
project_source_dir = THIS_FILE_DIR / "sort_test"
main_file_source = project_source_dir / "src" / "main.rs"
with open(main_file_source, "r") as f:
main_content = f.read()
main_path = project_dir / "src" / "main.rs"
with open(main_path, "w") as f:
f.write(main_content)
cargo_toml_source = project_source_dir / "Cargo.toml"
with open(cargo_toml_source, "r") as f:
cargo_toml_content = f.read()
cargo_toml_path = project_dir / "Cargo.toml"
with open(cargo_toml_path, "w") as f:
f.write(cargo_toml_content)
cargo_lock_source = project_source_dir / "Cargo.lock"
with open(cargo_lock_source, "r") as f:
cargo_lock_content = f.read()
cargo_lock_path = project_dir / "Cargo.lock"
with open(cargo_lock_path, "w") as f:
f.write(cargo_lock_content)
# Build the project
build_result = subprocess.run(
["cargo", "build", "--release"],
cwd=project_dir,
capture_output=True,
text=True,
timeout=60,
)
if build_result.returncode != 0:
# Extract compilation errors
return EvaluationResult(
metrics={
"score": 0.0,
"compile_success": 0.0,
"correctness": 0.0,
"performance_score": 0.0,
"adaptability_score": 0.0,
},
artifacts={
"error": "Compilation failed",
"stderr": build_result.stderr,
"stdout": build_result.stdout,
},
)
# Run the benchmark
run_result = subprocess.run(
["cargo", "run", "--release"],
cwd=project_dir,
capture_output=True,
text=True,
timeout=30,
)
if run_result.returncode != 0:
return EvaluationResult(
metrics={
"score": 0.0,
"compile_success": 1.0,
"correctness": 0.0,
"performance_score": 0.0,
"adaptability_score": 0.0,
},
artifacts={"error": "Runtime error", "stderr": run_result.stderr},
)
# Parse JSON output
try:
# Find JSON in output (between first { and last })
output = run_result.stdout
start = output.find("{")
end = output.rfind("}") + 1
json_str = output[start:end]
results = json.loads(json_str)
# Calculate overall score
correctness = results["correctness"]
performance = results["performance_score"]
adaptability = results["adaptability_score"]
# Weighted score (correctness is mandatory)
if correctness < 1.0:
overall_score = 0.0
else:
overall_score = 0.6 * performance + 0.4 * adaptability
# Check for memory safety (basic check via valgrind if available)
memory_safe = 1.0 # Rust is memory safe by default
return EvaluationResult(
metrics={
"score": overall_score,
"compile_success": 1.0,
"correctness": correctness,
"performance_score": performance,
"adaptability_score": adaptability,
"avg_time": results["avg_time"],
"memory_safe": memory_safe,
},
artifacts={
"times": results["times"],
"all_correct": results["all_correct"],
"build_output": build_result.stdout,
},
)
except (json.JSONDecodeError, KeyError) as e:
return EvaluationResult(
metrics={
"score": 0.0,
"compile_success": 1.0,
"correctness": 0.0,
"performance_score": 0.0,
"adaptability_score": 0.0,
},
artifacts={
"error": f"Failed to parse results: {str(e)}",
"stdout": run_result.stdout,
},
)
except subprocess.TimeoutExpired:
return EvaluationResult(
metrics={
"score": 0.0,
"compile_success": 0.0,
"correctness": 0.0,
"performance_score": 0.0,
"adaptability_score": 0.0,
},
artifacts={"error": "Timeout during evaluation"},
)
except Exception as e:
return EvaluationResult(
metrics={
"score": 0.0,
"compile_success": 0.0,
"correctness": 0.0,
"performance_score": 0.0,
"adaptability_score": 0.0,
},
artifacts={"error": str(e), "type": "evaluation_error"},
)
# For testing
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
result = evaluate(sys.argv[1])
print(f"Score: {result.metrics['score']:.4f}")
print(f"Correctness: {result.metrics['correctness']:.4f}")
print(f"Performance: {result.metrics['performance_score']:.4f}")
print(f"Adaptability: {result.metrics['adaptability_score']:.4f}")