@reicek/neataptic-ts 0.1.21 → 0.1.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/.github/agents/boundary-mapper.agent.md +29 -0
  2. package/.github/agents/docs-scout.agent.md +29 -0
  3. package/.github/agents/plan-scout.agent.md +29 -0
  4. package/.github/agents/solid-split.agent.md +138 -0
  5. package/.github/copilot-instructions.md +103 -0
  6. package/package.json +6 -3
  7. package/plans/ES2023 migration +13 -8
  8. package/plans/Evolution_Training_Interoperability_Contracts.md +1 -1
  9. package/plans/Interactive_Examples_and_Learning_Path.md +10 -2
  10. package/plans/Memory_Optimization.md +3 -3
  11. package/plans/README.md +63 -0
  12. package/plans/Roadmap.md +15 -3
  13. package/plans/asciiMaze_SOLID_split.done.md +130 -0
  14. package/plans/flappy_bird_SOLID_split.done.md +67 -0
  15. package/scripts/assets/theme.css +221 -34
  16. package/scripts/copy-examples.mjs +9 -5
  17. package/scripts/export-onnx.mjs +3 -3
  18. package/scripts/generate-bench-tables.mjs +10 -10
  19. package/scripts/generate-bench-tables.ts +10 -10
  20. package/scripts/generate-docs.ts +1415 -449
  21. package/scripts/render-docs-html.ts +15 -8
  22. package/src/README.md +101 -223
  23. package/src/architecture/README.md +57 -185
  24. package/src/architecture/layer/README.md +38 -38
  25. package/src/architecture/network/README.md +33 -31
  26. package/src/architecture/network/activate/README.md +77 -77
  27. package/src/architecture/network/connect/README.md +15 -13
  28. package/src/architecture/network/deterministic/README.md +7 -7
  29. package/src/architecture/network/evolve/README.md +44 -44
  30. package/src/architecture/network/gating/README.md +20 -20
  31. package/src/architecture/network/genetic/README.md +51 -51
  32. package/src/architecture/network/mutate/README.md +97 -97
  33. package/src/architecture/network/onnx/README.md +264 -264
  34. package/src/architecture/network/prune/README.md +39 -39
  35. package/src/architecture/network/remove/README.md +26 -26
  36. package/src/architecture/network/serialize/README.md +56 -56
  37. package/src/architecture/network/slab/README.md +61 -61
  38. package/src/architecture/network/standalone/README.md +24 -24
  39. package/src/architecture/network/stats/README.md +9 -9
  40. package/src/architecture/network/topology/README.md +46 -46
  41. package/src/architecture/network/training/README.md +21 -21
  42. package/src/methods/README.md +9 -87
  43. package/src/multithreading/README.md +8 -77
  44. package/src/multithreading/workers/README.md +2 -2
  45. package/src/multithreading/workers/browser/README.md +0 -6
  46. package/src/multithreading/workers/node/README.md +0 -3
  47. package/src/neat/README.md +562 -568
  48. package/src/utils/README.md +18 -18
  49. package/test/examples/asciiMaze/README.md +59 -59
  50. package/test/examples/asciiMaze/asciiMaze.e2e.test.ts +14 -9
  51. package/test/examples/asciiMaze/browser-entry/README.md +196 -0
  52. package/test/examples/asciiMaze/browser-entry/browser-entry.abort.services.ts +95 -0
  53. package/test/examples/asciiMaze/browser-entry/browser-entry.constants.ts +23 -0
  54. package/test/examples/asciiMaze/browser-entry/browser-entry.curriculum.services.ts +115 -0
  55. package/test/examples/asciiMaze/browser-entry/browser-entry.globals.services.ts +106 -0
  56. package/test/examples/asciiMaze/browser-entry/browser-entry.host.services.ts +157 -0
  57. package/test/examples/asciiMaze/browser-entry/browser-entry.services.ts +14 -0
  58. package/test/examples/asciiMaze/browser-entry/browser-entry.ts +129 -0
  59. package/test/examples/asciiMaze/browser-entry/browser-entry.types.ts +120 -0
  60. package/test/examples/asciiMaze/browser-entry/browser-entry.utils.ts +98 -0
  61. package/test/examples/asciiMaze/browser-entry.ts +10 -576
  62. package/test/examples/asciiMaze/dashboardManager/README.md +276 -0
  63. package/test/examples/asciiMaze/dashboardManager/archive/README.md +16 -0
  64. package/test/examples/asciiMaze/dashboardManager/archive/dashboardManager.archive.services.ts +267 -0
  65. package/test/examples/asciiMaze/dashboardManager/dashboardManager.constants.ts +35 -0
  66. package/test/examples/asciiMaze/dashboardManager/dashboardManager.services.ts +103 -0
  67. package/test/examples/asciiMaze/dashboardManager/dashboardManager.ts +181 -0
  68. package/test/examples/asciiMaze/dashboardManager/dashboardManager.types.ts +267 -0
  69. package/test/examples/asciiMaze/dashboardManager/dashboardManager.utils.ts +254 -0
  70. package/test/examples/asciiMaze/dashboardManager/live/README.md +14 -0
  71. package/test/examples/asciiMaze/dashboardManager/live/dashboardManager.live.services.ts +264 -0
  72. package/test/examples/asciiMaze/dashboardManager/telemetry/README.md +47 -0
  73. package/test/examples/asciiMaze/dashboardManager/telemetry/dashboardManager.telemetry.services.ts +513 -0
  74. package/test/examples/asciiMaze/dashboardManager.ts +13 -2335
  75. package/test/examples/asciiMaze/evolutionEngine/README.md +1058 -0
  76. package/test/examples/asciiMaze/evolutionEngine/curriculumPhase.ts +90 -0
  77. package/test/examples/asciiMaze/evolutionEngine/engineState.constants.ts +36 -0
  78. package/test/examples/asciiMaze/evolutionEngine/engineState.ts +58 -513
  79. package/test/examples/asciiMaze/evolutionEngine/engineState.types.ts +212 -0
  80. package/test/examples/asciiMaze/evolutionEngine/engineState.utils.ts +301 -0
  81. package/test/examples/asciiMaze/evolutionEngine/evolutionEngine.types.ts +445 -0
  82. package/test/examples/asciiMaze/evolutionEngine/evolutionLoop.ts +81 -50
  83. package/test/examples/asciiMaze/evolutionEngine/optionsAndSetup.ts +2 -4
  84. package/test/examples/asciiMaze/evolutionEngine/populationDynamics.ts +17 -33
  85. package/test/examples/asciiMaze/evolutionEngine/populationPruning.ts +1 -1
  86. package/test/examples/asciiMaze/evolutionEngine/rngAndTiming.ts +1 -2
  87. package/test/examples/asciiMaze/evolutionEngine/sampling.ts +1 -1
  88. package/test/examples/asciiMaze/evolutionEngine/scratchPools.ts +2 -5
  89. package/test/examples/asciiMaze/evolutionEngine/setupHelpers.ts +30 -37
  90. package/test/examples/asciiMaze/evolutionEngine/telemetryMetrics.ts +16 -58
  91. package/test/examples/asciiMaze/evolutionEngine/trainingWarmStart.ts +2 -2
  92. package/test/examples/asciiMaze/evolutionEngine.ts +55 -55
  93. package/test/examples/asciiMaze/fitness.ts +2 -2
  94. package/test/examples/asciiMaze/fitness.types.ts +65 -0
  95. package/test/examples/asciiMaze/interfaces.ts +64 -1352
  96. package/test/examples/asciiMaze/mazeMovement/README.md +356 -0
  97. package/test/examples/asciiMaze/mazeMovement/finalization/README.md +49 -0
  98. package/test/examples/asciiMaze/mazeMovement/finalization/mazeMovement.finalization.ts +138 -0
  99. package/test/examples/asciiMaze/mazeMovement/mazeMovement.constants.ts +101 -0
  100. package/test/examples/asciiMaze/mazeMovement/mazeMovement.services.ts +230 -0
  101. package/test/examples/asciiMaze/mazeMovement/mazeMovement.ts +299 -0
  102. package/test/examples/asciiMaze/mazeMovement/mazeMovement.types.ts +185 -0
  103. package/test/examples/asciiMaze/mazeMovement/mazeMovement.utils.ts +153 -0
  104. package/test/examples/asciiMaze/mazeMovement/policy/README.md +91 -0
  105. package/test/examples/asciiMaze/mazeMovement/policy/mazeMovement.policy.ts +467 -0
  106. package/test/examples/asciiMaze/mazeMovement/runtime/README.md +95 -0
  107. package/test/examples/asciiMaze/mazeMovement/runtime/mazeMovement.runtime.ts +354 -0
  108. package/test/examples/asciiMaze/mazeMovement/shaping/README.md +124 -0
  109. package/test/examples/asciiMaze/mazeMovement/shaping/mazeMovement.shaping.ts +459 -0
  110. package/test/examples/asciiMaze/mazeMovement.ts +12 -2978
  111. package/test/examples/flappy_bird/Trace-20260309T191949.json +24124 -0
  112. package/test/examples/flappy_bird/browser-entry/README.md +1129 -0
  113. package/test/examples/flappy_bird/browser-entry/browser-entry.host.utils.ts +4 -324
  114. package/test/examples/flappy_bird/browser-entry/browser-entry.network-view.utils.ts +6 -399
  115. package/test/examples/flappy_bird/browser-entry/browser-entry.playback.utils.ts +1 -717
  116. package/test/examples/flappy_bird/browser-entry/browser-entry.spawn.utils.ts +11 -31
  117. package/test/examples/flappy_bird/browser-entry/browser-entry.visualization.utils.ts +15 -893
  118. package/test/examples/flappy_bird/browser-entry/host/README.md +307 -0
  119. package/test/examples/flappy_bird/browser-entry/host/host.resize.service.ts +1 -295
  120. package/test/examples/flappy_bird/browser-entry/host/host.ts +562 -6
  121. package/test/examples/flappy_bird/browser-entry/host/resize/README.md +274 -0
  122. package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.constants.ts +31 -0
  123. package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.services.ts +360 -0
  124. package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.ts +117 -0
  125. package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.types.ts +63 -0
  126. package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.utils.ts +250 -0
  127. package/test/examples/flappy_bird/browser-entry/network-view/README.md +399 -0
  128. package/test/examples/flappy_bird/browser-entry/network-view/network-view.topology.utils.ts +255 -0
  129. package/test/examples/flappy_bird/browser-entry/network-view/network-view.ts +802 -7
  130. package/test/examples/flappy_bird/browser-entry/playback/README.md +684 -0
  131. package/test/examples/flappy_bird/browser-entry/playback/background/README.md +277 -0
  132. package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/README.md +770 -0
  133. package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.cache.services.ts +178 -0
  134. package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.constants.ts +107 -0
  135. package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.geometry.utils.ts +518 -0
  136. package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.math.utils.ts +117 -0
  137. package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.pulse.utils.ts +233 -0
  138. package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.services.ts +211 -0
  139. package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.ts +48 -0
  140. package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.types.ts +212 -0
  141. package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.utils.ts +81 -0
  142. package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.cache.services.ts +96 -0
  143. package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.constants.ts +62 -0
  144. package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.services.ts +244 -0
  145. package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.ts +53 -0
  146. package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.types.ts +68 -0
  147. package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.utils.ts +100 -0
  148. package/test/examples/flappy_bird/browser-entry/playback/frame-render/README.md +310 -0
  149. package/test/examples/flappy_bird/browser-entry/playback/frame-render/playback.frame-render.service.ts +92 -0
  150. package/test/examples/flappy_bird/browser-entry/playback/frame-render/playback.frame-render.services.ts +272 -0
  151. package/test/examples/flappy_bird/browser-entry/playback/frame-render/playback.frame-render.types.ts +39 -0
  152. package/test/examples/flappy_bird/browser-entry/playback/frame-render/playback.frame-render.utils.ts +493 -0
  153. package/test/examples/flappy_bird/browser-entry/playback/playback.constants.ts +1 -1
  154. package/test/examples/flappy_bird/browser-entry/playback/playback.frame-render.service.ts +4 -0
  155. package/test/examples/flappy_bird/browser-entry/playback/playback.snapshot.utils.ts +44 -0
  156. package/test/examples/flappy_bird/browser-entry/playback/playback.starfield.service.ts +39 -122
  157. package/test/examples/flappy_bird/browser-entry/playback/playback.starfield.services.ts +272 -0
  158. package/test/examples/flappy_bird/browser-entry/playback/playback.starfield.types.ts +62 -0
  159. package/test/examples/flappy_bird/browser-entry/playback/playback.starfield.utils.ts +11 -4
  160. package/test/examples/flappy_bird/browser-entry/playback/playback.ts +409 -8
  161. package/test/examples/flappy_bird/browser-entry/playback/playback.types.ts +4 -12
  162. package/test/examples/flappy_bird/browser-entry/runtime/README.md +235 -0
  163. package/test/examples/flappy_bird/browser-entry/runtime/runtime.evolution-launch.service.ts +45 -0
  164. package/test/examples/flappy_bird/browser-entry/runtime/runtime.lifecycle.service.ts +81 -0
  165. package/test/examples/flappy_bird/browser-entry/runtime/runtime.startup.service.ts +74 -0
  166. package/test/examples/flappy_bird/browser-entry/runtime/runtime.ts +31 -121
  167. package/test/examples/flappy_bird/browser-entry/runtime/runtime.types.ts +36 -0
  168. package/test/examples/flappy_bird/browser-entry/visualization/README.md +557 -0
  169. package/test/examples/flappy_bird/browser-entry/visualization/visualization.constants.ts +110 -0
  170. package/test/examples/flappy_bird/browser-entry/visualization/visualization.draw.service.ts +957 -19
  171. package/test/examples/flappy_bird/browser-entry/visualization/visualization.legend.utils.ts +138 -3
  172. package/test/examples/flappy_bird/browser-entry/visualization/visualization.topology.utils.ts +3 -27
  173. package/test/examples/flappy_bird/browser-entry/visualization/visualization.ts +1 -23
  174. package/test/examples/flappy_bird/browser-entry/worker-channel/README.md +156 -0
  175. package/test/examples/flappy_bird/constants/README.md +1179 -0
  176. package/test/examples/flappy_bird/constants/constants.network-view.ts +24 -0
  177. package/test/examples/flappy_bird/constants/constants.palette.ts +7 -0
  178. package/test/examples/flappy_bird/constants/constants.starfield.ts +78 -3
  179. package/test/examples/flappy_bird/environment/README.md +143 -0
  180. package/test/examples/flappy_bird/environment/environment.observation.utils.ts +1 -19
  181. package/test/examples/flappy_bird/environment/environment.step.service.ts +3 -66
  182. package/test/examples/flappy_bird/evaluation/README.md +130 -0
  183. package/test/examples/flappy_bird/evaluation/evaluation.fitness.utils.ts +1 -1
  184. package/test/examples/flappy_bird/evaluation/evaluation.rollout.service.ts +5 -375
  185. package/test/examples/flappy_bird/evaluation/rollout/README.md +291 -0
  186. package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.constants.ts +30 -0
  187. package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.service.ts +58 -0
  188. package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.services.ts +310 -0
  189. package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.types.ts +56 -0
  190. package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.utils.ts +368 -0
  191. package/test/examples/flappy_bird/flappy-evolution-worker/README.md +618 -0
  192. package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.playback.service.ts +7 -7
  193. package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.simulation.frame.service.ts +364 -0
  194. package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.simulation.types.ts +14 -0
  195. package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.simulation.utils.ts +4 -201
  196. package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.ts +184 -345
  197. package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.warm-start.service.ts +291 -0
  198. package/test/examples/flappy_bird/flappy.simulation.shared.utils.ts +5 -0
  199. package/test/examples/flappy_bird/simulation-shared/README.md +417 -0
  200. package/test/examples/flappy_bird/simulation-shared/observation/README.md +183 -0
  201. package/test/examples/flappy_bird/simulation-shared/observation/observation.features.utils.ts +301 -0
  202. package/test/examples/flappy_bird/simulation-shared/observation/observation.ts +9 -0
  203. package/test/examples/flappy_bird/simulation-shared/observation/observation.vector.utils.ts +59 -0
  204. package/test/examples/flappy_bird/simulation-shared/simulation-shared.observation.utils.ts +5 -403
  205. package/test/examples/flappy_bird/simulation-shared/simulation-shared.spawn.utils.ts +20 -6
  206. package/test/examples/flappy_bird/{evaluation/evaluation.statistics.utils.ts → simulation-shared/simulation-shared.statistics.utils.ts} +23 -8
  207. package/test/examples/flappy_bird/trainer/README.md +563 -0
  208. package/test/examples/flappy_bird/trainer/evaluation/README.md +199 -0
  209. package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.constants.ts +9 -0
  210. package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.services.ts +73 -0
  211. package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.ts +165 -0
  212. package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.types.ts +25 -0
  213. package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.utils.ts +161 -0
  214. package/test/examples/flappy_bird/trainer/trainer.evaluation.service.ts +13 -0
  215. package/test/examples/flappy_bird/trainer/trainer.report.service.services.ts +181 -0
  216. package/test/examples/flappy_bird/trainer/trainer.report.service.ts +126 -0
  217. package/test/examples/flappy_bird/trainer/trainer.selection.utils.ts +89 -0
  218. package/test/examples/flappy_bird/trainer/trainer.ts +11 -553
  219. package/test/examples/flappy_bird/browser-entry/browser-entry.utils.ts +0 -12
  220. package/test/examples/flappy_bird/environment/environment.ts +0 -7
  221. package/test/examples/flappy_bird/evaluation/evaluation.ts +0 -7
  222. package/test/examples/flappy_bird/simulation-shared/simulation-shared.ts +0 -15
  223. package/test/examples/flappy_bird/trainer/trainer.statistics.utils.ts +0 -78
@@ -1,3 +1,61 @@
1
+ import type {
2
+ EngineProfilingState,
3
+ EngineScratchState,
4
+ EngineState,
5
+ EngineToggleState,
6
+ RngCacheHandles,
7
+ RngCacheParameters,
8
+ TelemetryScratchHandles,
9
+ TelemetryScratchRequest,
10
+ VisitedHashScratchHandles,
11
+ } from './engineState.types';
12
+ import {
13
+ ACTION_OUTPUT_DIMENSION,
14
+ DEFAULT_CONNECTION_FLAG_CAPACITY,
15
+ DEFAULT_HISTORY_BUFFER_CAPACITY,
16
+ DEFAULT_NODE_INDEX_BUFFER_CAPACITY,
17
+ DEFAULT_QUICKSORT_STACK_CAPACITY,
18
+ DEFAULT_RNG_CACHE_BATCH_SIZE,
19
+ DEFAULT_SAMPLE_POOL_SIZE,
20
+ DEFAULT_SMALL_EXPLORE_TABLE_CAPACITY,
21
+ DEFAULT_SORTED_INDEX_CAPACITY,
22
+ DEFAULT_SPECIES_SCRATCH_CAPACITY,
23
+ DEFAULT_STRING_BUFFER_CAPACITY,
24
+ DEFAULT_VISITED_HASH_LOAD_FACTOR,
25
+ RNG_GOLDEN_RATIO_SEED,
26
+ } from './engineState.constants';
27
+ import {
28
+ buildTelemetryHandles,
29
+ createLogitsRing,
30
+ createProfilingAccumulators,
31
+ createSnapshotReusableObject,
32
+ ensureTelemetryFloatPools,
33
+ ensureTelemetryStringBuffer,
34
+ normaliseRngBatchSize,
35
+ normaliseRngSeed,
36
+ normaliseTelemetryCapacityHints,
37
+ normaliseVisitedHashEntries,
38
+ normaliseVisitedHashLoad,
39
+ nextPowerOfTwo,
40
+ resolveProfilingEnabled,
41
+ } from './engineState.utils';
42
+
43
+ export type {
44
+ EngineProfilingState,
45
+ EngineScratchState,
46
+ EngineState,
47
+ EngineToggleState,
48
+ RngCacheHandles,
49
+ RngCacheParameters,
50
+ TelemetryScratchHandles,
51
+ TelemetryScratchRequest,
52
+ VisitedHashScratchHandles,
53
+ } from './engineState.types';
54
+ export {
55
+ DEFAULT_RNG_CACHE_BATCH_SIZE,
56
+ DEFAULT_VISITED_HASH_LOAD_FACTOR,
57
+ } from './engineState.constants';
58
+
1
59
  /**
2
60
  * Centralised shared state for the ASCII maze evolution façade.
3
61
  *
@@ -9,220 +67,6 @@
9
67
  *
10
68
  * Callers mutate the returned scratch instances in place to avoid per-generation allocations; higher-level modules should treat the helpers as the sole entry point for sizing or resetting shared buffers.
11
69
  */
12
- export interface EngineScratchState {
13
- /** Softmax exponent scratch reused by telemetry entropy calculations. */
14
- exps: Float64Array;
15
- /** Mean accumulator reused for output bias statistics. */
16
- means: Float64Array;
17
- /** Standard deviation accumulator shared by telemetry computations. */
18
- standardDeviations: Float64Array;
19
- /** Bias scratch workspace reused for output-bias telemetry calculations. */
20
- biasTelemetryScratch: Float64Array;
21
- /** Optional kurtosis accumulator allocated when full telemetry is enabled. */
22
- kurtosis?: Float64Array;
23
- /** Second raw moment buffer for variance calculations. */
24
- secondMomentRaw: Float64Array;
25
- /** Optional third raw moment buffer for skewness metrics. */
26
- thirdMomentRaw?: Float64Array;
27
- /** Optional fourth raw moment buffer for kurtosis metrics. */
28
- fourthMomentRaw?: Float64Array;
29
- /** Directional move counters reused during exploration stats. */
30
- moveCounts: Int32Array;
31
- /** Open-address hash table used for visited coordinate detection. */
32
- visitedHashTable: Int32Array;
33
- /** Current load factor threshold guiding visited-hash resizes. */
34
- visitedHashLoadFactor: number;
35
- /** Species id scratch array reused by population sorting. */
36
- speciesIds: Int32Array;
37
- /** Species count scratch array kept parallel to {@link speciesIds}. */
38
- speciesCounts: Int32Array;
39
- /** Candidate connection objects pooled for pruning heuristics. */
40
- connectionCandidates: Record<string, unknown>[];
41
- /** Hidden-to-output connection list reused during inspection. */
42
- hiddenToOutputConnections: Record<string, unknown>[];
43
- /** Bit flags for connection enable/disable states. */
44
- connectionFlags: Uint8Array;
45
- /** Bitmap reused when detecting recurrent or gated connections. */
46
- connectionFlagBitmap?: Int8Array;
47
- /** Maze tail history reused to avoid reallocating per telemetry call. */
48
- tailHistoryBuffer: Array<unknown>;
49
- /** Sampled result buffer reused by array sampling helpers. */
50
- sampleResultBuffer: Array<unknown>;
51
- /** Sorted index scratch reused when ranking genomes. */
52
- sortedIndexBuffer: number[];
53
- /** Optional typed view of {@link sortedIndexBuffer} for faster sorts. */
54
- sortedIndexTypedArray?: Int32Array;
55
- /** Quicksort stack storing pending index ranges. */
56
- quicksortStack: Int32Array;
57
- /** Temporary population clone array used during expansion. */
58
- populationCloneBuffer: Record<string, unknown>[];
59
- /** Activation name buffer reused by inspection routines. */
60
- activationNameBuffer: string[];
61
- /** Node classification buckets reused by inspection routines. */
62
- nodeBuckets: [
63
- Record<string, unknown>[],
64
- Record<string, unknown>[],
65
- Record<string, unknown>[],
66
- ];
67
- /** Top entry objects reused when generating snapshots. */
68
- snapshotTopEntries: Record<string, unknown>[];
69
- /** Snapshot metadata object reused per persistence write. */
70
- snapshotReusableObject: Record<string, unknown>;
71
- /** Mutation operator index buffer shuffled each generation. */
72
- mutationOperatorIndices: Uint16Array;
73
- /** Object pool used when sampling individuals for telemetry. */
74
- samplePool: Array<unknown>;
75
- /** Character array reused when assembling debug strings. */
76
- stringAssemblyBuffer: string[];
77
- /** Small exploration table for low-cost duplicate detection. */
78
- smallExploreTable: Int32Array;
79
- /** Non-shared logits ring storing recent action logits. */
80
- logitsRing: Float32Array[];
81
- /** Write cursor for the non-shared logits ring. */
82
- logitsRingWriteCursor: number;
83
- /** Shared float buffer backing the logits ring when SAB is available. */
84
- sharedLogits?: Float32Array;
85
- /** Shared atomic write index for the SAB-backed logits ring. */
86
- sharedLogitsWriteIndex?: Int32Array;
87
- /** Scratch node index pool reused by topology inspection. */
88
- nodeIndexBuffer: Int32Array;
89
- /** Small profiling scratch buffer reused during timing accumulation. */
90
- profilingScratch?: Float64Array;
91
- /** Linear congruential RNG state preserved across cache refills. */
92
- rngState: number;
93
- /** Cached batch of uniform random numbers reused by the façade. */
94
- rngCache: Float64Array;
95
- /** Next unread index within {@link rngCache}. */
96
- rngCacheIndex: number;
97
- /** Allow additional properties for extensibility */
98
- [key: string]: unknown;
99
- }
100
-
101
- /** Default logits ring length used when allocating pooled softmax buffers. */
102
- const DEFAULT_LOGITS_RING_CAPACITY = 512;
103
- /** Number of action outputs (N, E, S, W) represented in each logits row. */
104
- const ACTION_OUTPUT_DIMENSION = 4;
105
- /** Default load factor target for the visited coordinate hash table. */
106
- export const DEFAULT_VISITED_HASH_LOAD_FACTOR = 0.7;
107
- /** Minimum safe load factor applied when normalising visited-hash configuration. */
108
- const MIN_VISITED_HASH_LOAD_FACTOR = 0.1;
109
- /** Maximum safe load factor applied when normalising visited-hash configuration. */
110
- const MAX_VISITED_HASH_LOAD_FACTOR = 0.95;
111
- /** Default RNG cache batch size mirroring the façade constant. */
112
- export const DEFAULT_RNG_CACHE_BATCH_SIZE = 4;
113
-
114
- /** Default capacity reserved for species identifier scratch arrays. */
115
- const DEFAULT_SPECIES_SCRATCH_CAPACITY = 64;
116
- /** Default capacity reserved for connection flag buffers. */
117
- const DEFAULT_CONNECTION_FLAG_CAPACITY = 128;
118
- /** Default capacity reused by history and sampling scratch arrays. */
119
- const DEFAULT_HISTORY_BUFFER_CAPACITY = 64;
120
- /** Default capacity reserved for sorted index scratch arrays. */
121
- const DEFAULT_SORTED_INDEX_CAPACITY = 512;
122
- /** Default stack depth reserved for quicksort range storage. */
123
- const DEFAULT_QUICKSORT_STACK_CAPACITY = 128;
124
- /** Default pool size for telemetry sampling helpers. */
125
- const DEFAULT_SAMPLE_POOL_SIZE = 40;
126
- /** Default capacity for telemetry string assembly buffers. */
127
- const DEFAULT_STRING_BUFFER_CAPACITY = 64;
128
- /** Default capacity for the small exploration table scratch. */
129
- const DEFAULT_SMALL_EXPLORE_TABLE_CAPACITY = 64;
130
- /** Default capacity for the node index buffer used during inspection. */
131
- const DEFAULT_NODE_INDEX_BUFFER_CAPACITY = 64;
132
- /** Knuth-derived 32-bit constant used when seeding the RNG state. */
133
- const RNG_GOLDEN_RATIO_SEED = 0x9e3779b9;
134
-
135
- /**
136
- * Build a logits ring sized to the default capacity.
137
- * @returns Array of Float32Array rows sized to {@link ACTION_OUTPUT_DIMENSION}.
138
- */
139
- const createLogitsRing = (): Float32Array[] => {
140
- const rows = new Array<Float32Array>(DEFAULT_LOGITS_RING_CAPACITY);
141
- for (let ringIndex = 0; ringIndex < DEFAULT_LOGITS_RING_CAPACITY; ringIndex++)
142
- rows[ringIndex] = new Float32Array(ACTION_OUTPUT_DIMENSION);
143
- return rows;
144
- };
145
-
146
- /**
147
- * Runtime switches that adjust telemetry verbosity and optional training phases.
148
- */
149
- export interface EngineToggleState {
150
- /** Softens telemetry to a reduced metric set. */
151
- reducedTelemetry: boolean;
152
- /** Enables the most compact telemetry output footprint. */
153
- telemetryMinimal: boolean;
154
- /** Disables the Baldwin (Lamarckian) warm-start phase. */
155
- disableBaldwinPhase: boolean;
156
- }
157
-
158
- /**
159
- * Aggregated profiling configuration and accumulators shared across the evolution run.
160
- */
161
- export interface EngineProfilingState {
162
- /** Indicates whether detailed profiling accumulation is active. */
163
- detailsEnabled: boolean;
164
- /** Rolling millisecond totals grouped by profiling segment key. */
165
- accumulators: Record<string, number>;
166
- }
167
-
168
- /** Compute whether detailed profiling is enabled via the environment flag. */
169
- const resolveProfilingEnabled = (): boolean => {
170
- try {
171
- return (
172
- typeof process !== 'undefined' &&
173
- process?.env?.ASCII_MAZE_PROFILE_DETAILS === '1'
174
- );
175
- } catch {
176
- return false;
177
- }
178
- };
179
-
180
- /**
181
- * Build a fresh profiling accumulator map seeded with zero totals.
182
- * @returns Accumulator record keyed by profiling segment name.
183
- */
184
- const createProfilingAccumulators = (): Record<string, number> => ({
185
- telemetry: 0,
186
- simplify: 0,
187
- snapshot: 0,
188
- prune: 0,
189
- });
190
-
191
- /**
192
- * Shared engine state instance combining pooled scratch buffers with toggle flags.
193
- */
194
- export interface EngineState {
195
- /** Bundle of pooled scratch buffers shared by the façade. */
196
- scratch: EngineScratchState;
197
- /** Runtime toggles influencing telemetry and training behaviour. */
198
- toggles: EngineToggleState;
199
- /** Profiling configuration and accumulated timings. */
200
- profiling: EngineProfilingState;
201
- /** Indicates whether deterministic RNG mode is enabled. */
202
- deterministicMode: boolean;
203
- }
204
-
205
- /**
206
- * Build the reusable snapshot metadata payload consumed by persistence helpers.
207
- * @returns Snapshot placeholder populated with neutral defaults.
208
- */
209
- const createSnapshotReusableObject = () => ({
210
- /** Snapshot generation index. */
211
- generation: 0,
212
- /** Snapshot best fitness value. */
213
- bestFitness: 0,
214
- /** Flag indicating simplify mode state. */
215
- simplifyMode: false,
216
- /** Plateau counter for simplify heuristics. */
217
- plateauCounter: 0,
218
- /** Snapshot timestamp. */
219
- timestamp: 0,
220
- /** Telemetry tail cache. */
221
- telemetryTail: undefined,
222
- /** Top entry payload. */
223
- top: undefined,
224
- });
225
-
226
70
  /**
227
71
  * Fabricates the default pooled scratch buffers shared by the evolution façade.
228
72
  * @returns Scratch bundle sized for the baseline maze curriculum workload.
@@ -352,76 +196,6 @@ export const createEngineState = (): EngineState => ({
352
196
  deterministicMode: false,
353
197
  });
354
198
 
355
- /**
356
- * Configuration describing which telemetry scratch buffers require capacity guarantees.
357
- */
358
- export interface TelemetryScratchRequest {
359
- /** Desired action dimensionality for logit statistics helpers. */
360
- actionDimension?: number;
361
- /** When true ensure higher-moment buffers (M3/M4/kurtosis) are allocated. */
362
- includeHigherMoments?: boolean;
363
- /** Number of output biases expected when formatting telemetry strings. */
364
- biasCount?: number;
365
- /** String assembly buffer length requirement (defaults to {@link biasCount}). */
366
- stringBufferLength?: number;
367
- }
368
-
369
- /**
370
- * Collection of scratch buffers handed back after initialisation for convenience.
371
- */
372
- export interface TelemetryScratchHandles {
373
- /** Float64 scratch used for exponentiation during entropy calculations. */
374
- exponentScratch: Float64Array;
375
- /** Running mean accumulator workspace. */
376
- meanScratch: Float64Array;
377
- /** Population standard deviation workspace. */
378
- standardDeviationScratch: Float64Array;
379
- /** Second raw moment workspace. */
380
- secondMomentScratch: Float64Array;
381
- /** Optional third raw moment workspace (present when higher moments requested). */
382
- thirdMomentScratch?: Float64Array;
383
- /** Optional fourth raw moment workspace (present when higher moments requested). */
384
- fourthMomentScratch?: Float64Array;
385
- /** Optional kurtosis workspace (present when higher moments requested). */
386
- kurtosisScratch?: Float64Array;
387
- /** Bias accumulator buffer used by output-bias telemetry helpers. */
388
- biasScratch: Float64Array;
389
- /** Shared string assembly buffer reused across telemetry loggers. */
390
- stringBuffer: string[];
391
- }
392
-
393
- /** Parameters controlling the RNG cache refill process. */
394
- export interface RngCacheParameters {
395
- /** Number of samples generated per congruential batch. */
396
- batchSize: number;
397
- /** Linear congruential multiplier component. */
398
- multiplier: number;
399
- /** Linear congruential increment component. */
400
- increment: number;
401
- /** Bit shift applied before scaling floats into [0,1). */
402
- shift: number;
403
- /** Scalar applied to produce [0,1) floats from shifted integers. */
404
- scale: number;
405
- }
406
-
407
- /** Handles returned after ensuring the RNG cache is ready for consumption. */
408
- export interface RngCacheHandles {
409
- /** Cached uniform samples ready for reuse. */
410
- cache: Float64Array;
411
- /** Batch size associated with the cache. */
412
- batchSize: number;
413
- }
414
-
415
- /**
416
- * Handles exposed after ensuring the visited-coordinate hash table capacity.
417
- */
418
- export interface VisitedHashScratchHandles {
419
- /** Cleared Int32Array hash table ready for inserts. */
420
- table: Int32Array;
421
- /** Bitmask used for wraparound during linear probing (table.length - 1). */
422
- slotMask: number;
423
- }
424
-
425
199
  /**
426
200
  * Ensure the visited-coordinate hash table can store the requested entry count.
427
201
  *
@@ -548,235 +322,6 @@ export const reseedRngState = (
548
322
  return normalisedSeed;
549
323
  };
550
324
 
551
- interface TelemetryCapacityHints {
552
- actionDimension: number;
553
- requiresHigherMoments: boolean;
554
- biasCount: number;
555
- stringLength: number;
556
- }
557
-
558
- /**
559
- * Derive normalised capacity hints from the raw telemetry scratch request.
560
- * @param request Raw capacity request supplied by callers.
561
- * @returns Sanitised capacity values used during buffer initialisation.
562
- */
563
- const normaliseTelemetryCapacityHints = (
564
- request: TelemetryScratchRequest,
565
- ): TelemetryCapacityHints => {
566
- const normaliseSize = (value: number | undefined, fallback = 0): number => {
567
- if (!Number.isFinite(value as number))
568
- return Math.max(0, Math.floor(fallback));
569
- return Math.max(0, Math.floor(value as number));
570
- };
571
-
572
- const biasCount = normaliseSize(request.biasCount);
573
- return {
574
- actionDimension: normaliseSize(request.actionDimension),
575
- requiresHigherMoments: Boolean(request.includeHigherMoments),
576
- biasCount,
577
- stringLength: normaliseSize(request.stringBufferLength, biasCount),
578
- };
579
- };
580
-
581
- /**
582
- * Ensure all Float64 scratch pools required for telemetry are adequately sized.
583
- * @param scratch Shared scratch state mutated in place.
584
- * @param hints Normalised capacity hints (action dimension, bias count, etc.).
585
- */
586
- const ensureTelemetryFloatPools = (
587
- scratch: EngineScratchState,
588
- hints: TelemetryCapacityHints,
589
- ): void => {
590
- const minActionDim = Math.max(4, hints.actionDimension);
591
- scratch.exps = ensureFloat64Pool(
592
- scratch.exps,
593
- minActionDim,
594
- 4,
595
- ); /* exponent scratch for entropy */
596
- scratch.means = ensureFloat64Pool(
597
- scratch.means,
598
- hints.actionDimension,
599
- 1,
600
- ); /* running mean per action */
601
- scratch.standardDeviations = ensureFloat64Pool(
602
- scratch.standardDeviations,
603
- hints.actionDimension,
604
- 1,
605
- ); /* std aggregation buffer */
606
- scratch.secondMomentRaw = ensureFloat64Pool(
607
- scratch.secondMomentRaw,
608
- hints.actionDimension,
609
- 1,
610
- ); /* Welford M2 accumulator */
611
- scratch.biasTelemetryScratch = ensureFloat64Pool(
612
- scratch.biasTelemetryScratch,
613
- hints.biasCount,
614
- 1,
615
- ); /* bias stats workspace */
616
-
617
- if (!hints.requiresHigherMoments) return;
618
-
619
- scratch.thirdMomentRaw = ensureOptionalFloat64Pool(
620
- scratch.thirdMomentRaw,
621
- hints.actionDimension,
622
- ); /* optional skewness (M3) */
623
- scratch.fourthMomentRaw = ensureOptionalFloat64Pool(
624
- scratch.fourthMomentRaw,
625
- hints.actionDimension,
626
- ); /* optional kurtosis (M4) */
627
- scratch.kurtosis = ensureOptionalFloat64Pool(
628
- scratch.kurtosis,
629
- hints.actionDimension,
630
- ); /* derived excess kurtosis */
631
- };
632
-
633
- /**
634
- * Ensure the reusable string assembly buffer has sufficient capacity.
635
- * @param buffer Existing string buffer instance.
636
- * @param required Minimum number of slots needed for upcoming telemetry joins.
637
- * @returns Original buffer when large enough, otherwise a grown copy.
638
- */
639
- const ensureTelemetryStringBuffer = (
640
- buffer: string[],
641
- required: number,
642
- ): string[] => ensureArrayCapacity(buffer, required);
643
-
644
- /**
645
- * Build typed handles referencing the ensured telemetry scratch buffers.
646
- * @param scratch Scratch state containing the prepared buffers.
647
- * @returns Structured handles consumed by telemetry helpers.
648
- */
649
- const buildTelemetryHandles = (
650
- scratch: EngineScratchState,
651
- ): TelemetryScratchHandles => ({
652
- exponentScratch: scratch.exps,
653
- meanScratch: scratch.means,
654
- standardDeviationScratch: scratch.standardDeviations,
655
- secondMomentScratch: scratch.secondMomentRaw,
656
- thirdMomentScratch: scratch.thirdMomentRaw,
657
- fourthMomentScratch: scratch.fourthMomentRaw,
658
- kurtosisScratch: scratch.kurtosis,
659
- biasScratch: scratch.biasTelemetryScratch,
660
- stringBuffer: scratch.stringAssemblyBuffer,
661
- });
662
-
663
- /**
664
- * Ensure a required Float64Array pool satisfies the requested capacity using power-of-two growth.
665
- * @param buffer Existing Float64Array instance reused by the engine.
666
- * @param required Minimum length the caller needs.
667
- * @param minimum Optional lower bound applied before comparison.
668
- * @returns Buffer with sufficient capacity (original or grown).
669
- */
670
- const ensureFloat64Pool = (
671
- buffer: Float64Array,
672
- required: number,
673
- minimum = 0,
674
- ): Float64Array => {
675
- const target = Math.max(required, minimum);
676
- if (target <= 0 || buffer.length >= target) return buffer;
677
- const nextSize = nextPowerOfTwo(target);
678
- const next = new Float64Array(nextSize);
679
- if (buffer.length > 0) {
680
- next.set(buffer.subarray(0, Math.min(buffer.length, nextSize)));
681
- }
682
- return next;
683
- };
684
-
685
- /**
686
- * Ensure an optional Float64Array is present and sized appropriately.
687
- * @param buffer Optional Float64Array reference to validate.
688
- * @param required Minimum required capacity; zero preserves the existing buffer.
689
- * @returns Buffer with sufficient capacity or undefined when no allocation is required.
690
- */
691
- const ensureOptionalFloat64Pool = (
692
- buffer: Float64Array | undefined,
693
- required: number,
694
- ): Float64Array | undefined => {
695
- if (required <= 0) return buffer;
696
- if (!buffer) return new Float64Array(nextPowerOfTwo(required));
697
- if (buffer.length >= required) return buffer;
698
- const nextSize = nextPowerOfTwo(required);
699
- const next = new Float64Array(nextSize);
700
- next.set(buffer.subarray(0, Math.min(buffer.length, nextSize)));
701
- return next;
702
- };
703
-
704
- /**
705
- * Ensure a generic Array buffer has enough slots, reusing existing entries when grown.
706
- * @param buffer Buffer instance to validate.
707
- * @param required Minimum number of elements required.
708
- * @returns Buffer with sufficient capacity (original or grown).
709
- */
710
- const ensureArrayCapacity = <T>(buffer: T[], required: number): T[] => {
711
- if (required <= 0 || buffer.length >= required) return buffer;
712
- const nextSize = nextPowerOfTwo(required);
713
- const grown = new Array<T>(nextSize);
714
- for (let index = 0; index < buffer.length; index++) {
715
- grown[index] = buffer[index];
716
- }
717
- return grown;
718
- };
719
-
720
- /**
721
- * Compute the next power-of-two for geometric growth.
722
- * @param candidate Raw size candidate.
723
- * @returns Smallest power-of-two >= candidate.
724
- */
725
- const nextPowerOfTwo = (candidate: number): number => {
726
- if (candidate <= 1) return 1;
727
- return 1 << Math.ceil(Math.log2(candidate));
728
- };
729
-
730
- /**
731
- * Clamp the requested target entry count to a non-negative integer.
732
- * @param requestedEntries Raw target entry count supplied by callers.
733
- * @returns Sanitised entry count used for capacity planning.
734
- */
735
- const normaliseVisitedHashEntries = (requestedEntries: number): number => {
736
- if (!Number.isFinite(requestedEntries)) return 0;
737
- return Math.max(0, Math.floor(requestedEntries));
738
- };
739
-
740
- /**
741
- * Ensure the visited hash load factor falls within a sensible (0,1) range.
742
- * @param requestedLoadFactor Proposed load factor from configuration or state.
743
- * @returns Clamped load factor with a fallback to the project default.
744
- */
745
- const normaliseVisitedHashLoad = (requestedLoadFactor: number): number => {
746
- if (!Number.isFinite(requestedLoadFactor)) {
747
- return DEFAULT_VISITED_HASH_LOAD_FACTOR;
748
- }
749
- if (
750
- requestedLoadFactor <= MIN_VISITED_HASH_LOAD_FACTOR ||
751
- requestedLoadFactor >= MAX_VISITED_HASH_LOAD_FACTOR
752
- ) {
753
- return DEFAULT_VISITED_HASH_LOAD_FACTOR;
754
- }
755
- return requestedLoadFactor;
756
- };
757
-
758
- /**
759
- * Clamp the RNG cache batch size to a positive integer.
760
- * @param requestedBatchSize Raw batch size requested by callers.
761
- * @returns Valid batch size (minimum of one sample per refill).
762
- */
763
- const normaliseRngBatchSize = (requestedBatchSize: number): number => {
764
- if (!Number.isFinite(requestedBatchSize)) {
765
- return DEFAULT_RNG_CACHE_BATCH_SIZE;
766
- }
767
- return Math.max(1, Math.floor(requestedBatchSize));
768
- };
769
-
770
- /**
771
- * Normalise a raw numeric seed into an unsigned 32-bit value (remapping zero).
772
- * @param rawSeed Raw seed provided by the caller.
773
- * @returns Unsigned 32-bit seed suitable for the congruential generator.
774
- */
775
- const normaliseRngSeed = (rawSeed: number): number => {
776
- const candidate = rawSeed >>> 0;
777
- return candidate === 0 ? 0x9e3779b9 : candidate;
778
- };
779
-
780
325
  /**
781
326
  * Singleton engine state shared by the evolution façade and extracted modules.
782
327
  */