File size: 7,045 Bytes
da3c3a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Task definitions and deterministic graders for all 3 Wildfire tasks.

Fixes vs original:
  1. Task 3 speed_factor denominator corrected 60 → 75
  2. Task 1 grader: perfect containment (no burning cells) now scores near 1.0
     instead of being penalised by the speed bonus formula
  3. Task 2 grader: containment_ratio now based on burning cells only
     (not total cells) for a more meaningful score
  4. Task 3 grader: division D correctly excluded from front containment
     scoring since it starts clear (it was already excluded but now explicit)
"""

from typing import List

BURNING = 1
BURNT   = 2

DIVISIONS = {
    "A": {"rows": (0,  9),  "cols": (0,  9)},
    "B": {"rows": (0,  9),  "cols": (10, 19)},
    "C": {"rows": (10, 19), "cols": (0,  9)},
    "D": {"rows": (10, 19), "cols": (10, 19)},
}


class Task:
    def __init__(self, id: str, name: str, difficulty: str,
                 description: str, max_steps: int, grader: str):
        self.id          = id
        self.name        = name
        self.difficulty  = difficulty
        self.description = description
        self.max_steps   = max_steps
        self.grader      = grader


class TaskGrader:
    """
    Deterministic grader for all 3 Wildfire tasks.
    Returns a score between 0.0 and 1.0.
    All graders use env.get_internal_state() for evaluation.
    """

    def __init__(self, env):
        self.env = env

    # ------------------------------------------------------------------
    # Task 1 — Single Front Containment
    # ------------------------------------------------------------------

    def grade_task_1(self) -> float:
        """
        Score = (cells_contained_in_A / total_burning) * speed_bonus

        FIX 2: When fire is fully contained (total_burning == 0), return
        speed_bonus directly instead of 1.0 * speed_bonus, which previously
        penalised perfect containment at late timesteps.
        A perfect containment always scores at least 0.85.
        """
        state = self.env.get_internal_state()
        grid  = state["grid"]
        zone  = DIVISIONS["A"]

        total_burning = sum(
            1 for r in range(20) for c in range(20)
            if grid[r][c] == BURNING
        )

        # FIX: Perfect containment — no burning cells anywhere
        if total_burning == 0:
            timestep    = state["timestep"]
            speed_bonus = max(0.85, 1.0 - (timestep / 30) * 0.15)
            return round(min(0.999, speed_bonus), 4)

        outside_burning = sum(
            1 for r in range(20) for c in range(20)
            if grid[r][c] == BURNING
            and not (
                zone["rows"][0] <= r <= zone["rows"][1]
                and zone["cols"][0] <= c <= zone["cols"][1]
            )
        )

        in_A = total_burning - outside_burning
        containment_score = in_A / total_burning

        timestep    = state["timestep"]
        speed_bonus = max(0.5, 1.0 - (timestep / 30) * 0.5)

        return round(max(0.001, min(0.999, containment_score * speed_bonus)), 4)

    # ------------------------------------------------------------------
    # Task 2 — Asset Protection
    # ------------------------------------------------------------------

    def grade_task_2(self) -> float:
        """
        Score = containment_ratio * (assets_saved / 3)

        FIX 3: containment_ratio is now computed as the fraction of the
        *initially burning area* that has been contained, not a ratio against
        the full 400-cell grid.  The original formula gave ~0.97 even with
        50 burning cells because 350/400 = 0.875 looks like "good containment".
        Using burning / (burning + burnt) gives a meaningful signal.
        """
        state = self.env.get_internal_state()
        grid  = state["grid"]

        burning = sum(
            1 for r in range(20) for c in range(20)
            if grid[r][c] == BURNING
        )
        burnt = sum(
            1 for r in range(20) for c in range(20)
            if grid[r][c] == BURNT
        )
        total_affected = burning + burnt
        if total_affected == 0:
            containment_ratio = 1.0
        else:
            containment_ratio = 1.0 - (burning / total_affected)

        assets      = state["assets"]
        assets_saved = sum(1 for a in assets if a["status"] == "safe")
        asset_score = assets_saved / 3 if assets else 1.0

        return round(max(0.001, min(0.999, containment_ratio * asset_score)), 4)

    # ------------------------------------------------------------------
    # Task 3 — Multi-Front Outbreak
    # ------------------------------------------------------------------

    def grade_task_3(self) -> float:
        """
        Score = weighted_containment * speed_factor * asset_factor - merge_penalty

        FIX 1: speed_factor denominator corrected to 75 (was 60).
        FIX 4: Division D explicitly labelled as 'starts clear' and excluded
        from containment scoring to avoid unfairly penalising the agent.
        """
        state        = self.env.get_internal_state()
        grid         = state["grid"]
        timestep     = state["timestep"]
        front_merges = state.get("front_merges", 0)

        # Per-division containment for A, B, C only (D starts clear)
        div_scores = []
        for label in ["A", "B", "C"]:
            zone = DIVISIONS[label]
            burning = sum(
                1 for r in range(zone["rows"][0], zone["rows"][1] + 1)
                for c in range(zone["cols"][0], zone["cols"][1] + 1)
                if grid[r][c] == BURNING
            )
            total = 100  # 10×10 division
            div_scores.append(1.0 - (burning / total))

        avg_containment = sum(div_scores) / len(div_scores)

        # FIX 1: Correct denominator (75 not 60)
        speed_factor = max(0.5, 1.0 - (timestep / 75) * 0.5)

        # Asset factor
        assets       = state["assets"]
        assets_saved = sum(1 for a in assets if a["status"] == "safe")
        asset_factor = assets_saved / len(assets) if assets else 1.0

        # Merge penalty
        merge_penalty = min(0.5, front_merges * 0.1)

        score = avg_containment * speed_factor * asset_factor - merge_penalty
        return round(max(0.001, min(0.999, score)), 4)


TASKS = [
    Task(
        "task_1",
        "Single Front Containment",
        "Easy",
        "Fire starts in Division A. Contain all fire within Division A within 30 steps.",
        30,
        "TaskGrader.grade_task_1()",
    ),
    Task(
        "task_2",
        "Asset Protection",
        "Medium",
        "Fire starts on north edge. Suppress fire AND keep Town, Water Tower, "
        "and Road Junction safe within 50 steps.",
        50,
        "TaskGrader.grade_task_2()",
    ),
    Task(
        "task_3",
        "Multi-Front Outbreak",
        "Hard",
        "Fire ignites in 3 divisions simultaneously. Coordinate all crews to "
        "contain all fronts within 75 steps.",
        75,
        "TaskGrader.grade_task_3()",
    ),
]