File size: 67,500 Bytes
bea6321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea6321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea6321
b596ed6
 
 
bea6321
b596ed6
 
 
bea6321
b596ed6
bea6321
 
 
 
 
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea6321
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
 
 
 
 
 
 
bea6321
 
 
 
 
 
 
b596ed6
 
 
 
 
 
bea6321
 
 
 
 
 
 
 
 
 
 
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea6321
 
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea6321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea6321
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea6321
b596ed6
bea6321
b596ed6
 
 
 
 
bea6321
 
 
 
 
 
b596ed6
bea6321
 
 
 
 
b596ed6
 
 
 
 
bea6321
 
 
 
 
 
 
 
 
 
 
b596ed6
 
 
 
bea6321
 
 
 
 
 
 
 
 
 
 
b596ed6
bea6321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
 
 
 
 
 
 
 
bea6321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
bea6321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
bea6321
 
 
 
b596ed6
bea6321
 
 
 
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea6321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
 
bea6321
 
 
 
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea6321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
 
 
 
 
 
 
 
 
 
 
bea6321
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
 
bea6321
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
 
 
bea6321
 
b596ed6
 
bea6321
 
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea6321
 
 
 
 
 
 
 
 
 
 
 
 
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
#!/usr/bin/env python3
"""Soci Agent NN β€” Local Training Script

Equivalent to notebooks/soci_agent_nn.ipynb but runs as a standalone script.
Trains the SociAgentTransformer, exports to ONNX, and optionally pushes to HF Hub.

Usage:
    python scripts/nn_train.py                          # Train from scratch (synthetic data)
    python scripts/nn_train.py --data data/nn_training  # Train on collected + synthetic data
    python scripts/nn_train.py --push                   # Train and push to HF Hub
    python scripts/nn_train.py --epochs 50 --lr 1e-4    # Custom hyperparameters
    python scripts/nn_train.py --resume                 # Resume from existing weights

Requires: pip install torch onnx onnxruntime numpy huggingface_hub
"""

from __future__ import annotations

import argparse
import json
import logging
import math
import os
import random
import sys
import time
from collections import Counter
from pathlib import Path

import numpy as np

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(name)s: %(message)s",
    stream=sys.stdout,
)
logger = logging.getLogger("nn_train")

# ── Paths ────────────────────────────────────────────────────────────────
SCRIPT_DIR = Path(__file__).parent
PROJECT_DIR = SCRIPT_DIR.parent
MODEL_DIR = PROJECT_DIR / "models"
DATA_DIR = PROJECT_DIR / "data" / "nn_training"
SAMPLES_FILE = DATA_DIR / "collected_samples.jsonl"

# ══════════════════════════════════════════════════════════════════════════
# 1. Domain Constants β€” must match the Soci simulation
# ══════════════════════════════════════════════════════════════════════════

ACTION_TYPES = ["move", "work", "eat", "sleep", "talk", "exercise", "shop", "relax", "wander"]
ACTION_TO_IDX = {a: i for i, a in enumerate(ACTION_TYPES)}
NUM_ACTIONS = len(ACTION_TYPES)

LOCATIONS = [
    # Residential (17)
    "house_elena", "house_marcus", "house_helen", "house_diana", "house_kai",
    "house_priya", "house_james", "house_rosa", "house_yuki", "house_frank",
    "apartment_block_1", "apartment_block_2", "apartment_block_3",
    "apt_northeast", "apt_northwest", "apt_southeast", "apt_southwest",
    # Commercial (8)
    "cafe", "grocery", "bar", "restaurant", "bakery", "cinema", "diner", "pharmacy",
    # Work (5)
    "office", "office_tower", "factory", "school", "hospital",
    # Public (8)
    "park", "gym", "library", "church", "town_square", "sports_field",
    "street_north", "street_south", "street_east", "street_west",
]
LOC_TO_IDX = {loc: i for i, loc in enumerate(LOCATIONS)}
NUM_LOCATIONS = len(LOCATIONS)

# Zone encoding
LOC_ZONE = {}
for _loc in LOCATIONS:
    if _loc.startswith(("house_", "apartment_", "apt_")):
        LOC_ZONE[_loc] = 0
    elif _loc in ("cafe", "grocery", "bar", "restaurant", "bakery", "cinema", "diner", "pharmacy"):
        LOC_ZONE[_loc] = 1
    elif _loc in ("office", "office_tower", "factory", "school", "hospital"):
        LOC_ZONE[_loc] = 2
    else:
        LOC_ZONE[_loc] = 3

ACTION_NEEDS = {
    "work":     {"purpose": 0.3},
    "eat":      {"hunger": 0.5},
    "sleep":    {"energy": 0.6},
    "talk":     {"social": 0.3},
    "exercise": {"energy": -0.1, "fun": 0.2, "comfort": 0.1},
    "shop":     {"hunger": 0.1, "comfort": 0.1},
    "relax":    {"energy": 0.1, "fun": 0.2, "comfort": 0.2},
    "wander":   {"fun": 0.1},
    "move":     {},
}

ACTION_DURATIONS = {"move": 1, "work": 4, "eat": 2, "sleep": 8, "talk": 2, "exercise": 3, "shop": 2, "relax": 2, "wander": 1}
NEED_NAMES = ["hunger", "energy", "social", "purpose", "comfort", "fun"]
PERSONALITY_NAMES = ["openness", "conscientiousness", "extraversion", "agreeableness", "neuroticism"]

NUM_TIME_PERIODS = 7
FEATURE_DIM = 47


# ══════════════════════════════════════════════════════════════════════════
# 2. Personas β€” 20 Soci characters (from personas.yaml)
# ══════════════════════════════════════════════════════════════════════════

PERSONAS = [
    # House 1 β€” Elena & Lila (roommates)
    {"id": "elena",  "name": "Elena Vasquez",    "age": 34, "gender": "female",    "occ": "software engineer",
     "O": 8, "C": 7, "E": 4, "A": 6, "N": 5, "home": "house_elena", "work": "office",
     "tags": ["freelance", "introvert", "tech"],
     "hangouts": ["cafe", "library"],           # where she goes to think/work remotely
     "routine_bias": {}},

    {"id": "lila",   "name": "Lila Santos",      "age": 33, "gender": "female",    "occ": "artist",
     "O": 10, "C": 3, "E": 6, "A": 7, "N": 7, "home": "house_elena", "work": "library",
     "tags": ["creative", "emotional", "crush_elena"],
     "hangouts": ["park", "cafe", "library"],   # paints outdoors, hangs near Elena
     "routine_bias": {"relax": 0.15, "wander": 0.10}},

    # House 2 β€” Marcus & Zoe (siblings)
    {"id": "marcus", "name": "Marcus Chen",      "age": 28, "gender": "male",      "occ": "fitness trainer",
     "O": 5, "C": 8, "E": 9, "A": 7, "N": 3, "home": "house_marcus", "work": "gym",
     "tags": ["athletic", "extrovert", "community"],
     "hangouts": ["park", "sports_field", "cafe"],
     "routine_bias": {"exercise": 0.20, "talk": 0.10}},

    {"id": "zoe",    "name": "Zoe Chen-Williams", "age": 19, "gender": "female",   "occ": "college student",
     "O": 8, "C": 4, "E": 8, "A": 6, "N": 7, "home": "house_marcus", "work": "library",
     "tags": ["student", "social_media", "young"],
     "hangouts": ["cafe", "cinema", "park", "town_square"],
     "routine_bias": {"talk": 0.15, "wander": 0.10}},

    # House 3 β€” Helen & Alice (close friends)
    {"id": "helen",  "name": "Helen Park",       "age": 67, "gender": "female",    "occ": "retired teacher",
     "O": 6, "C": 8, "E": 6, "A": 8, "N": 4, "home": "house_helen", "work": "library",
     "tags": ["retired", "bookworm", "widow"],
     "hangouts": ["library", "park", "bakery", "church"],
     "routine_bias": {"relax": 0.15}},

    {"id": "alice",  "name": "Alice Fontaine",   "age": 58, "gender": "female",    "occ": "retired accountant",
     "O": 5, "C": 8, "E": 6, "A": 8, "N": 3, "home": "house_helen", "work": "bakery",
     "tags": ["retired", "baker", "nurturing"],
     "hangouts": ["bakery", "grocery", "church"],
     "routine_bias": {"work": 0.10}},   # loves baking, spends extra time at bakery

    # House 4 β€” Diana & Marco (mother & son)
    {"id": "diana",  "name": "Diana Novak",      "age": 41, "gender": "female",    "occ": "grocery store owner",
     "O": 4, "C": 9, "E": 5, "A": 6, "N": 7, "home": "house_diana", "work": "grocery",
     "tags": ["business_owner", "single_mother", "protective"],
     "hangouts": ["grocery"],                   # rarely leaves the store
     "routine_bias": {"work": 0.20}},

    {"id": "marco",  "name": "Marco Delgado",    "age": 16, "gender": "male",      "occ": "high school student",
     "O": 7, "C": 4, "E": 6, "A": 5, "N": 6, "home": "house_diana", "work": "school",
     "tags": ["student", "teen", "gamer"],
     "hangouts": ["park", "cinema", "cafe", "sports_field"],
     "routine_bias": {"relax": 0.10, "wander": 0.10}},

    # House 5 β€” Kai (lives alone)
    {"id": "kai",    "name": "Kai Okonkwo",      "age": 22, "gender": "nonbinary", "occ": "barista",
     "O": 9, "C": 3, "E": 7, "A": 5, "N": 6, "home": "house_kai", "work": "cafe",
     "tags": ["musician", "creative", "dropout"],
     "hangouts": ["bar", "park", "town_square"],  # plays music, socializes
     "routine_bias": {"relax": 0.10, "talk": 0.10}},

    # House 6 β€” Priya & Nina (flatmates)
    {"id": "priya",  "name": "Priya Sharma",     "age": 38, "gender": "female",    "occ": "doctor",
     "O": 7, "C": 9, "E": 5, "A": 8, "N": 6, "home": "house_priya", "work": "hospital",
     "tags": ["overworked", "caring", "guilt"],
     "hangouts": ["hospital", "pharmacy"],      # rarely leaves work orbit
     "routine_bias": {"work": 0.25}},           # long hospital hours

    {"id": "nina",   "name": "Nina Volkov",      "age": 29, "gender": "female",    "occ": "real estate agent",
     "O": 5, "C": 8, "E": 9, "A": 4, "N": 5, "home": "house_priya", "work": "office",
     "tags": ["ambitious", "networker", "suspicious"],
     "hangouts": ["cafe", "restaurant", "office_tower"],
     "routine_bias": {"talk": 0.15, "work": 0.10}},

    # House 7 β€” James & Theo (housemates)
    {"id": "james",  "name": "James O'Brien",    "age": 55, "gender": "male",      "occ": "bar owner",
     "O": 5, "C": 6, "E": 8, "A": 7, "N": 4, "home": "house_james", "work": "bar",
     "tags": ["social_hub", "divorced", "storyteller"],
     "hangouts": ["bar"],                       # his whole life revolves around the bar
     "routine_bias": {"talk": 0.20}},

    {"id": "theo",   "name": "Theo Blackwood",   "age": 45, "gender": "male",      "occ": "construction worker",
     "O": 3, "C": 7, "E": 4, "A": 5, "N": 5, "home": "house_james", "work": "factory",
     "tags": ["blue_collar", "stoic", "handy"],
     "hangouts": ["bar", "diner"],              # bar after work
     "routine_bias": {"work": 0.15}},

    # House 8 β€” Rosa & Omar
    {"id": "rosa",   "name": "Rosa Martelli",    "age": 62, "gender": "female",    "occ": "restaurant owner",
     "O": 6, "C": 9, "E": 7, "A": 8, "N": 5, "home": "house_rosa", "work": "restaurant",
     "tags": ["nurturing", "italian", "community_mother"],
     "hangouts": ["restaurant", "grocery"],     # buys ingredients, feeds everyone
     "routine_bias": {"work": 0.20, "eat": 0.05}},

    {"id": "omar",   "name": "Omar Hassan",      "age": 50, "gender": "male",      "occ": "taxi driver",
     "O": 6, "C": 6, "E": 7, "A": 7, "N": 4, "home": "house_rosa", "work": "restaurant",
     "tags": ["immigrant", "philosophical", "hardworking"],
     "hangouts": ["restaurant", "cafe", "park"],
     "routine_bias": {"wander": 0.15}},         # drives around town = wander

    # House 9 β€” Yuki & Devon (flatmates)
    {"id": "yuki",   "name": "Yuki Tanaka",      "age": 26, "gender": "female",    "occ": "yoga instructor",
     "O": 8, "C": 6, "E": 5, "A": 9, "N": 3, "home": "house_yuki", "work": "gym",
     "tags": ["mindful", "calm", "empathetic"],
     "hangouts": ["park", "gym", "library"],    # meditates in park
     "routine_bias": {"exercise": 0.15, "relax": 0.10}},

    {"id": "devon",  "name": "Devon Reeves",     "age": 30, "gender": "male",      "occ": "freelance journalist",
     "O": 9, "C": 5, "E": 6, "A": 4, "N": 6, "home": "house_yuki", "work": "office",
     "tags": ["investigative", "paranoid", "curious"],
     "hangouts": ["cafe", "bar", "library", "town_square"],  # interviews, research
     "routine_bias": {"wander": 0.15, "talk": 0.10}},

    # House 10 β€” Frank, George & Sam
    {"id": "frank",  "name": "Frank Kowalski",   "age": 72, "gender": "male",      "occ": "retired mechanic",
     "O": 3, "C": 7, "E": 5, "A": 4, "N": 5, "home": "house_frank", "work": "bar",
     "tags": ["retired", "cantankerous", "creature_of_habit"],
     "hangouts": ["bar", "diner"],              # same bar stool every night
     "routine_bias": {"relax": 0.15}},

    {"id": "george", "name": "George Adeyemi",   "age": 47, "gender": "male",      "occ": "night shift security",
     "O": 4, "C": 7, "E": 3, "A": 6, "N": 4, "home": "house_frank", "work": "factory",
     "tags": ["night_shift", "widower", "observant"],
     "hangouts": ["park"],                      # naps in park during day
     "routine_bias": {}},                       # schedule handled by night_shift tag

    {"id": "sam",    "name": "Sam Nakamura",     "age": 40, "gender": "nonbinary", "occ": "librarian",
     "O": 7, "C": 8, "E": 3, "A": 7, "N": 4, "home": "house_frank", "work": "library",
     "tags": ["quiet", "bookish", "inclusive"],
     "hangouts": ["library", "park", "cafe"],
     "routine_bias": {"work": 0.10, "relax": 0.05}},
]


# ══════════════════════════════════════════════════════════════════════════
# 3. Feature Encoding
# ══════════════════════════════════════════════════════════════════════════

def _time_period(hour: int) -> int:
    if hour < 6: return 0
    if hour < 9: return 1
    if hour < 12: return 2
    if hour < 14: return 3
    if hour < 18: return 4
    if hour < 22: return 5
    return 6


def encode_features(
    persona: dict, hour: int, minute: int, day: int,
    needs: dict, mood: float, current_loc: str,
    num_people_here: int = 0,
) -> list[float]:
    """Encode agent state into 47-dim feature vector."""
    f: list[float] = []
    # Personality (5)
    f.append(persona.get("O", persona.get("openness", 5)) / 10.0)
    f.append(persona.get("C", persona.get("conscientiousness", 5)) / 10.0)
    f.append(persona.get("E", persona.get("extraversion", 5)) / 10.0)
    f.append(persona.get("A", persona.get("agreeableness", 5)) / 10.0)
    f.append(persona.get("N", persona.get("neuroticism", 5)) / 10.0)
    # Age (1)
    f.append(persona.get("age", 30) / 100.0)
    # Time cyclical (4)
    f.append(math.sin(2 * math.pi * hour / 24))
    f.append(math.cos(2 * math.pi * hour / 24))
    f.append(math.sin(2 * math.pi * minute / 60))
    f.append(math.cos(2 * math.pi * minute / 60))
    # Day (2)
    dow = ((day - 1) % 7)
    f.append(dow / 7.0)
    f.append(1.0 if dow >= 5 else 0.0)
    # Needs (6)
    for n in NEED_NAMES:
        f.append(needs.get(n, 0.5))
    # Mood (1)
    f.append(max(-1.0, min(1.0, mood)))
    # Urgency (2)
    vals = [needs.get(n, 0.5) for n in NEED_NAMES]
    urgent_idx = int(np.argmin(vals))
    f.append(urgent_idx / 5.0)
    f.append(1.0 if any(v < 0.15 for v in vals) else 0.0)
    # Location zone (1)
    zone = LOC_ZONE.get(current_loc, 3)
    f.append(zone / 3.0)
    # Home/work flags (2)
    home = persona.get("home", persona.get("home_location", ""))
    work = persona.get("work", persona.get("work_location", ""))
    f.append(1.0 if current_loc == home else 0.0)
    f.append(1.0 if current_loc == work else 0.0)
    # People density (1)
    f.append(min(num_people_here / 10.0, 1.0))
    # Location type one-hot (6)
    loc_oh = [0.0] * 6
    if current_loc.startswith(("house_", "apartment_", "apt_")):
        loc_oh[0] = 1.0
    elif zone == 1:
        loc_oh[1] = 1.0
    elif zone == 2:
        loc_oh[2] = 1.0
    elif current_loc.startswith("street_"):
        loc_oh[4] = 1.0
    else:
        loc_oh[3] = 1.0
    if current_loc == home:
        loc_oh[5] = 1.0
    f.extend(loc_oh)
    # Time period one-hot (7)
    tp = [0.0] * NUM_TIME_PERIODS
    tp[_time_period(hour)] = 1.0
    f.extend(tp)
    # Last action one-hot (9) β€” random for synthetic, zeros for real
    last_action_oh = [0.0] * NUM_ACTIONS
    if random.random() < 0.8:
        last_action_oh[random.randint(0, NUM_ACTIONS - 1)] = 1.0
    f.extend(last_action_oh)
    return f


# ══════════════════════════════════════════════════════════════════════════
# 4. Synthetic Data Generator
# ══════════════════════════════════════════════════════════════════════════

def _is_night_shift(persona: dict) -> bool:
    return "night_shift" in persona.get("tags", [])


def _is_retired(persona: dict) -> bool:
    return "retired" in persona.get("tags", [])


def _is_student(persona: dict) -> bool:
    return "student" in persona.get("tags", [])


def _persona_hangout(persona: dict, fallbacks: list[str]) -> str:
    """Pick a location the persona naturally gravitates toward."""
    hangouts = persona.get("hangouts", [])
    if hangouts and random.random() < 0.6:
        return random.choice(hangouts)
    return random.choice(fallbacks)


def _apply_routine_bias(persona: dict, action: str | None) -> str | None:
    """Probabilistically override action based on persona routine_bias."""
    bias = persona.get("routine_bias", {})
    for biased_action, prob in bias.items():
        if random.random() < prob:
            return biased_action
    return action


def _generate_needs_for_persona(persona: dict, hour: int) -> dict:
    """Generate needs influenced by persona lifestyle, not purely random."""
    needs = {}
    tags = persona.get("tags", [])
    is_night = _is_night_shift(persona)

    for n in NEED_NAMES:
        # Base: 15% chance critical, else moderate-to-full
        if random.random() < 0.15:
            needs[n] = round(random.uniform(0.0, 0.2), 2)
        else:
            needs[n] = round(random.uniform(0.2, 1.0), 2)

    # Persona-specific need tendencies
    if "overworked" in tags:
        # Priya: chronically low energy, low social
        needs["energy"] = round(min(needs["energy"], random.uniform(0.1, 0.5)), 2)
        needs["social"] = round(min(needs["social"], random.uniform(0.1, 0.5)), 2)
    if "athletic" in tags:
        # Marcus: high energy baseline, low fun without exercise
        needs["energy"] = round(max(needs["energy"], random.uniform(0.5, 0.9)), 2)
    if "emotional" in tags:
        # Lila: volatile needs
        swing = random.choice(NEED_NAMES)
        needs[swing] = round(random.uniform(0.0, 0.3), 2)
    if "creature_of_habit" in tags:
        # Frank: stable moderate needs
        for n in NEED_NAMES:
            needs[n] = round(needs[n] * 0.7 + 0.2, 2)
    if is_night:
        # George: energy inverted β€” tired during day, awake at night
        if 6 <= hour <= 18:
            needs["energy"] = round(min(needs["energy"], random.uniform(0.05, 0.35)), 2)
        else:
            needs["energy"] = round(max(needs["energy"], random.uniform(0.5, 0.9)), 2)
    if "student" in tags:
        # Students: higher social need, lower purpose
        needs["social"] = round(max(needs["social"], random.uniform(0.3, 0.7)), 2)
        needs["fun"] = round(max(needs["fun"], random.uniform(0.2, 0.5)), 2)
    if "nurturing" in tags or "community_mother" in tags:
        # Rosa, Alice: high comfort, purpose from feeding/helping others
        needs["purpose"] = round(max(needs["purpose"], random.uniform(0.4, 0.8)), 2)
    if "mindful" in tags:
        # Yuki: generally balanced, rarely critical
        for n in NEED_NAMES:
            needs[n] = round(max(needs[n], 0.2), 2)

    return needs


def _mood_for_persona(persona: dict, needs: dict) -> float:
    """Generate mood influenced by personality and current needs."""
    tags = persona.get("tags", [])
    # Base mood from needs average
    avg_need = sum(needs.values()) / len(needs)
    base_mood = (avg_need - 0.5) * 2  # maps 0-1 to -1..+1

    # Neuroticism makes mood more volatile
    n_factor = persona.get("N", 5) / 10.0
    volatility = random.uniform(-0.5, 0.5) * n_factor
    base_mood += volatility

    if "calm" in tags or "mindful" in tags:
        base_mood = base_mood * 0.6 + 0.2  # dampen toward positive
    if "emotional" in tags:
        base_mood += random.uniform(-0.4, 0.4)

    return round(max(-1.0, min(1.0, base_mood)), 2)


def _starting_location(persona: dict, hour: int, is_weekend: bool) -> str:
    """Pick a realistic starting location based on time and persona."""
    tags = persona.get("tags", [])
    is_night = _is_night_shift(persona)
    period = _time_period(hour)

    # Night shift workers: at work during night, home during day
    if is_night:
        if period in (0, 6):  # late night / night β€” at work
            return persona["work"]
        elif period in (1, 2):  # morning β€” heading home or sleeping
            return random.choice([persona["home"], persona["work"]])
        else:  # daytime β€” at home (sleeping) or park (napping)
            return random.choice([persona["home"], "park"] if random.random() < 0.7
                                 else [persona["home"]])

    # Normal schedule
    if period == 0:  # late night β€” home
        return persona["home"]
    elif period == 1:  # early morning β€” home or commuting
        return random.choice([persona["home"], persona["work"]])
    elif period in (2, 4) and not is_weekend:  # working hours
        if _is_retired(persona):
            return random.choice([persona["home"]] + persona.get("hangouts", ["park"]))
        if _is_student(persona):
            return random.choice([persona["work"], "library", persona["home"]])
        return random.choice([persona["work"], persona["work"], persona["work"],
                              _persona_hangout(persona, ["cafe"])])
    elif period == 3:  # lunch
        return random.choice([persona["work"], "cafe", "restaurant", "diner", "park"])
    elif period == 5:  # evening
        return random.choice([persona["home"], _persona_hangout(persona, ["bar", "cafe", "park"])])
    elif period == 6:  # night
        return random.choice([persona["home"], persona["home"], _persona_hangout(persona, ["bar"])])

    return persona["home"]


def generate_action_example(persona: dict) -> dict:
    """Generate one training example with persona-aware rule-based labels."""
    hour = random.randint(0, 23)
    minute = random.choice([0, 15, 30, 45])
    day = random.randint(1, 30)
    is_weekend = ((day - 1) % 7) >= 5
    tags = persona.get("tags", [])
    is_night = _is_night_shift(persona)

    needs = _generate_needs_for_persona(persona, hour)
    mood = _mood_for_persona(persona, needs)
    current_loc = _starting_location(persona, hour, is_weekend)

    # --- Determine action using rule-based logic ---
    # Priority 1: Critical needs
    urgent = [(n, v) for n, v in needs.items() if v < 0.15]
    urgent.sort(key=lambda x: x[1])

    action = None
    target_loc = current_loc
    duration = 1

    if urgent:
        need_name = urgent[0][0]
        if need_name == "hunger":
            action = "eat"
            # Persona-aware eating locations
            eat_locs = ["cafe", "restaurant", "grocery", "bakery", "diner", persona["home"]]
            if "community_mother" in tags:  # Rosa eats at her restaurant
                eat_locs = ["restaurant", persona["home"]]
            elif "baker" in tags:  # Alice eats at bakery or home
                eat_locs = ["bakery", persona["home"]]
            target_loc = random.choice(eat_locs)
            duration = 2
        elif need_name == "energy":
            action = "sleep"
            target_loc = persona["home"]
            duration = random.choice([4, 6, 8])
        elif need_name == "social":
            action = "talk"
            social_locs = ["cafe", "bar", "park", "town_square", current_loc]
            if "social_hub" in tags:  # James talks at his bar
                social_locs = ["bar", "bar", "restaurant", "park"]
            elif "networker" in tags:  # Nina networks everywhere
                social_locs = ["cafe", "restaurant", "office", "office_tower"]
            target_loc = random.choice(social_locs)
            duration = 2
        elif need_name == "purpose":
            action = "work"
            target_loc = persona["work"]
            duration = 4
        elif need_name == "comfort":
            action = "relax"
            target_loc = random.choice([persona["home"], "park", "library"])
            duration = 2
        elif need_name == "fun":
            action = random.choice(["relax", "exercise", "wander"])
            fun_locs = ["park", "gym", "cinema", "bar", "sports_field"]
            if "teen" in tags or "student" in tags:
                fun_locs = ["cinema", "park", "cafe", "sports_field", "town_square"]
            target_loc = random.choice(fun_locs)
            duration = 2

    # Priority 2: Night shift inverted schedule (George)
    if action is None and is_night:
        period = _time_period(hour)
        if period in (0, 6):  # night β€” George is at work
            action = "work"
            target_loc = persona["work"]
            duration = 4
        elif period == 1:  # early morning β€” heading home
            action = "move"
            target_loc = persona["home"]
            duration = 1
        elif period in (2, 3):  # day β€” sleeping
            if needs["energy"] < 0.6:
                action = "sleep"
                target_loc = persona["home"]
                duration = random.choice([4, 6, 8])
            else:
                # Sometimes naps in park
                action = "relax"
                target_loc = random.choice([persona["home"], "park"])
                duration = 2
        elif period in (4, 5):  # afternoon/evening β€” wake up, eat, prep for work
            r = random.random()
            if needs["hunger"] < 0.5:
                action = "eat"
                target_loc = random.choice(["diner", "restaurant", persona["home"]])
                duration = 2
            elif r < 0.3:
                action = "talk"
                target_loc = random.choice(["park", "cafe"])
                duration = 2
            else:
                action = "move"
                target_loc = persona["work"]
                duration = 1

    # Priority 3: Persona-specific behavioral patterns
    if action is None:
        period = _time_period(hour)

        # Frank: same bar stool every evening/night
        if persona["id"] == "frank" and period in (5, 6):
            if random.random() < 0.7:
                action = "relax"
                target_loc = "bar"
                duration = 3

        # Lila: gravitates toward Elena (crush) β€” seeks her hangouts
        elif persona["id"] == "lila" and random.random() < 0.15:
            action = random.choice(["wander", "talk", "relax"])
            target_loc = random.choice(["house_elena", "cafe", "library", "office"])
            duration = 2

        # Rosa: spends mornings buying ingredients, cooks all day
        elif persona["id"] == "rosa" and period in (1, 2):
            if random.random() < 0.4:
                action = "shop"
                target_loc = "grocery"
                duration = 2

        # Devon: investigative journalist, wanders and interviews
        elif persona["id"] == "devon" and period in (2, 4):
            if random.random() < 0.3:
                action = random.choice(["wander", "talk"])
                target_loc = random.choice(["cafe", "bar", "town_square", "library", "park"])
                duration = 2

        # Omar: taxi driver β€” wanders the streets during work hours
        elif persona["id"] == "omar" and period in (2, 3, 4) and not is_weekend:
            if random.random() < 0.5:
                action = "wander"
                target_loc = random.choice(["street_north", "street_south", "street_east", "street_west",
                                            "town_square", "cafe", "restaurant"])
                duration = 2

        # Diana: barely leaves the grocery store on weekdays
        elif persona["id"] == "diana" and not is_weekend and period in (2, 3, 4):
            if random.random() < 0.7:
                action = "work"
                target_loc = "grocery"
                duration = 4

        # Marcus: morning exercise is sacred
        elif persona["id"] == "marcus" and period == 1:
            if random.random() < 0.6:
                action = "exercise"
                target_loc = random.choice(["gym", "park", "sports_field"])
                duration = 3

        # Yuki: morning meditation/yoga
        elif persona["id"] == "yuki" and period == 1:
            if random.random() < 0.5:
                action = "exercise"
                target_loc = random.choice(["park", "gym"])
                duration = 3

    # Priority 4: Apply routine_bias override
    if action is None:
        biased = _apply_routine_bias(persona, None)
        if biased:
            action = biased
            target_loc = _persona_hangout(persona, ["park", "cafe", persona["home"]])
            duration = 2

    # Priority 5: General time-of-day patterns (fallback)
    if action is None:
        period = _time_period(hour)

        if period == 0:  # Late night
            action = "sleep"
            target_loc = persona["home"]
            duration = 8

        elif period == 1:  # Early morning
            r = random.random()
            if needs["hunger"] < 0.5:
                action = "eat"
                target_loc = random.choice(["cafe", "bakery", persona["home"]])
                duration = 2
            elif r < 0.3 and persona["E"] >= 6:
                action = "exercise"
                target_loc = random.choice(["gym", "park", "sports_field"])
                duration = 3
            else:
                action = "move"
                target_loc = persona["work"]
                duration = 1

        elif period in (2, 4):  # Mid-morning / Afternoon
            if is_weekend:
                r = random.random()
                if _is_retired(persona):
                    # Retired: relaxed weekend routine
                    if r < 0.35:
                        action = "relax"
                        target_loc = _persona_hangout(persona, ["park", "library", persona["home"]])
                    elif r < 0.55:
                        action = "talk"
                        target_loc = _persona_hangout(persona, ["cafe", "park", "church"])
                    elif r < 0.7:
                        action = "shop"
                        target_loc = random.choice(["grocery", "pharmacy", "bakery"])
                    else:
                        action = "wander"
                        target_loc = random.choice(["park", "town_square", "street_north"])
                    duration = random.choice([2, 3])
                elif _is_student(persona):
                    # Students: social weekends
                    if r < 0.3:
                        action = "talk"
                        target_loc = random.choice(["cafe", "park", "cinema", "town_square"])
                    elif r < 0.5:
                        action = "relax"
                        target_loc = random.choice(["cinema", "park", persona["home"]])
                    elif r < 0.65:
                        action = "exercise"
                        target_loc = random.choice(["gym", "park", "sports_field"])
                    elif r < 0.8:
                        action = "wander"
                        target_loc = random.choice(["town_square", "street_north", "street_south"])
                    else:
                        action = "shop"
                        target_loc = random.choice(["grocery", "pharmacy"])
                    duration = random.choice([2, 3])
                else:
                    if r < 0.25:
                        action = "relax"
                        target_loc = _persona_hangout(persona, ["park", "cafe", "library", persona["home"]])
                    elif r < 0.45 and persona["E"] >= 6:
                        action = "talk"
                        target_loc = _persona_hangout(persona, ["cafe", "park", "town_square"])
                    elif r < 0.6:
                        action = "shop"
                        target_loc = random.choice(["grocery", "pharmacy"])
                    elif r < 0.8:
                        action = "exercise"
                        target_loc = random.choice(["gym", "park", "sports_field"])
                    else:
                        action = "wander"
                        target_loc = random.choice(["park", "town_square", "street_north", "street_south"])
                    duration = random.choice([2, 3])
            else:
                # Weekday work hours
                work_prob = 0.5 + persona["C"] * 0.05
                # Business owners and doctors work even harder
                if "business_owner" in tags or persona["occ"] == "doctor":
                    work_prob += 0.15
                if _is_retired(persona):
                    work_prob = 0.15  # retired people rarely "work"
                if random.random() < work_prob:
                    action = "work"
                    target_loc = persona["work"]
                    duration = 4
                else:
                    action = random.choice(["wander", "relax", "talk"])
                    target_loc = _persona_hangout(persona, ["cafe", "park", "town_square"])
                    duration = 2

        elif period == 3:  # Midday / lunch
            if needs["hunger"] < 0.6:
                action = "eat"
                lunch_locs = ["cafe", "restaurant", "bakery", "diner", "park"]
                # People eat near their workplace
                if current_loc == persona["work"]:
                    lunch_locs = ["cafe", "restaurant", "diner", "bakery"]
                target_loc = random.choice(lunch_locs)
                duration = 2
            else:
                action = "relax"
                target_loc = random.choice(["park", "cafe"])
                duration = 1

        elif period == 5:  # Evening
            r = random.random()
            social_bias = persona["E"] / 10.0
            if r < social_bias * 0.5:
                action = "talk"
                evening_social = ["bar", "restaurant", "park", "cafe"]
                if "social_hub" in tags:
                    evening_social = ["bar", "bar", "restaurant"]
                target_loc = random.choice(evening_social)
                duration = 2
            elif r < 0.4:
                action = "eat"
                target_loc = random.choice(["restaurant", "bar", "diner", persona["home"]])
                duration = 2
            elif r < 0.55:
                action = "exercise"
                target_loc = random.choice(["gym", "park", "sports_field"])
                duration = 3
            elif r < 0.7:
                action = "relax"
                target_loc = _persona_hangout(persona, ["cinema", "bar", persona["home"], "library"])
                duration = 2
            else:
                action = "relax"
                target_loc = persona["home"]
                duration = 2

        elif period == 6:  # Night
            if needs["energy"] < 0.4:
                action = "sleep"
                target_loc = persona["home"]
                duration = 8
            else:
                action = "relax"
                target_loc = persona["home"]
                duration = 2

    # 30% chance of picking "move" if target != current
    if target_loc != current_loc and action != "move":
        if random.random() < 0.3:
            action = "move"
            duration = 1

    # Retired and elderly people do shorter activities
    if _is_retired(persona) and duration > 3 and action not in ("sleep", "work"):
        duration = min(duration, 3)

    # Teens/students have shorter attention spans for non-social activities
    if _is_student(persona) and action in ("relax", "work") and random.random() < 0.3:
        duration = max(1, duration - 1)

    features = encode_features(
        persona=persona, hour=hour, minute=minute, day=day,
        needs=needs, mood=mood, current_loc=current_loc,
        num_people_here=random.randint(0, 8),
    )

    return {
        "features": features,
        "action_idx": ACTION_TO_IDX[action],
        "target_loc_idx": LOC_TO_IDX.get(target_loc, 0),
        "duration": min(max(duration, 1), 8),
    }


def generate_dataset(n: int) -> list[dict]:
    """Generate n synthetic training examples."""
    data = []
    for _ in range(n):
        persona = random.choice(PERSONAS)
        data.append(generate_action_example(persona))
    return data


# ══════════════════════════════════════════════════════════════════════════
# 5. Model Architecture β€” SociAgentTransformer
# ══════════════════════════════════════════════════════════════════════════

def build_model():
    """Build the SociAgentTransformer model."""
    import torch
    import torch.nn as nn
    import torch.nn.functional as F

    class FeatureTokenizer(nn.Module):
        GROUPS = [
            ("personality", 0, 6),
            ("time", 6, 12),
            ("needs", 12, 21),
            ("location", 21, 31),
            ("time_period", 31, 38),
            ("last_action", 38, 47),
        ]

        def __init__(self, d_model: int):
            super().__init__()
            self.projections = nn.ModuleList()
            for name, start, end in self.GROUPS:
                self.projections.append(nn.Sequential(
                    nn.Linear(end - start, d_model),
                    nn.LayerNorm(d_model),
                    nn.GELU(),
                ))
            self.pos_embed = nn.Parameter(torch.randn(1, len(self.GROUPS), d_model) * 0.02)

        def forward(self, features):
            tokens = []
            for i, (_, start, end) in enumerate(self.GROUPS):
                tokens.append(self.projections[i](features[:, start:end]))
            tokens = torch.stack(tokens, dim=1)
            return tokens + self.pos_embed

    class MoEFeedForward(nn.Module):
        def __init__(self, d_model, d_ff, num_experts=4, top_k=2):
            super().__init__()
            self.num_experts = num_experts
            self.top_k = top_k
            self.gate = nn.Linear(d_model, num_experts, bias=False)
            self.experts = nn.ModuleList([
                nn.Sequential(nn.Linear(d_model, d_ff), nn.GELU(), nn.Linear(d_ff, d_model))
                for _ in range(num_experts)
            ])

        def forward(self, x):
            B, S, D = x.shape
            gate_probs = F.softmax(self.gate(x), dim=-1)
            top_k_probs, top_k_idx = gate_probs.topk(self.top_k, dim=-1)
            top_k_probs = top_k_probs / top_k_probs.sum(dim=-1, keepdim=True)
            output = torch.zeros_like(x)
            for k in range(self.top_k):
                eidx = top_k_idx[:, :, k]
                w = top_k_probs[:, :, k].unsqueeze(-1)
                for e in range(self.num_experts):
                    mask = (eidx == e).unsqueeze(-1)
                    if mask.any():
                        output = output + mask.float() * w * self.experts[e](x)
            return output

    class TransformerBlock(nn.Module):
        def __init__(self, d_model, nhead, d_ff, num_experts=4, dropout=0.1):
            super().__init__()
            self.attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout, batch_first=True)
            self.norm1 = nn.LayerNorm(d_model)
            self.moe_ff = MoEFeedForward(d_model, d_ff, num_experts)
            self.norm2 = nn.LayerNorm(d_model)
            self.dropout = nn.Dropout(dropout)

        def forward(self, x):
            attn_out, _ = self.attn(x, x, x)
            x = self.norm1(x + self.dropout(attn_out))
            ff_out = self.moe_ff(x)
            return self.norm2(x + self.dropout(ff_out))

    class SociAgentTransformer(nn.Module):
        def __init__(self, d_model=128, nhead=8, num_layers=4, d_ff=256,
                     num_experts=4, dropout=0.1):
            super().__init__()
            self.tokenizer = FeatureTokenizer(d_model)
            self.layers = nn.ModuleList([
                TransformerBlock(d_model, nhead, d_ff, num_experts, dropout)
                for _ in range(num_layers)
            ])
            self.cls_query = nn.Parameter(torch.randn(1, 1, d_model) * 0.02)
            self.cls_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout, batch_first=True)
            self.cls_norm = nn.LayerNorm(d_model)
            self.action_head = nn.Sequential(
                nn.Linear(d_model, d_model), nn.GELU(), nn.Dropout(dropout),
                nn.Linear(d_model, NUM_ACTIONS),
            )
            self.location_head = nn.Sequential(
                nn.Linear(d_model + NUM_ACTIONS, d_model), nn.GELU(), nn.Dropout(dropout),
                nn.Linear(d_model, NUM_LOCATIONS),
            )
            self.duration_head = nn.Sequential(
                nn.Linear(d_model + NUM_ACTIONS, d_model // 2), nn.GELU(),
                nn.Linear(d_model // 2, 1),
            )

        def forward(self, features):
            tokens = self.tokenizer(features)
            for layer in self.layers:
                tokens = layer(tokens)
            B = features.shape[0]
            cls = self.cls_query.expand(B, -1, -1)
            cls_out, _ = self.cls_attn(cls, tokens, tokens)
            h = self.cls_norm(cls_out.squeeze(1))
            action_logits = self.action_head(h)
            action_probs = F.softmax(action_logits.detach(), dim=-1)
            h_a = torch.cat([h, action_probs], dim=-1)
            location_logits = self.location_head(h_a)
            duration = torch.sigmoid(self.duration_head(h_a)) * 7.0 + 1.0
            return {
                "action_logits": action_logits,
                "location_logits": location_logits,
                "duration": duration.squeeze(-1),
            }

    return SociAgentTransformer()


# ══════════════════════════════════════════════════════════════════════════
# 6. Training
# ══════════════════════════════════════════════════════════════════════════

def train(
    epochs: int = 30,
    batch_size: int = 512,
    lr: float = 3e-4,
    num_train: int = 100_000,
    num_val: int = 10_000,
    data_dir: str | None = None,
    resume: bool = False,
):
    """Full training pipeline: generate/load data, train, export ONNX."""
    import torch
    import torch.nn as nn
    from torch.utils.data import Dataset, DataLoader

    DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    logger.info(f"Device: {DEVICE}")
    if DEVICE.type == "cuda":
        logger.info(f"GPU: {torch.cuda.get_device_name()}")

    MODEL_DIR.mkdir(parents=True, exist_ok=True)
    best_pt = MODEL_DIR / "soci_agent_best.pt"
    onnx_path = MODEL_DIR / "soci_agent.onnx"

    # ── Load / generate data ─────────────────────────────────────────
    collected = []
    source_counts: dict[str, int] = {}

    # Load collected samples from live sim (if available)
    samples_file = Path(data_dir) / "collected_samples.jsonl" if data_dir else SAMPLES_FILE
    if samples_file.exists():
        with open(samples_file) as f:
            for line in f:
                line = line.strip()
                if line:
                    sample = json.loads(line)
                    collected.append(sample)
                    src = sample.get("source", "unknown")
                    source_counts[src] = source_counts.get(src, 0) + 1
        logger.info(f"Loaded {len(collected):,} collected samples β€” sources: {source_counts}")

        # Oversample LLM-sourced data 3x (higher quality than NN/routine)
        llm_sources = {"gemini", "claude", "groq"}
        llm_samples = [s for s in collected if s.get("source", "") in llm_sources]
        if llm_samples:
            logger.info(f"Oversampling {len(llm_samples):,} LLM-sourced samples (3x weight)")
            collected.extend(llm_samples * 2)

    # Generate synthetic data to fill up to target size
    total_target = num_train + num_val
    synthetic_needed = max(0, total_target - len(collected))
    if synthetic_needed > 0:
        logger.info(f"Generating {synthetic_needed:,} synthetic samples...")
        random.seed(42)
        collected.extend(generate_dataset(synthetic_needed))

    random.shuffle(collected)
    split = int(len(collected) * 0.9)
    train_data = collected[:split]
    val_data = collected[split:]

    # ── Dataset ──────────────────────────────────────────────────────
    class ActionDataset(Dataset):
        def __init__(self, data):
            self.features = torch.tensor([d["features"] for d in data], dtype=torch.float32)
            self.actions = torch.tensor([d["action_idx"] for d in data], dtype=torch.long)
            self.locations = torch.tensor([d["target_loc_idx"] for d in data], dtype=torch.long)
            self.durations = torch.tensor([d["duration"] for d in data], dtype=torch.float32)

        def __len__(self):
            return len(self.actions)

        def __getitem__(self, idx):
            return {
                "features": self.features[idx],
                "action": self.actions[idx],
                "location": self.locations[idx],
                "duration": self.durations[idx],
            }

    train_ds = ActionDataset(train_data)
    val_ds = ActionDataset(val_data)
    train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True,
                              num_workers=0, pin_memory=(DEVICE.type == "cuda"))
    val_loader = DataLoader(val_ds, batch_size=1024, shuffle=False,
                            num_workers=0, pin_memory=(DEVICE.type == "cuda"))
    logger.info(f"Train: {len(train_ds):,}, Val: {len(val_ds):,}")

    # ── Model ────────────────────────────────────────────────────────
    model = build_model().to(DEVICE)

    total_params = sum(p.numel() for p in model.parameters())
    logger.info(f"Model parameters: {total_params:,} ({total_params * 4 / 1024 / 1024:.1f} MB fp32)")

    if resume and best_pt.exists():
        model.load_state_dict(torch.load(str(best_pt), map_location=DEVICE, weights_only=True))
        logger.info(f"Resumed from {best_pt}")

    # ── Class weights ────────────────────────────────────────────────
    action_counts = torch.zeros(NUM_ACTIONS)
    for d in train_data:
        action_counts[d["action_idx"]] += 1
    action_weights = 1.0 / (action_counts + 1.0)
    action_weights = action_weights / action_weights.sum() * NUM_ACTIONS
    action_weights = action_weights.to(DEVICE)

    logger.info("Action distribution:")
    for idx in range(NUM_ACTIONS):
        count = int(action_counts[idx])
        pct = count / len(train_data) * 100
        logger.info(f"  {ACTION_TYPES[idx]:>10s}: {count:6d} ({pct:.1f}%)")

    # ── Loss & optimizer ─────────────────────────────────────────────
    action_loss_fn = nn.CrossEntropyLoss(weight=action_weights)
    location_loss_fn = nn.CrossEntropyLoss()
    duration_loss_fn = nn.MSELoss()

    W_ACTION = 1.0
    W_LOCATION = 0.5
    W_DURATION = 0.2

    optimizer = torch.optim.AdamW(model.parameters(), lr=lr, weight_decay=1e-4)
    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=epochs, eta_min=1e-6)

    logger.info(f"Training for {epochs} epochs, LR={lr}, batch_size={batch_size}")

    # ── Training loop ────────────────────────────────────────────────
    best_val_acc = 0.0
    history = {"train_loss": [], "val_loss": [], "val_action_acc": [], "val_loc_acc": []}

    for epoch in range(epochs):
        # Train
        model.train()
        total_loss = 0.0
        n_batches = 0
        for batch in train_loader:
            feat = batch["features"].to(DEVICE)
            out = model(feat)
            loss = (
                W_ACTION * action_loss_fn(out["action_logits"], batch["action"].to(DEVICE))
                + W_LOCATION * location_loss_fn(out["location_logits"], batch["location"].to(DEVICE))
                + W_DURATION * duration_loss_fn(out["duration"], batch["duration"].to(DEVICE))
            )
            optimizer.zero_grad()
            loss.backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
            optimizer.step()
            total_loss += loss.item()
            n_batches += 1
        scheduler.step()
        avg_train_loss = total_loss / n_batches

        # Validate
        model.eval()
        val_loss = 0.0
        correct_action = 0
        correct_loc = 0
        total = 0
        with torch.no_grad():
            for batch in val_loader:
                feat = batch["features"].to(DEVICE)
                out = model(feat)
                loss = (
                    W_ACTION * action_loss_fn(out["action_logits"], batch["action"].to(DEVICE))
                    + W_LOCATION * location_loss_fn(out["location_logits"], batch["location"].to(DEVICE))
                    + W_DURATION * duration_loss_fn(out["duration"], batch["duration"].to(DEVICE))
                )
                val_loss += loss.item()
                pred_action = out["action_logits"].argmax(dim=-1)
                pred_loc = out["location_logits"].argmax(dim=-1)
                correct_action += (pred_action == batch["action"].to(DEVICE)).sum().item()
                correct_loc += (pred_loc == batch["location"].to(DEVICE)).sum().item()
                total += feat.shape[0]

        avg_val_loss = val_loss / len(val_loader)
        action_acc = correct_action / total if total > 0 else 0
        loc_acc = correct_loc / total if total > 0 else 0

        history["train_loss"].append(avg_train_loss)
        history["val_loss"].append(avg_val_loss)
        history["val_action_acc"].append(action_acc)
        history["val_loc_acc"].append(loc_acc)

        if action_acc > best_val_acc:
            best_val_acc = action_acc
            torch.save(model.state_dict(), str(best_pt))

        if (epoch + 1) % 5 == 0 or epoch == 0:
            lr_now = scheduler.get_last_lr()[0]
            logger.info(
                f"Epoch {epoch+1:3d}/{epochs} | "
                f"Train: {avg_train_loss:.4f} | "
                f"Val: {avg_val_loss:.4f} | "
                f"Act Acc: {action_acc:.1%} | "
                f"Loc Acc: {loc_acc:.1%} | "
                f"LR: {lr_now:.2e}"
            )

    logger.info(f"Best validation action accuracy: {best_val_acc:.1%}")

    # ── Per-action accuracy ──────────────────────────────────────────
    model.load_state_dict(torch.load(str(best_pt), map_location=DEVICE, weights_only=True))
    model.eval()
    cm = np.zeros((NUM_ACTIONS, NUM_ACTIONS), dtype=int)
    with torch.no_grad():
        for batch in val_loader:
            feat = batch["features"].to(DEVICE)
            out = model(feat)
            preds = out["action_logits"].argmax(dim=-1).cpu().numpy()
            labels = batch["action"].numpy()
            for p, l in zip(preds, labels):
                cm[l][p] += 1

    logger.info("Per-action accuracy:")
    for i, action in enumerate(ACTION_TYPES):
        row_total = cm[i].sum()
        correct = cm[i][i]
        acc = correct / row_total if row_total > 0 else 0
        logger.info(f"  {action:>10s}: {acc:.1%} ({correct}/{row_total})")

    # ── Test scenarios ───────────────────────────────────────────────
    import torch.nn.functional as F

    @torch.no_grad()
    def predict(persona, hour, minute, day, needs, mood, loc, num_people=0):
        features = encode_features(persona, hour, minute, day, needs, mood, loc, num_people)
        feat_t = torch.tensor([features], dtype=torch.float32, device=DEVICE)
        out = model(feat_t)
        action_probs = F.softmax(out["action_logits"][0] / 0.7, dim=-1)
        action_idx = action_probs.argmax().item()
        loc_idx = out["location_logits"][0].argmax().item()
        dur = max(1, min(8, round(out["duration"][0].item())))
        return ACTION_TYPES[action_idx], LOCATIONS[loc_idx], dur, action_probs[action_idx].item()

    logger.info("Test scenarios:")
    a, l, d, c = predict(PERSONAS[0], 0, 30, 5,
                         {"hunger": 0.5, "energy": 0.05, "social": 0.4, "purpose": 0.6, "comfort": 0.3, "fun": 0.3},
                         -0.3, "office")
    logger.info(f"  Elena midnight exhausted at office: {a} -> {l} ({d} ticks, {c:.0%})")

    a, l, d, c = predict(PERSONAS[2], 12, 30, 3,
                         {"hunger": 0.05, "energy": 0.7, "social": 0.5, "purpose": 0.6, "comfort": 0.5, "fun": 0.4},
                         0.2, "gym", 5)
    logger.info(f"  Marcus lunchtime starving at gym: {a} -> {l} ({d} ticks, {c:.0%})")

    a, l, d, c = predict(PERSONAS[8], 10, 0, 6,
                         {"hunger": 0.6, "energy": 0.7, "social": 0.5, "purpose": 0.5, "comfort": 0.7, "fun": 0.4},
                         0.5, "house_kai")
    logger.info(f"  Kai Saturday morning at home: {a} -> {l} ({d} ticks, {c:.0%})")

    # George (night shift) β€” should sleep during the day
    george = [p for p in PERSONAS if p["id"] == "george"][0]
    a, l, d, c = predict(george, 11, 0, 3,
                         {"hunger": 0.4, "energy": 0.15, "social": 0.5, "purpose": 0.7, "comfort": 0.5, "fun": 0.4},
                         -0.1, "house_frank")
    logger.info(f"  George midday after night shift: {a} -> {l} ({d} ticks, {c:.0%})")

    # Frank β€” evening at the bar
    frank = [p for p in PERSONAS if p["id"] == "frank"][0]
    a, l, d, c = predict(frank, 20, 0, 4,
                         {"hunger": 0.5, "energy": 0.4, "social": 0.3, "purpose": 0.6, "comfort": 0.5, "fun": 0.3},
                         0.1, "bar")
    logger.info(f"  Frank evening at the bar: {a} -> {l} ({d} ticks, {c:.0%})")

    # Priya β€” overworked at hospital
    priya = [p for p in PERSONAS if p["id"] == "priya"][0]
    a, l, d, c = predict(priya, 15, 0, 2,
                         {"hunger": 0.3, "energy": 0.2, "social": 0.3, "purpose": 0.8, "comfort": 0.4, "fun": 0.2},
                         -0.2, "hospital")
    logger.info(f"  Priya afternoon exhausted at hospital: {a} -> {l} ({d} ticks, {c:.0%})")

    # ── Export to ONNX ───────────────────────────────────────────────
    logger.info("Exporting to ONNX...")
    model.cpu().eval()
    dummy = torch.randn(1, FEATURE_DIM)
    torch.onnx.export(
        model, dummy, str(onnx_path),
        input_names=["features"],
        output_names=["action_logits", "location_logits", "duration"],
        dynamic_axes={"features": {0: "batch"}},
        opset_version=17,
        dynamo=False,
    )

    # Verify ONNX
    import onnx
    onnx_model = onnx.load(str(onnx_path))
    onnx.checker.check_model(onnx_model)
    onnx_size = onnx_path.stat().st_size / 1024
    logger.info(f"ONNX exported: {onnx_path} ({onnx_size:.0f} KB)")

    # Benchmark ONNX
    import onnxruntime as ort
    session = ort.InferenceSession(str(onnx_path))
    batch_input = np.random.randn(50, FEATURE_DIM).astype(np.float32)
    start = time.perf_counter()
    for _ in range(100):
        session.run(None, {"features": batch_input})
    elapsed = (time.perf_counter() - start) / 100
    logger.info(f"ONNX inference (50 agents): {elapsed*1000:.1f} ms per batch")

    # ── Save training stats ──────────────────────────────────────────
    stats = {
        "best_val_action_acc": best_val_acc,
        "epochs": epochs,
        "train_samples": len(train_ds),
        "val_samples": len(val_ds),
        "collected_samples": sum(source_counts.values()),
        "source_counts": source_counts,
        "model_size_kb": onnx_size,
        "timestamp": time.strftime("%Y-%m-%dT%H:%M:%S"),
        "history": history,
    }
    stats_path = MODEL_DIR / "training_stats.json"
    stats_path.write_text(json.dumps(stats, indent=2))
    logger.info(f"Stats saved to {stats_path}")

    # ── Plot training graphs ──────────────────────────────────────────
    plot_training_graphs(stats_path)

    return best_val_acc


def plot_training_graphs(stats_path: Path | str | None = None):
    """Plot training loss and accuracy curves from saved training stats.

    Saves the plot to models/training_graphs.png and displays it.
    """
    import matplotlib
    matplotlib.use("Agg")  # non-interactive backend as fallback
    import matplotlib.pyplot as plt

    stats_path = Path(stats_path) if stats_path else MODEL_DIR / "training_stats.json"
    if not stats_path.exists():
        logger.error(f"No training stats found at {stats_path}")
        return

    stats = json.loads(stats_path.read_text())
    history = stats.get("history", {})

    train_loss = history.get("train_loss", [])
    val_loss = history.get("val_loss", [])
    val_action_acc = history.get("val_action_acc", [])
    val_loc_acc = history.get("val_loc_acc", [])

    if not train_loss:
        logger.error("No training history found in stats file")
        return

    epochs_range = list(range(1, len(train_loss) + 1))

    fig, axes = plt.subplots(1, 3, figsize=(18, 5))
    fig.suptitle(
        f"Soci Agent NN Training β€” {stats.get('timestamp', '?')}  |  "
        f"Best Action Acc: {stats.get('best_val_action_acc', 0):.1%}",
        fontsize=13, fontweight="bold",
    )

    # Loss curves
    ax = axes[0]
    ax.plot(epochs_range, train_loss, label="Train Loss", color="#2196F3", linewidth=2)
    ax.plot(epochs_range, val_loss, label="Val Loss", color="#F44336", linewidth=2)
    ax.set_xlabel("Epoch")
    ax.set_ylabel("Loss")
    ax.set_title("Training & Validation Loss")
    ax.legend()
    ax.grid(True, alpha=0.3)
    ax.set_xlim(1, len(train_loss))

    # Action accuracy
    ax = axes[1]
    ax.plot(epochs_range, [a * 100 for a in val_action_acc], label="Action Accuracy",
            color="#4CAF50", linewidth=2)
    best_epoch = int(np.argmax(val_action_acc)) + 1
    best_acc = max(val_action_acc) * 100
    ax.axhline(y=best_acc, color="#4CAF50", linestyle="--", alpha=0.4)
    ax.annotate(f"Best: {best_acc:.1f}% (epoch {best_epoch})",
                xy=(best_epoch, best_acc), fontsize=9,
                xytext=(best_epoch + 1, best_acc - 3),
                arrowprops=dict(arrowstyle="->", color="#4CAF50"),
                color="#4CAF50")
    ax.set_xlabel("Epoch")
    ax.set_ylabel("Accuracy (%)")
    ax.set_title("Action Prediction Accuracy")
    ax.legend()
    ax.grid(True, alpha=0.3)
    ax.set_xlim(1, len(train_loss))

    # Location accuracy
    ax = axes[2]
    if val_loc_acc:
        ax.plot(epochs_range, [a * 100 for a in val_loc_acc], label="Location Accuracy",
                color="#FF9800", linewidth=2)
        best_loc_epoch = int(np.argmax(val_loc_acc)) + 1
        best_loc = max(val_loc_acc) * 100
        ax.axhline(y=best_loc, color="#FF9800", linestyle="--", alpha=0.4)
        ax.annotate(f"Best: {best_loc:.1f}% (epoch {best_loc_epoch})",
                    xy=(best_loc_epoch, best_loc), fontsize=9,
                    xytext=(best_loc_epoch + 1, best_loc - 3),
                    arrowprops=dict(arrowstyle="->", color="#FF9800"),
                    color="#FF9800")
    ax.set_xlabel("Epoch")
    ax.set_ylabel("Accuracy (%)")
    ax.set_title("Location Prediction Accuracy")
    ax.legend()
    ax.grid(True, alpha=0.3)
    ax.set_xlim(1, len(train_loss))

    # Footer with training info
    footer = (
        f"Train: {stats.get('train_samples', '?'):,} samples  |  "
        f"Val: {stats.get('val_samples', '?'):,} samples  |  "
        f"Collected: {stats.get('collected_samples', 0):,}  |  "
        f"Model: {stats.get('model_size_kb', 0):.0f} KB"
    )
    fig.text(0.5, 0.01, footer, ha="center", fontsize=9, color="gray")

    plt.tight_layout(rect=[0, 0.03, 1, 0.95])

    graph_path = MODEL_DIR / "training_graphs.png"
    fig.savefig(str(graph_path), dpi=150, bbox_inches="tight")
    logger.info(f"Training graphs saved to {graph_path}")

    # Try to display interactively
    try:
        import warnings
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            matplotlib.use("TkAgg")
            plt.show(block=False)
            plt.pause(0.5)
    except Exception:
        pass  # headless environment, PNG saved is enough

    plt.close(fig)


def _push_to_hub(best_pt, onnx_path, stats_path, repo_id, best_val_acc, epochs, num_train,
                 base_url: str = "https://raymelius-soci2.hf.space"):
    """Upload model files to HuggingFace Hub, then trigger live reload."""
    from huggingface_hub import HfApi, login

    token = os.environ.get("HF_TOKEN", "")
    if not token:
        logger.error("HF_TOKEN not set β€” cannot push. Export it: export HF_TOKEN=hf_...")
        return

    login(token=token)
    api = HfApi()
    api.create_repo(repo_id, exist_ok=True)

    # Config
    config = {
        "architecture": "SociAgentTransformer",
        "d_model": 128, "nhead": 8, "num_layers": 4, "d_ff": 256, "num_experts": 4,
        "feature_dim": FEATURE_DIM, "num_actions": NUM_ACTIONS, "num_locations": NUM_LOCATIONS,
        "action_types": ACTION_TYPES, "locations": LOCATIONS,
        "action_durations": ACTION_DURATIONS, "need_names": NEED_NAMES,
        "personality_names": PERSONALITY_NAMES,
        "best_val_action_acc": best_val_acc,
        "training_samples": num_train, "epochs": epochs,
    }
    config_path = MODEL_DIR / "config.json"
    config_path.write_text(json.dumps(config, indent=2))

    for local, remote in [
        (onnx_path, "soci_agent.onnx"),
        (best_pt, "soci_agent_best.pt"),
        (config_path, "config.json"),
        (stats_path, "training_stats.json"),
    ]:
        if local.exists():
            api.upload_file(
                path_or_fileobj=str(local),
                path_in_repo=remote,
                repo_id=repo_id,
                commit_message=f"Train: acc={best_val_acc:.1%}, {epochs} epochs",
            )
            logger.info(f"Uploaded {remote}")

    logger.info(f"Model pushed to https://huggingface.co/{repo_id}")

    # Trigger hot-reload on the live simulation
    try:
        import httpx
        resp = httpx.post(f"{base_url}/api/nn/reload", timeout=30.0)
        if resp.status_code == 200:
            logger.info(f"Live sim NN reloaded: {resp.json().get('message', 'ok')}")
        else:
            logger.warning(f"Could not reload live sim NN: HTTP {resp.status_code}")
    except Exception as e:
        logger.warning(f"Could not reach live sim for reload: {e}")


# ══════════════════════════════════════════════════════════════════════════
# CLI
# ══════════════════════════════════════════════════════════════════════════

def main():
    parser = argparse.ArgumentParser(
        description="Soci Agent NN β€” Local Training Script",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""Examples:
  python scripts/nn_train.py                             # Train from scratch
  python scripts/nn_train.py --resume --epochs 50        # Continue training
  python scripts/nn_train.py --data data/nn_training     # Use collected samples
  python scripts/nn_train.py --push                      # Push existing model to HF Hub
  python scripts/nn_train.py --graph                     # Show graphs from last training
""",
    )
    parser.add_argument("--epochs", type=int, default=30, help="Training epochs (default: 30)")
    parser.add_argument("--batch-size", type=int, default=512, help="Batch size (default: 512)")
    parser.add_argument("--lr", type=float, default=3e-4, help="Learning rate (default: 3e-4)")
    parser.add_argument("--train-samples", type=int, default=100_000,
                        help="Number of synthetic training samples (default: 100000)")
    parser.add_argument("--val-samples", type=int, default=10_000,
                        help="Number of validation samples (default: 10000)")
    parser.add_argument("--data", type=str, default=None,
                        help="Path to directory with collected_samples.jsonl")
    parser.add_argument("--resume", action="store_true",
                        help="Resume from existing weights in models/")
    parser.add_argument("--push", action="store_true",
                        help="Push existing model to HuggingFace Hub (no training)")
    parser.add_argument("--graph", action="store_true",
                        help="Display training graphs from last training run")
    parser.add_argument("--repo", default="RayMelius/soci-agent-nn",
                        help="HF Hub repo ID (default: RayMelius/soci-agent-nn)")
    parser.add_argument("--url", default="https://raymelius-soci2.hf.space",
                        help="Live simulation URL for hot-reload after push (default: HF Space)")
    args = parser.parse_args()

    # --graph: just display graphs and exit
    if args.graph:
        plot_training_graphs()
        return

    # --push: just push existing model to HF Hub and exit
    if args.push:
        stats_path = MODEL_DIR / "training_stats.json"
        best_pt = MODEL_DIR / "soci_agent_best.pt"
        onnx_path = MODEL_DIR / "soci_agent.onnx"
        if stats_path.exists():
            stats = json.loads(stats_path.read_text())
            best_val_acc = stats.get("best_val_action_acc", 0)
            ep = stats.get("epochs", 0)
            n_train = stats.get("train_samples", 0)
        else:
            best_val_acc, ep, n_train = 0, 0, 0
        _push_to_hub(best_pt, onnx_path, stats_path, args.repo, best_val_acc, ep, n_train,
                     base_url=args.url)
        return

    # Default: train
    train(
        epochs=args.epochs,
        batch_size=args.batch_size,
        lr=args.lr,
        num_train=args.train_samples,
        num_val=args.val_samples,
        data_dir=args.data,
        resume=args.resume,
    )


if __name__ == "__main__":
    main()