-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.py
More file actions
177 lines (154 loc) · 6.47 KB
/
Copy pathenvironment.py
File metadata and controls
177 lines (154 loc) · 6.47 KB
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
import sys
sys.path.insert(0, '/app/src/geoshield')
sys.path.insert(0, '/app/src/geoshield/server')
sys.path.insert(0, '/app')
"""
Core GeoShieldEnvironment — episodic RL environment for SIGINT triage.
"""
from typing import Optional, Tuple, Dict, Any
import sys, os
sys.path.insert(0, '/app')
from models import GeoShieldAction, GeoObservation, GeoReward, GeoState, SignalMetadata
from graders import GRADERS
TASK_ACTIONS = {
1: ["no_anomaly", "flag_for_review", "escalate_immediately"],
2: ["benign", "suspicious", "hostile", "critical"],
3: ["monitor", "deploy_surveillance", "alert_command", "immediate_intercept"],
}
MAX_STEPS = {1: 3, 2: 3, 3: 5}
import json, random, pathlib, os
def load_cases(task_id: int):
# Try multiple possible locations
possible_paths = [
pathlib.Path(__file__).parent / f"task{task_id}_train.jsonl",
pathlib.Path('/app') / f"task{task_id}_train.jsonl",
pathlib.Path('/app/data') / f"task{task_id}_train.jsonl",
pathlib.Path(f"task{task_id}_train.jsonl"),
]
path = None
for p in possible_paths:
if p.exists():
path = p
break
if path is None:
# Return dummy cases if no file found
return [{"id": f"t{task_id}_001", "task": task_id, "observation": "Signal detected", "context": "Normal conditions", "gold_action": "monitor" if task_id == 3 else "no_anomaly" if task_id == 1 else "benign", "difficulty": "easy", "category": "clear"}]
cases = []
with open(path) as f:
for line in f:
line = line.strip()
if line:
cases.append(json.loads(line))
return cases
def sample_case(task_id: int, seed=None):
cases = load_cases(task_id)
rng = random.Random(seed)
return rng.choice(cases)
def compute_reward(grader_reward: GeoReward, step: int, max_steps: int) -> float:
base = grader_reward.score
# Step penalty: lose up to 0.1 for being slow
step_penalty = 0.0
if step > 1:
step_penalty = min(0.1, (step - 1) * 0.03)
reward = max(0.0, base - step_penalty)
return round(reward, 3)
class GeoShieldEnvironment:
def __init__(self):
self._state: Optional[GeoState] = None
self._current_case: Optional[Dict[str, Any]] = None
self._task_id: int = 1
def reset(self, task_id: int = 1, seed: int = None) -> Tuple[GeoObservation, GeoState]:
self._task_id = task_id
case = sample_case(task_id, seed=seed)
self._current_case = case
difficulty = case.get("difficulty", "easy")
self._state = GeoState(
task_id=task_id,
case_id=case["id"],
completed=False,
step=0,
rewards=[],
current_observation=case["observation"],
total_score=0.0,
difficulty=difficulty,
)
# Build rich signal metadata if available
sig_meta = None
if "signal_metadata" in case:
m = case["signal_metadata"]
sig_meta = SignalMetadata(
frequency_mhz=m.get("frequency_mhz", 100.0),
power_dbm=m.get("power_dbm", -60.0),
modulation=m.get("modulation", "AM"),
duration_ms=m.get("duration_ms", 500),
region=m.get("region", "unknown"),
time_of_day=m.get("time_of_day", "day"),
repeat_count=m.get("repeat_count", 1),
known_pattern=m.get("known_pattern"),
)
obs = GeoObservation(
text=case["observation"],
context=case.get("context", ""),
task_id=task_id,
case_id=case["id"],
available_actions=TASK_ACTIONS[task_id],
step=0,
difficulty=difficulty,
signal_metadata=sig_meta,
signal_history=case.get("signal_history", []),
threat_indicators=case.get("threat_indicators", []),
)
return obs, self._state
def step(self, action: GeoShieldAction) -> Tuple[GeoObservation, float, bool, Dict[str, Any]]:
if self._state is None or self._current_case is None:
raise RuntimeError("Call reset() before step()")
self._state.step += 1
step = self._state.step
max_steps = MAX_STEPS.get(self._task_id, 3)
grader = GRADERS[self._task_id]
grader_reward: GeoReward = grader(action, self._current_case)
reward = compute_reward(grader_reward, step, max_steps)
self._state.rewards.append(reward)
self._state.total_score = round(sum(self._state.rewards) / len(self._state.rewards), 3)
correct = grader_reward.score >= 0.9
done = correct or step >= max_steps
self._state.completed = done
sig_meta = None
if "signal_metadata" in self._current_case:
m = self._current_case["signal_metadata"]
sig_meta = SignalMetadata(
frequency_mhz=m.get("frequency_mhz", 100.0),
power_dbm=m.get("power_dbm", -60.0),
modulation=m.get("modulation", "AM"),
duration_ms=m.get("duration_ms", 500),
region=m.get("region", "unknown"),
time_of_day=m.get("time_of_day", "day"),
repeat_count=m.get("repeat_count", 1),
known_pattern=m.get("known_pattern"),
)
obs = GeoObservation(
text=self._current_case["observation"],
context=self._current_case.get("context", ""),
task_id=self._task_id,
case_id=self._current_case["id"],
available_actions=TASK_ACTIONS[self._task_id],
step=step,
difficulty=self._current_case.get("difficulty", "easy"),
signal_metadata=sig_meta,
signal_history=self._current_case.get("signal_history", []),
threat_indicators=self._current_case.get("threat_indicators", []),
)
info = {
"grader_score": grader_reward.score,
"feedback": grader_reward.feedback,
"breakdown": grader_reward.breakdown,
"gold_action": self._current_case.get("gold_action"),
"case_difficulty": self._current_case.get("difficulty"),
"case_category": self._current_case.get("category"),
"step_penalty_applied": step > 1,
}
return obs, reward, done, info
def state(self) -> GeoState:
if self._state is None:
return GeoState(task_id=0, case_id="", completed=False, step=0)
return self._state