forked from SaiNivedh26/graphstrike
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbaseline_agent.py
More file actions
249 lines (197 loc) · 8.75 KB
/
baseline_agent.py
File metadata and controls
249 lines (197 loc) · 8.75 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""Baseline agents for all three tasks of the Fake Gang Detection environment.
Run directly:
python baseline_agent.py --task easy --episodes 10
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
from typing import List, Optional
sys.path.insert(0, str(Path(__file__).parent))
from client import FakeGangEnvClient, StepResult
from models import AccountProfile, FakeGangObservation, ActionType
# ---------------------------------------------------------------------------
# Scoring helper
# ---------------------------------------------------------------------------
def _gang_score(profile: AccountProfile, task: str) -> float:
"""Simple composite fake-likelihood score (0–1)."""
# Normalise account age: newer = more suspicious (fakes created recently)
age_score = max(0.0, 1.0 - profile.account_age_days / 500.0)
# Posting hour clustering: peak hour 14 is suspicious
hour_score = max(0.0, 1.0 - abs(profile.avg_post_hour - 14.0) / 12.0)
return (
profile.photo_reuse_score * 0.40
+ profile.bio_template_score * 0.30
+ age_score * 0.20
+ hour_score * 0.10
)
# ---------------------------------------------------------------------------
# Easy baseline: signal scanner
# ---------------------------------------------------------------------------
def easy_agent(env: FakeGangEnvClient, seed: int = 0) -> StepResult:
result = env.reset(task="easy", seed=seed)
obs = result.observation
PHOTO_THRESH = 0.50
BIO_THRESH = 0.40
while not obs.done:
# Pick an uninspected account to inspect
uninspected = [
i for i in obs.visible_account_ids if i not in obs.inspected_ids
]
if uninspected:
target = uninspected[0]
result = env.inspect(target)
obs = result.observation
# Flag if it matches both signal thresholds
profile = next((p for p in obs.visible_accounts if p.account_id == target), None)
if profile and (
profile.photo_reuse_score > PHOTO_THRESH
and profile.bio_template_score > BIO_THRESH
):
result = env.flag(target)
obs = result.observation
# Explore its network if we don't have enough flags yet
if len(obs.flagged_ids) < 10:
result = env.investigate_network(target)
obs = result.observation
else:
# Lower thresholds if we're running low on steps
if obs.steps_remaining < 10:
PHOTO_THRESH = max(0.20, PHOTO_THRESH - 0.10)
BIO_THRESH = max(0.20, BIO_THRESH - 0.10)
# Re-evaluate all inspected accounts with looser thresholds
for p in obs.visible_accounts:
if p.account_id not in obs.flagged_ids:
if (p.photo_reuse_score > PHOTO_THRESH
and p.bio_template_score > BIO_THRESH):
result = env.flag(p.account_id)
obs = result.observation
result = env.submit()
obs = result.observation
if obs.steps_remaining <= 0 or obs.done:
if not obs.done:
result = env.submit()
obs = result.observation
break
return result
# ---------------------------------------------------------------------------
# Medium baseline: time-aware scanner
# ---------------------------------------------------------------------------
def medium_agent(env: FakeGangEnvClient, seed: int = 0) -> StepResult:
result = env.reset(task="medium", seed=seed)
obs = result.observation
max_steps = 50
evasion_step = 20
while not obs.done:
steps_used = max_steps - obs.steps_remaining
uninspected = [i for i in obs.visible_account_ids if i not in obs.inspected_ids]
# Phase 1: race against evasion — use graph traversal
if steps_used < evasion_step - 5 and uninspected:
target = uninspected[0]
result = env.inspect(target)
obs = result.observation
profile = next((p for p in obs.visible_accounts if p.account_id == target), None)
if profile and profile.photo_reuse_score > 0.35:
result = env.flag(target)
obs = result.observation
result = env.investigate_network(target)
obs = result.observation
# Phase 2: after evasion — rely on features only
elif uninspected:
target = uninspected[0]
result = env.inspect(target)
obs = result.observation
profile = next((p for p in obs.visible_accounts if p.account_id == target), None)
if profile:
score = _gang_score(profile, "medium")
if score > 0.45:
result = env.flag(target)
obs = result.observation
else:
# No more to inspect — submit
result = env.submit()
obs = result.observation
break
if obs.steps_remaining <= 2 or obs.done:
if not obs.done:
result = env.submit()
obs = result.observation
break
return result
# ---------------------------------------------------------------------------
# Hard baseline: feature-only detective
# ---------------------------------------------------------------------------
def hard_agent(env: FakeGangEnvClient, seed: int = 0) -> StepResult:
result = env.reset(task="hard", seed=seed)
obs = result.observation
max_steps = 80
submit_by_step = 60 # submit before too many evasion events
while not obs.done:
steps_used = max_steps - obs.steps_remaining
uninspected = [i for i in obs.visible_account_ids if i not in obs.inspected_ids]
# Inspect until we have a dataset or time's up
if uninspected and steps_used < submit_by_step:
target = uninspected[0]
result = env.inspect(target)
obs = result.observation
# Discover more IDs via network expansion (spend the extra step)
if len(obs.visible_account_ids) < 100 and obs.steps_remaining > 15:
result = env.investigate_network(target)
obs = result.observation
else:
# Score all inspected accounts and flag top-12
scored = sorted(
obs.visible_accounts,
key=lambda p: _gang_score(p, "hard") + 0.3 * p.name_change_count,
reverse=True,
)
# Unflag everything first
for fid in list(obs.flagged_ids):
result = env.unflag(fid)
obs = result.observation
# Flag top 12 (a bit generous to improve recall)
for profile in scored[:12]:
result = env.flag(profile.account_id)
obs = result.observation
result = env.submit()
obs = result.observation
break
if obs.steps_remaining <= 2 or obs.done:
if not obs.done:
result = env.submit()
obs = result.observation
break
return result
# ---------------------------------------------------------------------------
# Evaluation runner
# ---------------------------------------------------------------------------
AGENTS = {
"easy": easy_agent,
"medium": medium_agent,
"hard": hard_agent,
}
def evaluate(task: str, episodes: int = 10, base_url: str = "http://localhost:8000") -> None:
agent_fn = AGENTS[task]
wins = 0
total_reward = 0.0
with FakeGangEnvClient(base_url=base_url) as env:
for seed in range(episodes):
result = agent_fn(env, seed=seed)
msg = result.message
reward = result.reward or 0.0
total_reward += reward
won = "[WIN]" in msg
if won:
wins += 1
print(f"Episode {seed:3d} | {'WIN ' if won else 'LOSS'} | reward={reward:+.2f} | {msg}")
print(f"\n{task.upper()} — wins: {wins}/{episodes} ({100*wins/episodes:.0f}%) | avg reward: {total_reward/episodes:.2f}")
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run baseline agent against the Fake Gang env.")
parser.add_argument("--task", choices=["easy", "medium", "hard"], default="easy")
parser.add_argument("--episodes", type=int, default=10)
parser.add_argument("--url", default="http://localhost:8000")
args = parser.parse_args()
evaluate(args.task, args.episodes, args.url)