ayushrwt's picture gensis88's picture
Update tasks.py (#6)
da3c3a6
Raw History Blame Contribute Delete
7.05 kB
"""
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()",
),
]