Repository navigation
Expand file tree
/
Copy pathparser.py
More file actions
65 lines (52 loc) · 2.17 KB
/
Copy pathparser.py
File metadata and controls
65 lines (52 loc) · 2.17 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
"""Output parser for opt-evolution-strategy.
Training feedback: TRAIN_METRICS gen=G best_fitness=F avg_fitness=A
Test metrics: TEST_METRICS best_fitness=F convergence_gen=G
Leaderboard metrics: best_fitness_<label>, convergence_gen_<label>
Lower best_fitness is better (minimization). Lower convergence_gen is better.
"""
import re
import sys
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent
sys.path.insert(0, str(PROJECT_ROOT / "src"))
from mlsbench.agent.parsers import OutputParser, ParseResult
class Parser(OutputParser):
"""Parser for evolutionary optimization benchmark output."""
def parse(self, cmd_label: str, raw_output: str) -> ParseResult:
feedback_parts = []
metrics = {}
# --- TRAIN_METRICS (feedback only) ---
train_lines = [
l.strip() for l in raw_output.splitlines()
if l.strip().startswith("TRAIN_METRICS")
]
if train_lines:
feedback_parts.append(
f"Training progress ({cmd_label}):\n" +
"\n".join(train_lines[-5:])
)
# --- TEST_METRICS (feedback + leaderboard metrics) ---
for line in raw_output.splitlines():
line = line.strip()
if line.startswith("TEST_METRICS"):
# Parse best_fitness
bf_match = re.search(r"best_fitness=([\d.eE+-]+)", line)
if bf_match:
val = float(bf_match.group(1))
key = f"best_fitness_{cmd_label}"
metrics[key] = val
feedback_parts.append(f"{key}: {val:.6e}")
# Parse convergence_gen
cg_match = re.search(r"convergence_gen=(\d+)", line)
if cg_match:
val = int(cg_match.group(1))
key = f"convergence_gen_{cmd_label}"
metrics[key] = val
feedback_parts.append(f"{key}: {val}")
# --- Fallback ---
if not feedback_parts:
feedback_parts.append(raw_output[-3000:])
return ParseResult(
feedback="\n".join(feedback_parts),
metrics=metrics,
)