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
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
/*
Appellation: impl_store <module>
Contrib: @FL03
*/
use crate::features::{FeaturePattern, FeatureRelationship, PersistentFeature, RelationshipType};
use crate::ledger::TopoLedger;
use eryon::{Direction, Head, LearnedRule, State};
use num_traits::{Float, FromPrimitive, NumAssign, ToPrimitive};
use rstmt::PitchMod;
use rstmt::nrt::{LPR, Triad, Triads};
use std::collections::{HashMap, HashSet};
impl<T> TopoLedger<T>
where
T: Float + FromPrimitive + ToPrimitive,
{
/// Add a relationship between features
pub fn add_relationship(
&mut self,
from_id: usize,
to_id: usize,
relationship_type: RelationshipType,
strength: T,
) {
// Check if this relationship already exists
if let Some(rel) = self.relationships_mut().iter_mut().find(|r| {
r.source_id == from_id && r.to_id == to_id && r.relationship_type == relationship_type
}) {
// Update existing relationship
rel.occurrences += 1;
rel.strength = rel.strength.max(strength);
return;
}
// Create new relationship
let relationship = FeatureRelationship {
source_id: from_id,
to_id,
relationship_type,
strength,
occurrences: 1,
};
self.relationships.push(relationship);
}
/// Dynamically adjust feature importance based on usage
pub fn adjust_feature_importance(&mut self, feature_id: usize, adjustment: T)
where
T: core::iter::Sum,
{
let scale = T::from_usize(10).unwrap();
if let Some(feature) = self.features.iter_mut().find(|f| f.id == feature_id) {
// Remove from current importance bucket
if let Some(bucket) = self
.index
.importance_index_mut()
.get_mut(&((feature.importance * scale).to_usize().unwrap()))
{
bucket.remove(&feature_id);
}
// Adjust importance with limits
feature.importance = (feature.importance + adjustment)
.max(T::zero())
.min(T::one());
// Add to new importance bucket
let new_bucket = (feature.importance * scale).to_usize().unwrap();
self.importance_index_mut()
.entry(new_bucket)
.or_default()
.insert(feature_id);
}
}
/// Analyze transformation frequency by chord class
pub fn analyze_transformation_by_class(&self) -> HashMap<Triads, HashMap<LPR, T>>
where
T: core::iter::Sum + NumAssign,
{
use strum::IntoEnumIterator;
// Initialize with all classes and transforms
let mut results: HashMap<_, _> = Triads::iter()
.map(|class| {
let transforms = LPR::iter().map(|cls| (cls, T::zero())).collect();
(class, transforms)
})
.collect();
// Get only active transformation features (dimension 1)
let transform_features: Vec<_> = self
.features
.iter()
.filter(|f| f.dimension == 1 && f.death.is_none() && f.content.len() >= 7)
.collect();
if transform_features.is_empty() {
return results;
}
// Process transformations in a single pass
for feature in transform_features {
// Ensure we have enough data for a valid transformation
if feature.content.len() < 7 {
continue;
}
// Get source notes and try to determine class directly
if let Ok(source_class) =
Triads::try_from_arr([feature.content[0], feature.content[1], feature.content[2]])
{
// Get the transformation type
if let Some(&transform_type) = feature.content.get(6) {
if transform_type <= 2 {
// Valid LPR type
// Convert to LPR type
let transform = LPR::from(transform_type);
// Increment the count for this class+transform
if let Some(transform_map) = results.get_mut(&source_class) {
if let Some(count) = transform_map.get_mut(&transform) {
*count += feature.importance;
}
}
}
}
}
}
// Normalize counts to frequencies
for (_, transforms) in results.iter_mut() {
let total = transforms.values().copied().sum();
if total > T::zero() {
for count in transforms.values_mut() {
*count /= total;
}
}
}
results
}
/// Optimize memory usage by compacting
pub fn compact(&mut self) {
// Skip if there are no dead features
if !self.features.iter().any(|f| f.death.is_some()) {
return;
}
// Remove features that have been dead for a while
let min_death_time = if self.current_epoch() > 100 {
self.current_epoch() - 100
} else {
0
};
self.features
.retain(|f| f.death.is_none() || f.death.unwrap() > min_death_time);
// Rebuild all indices
self.rebuild_indices();
}
/// Calculate similarity between two content vectors
pub fn content_similarity(&self, content1: &[usize], content2: &[usize]) -> T {
if content1.is_empty() || content2.is_empty() {
return T::zero();
}
// Calculate intersection size
let mut common = 0;
for &item in content1 {
if content2.contains(&item) {
common += 1;
}
}
// Jaccard similarity
let union = content1.len() + content2.len() - common;
if union == 0 {
T::one()
} else {
T::from_usize(common).unwrap() / T::from_usize(union).unwrap()
}
}
/// Create a new feature and add it to memory
pub fn create_feature(&mut self, dimension: usize, content: Vec<usize>) -> usize
where
T: ToPrimitive,
{
let id = self.next_feature_id();
// Default importance based on dimension (can be refined later)
let importance = match dimension {
0 => 0.3, // Points
1 => 0.5, // Edges/transformations
2 => 0.7, // Triads
_ => 0.4, // Other structures
};
let importance = T::from_f32(importance).unwrap();
let feature = PersistentFeature {
id,
birth: self.current_epoch(),
death: None,
dimension,
content: content.clone(),
importance,
occurrences: 1,
};
// Update indices
self.dimension_index_mut()
.entry(dimension)
.or_default()
.insert(id);
self.content_index_mut()
.entry(content)
.or_default()
.insert(id);
let importance_bucket = (importance * T::from_usize(10).unwrap())
.to_usize()
.unwrap();
self.importance_index_mut()
.entry(importance_bucket)
.or_default()
.insert(id);
self.features.push(feature);
id
}
/// Batch create multiple features
pub fn create_features_batch(&mut self, features: Vec<(usize, Vec<usize>)>) -> Vec<usize> {
let mut ids = Vec::with_capacity(features.len());
for (dimension, content) in features {
let id = self.create_feature(dimension, content);
ids.push(id);
}
ids
}
/// Create persistent features from temporal patterns
pub fn create_persistent_features(
&mut self,
patterns: &[Vec<usize>],
) -> crate::Result<Vec<usize>> {
let mut feature_ids = Vec::new();
// Compute filtration of the pattern space
let mut filtration = Vec::new();
for (i, pattern) in patterns.iter().enumerate() {
for window_size in 1..=pattern.len() {
for window in pattern.windows(window_size) {
// Add simplex to filtration with birth time based on position
filtration.push((
window.to_vec(),
T::from_usize(i).unwrap() / T::from_usize(patterns.len()).unwrap(),
));
}
}
}
// Sort filtration by birth time
filtration.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(core::cmp::Ordering::Equal));
// Track persistent features
let mut active_features = HashMap::new();
for (simplex, birth_time) in filtration {
// Calculate boundary
if simplex.len() > 1 {
// Record feature with persistence
let feature_id = self.create_feature(simplex.len() - 1, simplex);
active_features.insert(feature_id, birth_time);
feature_ids.push(feature_id);
}
}
Ok(feature_ids)
}
/// Efficient feature deletion with object pooling
pub fn delete_feature(&mut self, id: usize) -> bool {
// get a copy of the current epoch;
let epoch = self.current_epoch();
// Find the feature to mark as deleted
if let Some(feature) = self.features.iter_mut().find(|f| f.id == id) {
// Mark as dead instead of removing
feature.death = Some(epoch);
// Remove from indices but keep in features array
if let Some(dim_set) = self.index.dimension_index_mut().get_mut(&feature.dimension) {
dim_set.remove(&id);
}
true
} else {
false
}
}
/// Detect patterns in the recorded transformations
pub fn detect_patterns(&mut self) {
// Get transformation features
let transform_features = self.find_by_dimension(1);
if transform_features.len() < 3 {
return; // Need at least 3 transforms for meaningful patterns
}
// Extract transformation types (LPR values)
let transforms: Vec<usize> = transform_features
.iter()
.filter(|f| f.death.is_none() && f.content.len() > 6)
.map(|f| f.content[6]) // Last element is transform type
.collect();
// Look for repeating sequences (length 2-4)
for pattern_len in 2..=std::cmp::min(4, transforms.len() / 2) {
for i in 0..=(transforms.len() - pattern_len * 2) {
let pattern = &transforms[i..(i + pattern_len)];
// Look for repetition
let mut occurrences = 0;
for j in 0..=(transforms.len() - pattern_len) {
if transforms[j..(j + pattern_len)] == pattern[0..pattern_len] {
occurrences += 1;
}
}
// If pattern occurs at least twice
if occurrences >= 2 {
// Check if pattern already exists
let pattern_vec = pattern.to_vec();
let pattern_exists = self.patterns.iter().any(|p| p.sequence == pattern_vec);
if !pattern_exists {
// Create new pattern
let pattern_obj = FeaturePattern {
id: self.next_pattern_id(),
sequence: pattern_vec,
occurrences,
importance: T::from_usize(2).unwrap().recip(), // Initial importance
};
self.patterns.push(pattern_obj);
} else {
// Update existing pattern
if let Some(existing) =
self.patterns.iter_mut().find(|p| p.sequence == pattern_vec)
{
existing.occurrences = occurrences;
existing.update_importance_by_occurrences();
}
}
}
}
}
}
/// Detect patterns using the KMP algorithm for efficient matching
pub fn detect_patterns_kmp(&mut self) {
// Get the most recent transformation features (up to 20)
let transform_features = self
.features
.iter()
.filter(|f| f.dimension == 1 && f.death.is_none())
.collect::<Vec<_>>();
if transform_features.len() < 3 {
return; // Need at least 3 transforms for meaningful patterns
}
// Extract actual transformation types (LPR values) from features for more meaningful patterns
// Safely access the last element
let transforms = transform_features
.iter()
.filter_map(|feature| {
if feature.content.len() > 6 {
feature.content.get(6).copied() // Last element is the transform type (LPR)
} else {
None
}
})
.collect::<Vec<_>>();
// Look for patterns of different lengths (capped by the data size)
for pattern_len in 2..=3.min(transforms.len() / 2) {
// Check each possible pattern starting position
'outer: for i in 0..=(transforms.len() - pattern_len) {
if i + pattern_len > transforms.len() {
break;
}
let pattern = &transforms[i..(i + pattern_len)];
// Prevent overflow by checking bounds
if pattern.is_empty() || pattern.len() > transforms.len() {
continue 'outer;
}
// Use KMP-inspired approach for matches
let mut count = 0;
let mut pos = 0;
while pos <= transforms.len() - pattern.len() {
let mut found = true;
for j in 0..pattern.len() {
if pos + j >= transforms.len() || transforms[pos + j] != pattern[j] {
found = false;
break;
}
}
if found {
count += 1;
pos += pattern.len(); // Skip the entire pattern
} else {
pos += 1; // Move one position
}
}
// If pattern occurs more than once
if count >= 2 {
// Create or update the pattern
self.record_pattern(pattern);
}
}
}
}
/// Extract significant patterns as learning targets
pub fn extract_learning_targets(&self, min_importance: T) -> Vec<Vec<usize>> {
// Get the most important patterns
let mut important_patterns = self
.patterns
.iter()
.filter(|p| p.importance >= min_importance)
.collect::<Vec<_>>();
// Sort by importance (highest first)
important_patterns.sort_by(|a, b| {
b.importance
.partial_cmp(&a.importance)
.unwrap_or(std::cmp::Ordering::Equal)
});
// Return the sequences
important_patterns
.into_iter()
.map(|p| p.sequence.clone())
.collect()
}
/// Extract patterns with persistent homology analysis
pub fn extract_persistent_patterns(
&mut self,
persistence_threshold: T,
) -> Vec<FeaturePattern<T>> {
// Create a filtration from features
let mut filtration = Vec::new();
// Add vertices (0-dim features)
for feature in self
.features
.iter()
.filter(|f| f.dimension == 0 && f.death.is_none())
{
filtration.push((feature.importance, feature.id, 0, Vec::<usize>::new()));
}
// Add edges from relationships (1-dim features)
for rel in &self.relationships {
// Only consider active features
if self
.features
.iter()
.any(|f| f.id == rel.source_id && f.death.is_none())
&& self
.features
.iter()
.any(|f| f.id == rel.to_id && f.death.is_none())
{
filtration.push((
rel.strength,
self.state.next_feature_id(),
1,
vec![rel.source_id, rel.to_id],
));
}
}
// Sort by importance/strength (ascending for filtration)
filtration.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(core::cmp::Ordering::Equal));
// Compute persistence pairs
let mut persistent_patterns = Vec::new();
let mut active_simplices: HashMap<usize, Vec<usize>> = HashMap::new();
// Simulate persistence computation (simplified)
for (value, id, dimension, boundary) in filtration.clone() {
if dimension == 0 {
// Add vertex
active_simplices.insert(id, Vec::new());
} else {
// Process edge
// Find connected components that would merge
let mut components_to_merge = Vec::new();
for &vertex_id in &boundary {
for (&comp_id, vertices) in &active_simplices {
if vertices.contains(&vertex_id) {
components_to_merge.push(comp_id);
break;
}
}
}
// If this edge creates a persistent feature
if components_to_merge.len() == 2 {
let birth_value = filtration
.iter()
.find(|(_, id, _, _)| *id == components_to_merge[0])
.map(|(v, _, _, _)| *v)
.unwrap_or(T::zero());
let persistence = value - birth_value;
if persistence > persistence_threshold {
// Extract the edge sequence that forms this persistent feature
let pattern_sequence = self.extract_pattern_from_edge(&boundary);
if !pattern_sequence.is_empty() {
persistent_patterns.push(FeaturePattern {
id: self.next_pattern_id(),
sequence: pattern_sequence,
occurrences: 1,
importance: persistence,
});
}
}
// Merge components
let comp1 = components_to_merge[0];
let comp2 = components_to_merge[1];
if let Some(mut vertices) = active_simplices.remove(&comp2) {
if let Some(comp1_vertices) = active_simplices.get_mut(&comp1) {
comp1_vertices.append(&mut vertices);
}
}
}
}
}
persistent_patterns
}
/// Extract a pattern from edge boundary
fn extract_pattern_from_edge(&self, boundary: &[usize]) -> Vec<usize> {
// Map boundary vertices to related transformations
self.features
.iter()
.filter(|f| f.dimension == 1 && f.content.len() > 6)
.filter(|f| boundary.contains(&f.id))
.filter_map(|f| f.content.get(6).copied())
.collect::<Vec<_>>()
}
/// Find features with content matching a prefix
pub fn find_by_content_prefix(&self, prefix: &[usize]) -> Vec<&PersistentFeature<T>> {
if prefix.is_empty() {
return Vec::new();
}
self.content_index()
.iter()
.filter(|(content, _)| {
content.len() >= prefix.len() && content.iter().take(prefix.len()).eq(prefix.iter())
})
.flat_map(|(_, ids)| {
ids.iter().filter_map(|&id| {
self.features
.iter()
.find(|f| f.id == id && f.death.is_none())
})
})
.collect()
}
/// Find features by time range
pub fn find_by_time_window(
&self,
start_time: usize,
end_time: usize,
) -> Vec<&PersistentFeature<T>> {
self.features
.iter()
.filter(|f| f.birth >= start_time && f.birth <= end_time && f.death.is_none())
.collect()
}
/// Find features by dimension
pub fn find_by_dimension(&self, dimension: usize) -> Vec<&PersistentFeature<T>> {
match self.dimension_index().get(&dimension) {
Some(ids) => ids
.iter()
.filter_map(|&id| self.features.iter().find(|f| f.id == id))
.collect(),
None => Vec::new(),
}
}
/// Find features by minimum importance
pub fn find_by_importance(&self, min_importance: T) -> Vec<&PersistentFeature<T>> {
let min_bucket = (min_importance * T::from_usize(10).unwrap())
.to_usize()
.unwrap();
self.importance_index()
.iter()
.filter(|&(&bucket, _)| bucket >= min_bucket)
.flat_map(|(_, ids)| {
ids.iter()
.filter_map(|&id| self.features.iter().find(|f| f.id == id))
})
.collect()
}
/// Analyze the graph structure to find central features
pub fn find_central_features(&self, limit: usize) -> Vec<(usize, usize)> {
// Build adjacency map from relationships
let mut adjacency = HashMap::<usize, HashSet<usize>>::new();
for rel in &self.relationships {
adjacency
.entry(rel.source_id)
.or_default()
.insert(rel.to_id);
adjacency
.entry(rel.to_id)
.or_default()
.insert(rel.source_id);
}
// Calculate degree centrality for each feature
let mut centrality = adjacency
.iter()
.map(|(&id, connections)| {
// Only count if feature is still active
if self
.features
.iter()
.any(|f| f.id == id && f.death.is_none())
{
(id, connections.len())
} else {
(id, 0)
}
})
.collect::<Vec<_>>();
// Sort by centrality (degree), descending
centrality.sort_by(|a, b| b.1.cmp(&a.1));
centrality.into_iter().take(limit).collect()
}
/// Find events of a specific type
pub fn find_events(&self, event_type: &str) -> Vec<&PersistentFeature<T>> {
let event_code = event_type.bytes().fold(0, |acc, b| acc ^ b as usize);
self.features
.iter()
.filter(|f| f.dimension == 0 && f.content.first() == Some(&event_code))
.collect()
}
/// Find a feature by its exact content
pub fn find_feature_by_content(&self, content: &[usize]) -> Option<usize> {
if let Some(ids) = self.content_index().get(content) {
ids.iter().find_map(|&id| {
self.features
.iter()
.find(|f| f.id == id && f.death.is_none())
.map(|feature| feature.id())
})
} else {
None
}
}
pub fn find_feature_by_id(&self, id: usize) -> Option<&PersistentFeature<T>> {
self.features.iter().find(|f| f.id == id)
}
/// Find patterns that match or contain the given sequence
pub fn find_matching_patterns(&self, sequence: &[usize]) -> Vec<&FeaturePattern<T>> {
if sequence.is_empty() || self.patterns.is_empty() {
return Vec::new();
}
self.patterns
.iter()
.filter(|pattern| {
// Check if sequence is a prefix of the pattern
if sequence.len() <= pattern.sequence.len() {
pattern.sequence.starts_with(sequence)
} else {
// Or check if pattern is contained within the sequence
for i in 0..=(sequence.len() - pattern.sequence.len()) {
if pattern.sequence == sequence[i..(i + pattern.sequence.len())] {
return true;
}
}
false
}
})
.collect()
}
/// Find harmonic pathways between triads
pub fn find_harmonic_pathway(
&self,
from_notes: &[usize; 3],
to_notes: &[usize; 3],
max_length: usize,
) -> Option<Vec<LPR>> {
// Convert note vectors to content format
let from_content = from_notes.to_vec();
let to_content = to_notes.to_vec();
// Find features for these triads
let from_id = if let Some(ids) = self.content_index().get(&from_content) {
*ids.iter().next()?
} else {
return None;
};
let to_id = if let Some(ids) = self.content_index().get(&to_content) {
*ids.iter().next()?
} else {
return None;
};
// Find path between the two features using breadth-first search
let mut queue = std::collections::VecDeque::new();
let mut visited = HashSet::new();
let mut predecessors = HashMap::new();
queue.push_back(from_id);
visited.insert(from_id);
while let Some(current) = queue.pop_front() {
if current == to_id {
// Path found, reconstruct it
let mut path = Vec::new();
let mut current_id = current;
while current_id != from_id {
let (pred_id, transform) = predecessors.get(¤t_id)?;
path.push(*transform);
current_id = *pred_id;
}
// Reverse path since we built it backwards
path.reverse();
// Convert indices to LPR values
let lpr_path = path.into_iter().map(LPR::from).collect();
return Some(lpr_path);
}
// Explore neighbors
for rel in &self.relationships {
if rel.source_id == current && !visited.contains(&rel.to_id) {
// Find the transformation that connects these triads
let transform_features = self
.features
.iter()
.filter(|f| f.dimension == 1 && f.content.len() > 6)
.filter(|f| {
// Check if feature connects our triads
let from_match = self.features.iter().any(|triad| {
triad.id == current
&& triad.content.len() >= 3
&& f.content[0] == triad.content[0]
&& f.content[1] == triad.content[1]
&& f.content[2] == triad.content[2]
});
let to_match = self.features.iter().any(|triad| {
triad.id == rel.to_id
&& triad.content.len() >= 3
&& f.content[3] == triad.content[0]
&& f.content[4] == triad.content[1]
&& f.content[5] == triad.content[2]
});
from_match && to_match
})
.collect::<Vec<_>>();
if let Some(transform) = transform_features.first() {
if let Some(&transform_type) = transform.content.get(6) {
queue.push_back(rel.to_id);
visited.insert(rel.to_id);
predecessors.insert(rel.to_id, (current, transform_type));
// Stop if path gets too long
if visited.len() > max_length {
return None;
}
}
}
}
// Also check reverse direction
if rel.to_id == current && !visited.contains(&rel.source_id) {
// Similar logic for reverse transforms...
let transform_features = self
.features
.iter()
.filter(|f| f.dimension == 1 && f.content.len() > 6)
.filter(|f| {
let from_match = self.features.iter().any(|triad| {
triad.id == rel.source_id
&& triad.content.len() >= 3
&& f.content[0] == triad.content[0]
&& f.content[1] == triad.content[1]
&& f.content[2] == triad.content[2]
});
let to_match = self.features.iter().any(|triad| {
triad.id == current
&& triad.content.len() >= 3
&& f.content[3] == triad.content[0]
&& f.content[4] == triad.content[1]
&& f.content[5] == triad.content[2]
});
from_match && to_match
})
.collect::<Vec<_>>();
if let Some(transform) = transform_features.first() {
if let Some(&transform_type) = transform.content.get(6) {
queue.push_back(rel.source_id);
visited.insert(rel.source_id);
predecessors.insert(rel.source_id, (current, transform_type));
if visited.len() > max_length {
return None;
}
}
}
}
}
}
// No path found
None
}
/// Find structural isomorphisms between different pattern regions
pub fn find_isomorphisms(&self, min_size: usize) -> Vec<(Vec<usize>, Vec<usize>, T)>
where
T: core::iter::Sum,
{
// Find regions of connected features
let regions = self.identify_memory_regions(T::from_usize(2).unwrap().recip());
let mut isomorphisms = Vec::new();
// Compare each pair of regions
for (i, region1) in regions.iter().enumerate() {
if region1.len() < min_size {
continue;
}
for region2 in regions.iter().skip(i + 1) {
if region2.len() < min_size {
continue;
}
// Check for structural similarity
let similarity = self.region_structural_similarity(region1, region2);
// If similarity is high enough, record the isomorphism
if similarity > T::from_f32(0.7).unwrap() {
isomorphisms.push((region1.clone(), region2.clone(), similarity));
}
}
}
// Sort by similarity
isomorphisms.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));
isomorphisms
}
/// Find features with related content (partial overlap)
pub fn find_related_content(
&self,
content: &[usize],
min_overlap: usize,
) -> Vec<&PersistentFeature<T>> {
if content.is_empty() || min_overlap == 0 {
return Vec::new();
}
self.features
.iter()
.filter(|f| {
// Skip dead features
if f.death.is_some() {
return false;
}
// Count overlapping elements
let overlap_count = content
.iter()
.filter(|&item| f.content.contains(item))
.count();
overlap_count >= min_overlap
})
.collect()
}
/// Find features by their relationships
pub fn find_related_features(
&self,
feature_id: usize,
relationship_type: Option<RelationshipType>,
) -> Vec<&PersistentFeature<T>> {
// First find all relationships involving this feature
let related_ids = self
.relationships
.iter()
.filter(|rel| {
// Filter by relationship type if specified
relationship_type.is_none_or(|rt| rel.relationship_type == rt) &&
// Source or target matches the feature ID
(rel.source_id == feature_id || rel.to_id == feature_id)
})
.map(|rel| {
if rel.source_id == feature_id {
rel.to_id
} else {
rel.source_id
}
})
.collect::<HashSet<_>>();
// Then find the actual features
self.features
.iter()
.filter(|f| related_ids.contains(&f.id) && f.death.is_none())
.collect()
}
pub fn find_rule(&self, rule: LearnedRule<T, usize, usize>) -> Option<usize> {
let content = vec![
3,
**rule.head().state,
*rule.head().symbol,
**rule.tail().state,
*rule.tail().symbol,
(rule.tail.direction as usize) + 1,
];
self.find_feature_by_content(&content)
}
/// filter the rulespace by the head of the rule
pub fn find_rule_by_head(&self, head: Head<&usize, &usize>) -> Option<&LearnedRule<T>> {
self.rulespace.iter().find(|rule| rule.head() == head)
}
/// Generate a continuation of a given transformation sequence
pub fn generate_continuation(&self, seed: &[usize], length: usize) -> Vec<usize> {
let mut result = seed.to_vec();
// Generate continuations up to desired length
while result.len() < length {
if let Some((next, _)) = self.predict_next_transformation(&result) {
result.push(next);
} else {
break; // No more predictions possible
}
}
result
}
/// Generate a compact representation of the pattern network
pub fn generate_pattern_network(&self) -> String
where
T: core::fmt::Debug,
{
let mut json = String::from("{\n \"nodes\": [\n");
// Add pattern nodes
for (i, pattern) in self.patterns.iter().enumerate() {
let seq_str = pattern
.sequence
.iter()
.map(|&x| x.to_string())
.collect::<Vec<_>>()
.join(",");
json.push_str(&format!(
" {{\"id\": {}, \"sequence\": [{}], \"importance\": {:?}}}",
pattern.id, seq_str, pattern.importance
));
if i < self.patterns.len() - 1 {
json.push_str(",\n");
} else {
json.push('\n');
}
}
json.push_str(" ],\n \"links\": [\n");
// Find pattern relationships
let mut links = Vec::new();
for (i, p1) in self.patterns.iter().enumerate() {
for (j, p2) in self.patterns.iter().enumerate() {
if i >= j {
continue;
}
// Check for shared subsequences
let similarity = Self::sequence_similarity(&p1.sequence, &p2.sequence);
if similarity > T::from_usize(2).unwrap().recip() {
links.push((p1.id, p2.id, similarity));
}
}
}
// Output links
for (i, (source, target, weight)) in links.iter().enumerate() {
json.push_str(&format!(
" {{\"source\": {}, \"target\": {}, \"weight\": {:?}}}",
source, target, weight
));
if i < links.len() - 1 {
json.push_str(",\n");
} else {
json.push('\n');
}
}
json.push_str(" ]\n}\n");
json
}
/// Generate a training curriculum based on memory patterns
pub fn generate_training_curriculum(&self, stages: usize) -> Vec<Vec<(usize, T)>>
where
T: NumAssign,
{
// Early exit if we have no patterns
if self.patterns.is_empty() {
return Vec::new();
}
// Analyze which transformation types are most common/important
let mut transform_importance = HashMap::new();
for pattern in &self.patterns {
for &transform in &pattern.sequence {
let entry = transform_importance.entry(transform).or_insert(T::zero());
*entry += pattern.importance / T::from_usize(pattern.sequence().len()).unwrap();
}
}
// Sort transformations by importance
let mut transform_vec: Vec<_> = transform_importance.into_iter().collect();
transform_vec.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
// Generate curriculum stages
let mut curriculum = Vec::new();
let transforms_per_stage = (transform_vec.len() / stages).max(1);
for i in 0..stages {
let start = i * transforms_per_stage;
let end = if i == stages - 1 {
transform_vec.len()
} else {
(i + 1) * transforms_per_stage
};
if start < transform_vec.len() {
let stage_transforms = transform_vec[start..end.min(transform_vec.len())].to_vec();
curriculum.push(stage_transforms);
}
}
curriculum
}
/// Retrieve the best rule for a given state-symbol pair
pub fn get_best_rule(
&self,
Head { state, symbol }: Head<usize, usize>,
) -> Option<(usize, usize, Direction, T)> {
// Find all rules for this state-symbol pair
let mut rules = self
.features
.iter()
.filter(|f| {
f.dimension == 3
&& f.death.is_none()
&& f.content.len() >= 6
&& f.content[1] == *state
&& f.content[2] == symbol
})
.collect::<Vec<_>>();
// If no rules found, return None
if rules.is_empty() {
return None;
}
// Sort by confidence (importance)
rules.sort_by(|a, b| {
b.importance
.partial_cmp(&a.importance)
.unwrap_or(std::cmp::Ordering::Equal)
});
// Return the highest confidence rule
let best = rules[0];
// Extract the rule components
let next_state = best.content[3];
let next_symbol = best.content[4];
let direction = (best.content[5] as i8) - 1; // Convert back to -1, 0, 1
Some((next_state, next_symbol, direction.into(), best.importance))
}
/// Get all events in chronological order
pub fn get_recent_events(&self, limit: usize) -> Vec<&PersistentFeature<T>> {
let mut events: Vec<_> = self.features.iter().filter(|f| f.dimension == 0).collect();
// Sort by birth time (most recent first)
events.sort_by(|a, b| b.birth.cmp(&a.birth));
// Limit results
events.truncate(limit);
events
}
/// Get a recent pattern of transformations
pub fn get_recent_pattern(&self, max_length: usize) -> Option<Vec<usize>> {
let transform_features = self.recent_transformations(max_length);
if transform_features.is_empty() {
return None;
}
Some(transform_features)
}
/// Get statistics about the memory state
pub fn get_statistics(&self) -> crate::MemoryStatistics<T>
where
T: NumAssign,
{
let total_features = self.features.len();
let active_features = self.features.iter().filter(|f| f.death.is_none()).count();
// Count features by dimension
let mut dimension_counts = HashMap::new();
for feature in &self.features {
if feature.death.is_none() {
*dimension_counts.entry(feature.dimension).or_insert(0) += 1;
}
}
// Calculate average importance by dimension
let mut avg_importance_by_dim = HashMap::new();
for feature in &self.features {
if feature.death.is_none() {
let entry = avg_importance_by_dim
.entry(feature.dimension)
.or_insert((T::zero(), 0));
entry.0 += feature.importance;
entry.1 += 1;
}
}
// Finalize averages
let avg_importance_by_dim = avg_importance_by_dim
.into_iter()
.map(|(dim, (sum, count))| (dim, sum / T::from_usize(count).unwrap()))
.collect();
// Most common patterns
let mut pattern_counts = self
.patterns
.iter()
.map(|p| (p.sequence.clone(), p.occurrences))
.collect::<Vec<_>>();
pattern_counts.sort_by(|a, b| b.1.cmp(&a.1)); // Sort by frequency, descending
let most_common_patterns = pattern_counts.into_iter().take(5).collect();
crate::MemoryStatistics {
total_features,
active_features,
feature_counts: dimension_counts,
average_importance: avg_importance_by_dim,
relationship_count: self.relationships().len(),
pattern_count: self.patterns().len(),
common_patterns: most_common_patterns,
memory_time: self.current_epoch(),
}
}
/// Calculate the stability of a triad based on memory patterns and relationships
pub fn get_stability_for(&self, notes: &[usize; 3]) -> T
where
T: NumAssign,
{
// Convert to a vector for content lookup
let content = notes.to_vec();
// Try to find a feature for this triad
let triad_feature = if let Some(ids) = self.content_index().get(&content) {
// Find the first active feature with these notes
ids.iter()
.filter_map(|&id| {
self.features
.iter()
.find(|f| f.id == id && f.death.is_none())
})
.next()
} else {
None
};
// If we found a feature, calculate stability based on relationships and patterns
if let Some(feature) = triad_feature {
let scale = T::from_f32(0.2).unwrap();
// Base stability is the feature's importance
let mut stability = feature.importance;
// Adjust for feature age (older = more stable)
let feature_age = self.current_epoch().saturating_sub(feature.birth);
let age_factor =
(T::from_usize(feature_age).unwrap() / T::from_usize(10).unwrap()).min(T::one()); // Cap at T::one()
stability += age_factor * scale; // Age can add up to 0.2
// Adjust for connectedness (more connections = more stable)
let connections = self
.relationships
.iter()
.filter(|r| r.source_id == feature.id || r.to_id == feature.id)
.count();
let connection_factor =
(T::from_usize(connections).unwrap() / T::from_usize(5).unwrap()).min(T::one()); // Cap at T::one()
stability += connection_factor * scale; // Connections can add up to 0.2
// Adjust for pattern involvement
let pattern_involvement = self
.patterns
.iter()
.filter(|p| {
// Check if any transformation involving this triad is in the pattern
self.features
.iter()
.filter(|f| f.dimension == 1 && f.content.len() > 6)
.filter(|f| {
// Check if feature represents a transformation involving our triad
let from_match = f.content[0] == notes[0]
&& f.content[1] == notes[1]
&& f.content[2] == notes[2];
let to_match = f.content[3] == notes[0]
&& f.content[4] == notes[1]
&& f.content[5] == notes[2];
from_match || to_match
})
.any(|f| {
// Check if transformation type is in pattern
if let Some(&transform_type) = f.content.get(6) {
p.sequence.contains(&transform_type)
} else {
false
}
})
})
.count();
let pattern_factor = (T::from_usize(pattern_involvement).unwrap()
/ T::from_usize(3).unwrap())
.min(T::one()); // Cap at T::one()
stability += pattern_factor * T::from_f32(0.1).unwrap(); // Patterns can add up to 0.1
// Cap stability at T::one()
stability.min(T::one())
} else {
// If no feature exists, use default stability (caller will use defaults)
T::from_usize(2).unwrap().recip()
}
}
/// Get surface parameter at a position
pub fn get_surface_parameter(&self, position: &[usize]) -> Option<T> {
// Find features with this position prefix
let features = self.find_by_content_prefix(&{
let mut prefix = Vec::with_capacity(position.len() + 1);
prefix.push(4); // Dimension 4 for surface parameters
prefix.extend_from_slice(position);
prefix
});
if features.is_empty() {
return None;
}
// Get the most important feature
let best = features.into_iter().max_by(|a, b| {
a.importance
.partial_cmp(&b.importance)
.unwrap_or(std::cmp::Ordering::Equal)
})?;
// Extract value from content
if best.content.len() > position.len() + 1 {
let value_hash = best.content[position.len() + 1];
Some(T::from_usize(value_hash).unwrap() / T::from_usize(10000).unwrap())
} else {
None
}
}
/// Identify regions of memory that form coherent structures
pub fn identify_memory_regions(&self, coherence_threshold: T) -> Vec<Vec<usize>> {
// Create a graph from relationships
let mut adjacency = HashMap::<usize, Vec<(usize, T)>>::new();
for rel in &self.relationships {
// Only consider active features with sufficient strength
if rel.strength >= coherence_threshold
&& self
.features
.iter()
.any(|f| f.id == rel.source_id && f.death.is_none())
&& self
.features
.iter()
.any(|f| f.id == rel.to_id && f.death.is_none())
{
// Add bidirectional connections
adjacency
.entry(rel.source_id)
.or_default()
.push((rel.to_id, rel.strength));
adjacency
.entry(rel.to_id)
.or_default()
.push((rel.source_id, rel.strength));
}
}
// Find connected components using BFS
let mut visited = HashSet::new();
let mut regions = Vec::new();
for feature in self.features.iter().filter(|f| f.death.is_none()) {
if visited.contains(&feature.id()) {
continue;
}
// Start new region
let mut region = Vec::new();
let mut queue = std::collections::VecDeque::new();
queue.push_back(feature.id());
visited.insert(feature.id());
while let Some(current) = queue.pop_front() {
region.push(current);
// Add neighbors
if let Some(neighbors) = adjacency.get(¤t) {
for &(neighbor, _) in neighbors {
if !visited.contains(&neighbor) {
queue.push_back(neighbor);
visited.insert(neighbor);
}
}
}
}
if !region.is_empty() {
regions.push(region);
}
}
// Sort regions by size (largest first)
regions.sort_by_key(|r| -(r.len() as isize));
regions
}
/// Learn relationships between elements in headspace
pub fn learn_headspace_relationships(
&mut self,
notes: &[usize; 3],
relationships: &[(usize, usize, T)],
) {
let epoch = self.current_epoch();
// Create features for each note
let note_features = notes
.iter()
.map(|¬e| {
// Check if feature already exists
let content = vec![note];
let feature_ids = self
.index
.content_index_mut()
.entry(content.clone())
.or_default();
match feature_ids.iter().next() {
Some(&i) => i,
None => {
// Create new feature
let id = self.state.position.next_feature_id();
let feature = PersistentFeature {
id,
birth: epoch,
death: None,
dimension: 0, // Notes are 0-dim features
content: content.clone(),
importance: T::from_usize(2).unwrap().recip(), // Default importance
occurrences: 1,
};
feature_ids.insert(id);
self.dimension_index_mut().entry(0).or_default().insert(id);
self.features.push(feature);
id
}
}
})
.collect::<Vec<_>>();
// Create relationships between notes based on provided relationships
for &(i, j, strength) in relationships {
if i < note_features.len() && j < note_features.len() {
self.create_relationship(
note_features[i],
note_features[j],
RelationshipType::Interval,
strength,
);
}
}
}
/// Learn surface characteristics from critical points
pub fn learn_surface(
&mut self,
critical_points: &HashMap<String, usize>,
reference_note: usize,
) {
if critical_points.is_empty() {
return;
}
// Create a feature for the reference note (usually tonic)
let reference_content = vec![reference_note];
let reference_id = self.create_or_get_feature(
0, // Dimension
reference_content,
T::from_f32(0.8).unwrap(), // High importance as reference
);
// Create features for each critical point
for (name, &point) in critical_points {
// Create content with the point value and a name hash
let name_hash = name
.bytes()
.fold(0, |acc: usize, b| acc.wrapping_add(b as usize));
let content = vec![point, name_hash];
let point_id = self.create_or_get_feature(
0, // Dimension
content,
T::from_f32(0.7).unwrap(), // Important as critical point
);
// Create relationship to reference note
let distance = ((point as isize - reference_note as isize).pmod()) as usize;
let strength = T::one() - T::from_usize(distance).unwrap() / T::from_usize(6).unwrap(); // Normalize to T::zero()-T::one()
self.create_relationship(reference_id, point_id, RelationshipType::Critical, strength);
}
}
/// Learn surface parameters between critical points
pub fn learn_surface_parameter(&mut self, position: Vec<usize>, value: T) -> usize {
// Create a content vector for surface parameters
// Format: [dimension, ...position coordinates, value hash]
let mut content = Vec::with_capacity(position.len() + 2);
content.push(4); // Dimension 4 for surface parameters
content.extend_from_slice(&position);
// Hash the value to store it in the content
let value_hash = (value * T::from_usize(10000).unwrap()).to_usize().unwrap();
content.push(value_hash);
// Create or update the feature
self.create_or_get_feature(4, content, value.abs())
}
/// Merge another memory system into this one
///
/// This is used for sharing patterns and knowledge between nodes.
/// Only copies features, patterns and relationships from the source that don't exist in this memory.
pub fn merge(&mut self, source: &Self) {
// Skip if source is empty
if source.features.is_empty() && source.patterns.is_empty() {
return;
}
// Track original and new IDs for feature remapping
let mut id_mapping: HashMap<usize, usize> = HashMap::new();
// Copy active features
for feature in source.features.iter().filter(|f| f.death.is_none()) {
// Check if we already have this feature by content
let maybe_existing = if let Some(ids) = self.content_index().get(feature.content()) {
ids.iter().find_map(|&id| {
self.features
.iter()
.find(|f| f.id == id && f.death.is_none())
.map(|f| f.id)
})
} else {
None
};
if let Some(existing_id) = maybe_existing {
// Feature already exists, map source ID to our ID
id_mapping.insert(feature.id, existing_id);
// Update importance if source has higher importance
if let Some(our_feature) = self.features.iter_mut().find(|f| f.id == existing_id) {
if feature.importance() > our_feature.importance() {
// Remove from current importance bucket
if let Some(bucket) = self
.index
.importance_index_mut()
.get_mut(&(feature.importance_to_usize_by_10()))
{
bucket.remove(&existing_id);
}
// Update importance
our_feature.set_importance(*feature.importance());
// Add to new importance bucket
let new_bucket = feature.importance_to_usize_by_10();
self.importance_index_mut()
.entry(new_bucket)
.or_default()
.insert(existing_id);
}
}
} else {
// Create new feature with our own ID
let new_id = self.next_feature_id();
// Map source ID to our new ID
id_mapping.insert(feature.id(), new_id);
// Create the feature in our memory
let merged_feature = PersistentFeature {
id: new_id,
birth: self.current_epoch(), // Use our current epoch
death: None,
content: feature.content().clone(),
dimension: feature.dimension(),
importance: feature.importance,
occurrences: feature.occurrences(),
};
// Update indices
self.dimension_index_mut()
.entry(feature.dimension)
.or_default()
.insert(new_id);
self.content_index_mut()
.entry(feature.content().clone())
.or_default()
.insert(new_id);
let importance_bucket = feature.importance_to_usize_by_10();
self.importance_index_mut()
.entry(importance_bucket)
.or_default()
.insert(new_id);
self.features.push(merged_feature);
}
}
// Copy relationships, remapping IDs
for rel in source.relationships() {
// Map source IDs to our IDs
if let (Some(&from_id), Some(&to_id)) =
(id_mapping.get(&rel.source_id), id_mapping.get(&rel.to_id))
{
// Check if this relationship already exists
let rel_exists = self.relationships.iter().any(|r| {
r.source_id == from_id
&& r.to_id == to_id
&& r.relationship_type == rel.relationship_type
});
if !rel_exists {
// Create new relationship
let new_rel = FeatureRelationship {
source_id: from_id,
to_id,
..rel.clone()
};
self.relationships.push(new_rel);
} else {
// Update existing relationship if needed
if let Some(existing) = self.relationships.iter_mut().find(|r| {
r.source_id == from_id
&& r.to_id == to_id
&& r.relationship_type == rel.relationship_type
}) {
existing.occurrences += rel.occurrences;
existing.strength = existing.strength.max(rel.strength);
}
}
}
}
let point9 = T::from_f32(0.9).unwrap();
// Copy patterns
for pattern in &source.patterns {
// Check if we already have this pattern
let pattern_exists = self.patterns.iter().any(|p| p.sequence == pattern.sequence);
if !pattern_exists {
// Create new pattern with our own ID
let new_pattern = FeaturePattern {
id: self.next_pattern_id(),
sequence: pattern.sequence.clone(),
occurrences: pattern.occurrences,
importance: pattern.importance * point9, // Slightly reduce importance when sharing
};
self.patterns.push(new_pattern);
} else {
// Update existing pattern if appropriate
if let Some(existing) = self
.patterns
.iter_mut()
.find(|p| p.sequence == pattern.sequence)
{
// Increment occurrences
existing.occurrences += 1;
// Update importance as max of current and incoming (with slight discount)
existing.importance = existing.importance.max(pattern.importance * point9);
}
}
}
}
/// Merge two features, keeping the higher importance one
pub fn merge_features(&mut self, primary_id: usize, secondary_id: usize) {
// Get features
// TODO: make use of the primary feature or drop
// let primary = match self.find_feature_by_id(primary_id) {
// Some(f) if f.death.is_none() => f.clone(),
// _ => return,
// };
let secondary = match self.find_feature_by_id(secondary_id) {
Some(f) if f.death.is_none() => f.clone(),
_ => return,
};
// Increase importance of primary feature
if let Some(feature) = self.features.iter_mut().find(|f| f.id == primary_id) {
feature.importance =
feature.importance.max(secondary.importance) + T::from_f32(0.1).unwrap();
feature.occurrences += secondary.occurrences;
}
// get a copy of the current epoch
let epoch = self.current_epoch();
// Mark secondary feature as "dead"
if let Some(feature) = self
.features_mut()
.iter_mut()
.find(|f| f.id == secondary_id)
{
feature.death = Some(epoch);
}
// Update relationships
for rel in self.relationships_mut().iter_mut() {
if rel.source_id == secondary_id {
rel.source_id = primary_id;
}
if rel.to_id == secondary_id {
rel.to_id = primary_id;
}
}
}
/// Find most influential transformations in memory
pub fn most_influential_transformations(&self, limit: usize) -> Vec<(LPR, T)>
where
T: NumAssign,
{
// Count all transformation occurrences weighted by importance
let mut transform_influence = HashMap::new();
// From explicit transformations
for feature in self
.features
.iter()
.filter(|f| f.dimension == 1 && f.death.is_none())
{
if let Some(&transform_type) = feature.content.get(6) {
let entry = transform_influence
.entry(transform_type)
.or_insert(T::zero());
*entry += feature.importance;
}
}
// From patterns
for pattern in &self.patterns {
for &transform in &pattern.sequence {
let entry = transform_influence.entry(transform).or_insert(T::zero());
*entry += pattern.importance / T::from_usize(pattern.sequence.len()).unwrap();
}
}
// Convert to LPR types and limit results
let mut results: Vec<_> = transform_influence
.into_iter()
.map(|(t, influence)| (t.into(), influence))
.collect();
// Sort by influence, descending
results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
results.truncate(limit);
results
}
/// Optimize memory by consolidating similar features
pub fn optimize(&mut self)
where
T: NumAssign,
{
// First pass: mark similar events for merging
let mut to_merge = Vec::new();
// Check dimension 0 features (events) with similar content
let d0_features = self
.features
.iter()
.filter(|f| f.dimension == 0 && f.death.is_none())
.collect::<Vec<_>>();
for (i, f1) in d0_features.iter().enumerate() {
for f2 in d0_features.iter().skip(i + 1) {
if f1.content.len() == f2.content.len()
&& !f1.content.is_empty()
&& f1.content[0] == f2.content[0]
&& f1.birth.abs_diff(f2.birth) < 5
{
// Close in time
// Check content similarity
let mut similarity = T::one();
for j in 1..f1.content.len() {
if j < f2.content.len() && f1.content[j] != f2.content[j] {
similarity -= T::one() / T::from_usize(f1.content.len()).unwrap();
}
}
if similarity > T::from_f32(0.8).unwrap() {
// Merge f2 into f1 (keep the older one)
if f1.birth <= f2.birth {
to_merge.push((f2.id, f1.id));
} else {
to_merge.push((f1.id, f2.id));
}
}
}
}
}
// Perform merges
for (remove_id, keep_id) in to_merge {
// get a copy of the current epoch
let epoch = self.current_epoch();
// Mark the redundant feature as dead
if let Some(feature) = self.features.iter_mut().find(|f| f.id == remove_id) {
feature.death = Some(epoch);
}
// Update relationships to point to the kept feature
for rel in self.relationships.iter_mut() {
if rel.source_id == remove_id {
rel.source_id = keep_id;
}
if rel.to_id == remove_id {
rel.to_id = keep_id;
}
}
// Boost importance of the kept feature
if let Some(feature) = self.features.iter_mut().find(|f| f.id == keep_id) {
feature.importance = (feature.importance + T::from_f32(0.1).unwrap()).min(T::one());
}
}
// Rebuild indices to reflect changes
self.rebuild_indices();
// Now deduplicate relationships
let mut unique_rels = Vec::new();
let mut seen = HashSet::new();
for rel in self.relationships.drain(..) {
let key = (rel.source_id, rel.to_id, rel.relationship_type as usize);
if !seen.contains(&key) {
seen.insert(key);
unique_rels.push(rel);
}
}
self.relationships = unique_rels;
}
/// Predict the next likely transformation in a sequence
pub fn predict_next_transformation(&self, recent_transforms: &[usize]) -> Option<(usize, T)> {
if recent_transforms.is_empty() {
return None;
}
// Find patterns that match the recent sequence as a prefix
let mut candidates = Vec::new();
for pattern in &self.patterns {
// Skip patterns shorter than the input
if pattern.sequence.len() <= recent_transforms.len() {
continue;
}
// Check if pattern starts with our recent transforms
if pattern.sequence.starts_with(recent_transforms) {
// The next element in the pattern is our prediction
let predicted = pattern.sequence[recent_transforms.len()];
let confidence = pattern.importance
* (T::from_usize(pattern.occurrences).unwrap() / T::from_usize(10).unwrap())
.min(T::one());
candidates.push((predicted, confidence));
}
}
// If we found direct matches, use the highest confidence one
if !candidates.is_empty() {
candidates.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
return Some(candidates[0]);
}
// If no direct matches, try partial matches (fuzzy prediction)
let mut fuzzy_candidates = Vec::new();
let min_length = recent_transforms.len().min(3).max(1);
let recent_suffix = &recent_transforms[recent_transforms.len() - min_length..];
for pattern in &self.patterns {
if pattern.sequence.len() <= min_length {
continue;
}
// Look for the suffix in this pattern
for i in 0..=pattern.sequence.len() - min_length {
if &pattern.sequence[i..i + min_length] == recent_suffix {
// Found a match - predict what comes next in the pattern
if i + min_length < pattern.sequence.len() {
let predicted = pattern.sequence[i + min_length];
// Lower confidence for partial matches
let confidence = pattern.importance
* T::from_f32(0.7).unwrap()
* (T::from_usize(pattern.occurrences).unwrap()
/ T::from_usize(10).unwrap())
.min(T::one());
fuzzy_candidates.push((predicted, confidence));
}
}
}
}
// Return best fuzzy match if any
if !fuzzy_candidates.is_empty() {
fuzzy_candidates
.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(core::cmp::Ordering::Equal));
return Some(fuzzy_candidates[0]);
}
// No predictions found
None
}
/// Process patterns with minimum overhead
pub fn process_patterns_batch(&mut self, inputs: &[Vec<usize>]) -> Vec<usize> {
if inputs.is_empty() || self.patterns.is_empty() {
return Vec::new();
}
// Pre-allocate for matched indices
let mut matched_indices = Vec::new();
// Skip empty patterns - create references to active patterns
// This avoids repeated iteration over self.patterns
let active_patterns: Vec<(usize, &FeaturePattern<T>)> = self
.patterns
.iter()
.enumerate()
.filter(|(_, p)| !p.sequence.is_empty())
.collect();
if active_patterns.is_empty() {
return Vec::new();
}
// Process each input once
for input in inputs {
if input.is_empty() {
continue;
}
for &(idx, pattern) in &active_patterns {
// Quick length check first
if pattern.sequence.len() != input.len() {
continue;
}
// Do direct slice comparison
if pattern.sequence() == input {
matched_indices.push(idx);
}
}
}
// Update pattern occurrences for matches
for &idx in &matched_indices {
if let Some(pattern) = self.patterns.get_mut(idx) {
pattern.occurrences += 1;
}
}
matched_indices
}
/// Prune less important features when memory gets too large
pub fn prune(&mut self, max_features: usize) {
if self.features.len() <= max_features {
return;
}
// get a copy of the current epoch
let epoch = self.current_epoch();
// sort features by importance
let mut feature_ids: Vec<_> = self
.features
.iter()
.filter(|f| f.death.is_none()) // Only consider active features
.map(|f| (f.id, f.importance))
.collect();
feature_ids.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
// Determine how many features to prune
let to_prune = self.features.len() - max_features;
// Mark least important features as dead
for (id, _) in feature_ids.iter().take(to_prune) {
if let Some(feature) = self.features.iter_mut().find(|f| f.id == *id) {
feature.death = Some(epoch);
}
}
}
/// Record an event in the memory system
pub fn record_event(&mut self, event_type: &str, metadata: Option<Vec<usize>>) -> usize {
// Create a feature representing the event
let dimension = 0; // Events are 0-dimensional features
let content = if let Some(meta) = metadata {
// Convert event type to a numeric code and combine with metadata
let event_code = event_type.bytes().fold(0, |acc, b| acc ^ b as usize);
let mut content = vec![event_code];
content.extend(meta);
content
} else {
// Just use the event type code
let event_code = event_type.bytes().fold(0, |acc, b| acc ^ b as usize);
vec![event_code]
};
// Create the event feature with higher importance
let feature_id = self.next_feature_id();
let feature = PersistentFeature {
id: feature_id,
dimension,
birth: self.current_epoch(),
death: None,
content,
importance: T::from_f32(0.8).unwrap(), // Events are important
occurrences: 1,
};
// Add to indices
self.dimension_index_mut()
.entry(dimension)
.or_default()
.insert(feature_id);
// Add feature
self.features.push(feature);
feature_id
}
/// Mark a feature as pruned (dead)
pub fn prune_feature(&mut self, feature_id: usize) {
let epoch = self.current_epoch();
if let Some(feature) = self.features_mut().iter_mut().find(|f| f.id == feature_id) {
feature.death = Some(epoch);
}
}
/// Rebuild indices after removing features
pub fn rebuild_indices(&mut self) {
// Clear existing indices
self.index_mut().clear();
// Rebuild from remaining features
for feature in &self.features {
if feature.death.is_some() {
continue;
}
self.index
.dimension_index_mut()
.entry(feature.dimension)
.or_default()
.insert(feature.id);
self.index
.content_index_mut()
.entry(feature.content().to_vec())
.or_default()
.insert(feature.id);
let importance_bucket = feature.importance_to_usize_by_10();
self.index
.importance_index_mut()
.entry(importance_bucket)
.or_default()
.insert(feature.id);
}
// Clean up relationship references to dead features
self.relationships.retain(|rel| {
let from_exists = self
.features
.iter()
.any(|f| f.id == rel.source_id && f.death.is_none());
let to_exists = self
.features
.iter()
.any(|f| f.id == rel.to_id && f.death.is_none());
from_exists && to_exists
});
}
/// Records a pattern
pub fn record_pattern(&mut self, pattern: &[usize]) {
let pattern_vec = pattern.to_vec();
// Check if this pattern already exists
let pattern_exists = self.patterns.iter().any(|p| p.sequence == pattern_vec);
if !pattern_exists {
// Create new pattern
let pattern = FeaturePattern {
id: self.next_pattern_id(),
sequence: pattern_vec,
occurrences: 1,
importance: T::from_usize(2).unwrap().recip(), // Initial importance
};
self.patterns.push(pattern);
} else {
// Update existing pattern
if let Some(pattern) = self.patterns.iter_mut().find(|p| p.sequence == pattern_vec) {
pattern.occurrences += 1;
pattern.update_importance_by_occurrences();
}
}
}
/// records a pattern with the given importance
pub fn record_pattern_with_importance(&mut self, pattern: &[usize], importance: T) {
let pattern_vec = pattern.to_vec();
// Check if this pattern already exists
let pattern_exists = self.patterns.iter().any(|p| p.sequence == pattern_vec);
if !pattern_exists {
// Create new pattern
let pattern = FeaturePattern {
id: self.next_pattern_id(),
sequence: pattern_vec,
occurrences: 1,
importance,
};
self.patterns.push(pattern);
} else {
// Update existing pattern
if let Some(pattern) = self.patterns.iter_mut().find(|p| p.sequence == pattern_vec) {
pattern.occurrences += 1;
pattern.update_importance_by_occurrences();
}
}
}
/// Record a transformation between triads
pub fn record_transformation(
&mut self,
from_notes: [usize; 3],
from_class: Triads,
transform: LPR,
to_notes: [usize; 3],
to_class: Triads,
) -> usize {
// Record the transformation as a 1-dimensional feature
let mut content = Vec::with_capacity(7);
content.extend_from_slice(&from_notes);
content.extend_from_slice(&to_notes);
content.push(transform as usize);
let id = self.create_feature(1, content);
// Find the source and target triad features
let source = Triad::new(from_notes, from_class);
let source_id = self.find_or_create_triad(&source);
let target = Triad::new(to_notes, to_class);
let target_id = self.find_or_create_triad(&target);
// Create relationship between triads
self.add_relationship(
source_id,
target_id,
RelationshipType::Transformation,
T::from_f32(0.7).unwrap(),
);
// Look for patterns after recording transformation
self.detect_patterns();
id
}
/// Get the most recent transformation types in chronological order
pub fn recent_transformations(&self, count: usize) -> Vec<usize> {
// Find all transformation features (dimension 1)
let transform_features = self.find_by_dimension(1);
// Filter active features and sort by birth time (most recent first)
let mut recent_transforms = transform_features
.iter()
.filter(|f| f.death.is_none() && f.content.len() > 6)
.collect::<Vec<_>>();
// Sort by birth time (newest first)
recent_transforms.sort_by(|a, b| b.birth.cmp(&a.birth));
// Extract the transformation types (index 6 holds the LPR value)
recent_transforms
.into_iter()
.take(count)
.map(|f| f.content[6])
.collect()
}
/// Record multiple transformations in batch for efficiency
pub fn record_transformations_batch(
&mut self,
transforms: Vec<([usize; 3], Triads, LPR, [usize; 3], Triads)>,
) -> Vec<usize> {
if transforms.is_empty() {
return Vec::new();
}
let mut transform_ids = Vec::with_capacity(transforms.len());
for (from_notes, from_class, transform, to_notes, to_class) in transforms {
let id =
self.record_transformation(from_notes, from_class, transform, to_notes, to_class);
transform_ids.push(id);
}
transform_ids
}
/// Record a successful navigation between two points
pub fn record_navigation(
&mut self,
from_notes: &[usize],
to_notes: &[usize],
transforms_used: &[LPR],
) {
// Convert transforms to usize
let transform_ids: Vec<_> = transforms_used
.iter()
.map(|&t| match t {
LPR::Leading => 0,
LPR::Parallel => 1,
LPR::Relative => 2,
})
.collect();
// Create content by combining from_notes, to_notes and a hash of transforms
let mut content = Vec::with_capacity(from_notes.len() + to_notes.len() + 1);
content.extend_from_slice(from_notes);
content.extend_from_slice(to_notes);
// Add transform path hash
let path_hash = transform_ids.iter().fold(0, |acc, &t| acc * 3 + t);
content.push(path_hash);
// Create or update navigation feature
let _ = self.create_or_get_feature(
2, // Navigation features are dimension 2
content,
T::from_f32(0.6).unwrap(), // Medium-high importance
);
// Record the transformation sequence as a pattern
if !transform_ids.is_empty() {
self.record_pattern_with_importance(&transform_ids, T::from_f32(0.7).unwrap());
}
}
/// Reinforce a rule that led to successful outcomes
pub fn reinforce_rule(
&mut self,
State(state): State<usize>,
symbol: usize,
State(next_state): State<usize>,
next_symbol: usize,
direction: Direction,
amount: T,
) where
T: core::iter::Sum,
{
// Find the rule
let content = vec![
3,
state,
symbol,
next_state,
next_symbol,
(direction + 1) as usize,
];
if let Some(id) = self.find_feature_by_content(&content) {
self.adjust_feature_importance(id, amount);
}
}
/// returns the total size of the memory
pub fn size(&self) -> usize {
// Base size: fixed overhead
let mut total_size = 3 * core::mem::size_of::<usize>(); // epoch, next_id, next_pattern_id
// Features size
total_size += self
.features
.iter()
.map(|f| {
// Base feature size + content size
core::mem::size_of::<PersistentFeature<T>>()
+ f.content().len() * core::mem::size_of::<usize>()
})
.sum::<usize>();
// Relationships size
total_size += self.relationships.len() * core::mem::size_of::<FeatureRelationship<T>>();
// Rules size
total_size += self
.rulespace
.iter()
.map(|_r| {
// Base rule size + any dynamic content
core::mem::size_of::<LearnedRule>() + core::mem::size_of::<(usize, usize)>()
})
.sum::<usize>();
// Patterns size
total_size += self
.patterns
.iter()
.map(|p| {
// Base pattern size + sequence size
core::mem::size_of::<FeaturePattern<T>>()
+ p.sequence().len() * core::mem::size_of::<usize>()
})
.sum::<usize>();
// Index structures (approximate)
total_size +=
self.dimension_index().len() * core::mem::size_of::<(usize, HashSet<usize>)>();
total_size += self.content_index().len()
* self
.content_index()
.iter()
.map(|(k, v)| {
k.len() * core::mem::size_of::<usize>() + // key size
v.len() * core::mem::size_of::<usize>() // value set size
})
.sum::<usize>();
total_size +=
self.importance_index().len() * core::mem::size_of::<(usize, HashSet<usize>)>();
total_size
}
/// Store a learned rule with associated confidence
pub fn store_learned_rule(
&mut self,
State(state): State<usize>,
symbol: usize,
State(next_state): State<usize>,
next_symbol: usize,
direction: Direction,
confidence: T,
) -> usize {
// Create a content vector encoding the rule
// [dimension, state, symbol, next_state, next_symbol, direction+1]
let content = vec![
// LearnedRule::from_parts(State(state), symbol, direction, State(next_state), next_symbol, confidence)
3,
state,
symbol,
next_state,
next_symbol,
(direction as usize) + 1,
];
// Check if we already have this rule
if let Some(existing_id) = self.find_feature_by_content(&content) {
// Update existing rule's confidence
if let Some(feature) = self.features.iter_mut().find(|f| f.id == existing_id) {
let old_confidence = feature.importance;
let old_bucket = (old_confidence * T::from_usize(10).unwrap())
.to_usize()
.unwrap();
feature.importance = (old_confidence * T::from_f32(0.8).unwrap()
+ confidence * T::from_f32(0.2).unwrap())
.min(T::one());
// Update importance index
let new_bucket = feature.importance_to_usize_by_10();
if old_bucket != new_bucket {
if let Some(bucket) = self.importance_index_mut().get_mut(&old_bucket) {
bucket.remove(&existing_id);
}
self.importance_index_mut()
.entry(new_bucket)
.or_default()
.insert(existing_id);
}
}
return existing_id;
}
// Create a new rule feature
let id = self.create_feature(3, content);
// Adjust importance to match confidence
if let Some(feature) = self.features.iter_mut().find(|f| f.id == id) {
feature.importance = confidence;
// Update importance index
let bucket = (confidence * T::from_usize(10).unwrap())
.to_usize()
.unwrap();
if let Some(old_bucket) = self
.index
.importance_index_mut()
.get_mut(&feature.importance_to_usize_by_10())
{
old_bucket.remove(&id);
}
self.index
.importance_index_mut()
.entry(bucket)
.or_default()
.insert(id);
}
id
}
/// Generate a surface parameter prediction model
pub fn surface_parameter_model(&self) -> HashMap<Vec<usize>, T> {
let mut model = HashMap::new();
// Find all surface parameter features (dimension 4)
let surface_features = self
.features
.iter()
.filter(|f| f.dimension == 4 && f.death.is_none())
.collect::<Vec<_>>();
for feature in surface_features {
if feature.content.len() < 3 {
continue; // Need at least dimension + position + value
}
// Extract position (skip the dimension marker)
let position = feature.content[1..feature.content.len() - 1].to_vec();
// Extract value
let value_hash = feature.content[feature.content.len() - 1];
let value = T::from_usize(value_hash).unwrap() / T::from_usize(10000).unwrap();
// Add to model, weighted by importance
let entry = model.entry(position).or_insert(T::zero());
*entry = (*entry * T::from_f32(0.7).unwrap())
+ (value * feature.importance * T::from_f32(0.3).unwrap());
}
model
}
/// Calculate transformation probabilities from memory
pub fn transformation_probabilities(&self, current_triad: &Triad) -> Vec<(LPR, T)>
where
T: NumAssign,
{
// Find the triad features
let content = current_triad.notes().to_vec();
let _triad_id = if let Some(ids) = self.content_index().get(&content) {
if let Some(&id) = ids.iter().next() {
id
} else {
return vec![
(LPR::Leading, T::from_usize(3).unwrap().recip()),
(LPR::Parallel, T::from_usize(3).unwrap().recip()),
(LPR::Relative, T::from_usize(3).unwrap().recip()),
];
}
} else {
return vec![
(LPR::Leading, T::from_usize(3).unwrap().recip()),
(LPR::Parallel, T::from_usize(3).unwrap().recip()),
(LPR::Relative, T::from_usize(3).unwrap().recip()),
];
};
// Find all transformations from this triad
let transform_features = self
.features
.iter()
.filter(|f| f.dimension == 1 && f.death.is_none() && f.content.len() > 6)
.filter(|f| {
f.content[0] == current_triad[0]
&& f.content[1] == current_triad[1]
&& f.content[2] == current_triad[2]
})
.collect::<Vec<_>>();
// Count occurrences of each transform type
let mut counts = HashMap::new();
let mut total = T::zero();
for feature in transform_features {
if let Some(&transform_type) = feature.content.get(6) {
let entry = counts.entry(transform_type).or_insert(T::zero());
*entry += feature.importance;
total += feature.importance;
}
}
// Calculate probabilities
let mut probabilities = Vec::new();
let default_prop = T::from(0.1).unwrap();
let denom = total + T::from(0.3).unwrap();
// Leading transform (0)
let l_prob = counts.get(&0).copied().unwrap_or(default_prop) / denom;
probabilities.push((LPR::Leading, l_prob));
// Parallel transform (1)
let p_prob = counts.get(&1).copied().unwrap_or(default_prop) / denom;
probabilities.push((LPR::Parallel, p_prob));
// Relative transform (2)
let r_prob = counts.get(&2).copied().unwrap_or(default_prop) / denom;
probabilities.push((LPR::Relative, r_prob));
// Sort by probability
probabilities.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
probabilities
}
/// Generate a visualization of the memory topology
pub fn visualize(&self) -> String
where
T: core::fmt::Debug,
{
// Create a DOT format graph for visualization
let mut dot = String::from("digraph memory_topology {\n");
// Add nodes (features)
for feature in self.features.iter().filter(|f| f.death.is_none()) {
let color = match feature.dimension {
0 => "lightblue",
1 => "lightgreen",
2 => "orange",
3 => "pink",
_ => "gray",
};
let label = format!("{}\\nDim:{}", feature.id, feature.dimension);
dot.push_str(&format!(
" node{} [label=\"{}\", style=filled, fillcolor={}];\n",
feature.id, label, color
));
}
// Add edges (relationships)
for rel in &self.relationships {
// Only include relationships between active features
if self
.features
.iter()
.any(|f| f.id == rel.source_id && f.death.is_none())
&& self
.features
.iter()
.any(|f| f.id == rel.to_id && f.death.is_none())
{
let rel_type = match rel.relationship_type {
RelationshipType::Transformation => "transform",
RelationshipType::Interval => "interval",
RelationshipType::Critical => "critical",
_ => "other",
};
let width =
(rel.strength * T::from_usize(2).unwrap()).max(T::from_f32(0.5).unwrap());
dot.push_str(&format!(
" node{} -> node{} [label=\"{}\", penwidth={:?}];\n",
rel.source_id, rel.to_id, rel_type, width
));
}
}
dot.push_str("}\n");
dot
}
}
impl<T> TopoLedger<T>
where
T: Float + FromPrimitive,
{
/// Create or get a feature by content
pub(crate) fn create_or_get_feature(
&mut self,
dimension: usize,
content: Vec<usize>,
importance: T,
) -> usize {
// Check if feature already exists
if let Some(ids) = self.content_index().get(&content) {
if let Some(&id) = ids.iter().next() {
// Feature exists, update importance if necessary
if let Some(feature) = self.features.iter_mut().find(|f| f.id == id) {
if importance > feature.importance {
// Update importance
feature.importance = importance;
}
}
return id;
}
}
// Create new feature
let id = self.next_feature_id();
let feature = PersistentFeature {
id,
birth: self.current_epoch(),
death: None,
dimension,
content: content.clone(),
importance,
occurrences: 1,
};
// Update indices
self.content_index_mut()
.entry(content)
.or_default()
.insert(id);
self.dimension_index_mut()
.entry(dimension)
.or_default()
.insert(id);
let importance_bucket = feature.importance_to_usize_by_10();
self.importance_index_mut()
.entry(importance_bucket)
.or_default()
.insert(id);
self.features.push(feature);
id
}
/// Create a relationship between features
pub(crate) fn create_relationship(
&mut self,
from_id: usize,
to_id: usize,
rel_type: RelationshipType,
strength: T,
) {
// Check if relationship already exists
let existing = self.relationships.iter_mut().find(|r| {
r.source_id == from_id && r.to_id == to_id && r.relationship_type == rel_type
});
if let Some(rel) = existing {
// Update existing relationship
rel.occurrences += 1;
rel.strength = (rel.strength + strength) / T::from_usize(2).unwrap(); // Average strength
} else {
// Create new relationship
let relationship = FeatureRelationship::new(from_id, to_id, rel_type, strength);
self.relationships.push(relationship);
}
}
/// Find a triad feature or create it if not found
pub(crate) fn find_or_create_triad(&mut self, triad: &Triad) -> usize {
// Try to find an existing triad feature
let content = triad.notes().to_vec();
if let Some(ids) = self.content_index().get(&content) {
if let Some(&id) = ids.iter().next() {
return id;
}
}
// Create a new triad feature
self.create_feature(2, content)
}
/// Calculate structural similarity between two regions
pub(crate) fn region_structural_similarity(&self, region1: &[usize], region2: &[usize]) -> T
where
T: core::iter::Sum,
{
// Early exits for empty regions
if region1.is_empty() || region2.is_empty() {
return T::zero();
}
// Compute dimension distributions
let mut dim_dist1 = HashMap::new();
let mut dim_dist2 = HashMap::new();
for &id in region1 {
if let Some(feature) = self
.features
.iter()
.find(|f| f.id == id && f.death.is_none())
{
*dim_dist1.entry(feature.dimension).or_insert(0) += 1;
}
}
for &id in region2 {
if let Some(feature) = self
.features
.iter()
.find(|f| f.id == id && f.death.is_none())
{
*dim_dist2.entry(feature.dimension).or_insert(0) += 1;
}
}
// Calculate dimension distribution similarity
let mut total_dims = 0;
let mut matching_dims = 0;
for (&dim, &count1) in &dim_dist1 {
total_dims += count1;
if let Some(&count2) = dim_dist2.get(&dim) {
matching_dims += count1.min(count2);
}
}
for (&_, &count2) in &dim_dist2 {
total_dims += count2;
}
// Avoid double-counting
total_dims -= matching_dims;
let dim_similarity = if total_dims > 0 {
T::from_usize(matching_dims).unwrap() / T::from_usize(total_dims).unwrap()
} else {
T::zero()
};
// Build adjacency matrices for both regions
let mut adj1 = HashMap::new();
let mut adj2 = HashMap::new();
// Map IDs to positions for each region
let id_to_pos1: HashMap<_, _> =
region1.iter().enumerate().map(|(i, &id)| (id, i)).collect();
let id_to_pos2: HashMap<_, _> =
region2.iter().enumerate().map(|(i, &id)| (id, i)).collect();
// Build adjacency matrices from relationships
for rel in &self.relationships {
// For region 1
if let (Some(&pos_from), Some(&pos_to)) =
(id_to_pos1.get(&rel.source_id), id_to_pos1.get(&rel.to_id))
{
adj1.entry(pos_from)
.or_insert_with(Vec::new)
.push((pos_to, rel.relationship_type as usize));
}
// For region 2
if let (Some(&pos_from), Some(&pos_to)) =
(id_to_pos2.get(&rel.source_id), id_to_pos2.get(&rel.to_id))
{
adj2.entry(pos_from)
.or_insert_with(Vec::new)
.push((pos_to, rel.relationship_type as usize));
}
}
// Compare structural properties
// 1. Compare average degree
let avg_degree1 = if !region1.is_empty() {
T::from_usize(adj1.values().map(|v| v.len()).sum()).unwrap()
/ T::from_usize(region1.len()).unwrap()
} else {
T::zero()
};
let avg_degree2 = if !region2.is_empty() {
T::from_usize(adj2.values().map(|v| v.len()).sum()).unwrap()
/ T::from_usize(region2.len()).unwrap()
} else {
T::zero()
};
let degree_similarity = if avg_degree1 == T::zero() && avg_degree2 == T::zero() {
T::one() // Both have no edges
} else if avg_degree1 == T::zero() || avg_degree2 == T::zero() {
T::zero() // One has edges, one doesn't
} else if avg_degree1 > avg_degree2 {
avg_degree2 / avg_degree1
} else {
avg_degree1 / avg_degree2
};
// 2. Compare relationship type distributions
let mut rel_types1 = HashMap::new();
let mut rel_types2 = HashMap::new();
for connections in adj1.values() {
for &(_, rel_type) in connections {
*rel_types1.entry(rel_type).or_insert(0) += 1;
}
}
for connections in adj2.values() {
for &(_, rel_type) in connections {
*rel_types2.entry(rel_type).or_insert(0) += 1;
}
}
let mut total_rels = 0;
let mut matching_rels = 0;
for (&rel_type, &count1) in &rel_types1 {
total_rels += count1;
if let Some(&count2) = rel_types2.get(&rel_type) {
matching_rels += count1.min(count2);
}
}
for (&_, &count2) in &rel_types2 {
total_rels += count2;
}
// Avoid double-counting
total_rels -= matching_rels;
let rel_type_similarity = if total_rels > 0 {
T::from_usize(matching_rels).unwrap() / T::from_usize(total_rels).unwrap()
} else {
T::zero()
};
// 3. Compare connection patterns using Weisfeiler-Lehman test-inspired approach
// (simplified version that looks at "shapes" of connections)
// Create signature strings for each node based on its connections
let mut signatures1 = vec![String::new(); region1.len()];
let mut signatures2 = vec![String::new(); region2.len()];
// Generate initial signatures based on dimensions and outgoing edge types
for (node, connections) in &adj1 {
let mut sig = Vec::new();
// Add node dimension to signature
if let Some(idx) = region1.get(*node) {
if let Some(feature) = self
.features
.iter()
.find(|f| f.id == *idx && f.death.is_none())
{
sig.push(feature.dimension);
}
}
// Add outgoing connection types (sorted)
let mut rel_sigs: Vec<_> = connections.iter().map(|&(_, rt)| rt).collect();
rel_sigs.sort_unstable();
sig.extend(rel_sigs);
// Create signature string
signatures1[*node] = format!("{:?}", sig);
}
for (node, connections) in &adj2 {
let mut sig = Vec::new();
// Add node dimension to signature
if let Some(idx) = region2.get(*node) {
if let Some(feature) = self
.features
.iter()
.find(|f| f.id == *idx && f.death.is_none())
{
sig.push(feature.dimension);
}
}
// Add outgoing connection types (sorted)
let mut rel_sigs: Vec<_> = connections.iter().map(|&(_, rt)| rt).collect();
rel_sigs.sort_unstable();
sig.extend(rel_sigs);
// Create signature string
signatures2[*node] = format!("{:?}", sig);
}
// Count signature distributions
let mut sig_counts1 = HashMap::new();
let mut sig_counts2 = HashMap::new();
for sig in &signatures1 {
if !sig.is_empty() {
*sig_counts1.entry(sig).or_insert(0) += 1;
}
}
for sig in &signatures2 {
if !sig.is_empty() {
*sig_counts2.entry(sig).or_insert(0) += 1;
}
}
// Compare signature distributions
let mut matching_sigs = 0;
let mut total_sigs = 0;
for (sig, &count1) in &sig_counts1 {
total_sigs += count1;
if let Some(&count2) = sig_counts2.get(sig) {
matching_sigs += count1.min(count2);
}
}
for &count2 in sig_counts2.values() {
total_sigs += count2;
}
// Avoid double-counting
total_sigs -= matching_sigs;
let structure_similarity = if total_sigs > 0 {
T::from_usize(matching_sigs).unwrap() / T::from_usize(total_sigs).unwrap()
} else {
T::zero()
};
// Combine the different similarity measures with appropriate weights
let w1 = T::from_f32(0.2).unwrap();
let w2 = T::from_f32(0.3).unwrap();
let w3 = T::from_f32(0.2).unwrap();
let w4 = T::from_f32(0.3).unwrap();
w1 * dim_similarity
+ w2 * degree_similarity
+ w3 * rel_type_similarity
+ w4 * structure_similarity
}
/// Calculate similarity between two sequences
pub(crate) fn sequence_similarity(seq1: &[usize], seq2: &[usize]) -> T {
let max_len = seq1.len().max(seq2.len());
if max_len == 0 {
return T::zero();
}
// Find longest common subsequence
let mut dp = vec![vec![0; seq2.len() + 1]; seq1.len() + 1];
for i in 1..=seq1.len() {
for j in 1..=seq2.len() {
if seq1[i - 1] == seq2[j - 1] {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = dp[i - 1][j].max(dp[i][j - 1]);
}
}
}
let lcs_length = dp[seq1.len()][seq2.len()];
T::from_usize(lcs_length).unwrap() / T::from_usize(max_len).unwrap()
}
}