-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.py
More file actions
414 lines (354 loc) · 15.1 KB
/
analytics.py
File metadata and controls
414 lines (354 loc) · 15.1 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
"""
analytics.py — Project A.L.I.V.E. NEXUS
Performance Analytics & Telemetry Engine
Features:
• Rolling statistics with configurable window sizes
• Convergence / plateau detection
• Exploration heatmap analysis
• Agent capability scoring (composite metric)
• Learning phase classification
• Session export & report generation
"""
import numpy as np
from collections import deque
from typing import Dict, List, Tuple, Optional
import time
import json
import math
# ============================================================
# STATISTICAL UTILITIES
# ============================================================
def rolling_mean(data: deque, window: int = None) -> float:
if not data:
return 0.0
arr = list(data)
if window:
arr = arr[-window:]
return float(np.mean(arr))
def rolling_std(data: deque, window: int = None) -> float:
if len(data) < 2:
return 0.0
arr = list(data)
if window:
arr = arr[-window:]
return float(np.std(arr))
def exponential_moving_average(values: List[float], alpha: float = 0.1) -> List[float]:
if not values:
return []
ema = [values[0]]
for v in values[1:]:
ema.append(alpha * v + (1 - alpha) * ema[-1])
return ema
def linear_trend(values: List[float]) -> float:
"""Returns the slope of a linear fit to the last N values."""
if len(values) < 2:
return 0.0
n = len(values)
x = np.arange(n)
slope = np.polyfit(x, values, 1)[0]
return float(slope)
# ============================================================
# CONVERGENCE DETECTOR
# ============================================================
class ConvergenceDetector:
"""
Detects whether the learning curve has converged, plateaued,
is still improving, or has regressed.
"""
STATES = ['warming_up', 'rapid_learning', 'fine_tuning', 'converged', 'plateau', 'regressing']
def __init__(self, window: int = 50, tolerance: float = 0.01):
self.window = window
self.tolerance = tolerance
self.state = 'warming_up'
self._history: deque = deque(maxlen=window * 3)
self._state_duration = 0
def update(self, value: float) -> str:
self._history.append(value)
self._state_duration += 1
if len(self._history) < self.window:
self.state = 'warming_up'
return self.state
recent = list(self._history)[-self.window:]
older = list(self._history)[-self.window*2:-self.window] if len(self._history) >= self.window*2 else recent
recent_mean = np.mean(recent)
older_mean = np.mean(older)
recent_std = np.std(recent)
trend = linear_trend(recent)
prev_state = self.state
if trend > self.tolerance:
self.state = 'rapid_learning' if trend > self.tolerance * 3 else 'fine_tuning'
elif trend < -self.tolerance:
self.state = 'regressing'
elif recent_std < self.tolerance * 2:
self.state = 'converged'
else:
self.state = 'plateau'
if self.state != prev_state:
self._state_duration = 0
return self.state
@property
def state_icon(self) -> str:
return {
'warming_up': '🔥',
'rapid_learning': '🚀',
'fine_tuning': '⚙️',
'converged': '✅',
'plateau': '📊',
'regressing': '⬇️',
}.get(self.state, '❓')
@property
def state_description(self) -> str:
return {
'warming_up': 'Filling replay buffer. Learning hasn\'t begun.',
'rapid_learning': 'Strong positive gradient. Policy improving rapidly.',
'fine_tuning': 'Gradual improvement. Policy refining.',
'converged': 'Performance stable. Policy has reached equilibrium.',
'plateau': 'No clear trend. High variance. Might be stuck.',
'regressing': 'Negative trend detected. Check hyperparameters.',
}.get(self.state, '')
# ============================================================
# EPISODE TRACKER
# ============================================================
class EpisodeTracker:
"""Records and analyzes episode-level statistics."""
def __init__(self, maxlen: int = 1000):
self.episodes: List[Dict] = []
self.maxlen = maxlen
# Rolling deques for fast stats
self.rewards : deque = deque(maxlen=200)
self.steps : deque = deque(maxlen=200)
self.successes : deque = deque(maxlen=200)
self.losses : deque = deque(maxlen=500)
self.td_errors : deque = deque(maxlen=500)
self.epsilons : deque = deque(maxlen=500)
self.optimality: deque = deque(maxlen=200)
self.convergence = ConvergenceDetector()
self.total_steps = 0
self._session_start = time.time()
def record_episode(self, reward: float, steps: int, success: bool,
info: Dict = None):
info = info or {}
ep = {
'reward': round(reward, 3),
'steps': steps,
'success': success,
'timestamp': time.time(),
'optimality': info.get('optimality', 0.0),
'fog_coverage':info.get('fog_coverage', 1.0),
'level': info.get('level', 1),
}
self.episodes.append(ep)
if len(self.episodes) > self.maxlen:
self.episodes.pop(0)
self.rewards.append(reward)
self.steps.append(steps)
self.successes.append(float(success))
self.optimality.append(info.get('optimality', 0.0))
self.total_steps += steps
self.convergence.update(reward)
def record_step(self, loss: float, td_error: float, epsilon: float):
self.losses.append(loss)
self.td_errors.append(td_error)
self.epsilons.append(epsilon)
@property
def success_rate(self) -> float:
if not self.successes:
return 0.0
return rolling_mean(self.successes, 50)
@property
def avg_reward(self) -> float:
return rolling_mean(self.rewards, 50)
@property
def avg_steps(self) -> float:
return rolling_mean(self.steps, 50)
@property
def reward_trend(self) -> float:
return linear_trend(list(self.rewards)[-30:])
@property
def avg_optimality(self) -> float:
return rolling_mean(self.optimality, 30)
def get_chart_data(self, n: int = 200) -> Dict:
"""Return data dicts for all performance charts."""
eps = list(self.episodes)[-n:]
return {
'rewards': [e['reward'] for e in eps],
'steps': [e['steps'] for e in eps],
'successes': [int(e['success']) for e in eps],
'optimality': [e.get('optimality', 0) for e in eps],
'levels': [e.get('level', 1) for e in eps],
'losses': list(self.losses)[-n*5:],
'td_errors': list(self.td_errors)[-n*5:],
'epsilons': list(self.epsilons)[-n*5:],
'ema_rewards': exponential_moving_average([e['reward'] for e in eps], alpha=0.1),
}
def session_summary(self) -> Dict:
elapsed = time.time() - self._session_start
return {
'total_episodes': len(self.episodes),
'total_steps': self.total_steps,
'session_duration':round(elapsed, 1),
'success_rate': round(self.success_rate, 3),
'avg_reward': round(self.avg_reward, 3),
'avg_steps': round(self.avg_steps, 1),
'reward_trend': round(self.reward_trend, 5),
'convergence': self.convergence.state,
'avg_optimality': round(self.avg_optimality, 3),
}
# ============================================================
# HEATMAP TRACKER
# ============================================================
class HeatmapTracker:
"""
Tracks agent visits across maze cells across all episodes.
Generates normalized heatmaps for visualization.
"""
def __init__(self, max_h: int = 41, max_w: int = 45):
self.max_h = max_h
self.max_w = max_w
self.global_visits = np.zeros((max_h, max_w), dtype=np.float32)
self.episode_visits = np.zeros((max_h, max_w), dtype=np.float32)
def record_step(self, r: int, c: int):
if 0 <= r < self.max_h and 0 <= c < self.max_w:
self.global_visits[r, c] += 1.0
self.episode_visits[r, c] += 1.0
def new_episode(self):
self.episode_visits[:] = 0.0
def get_global_heatmap(self, h: int, w: int) -> np.ndarray:
"""Return normalized heatmap cropped to current maze size."""
sub = self.global_visits[:h, :w].copy()
if sub.max() > 0:
sub /= sub.max()
return sub
def get_episode_heatmap(self, h: int, w: int) -> np.ndarray:
sub = self.episode_visits[:h, :w].copy()
if sub.max() > 0:
sub /= sub.max()
return sub
def coverage(self, h: int, w: int, maze: np.ndarray = None) -> float:
"""Fraction of passable cells ever visited."""
visited = (self.global_visits[:h, :w] > 0)
if maze is not None:
passable = (maze == 0)
total = passable.sum()
return float((visited & passable).sum()) / max(total, 1)
return float(visited.sum()) / max(h * w, 1)
# ============================================================
# CAPABILITY SCORE
# ============================================================
class CapabilityScore:
"""
Composite metric (0-100) combining:
- Success rate (40%)
- Path efficiency vs A* (25%)
- Exploration coverage (15%)
- Learning convergence (10%)
- Curriculum level reached (10%)
"""
def __init__(self):
self.history: deque = deque(maxlen=100)
def compute(self, tracker: EpisodeTracker, curriculum_level: int,
exploration_coverage: float) -> float:
success_score = tracker.success_rate * 40.0
optimality_score = tracker.avg_optimality * 25.0
exploration_score = min(exploration_coverage, 1.0) * 15.0
conv_state = tracker.convergence.state
conv_score = {
'warming_up': 2.0, 'rapid_learning': 8.0, 'fine_tuning': 7.0,
'converged': 10.0, 'plateau': 4.0, 'regressing': 0.0,
}.get(conv_state, 0.0)
level_score = ((curriculum_level - 1) / 9.0) * 10.0
total = success_score + optimality_score + exploration_score + conv_score + level_score
total = float(np.clip(total, 0.0, 100.0))
self.history.append(total)
return total
@property
def trend(self) -> str:
if len(self.history) < 5:
return '→'
t = linear_trend(list(self.history)[-10:])
if t > 0.1: return '↑'
elif t < -0.1: return '↓'
return '→'
# ============================================================
# PERFORMANCE DASHBOARD (Aggregator)
# ============================================================
class PerformanceDashboard:
"""
The main analytics object. Aggregates all sub-systems and provides
a unified interface for the Streamlit frontend.
"""
def __init__(self):
self.tracker = EpisodeTracker()
self.heatmap = HeatmapTracker()
self.capability = CapabilityScore()
self._step_count = 0
# ----------------------------------------------------------
def record_step(self, agent_r: int, agent_c: int,
loss: float, td_error: float, epsilon: float):
self.tracker.record_step(loss, td_error, epsilon)
self.heatmap.record_step(agent_r, agent_c)
self._step_count += 1
# ----------------------------------------------------------
def record_episode(self, reward: float, steps: int, success: bool,
info: Dict, curriculum_level: int,
h: int, w: int, maze=None):
self.tracker.record_episode(reward, steps, success, info)
coverage = self.heatmap.coverage(h, w, maze)
cap = self.capability.compute(self.tracker, curriculum_level, coverage)
self.heatmap.new_episode()
return cap
# ----------------------------------------------------------
def get_live_stats(self) -> Dict:
"""Fast stat snapshot for live display."""
t = self.tracker
return {
'total_episodes': len(t.episodes),
'success_rate': round(t.success_rate * 100, 1),
'avg_reward': round(t.avg_reward, 2),
'avg_steps': round(t.avg_steps, 1),
'reward_trend': round(t.reward_trend, 5),
'convergence': t.convergence.state,
'convergence_icon':t.convergence.state_icon,
'convergence_desc':t.convergence.state_description,
'capability': round(self.capability.history[-1], 1) if self.capability.history else 0.0,
'capability_trend':self.capability.trend,
}
# ----------------------------------------------------------
def get_chart_data(self, n: int = 150) -> Dict:
return self.tracker.get_chart_data(n)
# ----------------------------------------------------------
def get_heatmap(self, h: int, w: int, episode: bool = False) -> np.ndarray:
if episode:
return self.heatmap.get_episode_heatmap(h, w)
return self.heatmap.get_global_heatmap(h, w)
# ----------------------------------------------------------
def get_session_report(self) -> str:
s = self.tracker.session_summary()
cap = self.capability.history[-1] if self.capability.history else 0.0
lines = [
"═══════════════════════════════════════",
" A.L.I.V.E. SESSION REPORT ",
"═══════════════════════════════════════",
f" Duration: {s['session_duration']}s",
f" Total Episodes: {s['total_episodes']}",
f" Total Steps: {s['total_steps']}",
f" Success Rate: {s['success_rate']*100:.1f}%",
f" Avg Reward: {s['avg_reward']}",
f" Avg Steps/Ep: {s['avg_steps']}",
f" Path Efficiency: {s['avg_optimality']*100:.1f}%",
f" Convergence: {s['convergence']}",
f" Capability Score:{cap:.1f}/100",
"═══════════════════════════════════════",
]
return '\n'.join(lines)
# ----------------------------------------------------------
def export_json(self) -> str:
"""Export session data as JSON string."""
data = {
'session_summary': self.tracker.session_summary(),
'episodes': self.tracker.episodes[-100:],
'capability_history': list(self.capability.history),
}
from memory_palace import PersistentStore
data = PersistentStore()._serialize(data)
return json.dumps(data, indent=2)