Files changed (1) hide show
  1. models.py +150 -69
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
- from typing import List, Dict, Any
12
- from openenv.core.env_server.types import Action, Observation
13
- from pydantic import Field
14
-
15
-
16
- class WildfireAction(Action):
17
- """Action for the Wildfire environment - commanding a crew."""
18
-
19
- division_id: int = Field(..., description="0=A, 1=B, 2=C, 3=D")
20
- crew_id: int = Field(..., description="0 or 1 within that division")
21
- action_type: str = Field(..., description="SUPPRESS | FIREBREAK | MOVE | HOLD")
22
- target_cell: List[int] = Field(..., description="[row, col]")
23
-
24
-
25
- class WildfireObservation(Observation):
26
- """Observation from the Wildfire environment - full state footprint."""
27
-
28
- grid: List[List[int]] = Field(default_factory=list, description="20x20 grid of cell states: 0=CLEAR, 1=BURNING, 2=BURNT, 3=FIREBREAK")
29
- division_zones: List[Dict[str, Any]] = Field(default_factory=list, description="Boundaries of divisions A, B, C, D")
30
- crew_positions: List[Dict[str, Any]] = Field(default_factory=list, description="Positions and status of all crews")
31
- wind: Dict[str, Any] = Field(default_factory=dict, description="Wind direction and speed")
32
- assets_at_risk: List[Dict[str, Any]] = Field(default_factory=list, description="Critical assets dynamically checked")
33
- timestep: int = Field(default=0, description="Current simulation step")
34
- containment_pct: float = Field(default=0.0, description="Percentage of fire contained")
35
- burning_per_division: Dict[str, int] = Field(default_factory=dict, description="Number of burning cells by division")
36
-
37
-
38
- from pydantic import BaseModel
39
-
40
- class TaskResponse(BaseModel):
41
- id: str
42
- name: str
43
- difficulty: str
44
- description: str
45
- grader: str
46
-
47
- class GraderResponse(BaseModel):
48
- task_scores: Dict[str, float]
49
-
50
- class BaselineResponse(BaseModel):
51
- task_scores: Dict[str, float]
52
- total_reward: float
53
- steps_taken: int
54
-
55
- class ResetRequest(BaseModel):
56
- task: int = 1
57
-
58
- class StepResponse(BaseModel):
59
- observation: Dict[str, Any]
60
- reward: float
61
- done: bool
62
- info: Dict[str, Any]
63
-
64
- class ResetResponse(BaseModel):
65
- observation: Dict[str, Any]
66
- info: Dict[str, Any] = {}
67
-
68
- class StateResponse(BaseModel):
69
- state: Dict[str, Any]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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]