File size: 3,091 Bytes
c30c6ee
85ea5e3
 
 
 
 
 
 
 
 
 
 
c30c6ee
85ea5e3
 
 
c30c6ee
 
 
 
85ea5e3
 
c30c6ee
a91b545
c30c6ee
 
 
85ea5e3
c30c6ee
 
85ea5e3
c30c6ee
 
 
85ea5e3
c30c6ee
85ea5e3
 
 
 
 
 
 
 
c30c6ee
85ea5e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c30c6ee
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
"""
Task 2 (Medium): Scheduling Conflict Management Grader.

Evaluates how well the agent handles scheduling conflicts:
double-booking prevention, recovery after errors, avoiding repeated
mistakes, and specialty compliance.

Scoring breakdown:
  - 35%: Booking success rate despite constraints
  - 30%: Conflict recovery (successful bookings after encountering errors)
  - 20%: No repeated identical errors (agent learns from mistakes)
  - 15%: Specialty compliance (correct specialty matching)
"""
import sys
import os

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


def _clamp(score: float) -> float:
    """Clamp score to [0.1, 0.9] for validator compliance."""
    if score != score:  # NaN check
        return 0.5
    return min(max(float(score), 0.1), 0.9)


def grade(env=None) -> float:
    """Grade task 2: conflict management and error recovery."""
    if env is None:
        from env import HealthcareEnvironment
        env = HealthcareEnvironment(seed=123)

    if not env.patients:
        return 0.5

    total_patients = len(env.patients)
    booked_count = sum(
        1 for p in env.patients.values()
        if p.get("status") == "booked"
    )
    history = getattr(env, "_action_history", [])
    total_actions = len(history)

    if total_actions == 0:
        return 0.5

    # ── Metric 1 (35%): Booking rate ──
    booking_rate = booked_count / total_patients

    # ── Metric 2 (30%): Conflict recovery ──
    # After encountering an error, does the agent successfully act within
    # the next 3 steps?
    error_indices = [
        i for i, a in enumerate(history)
        if a.get("info", {}).get("error") is not None
    ]

    recoveries = 0
    for ei in error_indices:
        for j in range(ei + 1, min(ei + 4, total_actions)):
            if history[j].get("info", {}).get("status") == "success":
                recoveries += 1
                break

    recovery_rate = (
        recoveries / len(error_indices)
        if error_indices
        else 1.0  # no errors = perfect recovery
    )

    # ── Metric 3 (20%): No repeated identical errors ──
    # An agent that keeps trying the same bad action is poor at conflict mgmt
    error_signatures = []
    for a in history:
        if a.get("info", {}).get("error"):
            sig = (a.get("type"), a.get("doctor_id"), a.get("slot_id"))
            error_signatures.append(sig)

    unique_errors = len(set(error_signatures))
    total_errors = len(error_signatures)
    repeat_avoidance = (
        unique_errors / total_errors
        if total_errors > 0
        else 1.0  # no errors = perfect
    )

    # ── Metric 4 (15%): Specialty compliance ──
    specialty_mismatches = sum(
        1 for a in history
        if a.get("info", {}).get("specialty_mismatch")
    )
    specialty_compliance = 1.0 - (specialty_mismatches / total_actions)

    raw_score = (
        0.35 * booking_rate
        + 0.30 * recovery_rate
        + 0.20 * repeat_avoidance
        + 0.15 * specialty_compliance
    )
    return _clamp(raw_score)