Spaces:
Running
Running
Update models.py
#9
by gensis88 - opened
models.py
CHANGED
|
@@ -1,69 +1,150 @@
|
|
| 1 |
-
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
-
# All rights reserved.
|
| 3 |
-
#
|
| 4 |
-
# This source code is licensed under the BSD-style license found in the
|
| 5 |
-
# LICENSE file in the root directory of this source tree.
|
| 6 |
-
|
| 7 |
-
"""
|
| 8 |
-
Data models for the Wildfire Environment.
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
Data models for the Wildfire Environment.
|
| 9 |
+
|
| 10 |
+
Fixes vs original:
|
| 11 |
+
1. ActionType is now a proper Enum — invalid action strings are rejected
|
| 12 |
+
at parse time with a clear validation error instead of silently no-oping
|
| 13 |
+
2. target_cell validated to be exactly 2 integers (was List[int] with no length check)
|
| 14 |
+
3. division_id and crew_id have explicit min/max bounds
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
from enum import Enum
|
| 18 |
+
from typing import List, Dict, Any
|
| 19 |
+
|
| 20 |
+
from openenv.core.env_server.types import Action, Observation
|
| 21 |
+
from pydantic import BaseModel, Field, field_validator
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# ------------------------------------------------------------------
|
| 25 |
+
# ActionType Enum
|
| 26 |
+
# ------------------------------------------------------------------
|
| 27 |
+
|
| 28 |
+
class ActionType(str, Enum):
|
| 29 |
+
SUPPRESS = "SUPPRESS"
|
| 30 |
+
FIREBREAK = "FIREBREAK"
|
| 31 |
+
MOVE = "MOVE"
|
| 32 |
+
HOLD = "HOLD"
|
| 33 |
+
WETLINE = "WETLINE"
|
| 34 |
+
SCRATCHLINE = "SCRATCHLINE"
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# ------------------------------------------------------------------
|
| 38 |
+
# Core env models
|
| 39 |
+
# ------------------------------------------------------------------
|
| 40 |
+
|
| 41 |
+
class WildfireAction(Action):
|
| 42 |
+
"""Action for the Wildfire environment — commanding a single crew."""
|
| 43 |
+
|
| 44 |
+
division_id: int = Field(
|
| 45 |
+
..., ge=0, le=3,
|
| 46 |
+
description="Division to command: 0=A, 1=B, 2=C, 3=D",
|
| 47 |
+
)
|
| 48 |
+
crew_id: int = Field(
|
| 49 |
+
..., ge=0, le=1,
|
| 50 |
+
description="Crew boss index within that division: 0 or 1",
|
| 51 |
+
)
|
| 52 |
+
action_type: ActionType = Field(
|
| 53 |
+
...,
|
| 54 |
+
description="SUPPRESS | FIREBREAK | MOVE | HOLD | WETLINE | SCRATCHLINE",
|
| 55 |
+
)
|
| 56 |
+
target_cell: List[int] = Field(
|
| 57 |
+
...,
|
| 58 |
+
description="[row, col] — grid coordinates (0-19 each)",
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
@field_validator("target_cell")
|
| 62 |
+
@classmethod
|
| 63 |
+
def validate_target_cell(cls, v: List[int]) -> List[int]:
|
| 64 |
+
if len(v) != 2:
|
| 65 |
+
raise ValueError("target_cell must have exactly 2 elements: [row, col]")
|
| 66 |
+
row, col = v
|
| 67 |
+
if not (0 <= row <= 19 and 0 <= col <= 19):
|
| 68 |
+
raise ValueError(
|
| 69 |
+
f"target_cell [{row}, {col}] out of bounds — must be 0-19 each"
|
| 70 |
+
)
|
| 71 |
+
return v
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
class WildfireObservation(Observation):
|
| 75 |
+
"""Observation from the Wildfire environment — full state footprint."""
|
| 76 |
+
|
| 77 |
+
grid: List[List[int]] = Field(
|
| 78 |
+
default_factory=list,
|
| 79 |
+
description="20×20 grid of cell states: 0=CLEAR, 1=BURNING, 2=BURNT, 3=FIREBREAK",
|
| 80 |
+
)
|
| 81 |
+
division_zones: List[Dict[str, Any]] = Field(
|
| 82 |
+
default_factory=list,
|
| 83 |
+
description="Boundaries of divisions A, B, C, D",
|
| 84 |
+
)
|
| 85 |
+
crew_positions: List[Dict[str, Any]] = Field(
|
| 86 |
+
default_factory=list,
|
| 87 |
+
description="Positions and status of all crews",
|
| 88 |
+
)
|
| 89 |
+
wind: Dict[str, Any] = Field(
|
| 90 |
+
default_factory=dict,
|
| 91 |
+
description="Wind direction (N/S/E/W) and speed (0.0–1.0)",
|
| 92 |
+
)
|
| 93 |
+
assets_at_risk: List[Dict[str, Any]] = Field(
|
| 94 |
+
default_factory=list,
|
| 95 |
+
description="Critical assets with id, name, cell, status",
|
| 96 |
+
)
|
| 97 |
+
timestep: int = Field(
|
| 98 |
+
default=0,
|
| 99 |
+
description="Current simulation step",
|
| 100 |
+
)
|
| 101 |
+
containment_pct: float = Field(
|
| 102 |
+
default=0.0,
|
| 103 |
+
description="Percentage of fire perimeter contained (0.0–1.0)",
|
| 104 |
+
)
|
| 105 |
+
burning_per_division: Dict[str, int] = Field(
|
| 106 |
+
default_factory=dict,
|
| 107 |
+
description="Number of burning cells keyed by division label",
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# ------------------------------------------------------------------
|
| 112 |
+
# API request / response models
|
| 113 |
+
# ------------------------------------------------------------------
|
| 114 |
+
|
| 115 |
+
class TaskResponse(BaseModel):
|
| 116 |
+
id: str
|
| 117 |
+
name: str
|
| 118 |
+
difficulty: str
|
| 119 |
+
description: str
|
| 120 |
+
grader: str
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
class GraderResponse(BaseModel):
|
| 124 |
+
task_scores: Dict[str, float]
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
class BaselineResponse(BaseModel):
|
| 128 |
+
task_scores: Dict[str, float]
|
| 129 |
+
total_reward: float
|
| 130 |
+
steps_taken: int
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
class ResetRequest(BaseModel):
|
| 134 |
+
task: int = Field(default=1, ge=1, le=3)
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
class StepResponse(BaseModel):
|
| 138 |
+
observation: Dict[str, Any]
|
| 139 |
+
reward: float
|
| 140 |
+
done: bool
|
| 141 |
+
info: Dict[str, Any]
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
class ResetResponse(BaseModel):
|
| 145 |
+
observation: Dict[str, Any]
|
| 146 |
+
info: Dict[str, Any] = {}
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
class StateResponse(BaseModel):
|
| 150 |
+
state: Dict[str, Any]
|