@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,301 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FLAPPY_BIRD_RADIUS_PX,
|
|
3
|
+
FLAPPY_BIRD_X_PX,
|
|
4
|
+
FLAPPY_FLAP_VELOCITY_PX_PER_FRAME,
|
|
5
|
+
FLAPPY_GRAVITY_PX_PER_FRAME2,
|
|
6
|
+
FLAPPY_MAX_FALL_SPEED_PX_PER_FRAME,
|
|
7
|
+
FLAPPY_PIPE_GAP_PX,
|
|
8
|
+
FLAPPY_PIPE_WIDTH_PX,
|
|
9
|
+
FLAPPY_WORLD_HEIGHT_PX,
|
|
10
|
+
} from '../../constants/constants';
|
|
11
|
+
import { FLAPPY_SHARED_DEFAULT_NORMALIZATION_EPSILON } from '../simulation-shared.constants';
|
|
12
|
+
import type {
|
|
13
|
+
SharedObservationFeatures,
|
|
14
|
+
SharedObservationInput,
|
|
15
|
+
SharedPipeLike,
|
|
16
|
+
} from '../simulation-shared.types';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Resolves the next two upcoming pipes in front of the bird.
|
|
20
|
+
*
|
|
21
|
+
* @param pipes - Current pipe list.
|
|
22
|
+
* @param birdCenterXPx - Bird center x-position.
|
|
23
|
+
* @param birdRadiusPx - Bird radius.
|
|
24
|
+
* @param pipeWidthPx - Pipe width.
|
|
25
|
+
* @returns Tuple of first and second upcoming pipes.
|
|
26
|
+
*/
|
|
27
|
+
export function resolveUpcomingPipes(
|
|
28
|
+
pipes: SharedPipeLike[],
|
|
29
|
+
birdCenterXPx = FLAPPY_BIRD_X_PX,
|
|
30
|
+
birdRadiusPx = FLAPPY_BIRD_RADIUS_PX,
|
|
31
|
+
pipeWidthPx = FLAPPY_PIPE_WIDTH_PX,
|
|
32
|
+
): [SharedPipeLike | undefined, SharedPipeLike | undefined] {
|
|
33
|
+
const birdFrontX = birdCenterXPx - birdRadiusPx;
|
|
34
|
+
const upcomingPipes = pipes.filter(
|
|
35
|
+
(pipe) => pipe.xPx + pipeWidthPx >= birdFrontX,
|
|
36
|
+
);
|
|
37
|
+
return [upcomingPipes[0], upcomingPipes[1]];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Builds the shared normalized observation feature set consumed by policies.
|
|
42
|
+
*
|
|
43
|
+
* Educational note:
|
|
44
|
+
* This helper stays focused on semantic feature assembly only. Projection into
|
|
45
|
+
* the canonical network vectors now lives in the neighboring vector module so
|
|
46
|
+
* observation policy and network-shape concerns can evolve independently.
|
|
47
|
+
*
|
|
48
|
+
* @param input - Observation input bundle.
|
|
49
|
+
* @returns Structured observation features.
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* const features = resolveObservationFeatures({
|
|
53
|
+
* birdYPx: 120,
|
|
54
|
+
* velocityYPxPerFrame: 2,
|
|
55
|
+
* pipes,
|
|
56
|
+
* visibleWorldWidthPx: 640,
|
|
57
|
+
* difficultyProfile,
|
|
58
|
+
* activeSpawnIntervalFrames: 90,
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function resolveObservationFeatures(
|
|
63
|
+
input: SharedObservationInput,
|
|
64
|
+
): SharedObservationFeatures {
|
|
65
|
+
// Step 1: Resolve normalization and geometry defaults.
|
|
66
|
+
const worldHeightPx = input.worldHeightPx ?? FLAPPY_WORLD_HEIGHT_PX;
|
|
67
|
+
const maxFallSpeedPxPerFrame =
|
|
68
|
+
input.maxFallSpeedPxPerFrame ?? FLAPPY_MAX_FALL_SPEED_PX_PER_FRAME;
|
|
69
|
+
const birdCenterXPx = input.birdCenterXPx ?? FLAPPY_BIRD_X_PX;
|
|
70
|
+
const birdRadiusPx = input.birdRadiusPx ?? FLAPPY_BIRD_RADIUS_PX;
|
|
71
|
+
const pipeWidthPx = input.pipeWidthPx ?? FLAPPY_PIPE_WIDTH_PX;
|
|
72
|
+
const defaultGapSizePx = input.defaultGapSizePx ?? FLAPPY_PIPE_GAP_PX;
|
|
73
|
+
const normalizationEpsilon =
|
|
74
|
+
input.normalizationEpsilon ?? FLAPPY_SHARED_DEFAULT_NORMALIZATION_EPSILON;
|
|
75
|
+
|
|
76
|
+
// Step 2: Resolve next-pipe geometry used by all near-term features.
|
|
77
|
+
const [nextPipe, secondPipe] = resolveUpcomingPipes(
|
|
78
|
+
input.pipes,
|
|
79
|
+
birdCenterXPx,
|
|
80
|
+
birdRadiusPx,
|
|
81
|
+
pipeWidthPx,
|
|
82
|
+
);
|
|
83
|
+
const normalizedBirdY = clamp01(input.birdYPx / worldHeightPx);
|
|
84
|
+
const normalizedVelocity = clamp(
|
|
85
|
+
input.velocityYPxPerFrame / maxFallSpeedPxPerFrame,
|
|
86
|
+
-1,
|
|
87
|
+
1,
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const distanceToNextPipePx = nextPipe
|
|
91
|
+
? nextPipe.xPx + pipeWidthPx - birdCenterXPx
|
|
92
|
+
: input.visibleWorldWidthPx;
|
|
93
|
+
const normalizedDistanceToNextPipe = clamp01(
|
|
94
|
+
distanceToNextPipePx / input.visibleWorldWidthPx,
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
const nextGapCenterYPx = nextPipe?.gapCenterYPx ?? worldHeightPx * 0.5;
|
|
98
|
+
const nextGapHalfPx = (nextPipe?.gapSizePx ?? defaultGapSizePx) * 0.5;
|
|
99
|
+
const normalizedNextGapTop = clamp01(
|
|
100
|
+
(nextGapCenterYPx - nextGapHalfPx) / worldHeightPx,
|
|
101
|
+
);
|
|
102
|
+
const normalizedNextGapBottom = clamp01(
|
|
103
|
+
(nextGapCenterYPx + nextGapHalfPx) / worldHeightPx,
|
|
104
|
+
);
|
|
105
|
+
const normalizedDeltaToNextGap = clamp(
|
|
106
|
+
(input.birdYPx - nextGapCenterYPx) / worldHeightPx,
|
|
107
|
+
-1,
|
|
108
|
+
1,
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const distanceToSecondPipePx = secondPipe
|
|
112
|
+
? secondPipe.xPx + pipeWidthPx - birdCenterXPx
|
|
113
|
+
: input.visibleWorldWidthPx;
|
|
114
|
+
const normalizedDistanceToSecondPipe = clamp01(
|
|
115
|
+
distanceToSecondPipePx / input.visibleWorldWidthPx,
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
const secondGapCenterYPx = secondPipe?.gapCenterYPx ?? nextGapCenterYPx;
|
|
119
|
+
const normalizedDeltaToSecondGap = clamp(
|
|
120
|
+
(input.birdYPx - secondGapCenterYPx) / worldHeightPx,
|
|
121
|
+
-1,
|
|
122
|
+
1,
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
// Step 3: Resolve timing, clearance, and controllability signals.
|
|
126
|
+
const estimatedFramesToNextPipe =
|
|
127
|
+
distanceToNextPipePx /
|
|
128
|
+
Math.max(normalizationEpsilon, input.difficultyProfile.pipeSpeedPxPerFrame);
|
|
129
|
+
const normalizedTimeToNextPipe = clamp01(
|
|
130
|
+
1 -
|
|
131
|
+
estimatedFramesToNextPipe / Math.max(1, input.activeSpawnIntervalFrames),
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
const distanceToNextGapCenterPx = Math.abs(input.birdYPx - nextGapCenterYPx);
|
|
135
|
+
const normalizedNextGapClearance = clamp(
|
|
136
|
+
(nextGapHalfPx - distanceToNextGapCenterPx) / Math.max(1, nextGapHalfPx),
|
|
137
|
+
-1,
|
|
138
|
+
1,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
const distanceToGapEntryPx = nextPipe
|
|
142
|
+
? nextPipe.xPx - (birdCenterXPx + birdRadiusPx)
|
|
143
|
+
: input.visibleWorldWidthPx;
|
|
144
|
+
const distanceToGapExitPx = nextPipe
|
|
145
|
+
? nextPipe.xPx + pipeWidthPx - (birdCenterXPx - birdRadiusPx)
|
|
146
|
+
: input.visibleWorldWidthPx;
|
|
147
|
+
|
|
148
|
+
const framesToGapEntry = Math.max(
|
|
149
|
+
0,
|
|
150
|
+
distanceToGapEntryPx /
|
|
151
|
+
Math.max(
|
|
152
|
+
normalizationEpsilon,
|
|
153
|
+
input.difficultyProfile.pipeSpeedPxPerFrame,
|
|
154
|
+
),
|
|
155
|
+
);
|
|
156
|
+
const framesToGapExit = Math.max(
|
|
157
|
+
framesToGapEntry,
|
|
158
|
+
distanceToGapExitPx /
|
|
159
|
+
Math.max(
|
|
160
|
+
normalizationEpsilon,
|
|
161
|
+
input.difficultyProfile.pipeSpeedPxPerFrame,
|
|
162
|
+
),
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
const normalizedFramesToGapEntry = clamp01(
|
|
166
|
+
framesToGapEntry / Math.max(1, input.activeSpawnIntervalFrames),
|
|
167
|
+
);
|
|
168
|
+
const normalizedFramesToGapExit = clamp01(
|
|
169
|
+
framesToGapExit / Math.max(1, input.activeSpawnIntervalFrames),
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
const requiredVerticalVelocityToNextGapPxPerFrame = clamp(
|
|
173
|
+
(nextGapCenterYPx - input.birdYPx) / Math.max(1, estimatedFramesToNextPipe),
|
|
174
|
+
-maxFallSpeedPxPerFrame,
|
|
175
|
+
maxFallSpeedPxPerFrame,
|
|
176
|
+
);
|
|
177
|
+
const normalizedRequiredVerticalVelocityToNextGap = clamp(
|
|
178
|
+
requiredVerticalVelocityToNextGapPxPerFrame / maxFallSpeedPxPerFrame,
|
|
179
|
+
-1,
|
|
180
|
+
1,
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
const requiredVerticalVelocityAtGapEntryPxPerFrame = clamp(
|
|
184
|
+
(nextGapCenterYPx - input.birdYPx) / Math.max(1, framesToGapEntry),
|
|
185
|
+
-maxFallSpeedPxPerFrame,
|
|
186
|
+
maxFallSpeedPxPerFrame,
|
|
187
|
+
);
|
|
188
|
+
const requiredVerticalVelocityAtGapExitPxPerFrame = clamp(
|
|
189
|
+
(nextGapCenterYPx - input.birdYPx) / Math.max(1, framesToGapExit),
|
|
190
|
+
-maxFallSpeedPxPerFrame,
|
|
191
|
+
maxFallSpeedPxPerFrame,
|
|
192
|
+
);
|
|
193
|
+
const normalizedRequiredVerticalVelocityAtGapEntry = clamp(
|
|
194
|
+
requiredVerticalVelocityAtGapEntryPxPerFrame / maxFallSpeedPxPerFrame,
|
|
195
|
+
-1,
|
|
196
|
+
1,
|
|
197
|
+
);
|
|
198
|
+
const normalizedRequiredVerticalVelocityAtGapExit = clamp(
|
|
199
|
+
requiredVerticalVelocityAtGapExitPxPerFrame / maxFallSpeedPxPerFrame,
|
|
200
|
+
-1,
|
|
201
|
+
1,
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
const normalizedEntryUrgency = clamp(
|
|
205
|
+
Math.abs(normalizedDeltaToNextGap) /
|
|
206
|
+
Math.max(normalizationEpsilon, normalizedFramesToGapEntry),
|
|
207
|
+
0,
|
|
208
|
+
1,
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
const predictedBirdYAtEntryWithoutFlap = resolvePredictedBirdYAtFrames(
|
|
212
|
+
input.birdYPx,
|
|
213
|
+
input.velocityYPxPerFrame,
|
|
214
|
+
framesToGapEntry,
|
|
215
|
+
);
|
|
216
|
+
const predictedBirdYAtEntryWithOneFlap = resolvePredictedBirdYAtFrames(
|
|
217
|
+
input.birdYPx,
|
|
218
|
+
FLAPPY_FLAP_VELOCITY_PX_PER_FRAME,
|
|
219
|
+
framesToGapEntry,
|
|
220
|
+
);
|
|
221
|
+
const noFlapEntryErrorPx = Math.abs(
|
|
222
|
+
predictedBirdYAtEntryWithoutFlap - nextGapCenterYPx,
|
|
223
|
+
);
|
|
224
|
+
const oneFlapEntryErrorPx = Math.abs(
|
|
225
|
+
predictedBirdYAtEntryWithOneFlap - nextGapCenterYPx,
|
|
226
|
+
);
|
|
227
|
+
const minimalEntryErrorPx = Math.min(noFlapEntryErrorPx, oneFlapEntryErrorPx);
|
|
228
|
+
const normalizedOneFlapReachabilityAtGapEntry =
|
|
229
|
+
minimalEntryErrorPx <= nextGapHalfPx ? 1 : 0;
|
|
230
|
+
|
|
231
|
+
const normalizedNextToSecondGapTransition = clamp(
|
|
232
|
+
(secondGapCenterYPx - nextGapCenterYPx) / worldHeightPx,
|
|
233
|
+
-1,
|
|
234
|
+
1,
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
// Step 4: Return the structured feature record consumed by both runtimes.
|
|
238
|
+
return {
|
|
239
|
+
normalizedBirdY,
|
|
240
|
+
normalizedVelocity,
|
|
241
|
+
normalizedDistanceToNextPipe,
|
|
242
|
+
normalizedDeltaToNextGap,
|
|
243
|
+
normalizedNextGapTop,
|
|
244
|
+
normalizedNextGapBottom,
|
|
245
|
+
normalizedDistanceToSecondPipe,
|
|
246
|
+
normalizedDeltaToSecondGap,
|
|
247
|
+
normalizedTimeToNextPipe,
|
|
248
|
+
normalizedNextGapClearance,
|
|
249
|
+
normalizedRequiredVerticalVelocityToNextGap,
|
|
250
|
+
normalizedNextToSecondGapTransition,
|
|
251
|
+
normalizedFramesToGapEntry,
|
|
252
|
+
normalizedFramesToGapExit,
|
|
253
|
+
normalizedRequiredVerticalVelocityAtGapEntry,
|
|
254
|
+
normalizedRequiredVerticalVelocityAtGapExit,
|
|
255
|
+
normalizedEntryUrgency,
|
|
256
|
+
normalizedOneFlapReachabilityAtGapEntry,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Predicts bird y-position after a frame horizon with constant gravity.
|
|
262
|
+
*
|
|
263
|
+
* @param startYPx - Current bird y-position.
|
|
264
|
+
* @param initialVerticalVelocityPxPerFrame - Initial vertical velocity.
|
|
265
|
+
* @param frameHorizon - Predicted horizon in simulation frames.
|
|
266
|
+
* @returns Predicted y-position.
|
|
267
|
+
*/
|
|
268
|
+
function resolvePredictedBirdYAtFrames(
|
|
269
|
+
startYPx: number,
|
|
270
|
+
initialVerticalVelocityPxPerFrame: number,
|
|
271
|
+
frameHorizon: number,
|
|
272
|
+
): number {
|
|
273
|
+
const safeFrameHorizon = Math.max(0, frameHorizon);
|
|
274
|
+
return (
|
|
275
|
+
startYPx +
|
|
276
|
+
initialVerticalVelocityPxPerFrame * safeFrameHorizon +
|
|
277
|
+
0.5 * FLAPPY_GRAVITY_PX_PER_FRAME2 * safeFrameHorizon * safeFrameHorizon
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Clamps a numeric value to the inclusive [min, max] interval.
|
|
283
|
+
*
|
|
284
|
+
* @param value - Candidate value.
|
|
285
|
+
* @param min - Inclusive lower bound.
|
|
286
|
+
* @param max - Inclusive upper bound.
|
|
287
|
+
* @returns Clamped value.
|
|
288
|
+
*/
|
|
289
|
+
function clamp(value: number, min: number, max: number): number {
|
|
290
|
+
return Math.min(max, Math.max(min, value));
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Clamps a numeric value to the inclusive [0, 1] interval.
|
|
295
|
+
*
|
|
296
|
+
* @param value - Candidate value.
|
|
297
|
+
* @returns Value clamped between 0 and 1.
|
|
298
|
+
*/
|
|
299
|
+
function clamp01(value: number): number {
|
|
300
|
+
return clamp(value, 0, 1);
|
|
301
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared observation public entry.
|
|
3
|
+
*
|
|
4
|
+
* This focused boundary keeps observation feature assembly separate from
|
|
5
|
+
* observation-vector projection so browser, environment, and worker callers can
|
|
6
|
+
* depend on a smaller, clearer public surface.
|
|
7
|
+
*/
|
|
8
|
+
export * from './observation.features.utils';
|
|
9
|
+
export * from './observation.vector.utils';
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { SharedObservationFeatures } from '../simulation-shared.types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts observation features to the canonical 12-value network input vector.
|
|
5
|
+
*
|
|
6
|
+
* Educational note:
|
|
7
|
+
* This module owns the network-shape projection so feature semantics can change
|
|
8
|
+
* independently from how the policy input is ordered.
|
|
9
|
+
*
|
|
10
|
+
* @param features - Structured feature object.
|
|
11
|
+
* @returns Ordered feature vector.
|
|
12
|
+
*/
|
|
13
|
+
export function resolveObservationVectorFromFeatures(
|
|
14
|
+
features: SharedObservationFeatures,
|
|
15
|
+
): number[] {
|
|
16
|
+
return [
|
|
17
|
+
features.normalizedBirdY,
|
|
18
|
+
features.normalizedVelocity,
|
|
19
|
+
features.normalizedDistanceToNextPipe,
|
|
20
|
+
features.normalizedDeltaToNextGap,
|
|
21
|
+
features.normalizedNextGapTop,
|
|
22
|
+
features.normalizedNextGapBottom,
|
|
23
|
+
features.normalizedDistanceToSecondPipe,
|
|
24
|
+
features.normalizedDeltaToSecondGap,
|
|
25
|
+
features.normalizedTimeToNextPipe,
|
|
26
|
+
features.normalizedNextGapClearance,
|
|
27
|
+
features.normalizedRequiredVerticalVelocityToNextGap,
|
|
28
|
+
features.normalizedNextToSecondGapTransition,
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Resolves the compact core vector used for temporal stacking.
|
|
34
|
+
*
|
|
35
|
+
* The core intentionally keeps directly observed kinematic and geometric
|
|
36
|
+
* channels while dropping derived one-step predictors that become redundant
|
|
37
|
+
* once short-term temporal memory is available.
|
|
38
|
+
*
|
|
39
|
+
* @param features - Structured observation features.
|
|
40
|
+
* @returns Core per-frame vector.
|
|
41
|
+
*/
|
|
42
|
+
export function resolveCoreObservationVectorFromFeatures(
|
|
43
|
+
features: SharedObservationFeatures,
|
|
44
|
+
): number[] {
|
|
45
|
+
return [
|
|
46
|
+
features.normalizedBirdY,
|
|
47
|
+
features.normalizedVelocity,
|
|
48
|
+
features.normalizedDistanceToNextPipe,
|
|
49
|
+
features.normalizedDeltaToNextGap,
|
|
50
|
+
features.normalizedNextGapTop,
|
|
51
|
+
features.normalizedNextGapBottom,
|
|
52
|
+
features.normalizedDistanceToSecondPipe,
|
|
53
|
+
features.normalizedDeltaToSecondGap,
|
|
54
|
+
features.normalizedNextGapClearance,
|
|
55
|
+
features.normalizedRequiredVerticalVelocityToNextGap,
|
|
56
|
+
features.normalizedEntryUrgency,
|
|
57
|
+
features.normalizedOneFlapReachabilityAtGapEntry,
|
|
58
|
+
];
|
|
59
|
+
}
|