@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.
- package/.github/agents/boundary-mapper.agent.md +29 -0
- package/.github/agents/docs-scout.agent.md +29 -0
- package/.github/agents/plan-scout.agent.md +29 -0
- package/.github/agents/solid-split.agent.md +138 -0
- package/.github/copilot-instructions.md +103 -0
- package/package.json +6 -3
- package/plans/ES2023 migration +13 -8
- package/plans/Evolution_Training_Interoperability_Contracts.md +1 -1
- package/plans/Interactive_Examples_and_Learning_Path.md +10 -2
- package/plans/Memory_Optimization.md +3 -3
- package/plans/README.md +63 -0
- package/plans/Roadmap.md +15 -3
- package/plans/asciiMaze_SOLID_split.done.md +130 -0
- package/plans/flappy_bird_SOLID_split.done.md +67 -0
- package/scripts/assets/theme.css +221 -34
- package/scripts/copy-examples.mjs +9 -5
- package/scripts/export-onnx.mjs +3 -3
- package/scripts/generate-bench-tables.mjs +10 -10
- package/scripts/generate-bench-tables.ts +10 -10
- package/scripts/generate-docs.ts +1415 -449
- package/scripts/render-docs-html.ts +15 -8
- package/src/README.md +101 -223
- package/src/architecture/README.md +57 -185
- package/src/architecture/layer/README.md +38 -38
- package/src/architecture/network/README.md +33 -31
- package/src/architecture/network/activate/README.md +77 -77
- package/src/architecture/network/connect/README.md +15 -13
- package/src/architecture/network/deterministic/README.md +7 -7
- package/src/architecture/network/evolve/README.md +44 -44
- package/src/architecture/network/gating/README.md +20 -20
- package/src/architecture/network/genetic/README.md +51 -51
- package/src/architecture/network/mutate/README.md +97 -97
- package/src/architecture/network/onnx/README.md +264 -264
- package/src/architecture/network/prune/README.md +39 -39
- package/src/architecture/network/remove/README.md +26 -26
- package/src/architecture/network/serialize/README.md +56 -56
- package/src/architecture/network/slab/README.md +61 -61
- package/src/architecture/network/standalone/README.md +24 -24
- package/src/architecture/network/stats/README.md +9 -9
- package/src/architecture/network/topology/README.md +46 -46
- package/src/architecture/network/training/README.md +21 -21
- package/src/methods/README.md +9 -87
- package/src/multithreading/README.md +8 -77
- package/src/multithreading/workers/README.md +2 -2
- package/src/multithreading/workers/browser/README.md +0 -6
- package/src/multithreading/workers/node/README.md +0 -3
- package/src/neat/README.md +562 -568
- package/src/utils/README.md +18 -18
- package/test/examples/asciiMaze/README.md +59 -59
- package/test/examples/asciiMaze/asciiMaze.e2e.test.ts +14 -9
- package/test/examples/asciiMaze/browser-entry/README.md +196 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.abort.services.ts +95 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.constants.ts +23 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.curriculum.services.ts +115 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.globals.services.ts +106 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.host.services.ts +157 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.services.ts +14 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.ts +129 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.types.ts +120 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.utils.ts +98 -0
- package/test/examples/asciiMaze/browser-entry.ts +10 -576
- package/test/examples/asciiMaze/dashboardManager/README.md +276 -0
- package/test/examples/asciiMaze/dashboardManager/archive/README.md +16 -0
- package/test/examples/asciiMaze/dashboardManager/archive/dashboardManager.archive.services.ts +267 -0
- package/test/examples/asciiMaze/dashboardManager/dashboardManager.constants.ts +35 -0
- package/test/examples/asciiMaze/dashboardManager/dashboardManager.services.ts +103 -0
- package/test/examples/asciiMaze/dashboardManager/dashboardManager.ts +181 -0
- package/test/examples/asciiMaze/dashboardManager/dashboardManager.types.ts +267 -0
- package/test/examples/asciiMaze/dashboardManager/dashboardManager.utils.ts +254 -0
- package/test/examples/asciiMaze/dashboardManager/live/README.md +14 -0
- package/test/examples/asciiMaze/dashboardManager/live/dashboardManager.live.services.ts +264 -0
- package/test/examples/asciiMaze/dashboardManager/telemetry/README.md +47 -0
- package/test/examples/asciiMaze/dashboardManager/telemetry/dashboardManager.telemetry.services.ts +513 -0
- package/test/examples/asciiMaze/dashboardManager.ts +13 -2335
- package/test/examples/asciiMaze/evolutionEngine/README.md +1058 -0
- package/test/examples/asciiMaze/evolutionEngine/curriculumPhase.ts +90 -0
- package/test/examples/asciiMaze/evolutionEngine/engineState.constants.ts +36 -0
- package/test/examples/asciiMaze/evolutionEngine/engineState.ts +58 -513
- package/test/examples/asciiMaze/evolutionEngine/engineState.types.ts +212 -0
- package/test/examples/asciiMaze/evolutionEngine/engineState.utils.ts +301 -0
- package/test/examples/asciiMaze/evolutionEngine/evolutionEngine.types.ts +445 -0
- package/test/examples/asciiMaze/evolutionEngine/evolutionLoop.ts +81 -50
- package/test/examples/asciiMaze/evolutionEngine/optionsAndSetup.ts +2 -4
- package/test/examples/asciiMaze/evolutionEngine/populationDynamics.ts +17 -33
- package/test/examples/asciiMaze/evolutionEngine/populationPruning.ts +1 -1
- package/test/examples/asciiMaze/evolutionEngine/rngAndTiming.ts +1 -2
- package/test/examples/asciiMaze/evolutionEngine/sampling.ts +1 -1
- package/test/examples/asciiMaze/evolutionEngine/scratchPools.ts +2 -5
- package/test/examples/asciiMaze/evolutionEngine/setupHelpers.ts +30 -37
- package/test/examples/asciiMaze/evolutionEngine/telemetryMetrics.ts +16 -58
- package/test/examples/asciiMaze/evolutionEngine/trainingWarmStart.ts +2 -2
- package/test/examples/asciiMaze/evolutionEngine.ts +55 -55
- package/test/examples/asciiMaze/fitness.ts +2 -2
- package/test/examples/asciiMaze/fitness.types.ts +65 -0
- package/test/examples/asciiMaze/interfaces.ts +64 -1352
- package/test/examples/asciiMaze/mazeMovement/README.md +356 -0
- package/test/examples/asciiMaze/mazeMovement/finalization/README.md +49 -0
- package/test/examples/asciiMaze/mazeMovement/finalization/mazeMovement.finalization.ts +138 -0
- package/test/examples/asciiMaze/mazeMovement/mazeMovement.constants.ts +101 -0
- package/test/examples/asciiMaze/mazeMovement/mazeMovement.services.ts +230 -0
- package/test/examples/asciiMaze/mazeMovement/mazeMovement.ts +299 -0
- package/test/examples/asciiMaze/mazeMovement/mazeMovement.types.ts +185 -0
- package/test/examples/asciiMaze/mazeMovement/mazeMovement.utils.ts +153 -0
- package/test/examples/asciiMaze/mazeMovement/policy/README.md +91 -0
- package/test/examples/asciiMaze/mazeMovement/policy/mazeMovement.policy.ts +467 -0
- package/test/examples/asciiMaze/mazeMovement/runtime/README.md +95 -0
- package/test/examples/asciiMaze/mazeMovement/runtime/mazeMovement.runtime.ts +354 -0
- package/test/examples/asciiMaze/mazeMovement/shaping/README.md +124 -0
- package/test/examples/asciiMaze/mazeMovement/shaping/mazeMovement.shaping.ts +459 -0
- package/test/examples/asciiMaze/mazeMovement.ts +12 -2978
- package/test/examples/flappy_bird/Trace-20260309T191949.json +24124 -0
- package/test/examples/flappy_bird/browser-entry/README.md +1129 -0
- package/test/examples/flappy_bird/browser-entry/browser-entry.host.utils.ts +4 -324
- package/test/examples/flappy_bird/browser-entry/browser-entry.network-view.utils.ts +6 -399
- package/test/examples/flappy_bird/browser-entry/browser-entry.playback.utils.ts +1 -717
- package/test/examples/flappy_bird/browser-entry/browser-entry.spawn.utils.ts +11 -31
- package/test/examples/flappy_bird/browser-entry/browser-entry.visualization.utils.ts +15 -893
- package/test/examples/flappy_bird/browser-entry/host/README.md +307 -0
- package/test/examples/flappy_bird/browser-entry/host/host.resize.service.ts +1 -295
- package/test/examples/flappy_bird/browser-entry/host/host.ts +562 -6
- package/test/examples/flappy_bird/browser-entry/host/resize/README.md +274 -0
- package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.constants.ts +31 -0
- package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.services.ts +360 -0
- package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.ts +117 -0
- package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.types.ts +63 -0
- package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.utils.ts +250 -0
- package/test/examples/flappy_bird/browser-entry/network-view/README.md +399 -0
- package/test/examples/flappy_bird/browser-entry/network-view/network-view.topology.utils.ts +255 -0
- package/test/examples/flappy_bird/browser-entry/network-view/network-view.ts +802 -7
- package/test/examples/flappy_bird/browser-entry/playback/README.md +684 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/README.md +277 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/README.md +770 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.cache.services.ts +178 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.constants.ts +107 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.geometry.utils.ts +518 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.math.utils.ts +117 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.pulse.utils.ts +233 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.services.ts +211 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.ts +48 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.types.ts +212 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.utils.ts +81 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.cache.services.ts +96 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.constants.ts +62 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.services.ts +244 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.ts +53 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.types.ts +68 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.utils.ts +100 -0
- package/test/examples/flappy_bird/browser-entry/playback/frame-render/README.md +310 -0
- package/test/examples/flappy_bird/browser-entry/playback/frame-render/playback.frame-render.service.ts +92 -0
- package/test/examples/flappy_bird/browser-entry/playback/frame-render/playback.frame-render.services.ts +272 -0
- package/test/examples/flappy_bird/browser-entry/playback/frame-render/playback.frame-render.types.ts +39 -0
- package/test/examples/flappy_bird/browser-entry/playback/frame-render/playback.frame-render.utils.ts +493 -0
- package/test/examples/flappy_bird/browser-entry/playback/playback.constants.ts +1 -1
- package/test/examples/flappy_bird/browser-entry/playback/playback.frame-render.service.ts +4 -0
- package/test/examples/flappy_bird/browser-entry/playback/playback.snapshot.utils.ts +44 -0
- package/test/examples/flappy_bird/browser-entry/playback/playback.starfield.service.ts +39 -122
- package/test/examples/flappy_bird/browser-entry/playback/playback.starfield.services.ts +272 -0
- package/test/examples/flappy_bird/browser-entry/playback/playback.starfield.types.ts +62 -0
- package/test/examples/flappy_bird/browser-entry/playback/playback.starfield.utils.ts +11 -4
- package/test/examples/flappy_bird/browser-entry/playback/playback.ts +409 -8
- package/test/examples/flappy_bird/browser-entry/playback/playback.types.ts +4 -12
- package/test/examples/flappy_bird/browser-entry/runtime/README.md +235 -0
- package/test/examples/flappy_bird/browser-entry/runtime/runtime.evolution-launch.service.ts +45 -0
- package/test/examples/flappy_bird/browser-entry/runtime/runtime.lifecycle.service.ts +81 -0
- package/test/examples/flappy_bird/browser-entry/runtime/runtime.startup.service.ts +74 -0
- package/test/examples/flappy_bird/browser-entry/runtime/runtime.ts +31 -121
- package/test/examples/flappy_bird/browser-entry/runtime/runtime.types.ts +36 -0
- package/test/examples/flappy_bird/browser-entry/visualization/README.md +557 -0
- package/test/examples/flappy_bird/browser-entry/visualization/visualization.constants.ts +110 -0
- package/test/examples/flappy_bird/browser-entry/visualization/visualization.draw.service.ts +957 -19
- package/test/examples/flappy_bird/browser-entry/visualization/visualization.legend.utils.ts +138 -3
- package/test/examples/flappy_bird/browser-entry/visualization/visualization.topology.utils.ts +3 -27
- package/test/examples/flappy_bird/browser-entry/visualization/visualization.ts +1 -23
- package/test/examples/flappy_bird/browser-entry/worker-channel/README.md +156 -0
- package/test/examples/flappy_bird/constants/README.md +1179 -0
- package/test/examples/flappy_bird/constants/constants.network-view.ts +24 -0
- package/test/examples/flappy_bird/constants/constants.palette.ts +7 -0
- package/test/examples/flappy_bird/constants/constants.starfield.ts +78 -3
- package/test/examples/flappy_bird/environment/README.md +143 -0
- package/test/examples/flappy_bird/environment/environment.observation.utils.ts +1 -19
- package/test/examples/flappy_bird/environment/environment.step.service.ts +3 -66
- package/test/examples/flappy_bird/evaluation/README.md +130 -0
- package/test/examples/flappy_bird/evaluation/evaluation.fitness.utils.ts +1 -1
- package/test/examples/flappy_bird/evaluation/evaluation.rollout.service.ts +5 -375
- package/test/examples/flappy_bird/evaluation/rollout/README.md +291 -0
- package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.constants.ts +30 -0
- package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.service.ts +58 -0
- package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.services.ts +310 -0
- package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.types.ts +56 -0
- package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.utils.ts +368 -0
- package/test/examples/flappy_bird/flappy-evolution-worker/README.md +618 -0
- package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.playback.service.ts +7 -7
- package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.simulation.frame.service.ts +364 -0
- package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.simulation.types.ts +14 -0
- package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.simulation.utils.ts +4 -201
- package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.ts +184 -345
- package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.warm-start.service.ts +291 -0
- package/test/examples/flappy_bird/flappy.simulation.shared.utils.ts +5 -0
- package/test/examples/flappy_bird/simulation-shared/README.md +417 -0
- package/test/examples/flappy_bird/simulation-shared/observation/README.md +183 -0
- package/test/examples/flappy_bird/simulation-shared/observation/observation.features.utils.ts +301 -0
- package/test/examples/flappy_bird/simulation-shared/observation/observation.ts +9 -0
- package/test/examples/flappy_bird/simulation-shared/observation/observation.vector.utils.ts +59 -0
- package/test/examples/flappy_bird/simulation-shared/simulation-shared.observation.utils.ts +5 -403
- package/test/examples/flappy_bird/simulation-shared/simulation-shared.spawn.utils.ts +20 -6
- package/test/examples/flappy_bird/{evaluation/evaluation.statistics.utils.ts → simulation-shared/simulation-shared.statistics.utils.ts} +23 -8
- package/test/examples/flappy_bird/trainer/README.md +563 -0
- package/test/examples/flappy_bird/trainer/evaluation/README.md +199 -0
- package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.constants.ts +9 -0
- package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.services.ts +73 -0
- package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.ts +165 -0
- package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.types.ts +25 -0
- package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.utils.ts +161 -0
- package/test/examples/flappy_bird/trainer/trainer.evaluation.service.ts +13 -0
- package/test/examples/flappy_bird/trainer/trainer.report.service.services.ts +181 -0
- package/test/examples/flappy_bird/trainer/trainer.report.service.ts +126 -0
- package/test/examples/flappy_bird/trainer/trainer.selection.utils.ts +89 -0
- package/test/examples/flappy_bird/trainer/trainer.ts +11 -553
- package/test/examples/flappy_bird/browser-entry/browser-entry.utils.ts +0 -12
- package/test/examples/flappy_bird/environment/environment.ts +0 -7
- package/test/examples/flappy_bird/evaluation/evaluation.ts +0 -7
- package/test/examples/flappy_bird/simulation-shared/simulation-shared.ts +0 -15
- package/test/examples/flappy_bird/trainer/trainer.statistics.utils.ts +0 -78
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { updateStatsTableValues } from '../host/host';
|
|
2
|
+
import { resolveRuntimeHudErrorStatus } from './runtime.errors';
|
|
3
|
+
import { runRuntimeEvolutionLoop } from './runtime.evolution-loop.service';
|
|
4
|
+
import type {
|
|
5
|
+
RuntimeMutableLifecycleState,
|
|
6
|
+
RuntimeStartContext,
|
|
7
|
+
} from './runtime.types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Starts the runtime evolution loop and routes unexpected failures to the HUD.
|
|
11
|
+
*
|
|
12
|
+
* @param runtimeStartContext - Shared runtime start context.
|
|
13
|
+
* @param runtimeLifecycleState - Mutable lifecycle state used for stop checks.
|
|
14
|
+
* @param stop - Idempotent stop function bound to the current runtime handle.
|
|
15
|
+
* @returns Nothing.
|
|
16
|
+
*/
|
|
17
|
+
export function launchRuntimeEvolution(
|
|
18
|
+
runtimeStartContext: RuntimeStartContext,
|
|
19
|
+
runtimeLifecycleState: RuntimeMutableLifecycleState,
|
|
20
|
+
stop: () => void,
|
|
21
|
+
): void {
|
|
22
|
+
const { config, evolutionWorker, runtimeTelemetryState, viewContext } =
|
|
23
|
+
runtimeStartContext;
|
|
24
|
+
|
|
25
|
+
// Step 1: Start the long-running evolution orchestration loop.
|
|
26
|
+
void runRuntimeEvolutionLoop({
|
|
27
|
+
evolutionWorker,
|
|
28
|
+
canvas: viewContext.canvas,
|
|
29
|
+
context: viewContext.context,
|
|
30
|
+
statsValueByKey: viewContext.statsValueByKey,
|
|
31
|
+
renderNetworkArchitecture: viewContext.renderNetworkArchitecture,
|
|
32
|
+
populationSize: config.populationSize,
|
|
33
|
+
elitismCount: config.elitismCount,
|
|
34
|
+
inputSize: config.inputSize,
|
|
35
|
+
outputSize: config.outputSize,
|
|
36
|
+
runtimeTelemetryState,
|
|
37
|
+
isStopped: () => runtimeLifecycleState.stopped,
|
|
38
|
+
}).catch((error: unknown) => {
|
|
39
|
+
// Step 2: Surface unexpected failures in the HUD and tear down the runtime.
|
|
40
|
+
updateStatsTableValues(viewContext.statsValueByKey, {
|
|
41
|
+
status: resolveRuntimeHudErrorStatus(error),
|
|
42
|
+
});
|
|
43
|
+
stop();
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { disconnectRuntimeTelemetry } from './runtime.telemetry.service';
|
|
2
|
+
import type {
|
|
3
|
+
RuntimeMutableLifecycleState,
|
|
4
|
+
RuntimeRunHandle,
|
|
5
|
+
RuntimeStartContext,
|
|
6
|
+
} from './runtime.types';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates mutable lifecycle state for stop semantics and completion signaling.
|
|
10
|
+
*
|
|
11
|
+
* @returns Mutable lifecycle state used by the run handle.
|
|
12
|
+
*/
|
|
13
|
+
export function createRuntimeLifecycleState(): RuntimeMutableLifecycleState {
|
|
14
|
+
let resolveDone: (() => void) | undefined;
|
|
15
|
+
const done = new Promise<void>((resolve) => {
|
|
16
|
+
resolveDone = resolve;
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// Step 1: Return idempotent lifecycle state with an externally resolvable promise.
|
|
20
|
+
return {
|
|
21
|
+
stopped: false,
|
|
22
|
+
resolveDone,
|
|
23
|
+
done,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Builds the public run handle and binds it to runtime teardown behavior.
|
|
29
|
+
*
|
|
30
|
+
* @param runtimeStartContext - Shared runtime start context.
|
|
31
|
+
* @param runtimeLifecycleState - Mutable lifecycle state for stop semantics.
|
|
32
|
+
* @returns Public run handle exposed to callers.
|
|
33
|
+
*/
|
|
34
|
+
export function createRuntimeRunHandle(
|
|
35
|
+
runtimeStartContext: RuntimeStartContext,
|
|
36
|
+
runtimeLifecycleState: RuntimeMutableLifecycleState,
|
|
37
|
+
): RuntimeRunHandle {
|
|
38
|
+
/**
|
|
39
|
+
* Stops the running demo and performs idempotent resource teardown.
|
|
40
|
+
*
|
|
41
|
+
* @returns Nothing.
|
|
42
|
+
*/
|
|
43
|
+
const stop = () => {
|
|
44
|
+
// Step 1: Keep stop idempotent for repeated calls from multiple paths.
|
|
45
|
+
if (runtimeLifecycleState.stopped) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Step 2: Flip the loop guard before releasing runtime resources.
|
|
50
|
+
runtimeLifecycleState.stopped = true;
|
|
51
|
+
|
|
52
|
+
// Step 3: Disconnect telemetry, notify the worker, and terminate it.
|
|
53
|
+
disconnectRuntimeTelemetry(runtimeStartContext.runtimeTelemetryState);
|
|
54
|
+
runtimeStartContext.evolutionWorker.postMessage({ type: 'stop' });
|
|
55
|
+
runtimeStartContext.evolutionWorker.terminate();
|
|
56
|
+
|
|
57
|
+
// Step 4: Resolve the externally visible completion promise.
|
|
58
|
+
runtimeLifecycleState.resolveDone?.();
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Reports whether the runtime loop has been stopped.
|
|
63
|
+
*
|
|
64
|
+
* @returns `true` when the runtime has been stopped.
|
|
65
|
+
*/
|
|
66
|
+
const isStopped = () => runtimeLifecycleState.stopped;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Reports whether the runtime loop is currently active.
|
|
70
|
+
*
|
|
71
|
+
* @returns `true` while the runtime remains active.
|
|
72
|
+
*/
|
|
73
|
+
const isRunning = () => !isStopped();
|
|
74
|
+
|
|
75
|
+
// Step 1: Return the public lifecycle handle bound to the mutable state.
|
|
76
|
+
return {
|
|
77
|
+
stop,
|
|
78
|
+
isRunning,
|
|
79
|
+
done: runtimeLifecycleState.done,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { createCanvasHost, updateStatsTableValues } from '../host/host';
|
|
2
|
+
import { createEvolutionWorker } from '../worker-channel/worker-channel';
|
|
3
|
+
import {
|
|
4
|
+
FLAPPY_BROWSER_ELITISM_COUNT,
|
|
5
|
+
FLAPPY_BROWSER_POPULATION_SIZE,
|
|
6
|
+
FLAPPY_HUD_INITIALIZING_TEXT,
|
|
7
|
+
FLAPPY_HUD_ZERO_TEXT,
|
|
8
|
+
} from '../../constants/constants';
|
|
9
|
+
import {
|
|
10
|
+
FLAPPY_NETWORK_INPUT_SIZE,
|
|
11
|
+
FLAPPY_NETWORK_OUTPUT_SIZE,
|
|
12
|
+
} from '../../constants/constants';
|
|
13
|
+
import { resolveRequiredRuntimeHostElement } from './runtime.errors';
|
|
14
|
+
import { createRuntimeTelemetryState } from './runtime.telemetry.service';
|
|
15
|
+
import type {
|
|
16
|
+
RuntimeContainerTarget,
|
|
17
|
+
RuntimeStartConfig,
|
|
18
|
+
RuntimeStartContext,
|
|
19
|
+
} from './runtime.types';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates the shared runtime startup dependencies used by the entry orchestration.
|
|
23
|
+
*
|
|
24
|
+
* @param container - Element id or HTMLElement to host the demo.
|
|
25
|
+
* @returns Shared runtime start context for setup and loop launch.
|
|
26
|
+
*/
|
|
27
|
+
export function createRuntimeStartContext(
|
|
28
|
+
container: RuntimeContainerTarget,
|
|
29
|
+
): RuntimeStartContext {
|
|
30
|
+
// Step 1: Resolve and validate the runtime host element.
|
|
31
|
+
const hostElement = resolveRequiredRuntimeHostElement(container);
|
|
32
|
+
|
|
33
|
+
// Step 2: Construct the static runtime configuration values.
|
|
34
|
+
const config = createRuntimeStartConfig();
|
|
35
|
+
|
|
36
|
+
// Step 3: Create the browser host, telemetry state, and worker channel.
|
|
37
|
+
return {
|
|
38
|
+
config,
|
|
39
|
+
viewContext: createCanvasHost(hostElement),
|
|
40
|
+
runtimeTelemetryState: createRuntimeTelemetryState(),
|
|
41
|
+
evolutionWorker: createEvolutionWorker(),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Paints the initial runtime HUD values before the evolution loop starts.
|
|
47
|
+
*
|
|
48
|
+
* @param runtimeStartContext - Shared runtime start context.
|
|
49
|
+
* @returns Nothing.
|
|
50
|
+
*/
|
|
51
|
+
export function initializeRuntimeHud(
|
|
52
|
+
runtimeStartContext: RuntimeStartContext,
|
|
53
|
+
): void {
|
|
54
|
+
// Step 1: Publish the initializing state and empty bird counters.
|
|
55
|
+
updateStatsTableValues(runtimeStartContext.viewContext.statsValueByKey, {
|
|
56
|
+
status: FLAPPY_HUD_INITIALIZING_TEXT,
|
|
57
|
+
birds: `${FLAPPY_HUD_ZERO_TEXT}/${runtimeStartContext.config.populationSize}`,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Resolves the static runtime configuration used during browser startup.
|
|
63
|
+
*
|
|
64
|
+
* @returns Runtime configuration derived from shared constants.
|
|
65
|
+
*/
|
|
66
|
+
function createRuntimeStartConfig(): RuntimeStartConfig {
|
|
67
|
+
// Step 1: Fold shared runtime constants into one descriptive config object.
|
|
68
|
+
return {
|
|
69
|
+
inputSize: FLAPPY_NETWORK_INPUT_SIZE,
|
|
70
|
+
outputSize: FLAPPY_NETWORK_OUTPUT_SIZE,
|
|
71
|
+
populationSize: FLAPPY_BROWSER_POPULATION_SIZE,
|
|
72
|
+
elitismCount: FLAPPY_BROWSER_ELITISM_COUNT,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -1,26 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
disconnectRuntimeTelemetry,
|
|
5
|
-
} from './runtime.telemetry.service';
|
|
6
|
-
import { createEvolutionWorker } from '../worker-channel/worker-channel';
|
|
7
|
-
import {
|
|
8
|
-
FLAPPY_BROWSER_ELITISM_COUNT,
|
|
9
|
-
FLAPPY_BROWSER_POPULATION_SIZE,
|
|
10
|
-
DEFAULT_CONTAINER_ID,
|
|
11
|
-
FLAPPY_HUD_INITIALIZING_TEXT,
|
|
12
|
-
FLAPPY_HUD_ZERO_TEXT,
|
|
13
|
-
} from '../../constants/constants';
|
|
1
|
+
import { DEFAULT_CONTAINER_ID } from '../../constants/constants';
|
|
2
|
+
import { installRuntimeBrowserGlobals } from './runtime.browser-globals.service';
|
|
3
|
+
import { launchRuntimeEvolution } from './runtime.evolution-launch.service';
|
|
14
4
|
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
} from '
|
|
5
|
+
createRuntimeLifecycleState,
|
|
6
|
+
createRuntimeRunHandle,
|
|
7
|
+
} from './runtime.lifecycle.service';
|
|
18
8
|
import {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
} from './runtime.
|
|
22
|
-
import { runRuntimeEvolutionLoop } from './runtime.evolution-loop.service';
|
|
23
|
-
import { installRuntimeBrowserGlobals } from './runtime.browser-globals.service';
|
|
9
|
+
createRuntimeStartContext,
|
|
10
|
+
initializeRuntimeHud,
|
|
11
|
+
} from './runtime.startup.service';
|
|
24
12
|
import type { RuntimeContainerTarget, RuntimeRunHandle } from './runtime.types';
|
|
25
13
|
|
|
26
14
|
export type { RuntimeRunHandle as FlappyBirdRunHandle } from './runtime.types';
|
|
@@ -48,106 +36,28 @@ export type { RuntimeRunHandle as FlappyBirdRunHandle } from './runtime.types';
|
|
|
48
36
|
export const start = async (
|
|
49
37
|
container: RuntimeContainerTarget = DEFAULT_CONTAINER_ID,
|
|
50
38
|
): Promise<RuntimeRunHandle> => {
|
|
51
|
-
// Step 1: Resolve
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
birds: `${FLAPPY_HUD_ZERO_TEXT}/${populationSize}`,
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
// Step 5: Create the evolution worker channel used by the orchestrator loop.
|
|
77
|
-
const evolutionWorker = createEvolutionWorker();
|
|
78
|
-
|
|
79
|
-
/** Mutable stop flag consulted by all loop and callback paths. */
|
|
80
|
-
let stopped = false;
|
|
81
|
-
/** Resolver captured for the externally exposed completion promise. */
|
|
82
|
-
let resolveDone: (() => void) | undefined;
|
|
83
|
-
/** Promise that resolves exactly once after stop/teardown is complete. */
|
|
84
|
-
const done = new Promise<void>((resolve) => {
|
|
85
|
-
resolveDone = resolve;
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Stops the running demo and performs idempotent resource teardown.
|
|
90
|
-
*
|
|
91
|
-
* Teardown includes:
|
|
92
|
-
* - disconnecting instrumentation observers,
|
|
93
|
-
* - notifying and terminating the worker,
|
|
94
|
-
* - resolving the `done` promise exactly once.
|
|
95
|
-
*
|
|
96
|
-
* @returns Nothing.
|
|
97
|
-
*/
|
|
98
|
-
const stop = () => {
|
|
99
|
-
// Step 1: Keep stop idempotent for repeated calls from multiple paths.
|
|
100
|
-
if (stopped) {
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Step 2: Flip loop guard first so no new async work starts.
|
|
105
|
-
stopped = true;
|
|
106
|
-
|
|
107
|
-
// Step 3: Release runtime resources in deterministic order.
|
|
108
|
-
disconnectRuntimeTelemetry(runtimeTelemetryState);
|
|
109
|
-
evolutionWorker.postMessage({ type: 'stop' });
|
|
110
|
-
evolutionWorker.terminate();
|
|
111
|
-
|
|
112
|
-
// Step 4: Resolve external completion handle.
|
|
113
|
-
resolveDone?.();
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Reports whether the demo loop has been stopped.
|
|
118
|
-
*
|
|
119
|
-
* @returns `true` when stopped.
|
|
120
|
-
*/
|
|
121
|
-
const isStopped = () => stopped;
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Reports whether the demo loop is currently active.
|
|
125
|
-
*
|
|
126
|
-
* @returns `true` when not stopped.
|
|
127
|
-
*/
|
|
128
|
-
const isRunning = () => !stopped;
|
|
129
|
-
|
|
130
|
-
// Step 6: Start orchestration loop and route unexpected errors to HUD + teardown.
|
|
131
|
-
void runRuntimeEvolutionLoop({
|
|
132
|
-
evolutionWorker,
|
|
133
|
-
canvas,
|
|
134
|
-
context,
|
|
135
|
-
statsValueByKey,
|
|
136
|
-
renderNetworkArchitecture,
|
|
137
|
-
populationSize,
|
|
138
|
-
elitismCount,
|
|
139
|
-
inputSize,
|
|
140
|
-
outputSize,
|
|
141
|
-
runtimeTelemetryState,
|
|
142
|
-
isStopped,
|
|
143
|
-
}).catch((error: unknown) => {
|
|
144
|
-
updateStatsTableValues(statsValueByKey, {
|
|
145
|
-
status: resolveRuntimeHudErrorStatus(error),
|
|
146
|
-
});
|
|
147
|
-
stop();
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
return { stop, isRunning, done };
|
|
39
|
+
// Step 1: Resolve the runtime view, worker, telemetry, and static config.
|
|
40
|
+
const runtimeStartContext = createRuntimeStartContext(container);
|
|
41
|
+
|
|
42
|
+
// Step 2: Paint the initial HUD state before evolution bootstrapping starts.
|
|
43
|
+
initializeRuntimeHud(runtimeStartContext);
|
|
44
|
+
|
|
45
|
+
// Step 3: Create lifecycle state and externally exposed run-handle methods.
|
|
46
|
+
const runtimeLifecycleState = createRuntimeLifecycleState();
|
|
47
|
+
const runtimeRunHandle = createRuntimeRunHandle(
|
|
48
|
+
runtimeStartContext,
|
|
49
|
+
runtimeLifecycleState,
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
// Step 4: Launch the runtime evolution loop with error-to-HUD routing.
|
|
53
|
+
launchRuntimeEvolution(
|
|
54
|
+
runtimeStartContext,
|
|
55
|
+
runtimeLifecycleState,
|
|
56
|
+
runtimeRunHandle.stop,
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
// Step 5: Return the public lifecycle controls to the caller.
|
|
60
|
+
return runtimeRunHandle;
|
|
151
61
|
};
|
|
152
62
|
|
|
153
63
|
installRuntimeBrowserGlobals(start);
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { createCanvasHost } from '../host/host';
|
|
1
2
|
import type {
|
|
2
3
|
FlappyBirdRunHandle,
|
|
3
4
|
RuntimeWindow,
|
|
4
5
|
} from '../browser-entry.types';
|
|
6
|
+
import type { RuntimeTelemetryState } from './runtime.telemetry.service';
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Public run handle returned by the browser runtime entrypoint.
|
|
@@ -17,3 +19,37 @@ export type RuntimeGlobalWindow = RuntimeWindow;
|
|
|
17
19
|
* Container argument accepted by the browser runtime start function.
|
|
18
20
|
*/
|
|
19
21
|
export type RuntimeContainerTarget = string | HTMLElement;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Static runtime configuration resolved before the browser loop starts.
|
|
25
|
+
*/
|
|
26
|
+
export interface RuntimeStartConfig {
|
|
27
|
+
inputSize: number;
|
|
28
|
+
outputSize: number;
|
|
29
|
+
populationSize: number;
|
|
30
|
+
elitismCount: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Browser host view handles used by the runtime entry orchestration.
|
|
35
|
+
*/
|
|
36
|
+
export type RuntimeHostViewContext = ReturnType<typeof createCanvasHost>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Mutable lifecycle state used to coordinate stop semantics and completion.
|
|
40
|
+
*/
|
|
41
|
+
export interface RuntimeMutableLifecycleState {
|
|
42
|
+
stopped: boolean;
|
|
43
|
+
resolveDone?: () => void;
|
|
44
|
+
done: Promise<void>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Shared runtime startup dependencies created before evolution begins.
|
|
49
|
+
*/
|
|
50
|
+
export interface RuntimeStartContext {
|
|
51
|
+
config: RuntimeStartConfig;
|
|
52
|
+
viewContext: RuntimeHostViewContext;
|
|
53
|
+
runtimeTelemetryState: RuntimeTelemetryState;
|
|
54
|
+
evolutionWorker: Worker;
|
|
55
|
+
}
|