fixed the constant 0.0000 display of reward
Browse files
graders/grader_detection.py
CHANGED
|
@@ -119,18 +119,21 @@ class FinAuditorGrader:
|
|
| 119 |
def grade(self, state: Any, ground_truth: dict[str, Any] | None = None) -> float:
|
| 120 |
"""Compute the final episode score.
|
| 121 |
|
|
|
|
|
|
|
|
|
|
| 122 |
Args:
|
| 123 |
state: Environment state object at episode end.
|
| 124 |
-
|
| 125 |
-
ground_truth: Unused — ground truth is implicit in the C++ engine.
|
| 126 |
|
| 127 |
Returns:
|
| 128 |
float strictly in (0.01, 0.99).
|
| 129 |
"""
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
| 134 |
|
| 135 |
total = tp + tn + fp + fn
|
| 136 |
if total == 0:
|
|
@@ -140,11 +143,9 @@ class FinAuditorGrader:
|
|
| 140 |
positive_signal = (tp * _TP_WEIGHT) + (tn * _TN_WEIGHT)
|
| 141 |
negative_signal = (fp * _FP_PENALTY) + (fn * _FN_PENALTY)
|
| 142 |
|
| 143 |
-
# Normalise against the theoretical maximum (all trades are TP)
|
| 144 |
max_signal = total * _TP_WEIGHT
|
| 145 |
raw_score = max(0.0, positive_signal - negative_signal) / max_signal
|
| 146 |
|
| 147 |
-
# Strict hackathon boundary — must not be exactly 0.0 or 1.0
|
| 148 |
score = max(0.01, min(0.99, raw_score))
|
| 149 |
|
| 150 |
self._record(
|
|
|
|
| 119 |
def grade(self, state: Any, ground_truth: dict[str, Any] | None = None) -> float:
|
| 120 |
"""Compute the final episode score.
|
| 121 |
|
| 122 |
+
Reads cumulative ``total_*`` counters (full episode) when available,
|
| 123 |
+
falling back to ``last_*`` (single-batch snapshot) for compatibility.
|
| 124 |
+
|
| 125 |
Args:
|
| 126 |
state: Environment state object at episode end.
|
| 127 |
+
ground_truth: Unused — truth is implicit in the C++ engine.
|
|
|
|
| 128 |
|
| 129 |
Returns:
|
| 130 |
float strictly in (0.01, 0.99).
|
| 131 |
"""
|
| 132 |
+
# Prefer full-episode accumulators; fall back to last-batch snapshot
|
| 133 |
+
tp = float(getattr(state, "total_tp", None) or getattr(state, "last_tp", 0))
|
| 134 |
+
tn = float(getattr(state, "total_tn", None) or getattr(state, "last_tn", 0))
|
| 135 |
+
fp = float(getattr(state, "total_fp", None) or getattr(state, "last_fp", 0))
|
| 136 |
+
fn = float(getattr(state, "total_fn", None) or getattr(state, "last_fn", 0))
|
| 137 |
|
| 138 |
total = tp + tn + fp + fn
|
| 139 |
if total == 0:
|
|
|
|
| 143 |
positive_signal = (tp * _TP_WEIGHT) + (tn * _TN_WEIGHT)
|
| 144 |
negative_signal = (fp * _FP_PENALTY) + (fn * _FN_PENALTY)
|
| 145 |
|
|
|
|
| 146 |
max_signal = total * _TP_WEIGHT
|
| 147 |
raw_score = max(0.0, positive_signal - negative_signal) / max_signal
|
| 148 |
|
|
|
|
| 149 |
score = max(0.01, min(0.99, raw_score))
|
| 150 |
|
| 151 |
self._record(
|
server/fin_auditor_environment.py
CHANGED
|
@@ -151,10 +151,27 @@ class FinAuditorEnvironment(Environment):
|
|
| 151 |
anomalies: list[list[float]] = self.engine.get_anomaly_matrix().tolist()
|
| 152 |
done = self._state.step_count >= self._MAX_EPISODE_STEPS
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
return FinAuditorObservation(
|
| 155 |
features=anomalies,
|
| 156 |
message=f"Processed batch. Found {len(anomalies)} expired trades.",
|
| 157 |
-
reward=
|
| 158 |
done=done
|
| 159 |
)
|
| 160 |
|
|
|
|
| 151 |
anomalies: list[list[float]] = self.engine.get_anomaly_matrix().tolist()
|
| 152 |
done = self._state.step_count >= self._MAX_EPISODE_STEPS
|
| 153 |
|
| 154 |
+
# 4. COMPUTE LIVE STEP REWARD from cumulative episode performance
|
| 155 |
+
# Uses same asymmetric weights as FinAuditorGrader so the dashboard
|
| 156 |
+
# value is consistent with the official final episode score.
|
| 157 |
+
tp = float(self._state.total_tp)
|
| 158 |
+
tn = float(self._state.total_tn)
|
| 159 |
+
fp = float(self._state.total_fp)
|
| 160 |
+
fn = float(self._state.total_fn)
|
| 161 |
+
total = tp + tn + fp + fn
|
| 162 |
+
|
| 163 |
+
if total > 0:
|
| 164 |
+
positive = tp * 1.0 + tn * 0.1
|
| 165 |
+
negative = fp * 0.1 + fn * 0.4
|
| 166 |
+
raw = max(0.0, positive - negative) / (total * 1.0)
|
| 167 |
+
step_reward = max(0.01, min(0.99, raw))
|
| 168 |
+
else:
|
| 169 |
+
step_reward = 0.01 # floor before any decisions are made
|
| 170 |
+
|
| 171 |
return FinAuditorObservation(
|
| 172 |
features=anomalies,
|
| 173 |
message=f"Processed batch. Found {len(anomalies)} expired trades.",
|
| 174 |
+
reward=step_reward,
|
| 175 |
done=done
|
| 176 |
)
|
| 177 |
|