-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojector.py
More file actions
362 lines (292 loc) · 12.4 KB
/
Copy pathprojector.py
File metadata and controls
362 lines (292 loc) · 12.4 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
from __future__ import annotations
from dataclasses import dataclass, field, asdict
from datetime import datetime, timedelta
from typing import Any, Literal
import math
ObjectType = Literal[
"document",
"event",
"narrative",
"market_move",
"entity",
"instrument",
]
@dataclass
class WorldObject:
object_id: str
object_type: ObjectType
timestamp: datetime
title: str
summary: str
related_entities: list[str] = field(default_factory=list)
related_instruments: list[str] = field(default_factory=list)
related_sectors: list[str] = field(default_factory=list)
related_narratives: list[str] = field(default_factory=list)
source_ids: list[str] = field(default_factory=list)
evidence_doc_ids: list[str] = field(default_factory=list)
evidence_spans: list[dict[str, Any]] = field(default_factory=list)
polarity: float | None = None
magnitude: float | None = None
uncertainty: float | None = None
source_reliability: float = 0.5
metadata: dict[str, Any] = field(default_factory=dict)
@dataclass
class AgentContext:
agent_id: str
role: str
portfolio: list[str]
watchlist: list[str]
preferred_sectors: list[str]
source_access: list[str]
information_channels: list[str]
network_position: dict[str, float]
risk_preference: float
memory_half_life_hours: float
attention_budget: dict[str, int]
role_interest_weights: dict[str, float]
@dataclass
class AgentHistory:
seen_object_ids: set[str]
previous_beliefs: dict[str, Any]
previous_actions: list[dict[str, Any]]
memory_items: list[dict[str, Any]]
class VisibilityFilter:
def __init__(self, channel_delays: dict[str, float]):
self.channel_delays = channel_delays
def is_visible(self, obj: WorldObject, agent: AgentContext, current_time: datetime) -> tuple[bool, list[str]]:
if obj.timestamp > current_time:
return False, ["future_information"]
if obj.source_ids:
if not any(src in agent.source_access for src in obj.source_ids):
return False, ["source_not_accessible"]
max_delay_minutes = 0.0
for src in obj.source_ids:
max_delay_minutes = max(max_delay_minutes, self.channel_delays.get(src, 0.0))
visible_time = obj.timestamp + timedelta(minutes=max_delay_minutes)
if visible_time > current_time:
return False, ["not_arrived_due_to_channel_delay"]
return True, ["time_visible", "source_accessible"]
def recall_candidates(visible_objects: list[WorldObject], agent: AgentContext, history: AgentHistory, max_candidates: int = 100) -> list[WorldObject]:
candidates: list[WorldObject] = []
portfolio_set = set(agent.portfolio)
watchlist_set = set(agent.watchlist)
sector_set = set(agent.preferred_sectors)
for obj in visible_objects:
instruments = set(obj.related_instruments)
sectors = set(obj.related_sectors)
direct_portfolio_hit = bool(instruments & portfolio_set)
watchlist_hit = bool(instruments & watchlist_set)
sector_hit = bool(sectors & sector_set)
is_market_move = obj.object_type == "market_move"
is_new = obj.object_id not in history.seen_object_ids
if direct_portfolio_hit or watchlist_hit or sector_hit or is_market_move or is_new:
candidates.append(obj)
return candidates[:max_candidates]
def portfolio_exposure_score(obj: WorldObject, agent: AgentContext) -> float:
if not obj.related_instruments:
return 0.0
instruments = set(obj.related_instruments)
if instruments & set(agent.portfolio):
return 1.0
if instruments & set(agent.watchlist):
return 0.6
return 0.0
def sector_exposure_score(obj: WorldObject, agent: AgentContext) -> float:
if not obj.related_sectors:
return 0.0
return 1.0 if set(obj.related_sectors) & set(agent.preferred_sectors) else 0.0
def role_affinity_score(obj: WorldObject, agent: AgentContext) -> float:
event_type = obj.metadata.get("event_type") or obj.metadata.get("move_type")
if not event_type:
return 0.3
return agent.role_interest_weights.get(event_type, 0.3)
def temporal_decay_score(obj: WorldObject, agent: AgentContext, current_time: datetime) -> float:
age_hours = (current_time - obj.timestamp).total_seconds() / 3600
half_life = agent.memory_half_life_hours
if half_life <= 0:
return 0.0
return math.exp(-math.log(2) * age_hours / half_life)
def novelty_score(obj: WorldObject, history: AgentHistory) -> float:
return 0.2 if obj.object_id in history.seen_object_ids else 1.0
def memory_continuity_score(obj: WorldObject, history: AgentHistory) -> float:
for instrument in set(obj.related_instruments):
if instrument in history.previous_beliefs:
return 0.8
return 0.0
def belief_relevance_score(obj: WorldObject, history: AgentHistory) -> float:
for inst in obj.related_instruments:
belief = history.previous_beliefs.get(inst)
if not belief:
continue
old_direction = belief.get("direction")
obj_polarity = obj.polarity
if old_direction == "bullish" and obj_polarity and obj_polarity > 0:
return 0.7
if old_direction == "bullish" and obj_polarity and obj_polarity < 0:
return 1.0
return 0.0
def network_exposure_score(obj: WorldObject, agent: AgentContext) -> float:
centrality = agent.network_position.get("centrality", 0.5)
social_media_exposure = agent.network_position.get("social_media_exposure", 0.5)
source_types = obj.metadata.get("source_types", [])
if "social_media" in source_types:
return social_media_exposure
if "institutional" in source_types:
return centrality
return 0.5
def market_move_relevance_score(obj: WorldObject) -> float:
relation = obj.metadata.get("market_move_relation")
if relation == "possible_trigger":
return 1.0
if relation == "same_sector_move":
return 0.6
if relation == "weakly_related":
return 0.3
return 0.0
@dataclass
class ProjectionWeights:
portfolio: float = 0.25
sector: float = 0.10
role: float = 0.15
temporal: float = 0.10
reliability: float = 0.10
novelty: float = 0.10
memory: float = 0.10
network: float = 0.05
market_move: float = 0.05
@dataclass
class PerceptionItem:
object_id: str
object_type: str
title: str
summary: str
salience_score: float
feature_scores: dict[str, float]
relevance_reasons: list[str]
evidence_doc_ids: list[str]
evidence_spans: list[dict[str, Any]]
metadata: dict[str, Any]
@dataclass
class PerceptionPack:
agent_id: str
timestamp: datetime
visible_items: list[PerceptionItem]
hidden_summary: dict[str, Any]
pack_metadata: dict[str, Any]
def to_dict(self) -> dict[str, Any]:
data = asdict(self)
data["timestamp"] = self.timestamp.isoformat()
return data
def compute_salience_score(obj: WorldObject, agent: AgentContext, history: AgentHistory, current_time: datetime, weights: ProjectionWeights) -> tuple[float, dict[str, float]]:
features = {
"portfolio": portfolio_exposure_score(obj, agent),
"sector": sector_exposure_score(obj, agent),
"role": role_affinity_score(obj, agent),
"temporal": temporal_decay_score(obj, agent, current_time),
"reliability": obj.source_reliability,
"novelty": novelty_score(obj, history),
"memory": max(memory_continuity_score(obj, history), belief_relevance_score(obj, history)),
"network": network_exposure_score(obj, agent),
"market_move": market_move_relevance_score(obj),
}
score = (
weights.portfolio * features["portfolio"]
+ weights.sector * features["sector"]
+ weights.role * features["role"]
+ weights.temporal * features["temporal"]
+ weights.reliability * features["reliability"]
+ weights.novelty * features["novelty"]
+ weights.memory * features["memory"]
+ weights.network * features["network"]
+ weights.market_move * features["market_move"]
)
uncertainty = obj.uncertainty or 0.0
risk_penalty = (1 - agent.risk_preference) * uncertainty * 0.2
score = max(0.0, score - risk_penalty)
return score, features
def select_with_budget(scored_items: list[dict[str, Any]], agent: AgentContext) -> list[dict[str, Any]]:
selected: list[dict[str, Any]] = []
used_events: set[str] = set()
type_counts: dict[str, int] = {}
for item in sorted(scored_items, key=lambda x: x["score"], reverse=True):
obj: WorldObject = item["object"]
obj_type = obj.object_type
max_count = agent.attention_budget.get(obj_type, 3)
if type_counts.get(obj_type, 0) >= max_count:
continue
canonical_event_id = obj.metadata.get("canonical_event_id")
if canonical_event_id and canonical_event_id in used_events:
continue
selected.append(item)
type_counts[obj_type] = type_counts.get(obj_type, 0) + 1
if canonical_event_id:
used_events.add(canonical_event_id)
return selected
def infer_relevance_reasons(feature_scores: dict[str, float]) -> list[str]:
reasons: list[str] = []
if feature_scores.get("portfolio", 0) > 0.8:
reasons.append("direct_portfolio_exposure")
elif feature_scores.get("portfolio", 0) > 0.4:
reasons.append("watchlist_exposure")
if feature_scores.get("sector", 0) > 0.8:
reasons.append("preferred_sector_exposure")
if feature_scores.get("role", 0) > 0.7:
reasons.append("role_affinity")
if feature_scores.get("memory", 0) > 0.7:
reasons.append("belief_or_memory_relevance")
if feature_scores.get("novelty", 0) > 0.8:
reasons.append("new_information")
if feature_scores.get("market_move", 0) > 0.7:
reasons.append("related_to_market_move")
if feature_scores.get("reliability", 0) > 0.8:
reasons.append("high_source_reliability")
return reasons
def build_perception_pack(selected_items: list[dict[str, Any]], agent: AgentContext, current_time: datetime, hidden_summary: dict[str, Any]) -> PerceptionPack:
perception_items: list[PerceptionItem] = []
for item in selected_items:
obj: WorldObject = item["object"]
perception_items.append(
PerceptionItem(
object_id=obj.object_id,
object_type=obj.object_type,
title=obj.title,
summary=obj.summary,
salience_score=item["score"],
feature_scores=item["feature_scores"],
relevance_reasons=infer_relevance_reasons(item["feature_scores"]),
evidence_doc_ids=obj.evidence_doc_ids,
evidence_spans=obj.evidence_spans,
metadata=obj.metadata,
)
)
return PerceptionPack(
agent_id=agent.agent_id,
timestamp=current_time,
visible_items=perception_items,
hidden_summary=hidden_summary,
pack_metadata={
"projection_version": "rule_based_v0.1",
"scoring_method": "weighted_linear",
},
)
class AgentConditionedProjector:
def __init__(self, visibility_filter: VisibilityFilter, weights: ProjectionWeights | None = None):
self.visibility_filter = visibility_filter
self.weights = weights or ProjectionWeights()
def project(self, world_objects: list[WorldObject], agent: AgentContext, history: AgentHistory, current_time: datetime) -> PerceptionPack:
visible_objects: list[WorldObject] = []
hidden_reasons: dict[str, int] = {}
for obj in world_objects:
visible, reasons = self.visibility_filter.is_visible(obj=obj, agent=agent, current_time=current_time)
if visible:
visible_objects.append(obj)
else:
for reason in reasons:
hidden_reasons[reason] = hidden_reasons.get(reason, 0) + 1
candidates = recall_candidates(visible_objects, agent, history)
scored_items: list[dict[str, Any]] = []
for obj in candidates:
score, feature_scores = compute_salience_score(obj, agent, history, current_time, self.weights)
scored_items.append({"object": obj, "score": score, "feature_scores": feature_scores})
selected_items = select_with_budget(scored_items, agent)
return build_perception_pack(selected_items, agent, current_time, hidden_reasons)