File size: 1,872 Bytes
7f84b94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
TerminationEngine — determines when an episode ends.

An episode terminates when any of these conditions is met:
  - max_steps reached
  - all active fraud routes have been deactivated (defender wins)
  - total laundered amount exceeds the episode threshold (fraudster wins)
"""
from __future__ import annotations

from dataclasses import dataclass
from typing import Optional

from scam_detection.hidden_world_state import HiddenWorldState


@dataclass
class TerminationResult:
    done: bool
    reason: Optional[str] = None   # "max_steps" | "all_routes_blocked" | "fraud_threshold_exceeded"


class TerminationEngine:
    """
    Checks whether the episode should end after a step.

    Parameters
    ----------
    fraud_threshold_ratio:
        Episode ends when laundered / total_fraud_amount exceeds this ratio.
        Represents the fraudster winning by extracting a significant portion.
    """

    def __init__(self, fraud_threshold_ratio: float = 0.8) -> None:
        self._threshold = fraud_threshold_ratio

    def check(self, world: HiddenWorldState) -> TerminationResult:
        """Return a TerminationResult after the current step."""
        # 1. Max steps
        if world.step >= world.max_steps:
            return TerminationResult(done=True, reason="max_steps")

        # 2. All fraud routes deactivated — defender wins
        if world.fraud_routes and all(not r.is_active for r in world.fraud_routes):
            return TerminationResult(done=True, reason="all_routes_blocked")

        # 3. Fraudster laundered a large fraction — fraudster wins
        if world.total_fraud_amount > 0:
            ratio = world.total_laundered / world.total_fraud_amount
            if ratio >= self._threshold:
                return TerminationResult(done=True, reason="fraud_threshold_exceeded")

        return TerminationResult(done=False)