File size: 3,724 Bytes
da0cecf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Reference solution for the Classification Fields RL environment
# (arXiv:2605.07119). Gets the FULL reward (1.0).
#
# Strategy: run the official nonlinear IFS pipeline of arXiv:2605.07119 via the
# environment's `run_reproduction` tool (official repo at pinned commit cb52f15,
# 5 seeds x 3 benchmarks, CPU, ~minutes), then submit the resulting Table 1
# cells with the paper's verdict. All 6 benchmark cells land within tolerance
# of the paper's numbers and the verdict matches, so the deterministic scorer
# grants full credit.
#
# Usage:
#   python scripts/reference_solution.py --base-url http://localhost:8000
#   python scripts/reference_solution.py --base-url \
#       https://arnavm10-rl-env-2605-07119-classification-fields.hf.space

import argparse
import sys

from classification_fields_env import CallToolAction, ClassificationFieldsEnv


def tool_data(result: object) -> dict:
    """Unwrap a tool result across client serialization variants."""
    if isinstance(result, dict):
        for key in ("data", "result"):
            inner = result.get(key)
            if isinstance(inner, dict):
                return inner
        return result
    for attr in ("data", "structured_content", "result"):
        inner = getattr(result, attr, None)
        if isinstance(inner, dict):
            return inner
    raise RuntimeError(f"could not unwrap tool result: {result!r}")


def main() -> int:
    parser = argparse.ArgumentParser(
        description=(
            "Reference solution: full-reward reproduction of Table 1 of "
            "arXiv:2605.07119 (Classification Fields, nonlinear IFS benchmarks)."
        )
    )
    parser.add_argument(
        "--base-url",
        required=True,
        help="URL of the Classification Fields environment server",
    )
    args = parser.parse_args()

    with ClassificationFieldsEnv(base_url=args.base_url).sync() as env:
        env.reset()
        print("episode reset; running the official arXiv:2605.07119 pipeline...")
        run = tool_data(env.call_tool("run_reproduction"))
        print(
            f"pipeline finished in {run['elapsed_seconds']:.1f}s "
            f"(commit {run['repo_commit']}, seeds {run['seeds']})"
        )
        for benchmark, methods in run["cells"].items():
            for method, stats in methods.items():
                print(
                    f"  {benchmark:>12s} {method:>6s}: "
                    f"{stats['ordered_mse_x1e3']:.2f} "
                    f"+/- {stats['ordered_mse_std_x1e3']:.2f} (x 1e-3)"
                )

        step = env.step(
            CallToolAction(
                tool_name="submit_table",
                arguments={
                    # The paper's verdict: the CFP beats the affine residual
                    # predictor by >= 10x lower rollout MSE on all three
                    # benchmarks of Table 1.
                    "verdict": "cfp_wins_all_three",
                    "cells": {
                        benchmark: {
                            method: stats["ordered_mse_x1e3"]
                            for method, stats in methods.items()
                        }
                        for benchmark, methods in run["cells"].items()
                    },
                },
            )
        )
        reward = step.reward
        details = step.observation.metadata.get("score_details", {}) if hasattr(step, "observation") else {}
        matched = details.get("matched_cells")
        total = details.get("total_cells")
        print(f"reward: {reward}")
        print(f"matched cells: {matched}/{total}, full credit: {details.get('full_credit')}")
        return 0 if reward == 1.0 else 1


if __name__ == "__main__":
    sys.exit(main())