@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,459 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reward-shaping helpers for the dedicated mazeMovement module.
|
|
3
|
+
*
|
|
4
|
+
* This file owns movement reward shaping, stagnation penalties, entropy-guided
|
|
5
|
+
* bonuses, and other post-action fitness adjustments.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { MAZE_MOVEMENT_CONSTANTS } from '../mazeMovement.constants';
|
|
9
|
+
import {
|
|
10
|
+
getMazeMovementRunServiceState,
|
|
11
|
+
requireMazeMovementBufferPools,
|
|
12
|
+
} from '../mazeMovement.services';
|
|
13
|
+
import type { SimulationState } from '../mazeMovement.types';
|
|
14
|
+
import { sumVisionGroup } from '../mazeMovement.utils';
|
|
15
|
+
import {
|
|
16
|
+
getMazeMovementDistance,
|
|
17
|
+
isMazeMovementCellOpen,
|
|
18
|
+
} from '../runtime/mazeMovement.runtime';
|
|
19
|
+
import { MazeUtils } from '../../mazeUtils';
|
|
20
|
+
|
|
21
|
+
const C = MAZE_MOVEMENT_CONSTANTS;
|
|
22
|
+
const SHAPING_VISION_SUM_SCRATCH = new Float64Array(4);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Execute the chosen move and apply the shaping terms tied to that move.
|
|
26
|
+
*
|
|
27
|
+
* @param state - Mutable simulation state for the active run.
|
|
28
|
+
* @param encodedMaze - Maze grid used for move validity and distance lookup.
|
|
29
|
+
* @param distanceMap - Optional precomputed distance map.
|
|
30
|
+
* @param coordinateScratch - Reused coordinate scratch buffer.
|
|
31
|
+
*/
|
|
32
|
+
export function executeMazeMovementAndRewards(
|
|
33
|
+
state: SimulationState,
|
|
34
|
+
encodedMaze: number[][],
|
|
35
|
+
distanceMap: number[][] | undefined,
|
|
36
|
+
coordinateScratch: Int32Array,
|
|
37
|
+
): void {
|
|
38
|
+
if (state.earlyTerminate) return;
|
|
39
|
+
|
|
40
|
+
const previousDistance = getMazeMovementDistance(
|
|
41
|
+
encodedMaze,
|
|
42
|
+
state.position,
|
|
43
|
+
distanceMap,
|
|
44
|
+
);
|
|
45
|
+
state.prevDistance = previousDistance;
|
|
46
|
+
state.moved = false;
|
|
47
|
+
|
|
48
|
+
const chosenAction = state.direction;
|
|
49
|
+
if (chosenAction >= 0 && chosenAction < C.ACTION_DIM) {
|
|
50
|
+
const [deltaX, deltaY] = C.DIRECTION_DELTAS[chosenAction];
|
|
51
|
+
const candidateX = (state.position[0] + deltaX) | 0;
|
|
52
|
+
const candidateY = (state.position[1] + deltaY) | 0;
|
|
53
|
+
coordinateScratch[0] = candidateX;
|
|
54
|
+
coordinateScratch[1] = candidateY;
|
|
55
|
+
|
|
56
|
+
if (
|
|
57
|
+
isMazeMovementCellOpen(
|
|
58
|
+
encodedMaze,
|
|
59
|
+
candidateX,
|
|
60
|
+
candidateY,
|
|
61
|
+
coordinateScratch,
|
|
62
|
+
)
|
|
63
|
+
) {
|
|
64
|
+
state.position[0] = candidateX;
|
|
65
|
+
state.position[1] = candidateY;
|
|
66
|
+
state.moved = true;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const rewardScale = C.REWARD_SCALE;
|
|
71
|
+
const bufferPools = requireMazeMovementBufferPools();
|
|
72
|
+
if (state.moved) {
|
|
73
|
+
const writeIndex = state.pathLength | 0;
|
|
74
|
+
bufferPools.pathX[writeIndex] = state.position[0];
|
|
75
|
+
bufferPools.pathY[writeIndex] = state.position[1];
|
|
76
|
+
state.pathLength = writeIndex + 1;
|
|
77
|
+
|
|
78
|
+
MazeUtils.pushHistory(
|
|
79
|
+
state.recentPositions,
|
|
80
|
+
[state.position[0], state.position[1]],
|
|
81
|
+
C.LOCAL_WINDOW,
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
applyMazeMovementLocalAreaPenalty(state, rewardScale, coordinateScratch);
|
|
85
|
+
|
|
86
|
+
const currentDistance = state.hasDistanceMap
|
|
87
|
+
? (state.distanceMap?.[state.position[1]]?.[state.position[0]] ??
|
|
88
|
+
Infinity)
|
|
89
|
+
: getMazeMovementDistance(encodedMaze, state.position, state.distanceMap);
|
|
90
|
+
const distanceDelta = previousDistance - currentDistance;
|
|
91
|
+
const improved = distanceDelta > 0;
|
|
92
|
+
const worsened = !improved && currentDistance > previousDistance;
|
|
93
|
+
applyMazeMovementProgressShaping(
|
|
94
|
+
state,
|
|
95
|
+
distanceDelta,
|
|
96
|
+
improved,
|
|
97
|
+
worsened,
|
|
98
|
+
rewardScale,
|
|
99
|
+
);
|
|
100
|
+
applyMazeMovementExplorationVisitAdjustment(
|
|
101
|
+
state,
|
|
102
|
+
rewardScale,
|
|
103
|
+
coordinateScratch,
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
if (state.direction >= 0) state.directionCounts[state.direction]++;
|
|
107
|
+
state.minDistanceToExit = Math.min(
|
|
108
|
+
state.minDistanceToExit,
|
|
109
|
+
currentDistance,
|
|
110
|
+
);
|
|
111
|
+
} else {
|
|
112
|
+
state.invalidMovePenalty -= C.INVALID_MOVE_PENALTY_MILD * rewardScale;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
applyMazeMovementGlobalDistanceImprovementBonus(
|
|
116
|
+
state,
|
|
117
|
+
encodedMaze,
|
|
118
|
+
rewardScale,
|
|
119
|
+
coordinateScratch,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Apply the post-action shaping and penalty aggregation phase.
|
|
125
|
+
*
|
|
126
|
+
* @param state - Mutable simulation state for the active run.
|
|
127
|
+
* @param coordinateScratch - Reused coordinate scratch buffer.
|
|
128
|
+
*/
|
|
129
|
+
export function applyMazeMovementPostActionPenalties(
|
|
130
|
+
state: SimulationState,
|
|
131
|
+
coordinateScratch: Int32Array,
|
|
132
|
+
): void {
|
|
133
|
+
if (state.earlyTerminate) return;
|
|
134
|
+
|
|
135
|
+
const rewardScale = C.REWARD_SCALE;
|
|
136
|
+
applyMazeMovementRepetitionAndBacktrackPenalties(
|
|
137
|
+
state,
|
|
138
|
+
rewardScale,
|
|
139
|
+
coordinateScratch,
|
|
140
|
+
);
|
|
141
|
+
if (state.moved) state.prevAction = state.direction;
|
|
142
|
+
applyMazeMovementEntropyGuidanceShaping(
|
|
143
|
+
state,
|
|
144
|
+
rewardScale,
|
|
145
|
+
coordinateScratch,
|
|
146
|
+
);
|
|
147
|
+
applyMazeMovementSaturationPenaltyCycle(
|
|
148
|
+
state,
|
|
149
|
+
rewardScale,
|
|
150
|
+
coordinateScratch,
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
coordinateScratch[0] =
|
|
154
|
+
(state.loopPenalty || 0) +
|
|
155
|
+
(state.memoryPenalty || 0) +
|
|
156
|
+
(state.revisitPenalty || 0);
|
|
157
|
+
state.invalidMovePenalty += coordinateScratch[0];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Apply a local-area stagnation penalty when the run oscillates in a tight window.
|
|
162
|
+
*
|
|
163
|
+
* @param state - Mutable simulation state for the active run.
|
|
164
|
+
* @param rewardScale - Global reward scale used for the penalty magnitude.
|
|
165
|
+
* @param coordinateScratch - Reused coordinate scratch buffer.
|
|
166
|
+
*/
|
|
167
|
+
export function applyMazeMovementLocalAreaPenalty(
|
|
168
|
+
state: SimulationState,
|
|
169
|
+
rewardScale: number,
|
|
170
|
+
coordinateScratch: Int32Array,
|
|
171
|
+
): void {
|
|
172
|
+
const recentWindow = state.recentPositions;
|
|
173
|
+
if (recentWindow.length !== C.LOCAL_WINDOW) return;
|
|
174
|
+
|
|
175
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
176
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
177
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
178
|
+
let maxY = Number.NEGATIVE_INFINITY;
|
|
179
|
+
|
|
180
|
+
for (let recentIndex = 0; recentIndex < recentWindow.length; recentIndex++) {
|
|
181
|
+
const [positionX, positionY] = recentWindow[recentIndex];
|
|
182
|
+
const coercedX = positionX | 0;
|
|
183
|
+
const coercedY = positionY | 0;
|
|
184
|
+
if (coercedX < minX) minX = coercedX;
|
|
185
|
+
if (coercedX > maxX) maxX = coercedX;
|
|
186
|
+
if (coercedY < minY) minY = coercedY;
|
|
187
|
+
if (coercedY > maxY) maxY = coercedY;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
coordinateScratch[0] = minX;
|
|
191
|
+
coordinateScratch[1] = minY;
|
|
192
|
+
const span = maxX - minX + (maxY - minY);
|
|
193
|
+
if (
|
|
194
|
+
span <= C.LOCAL_AREA_SPAN_THRESHOLD &&
|
|
195
|
+
state.stepsSinceImprovement > C.LOCAL_AREA_STAGNATION_STEPS
|
|
196
|
+
) {
|
|
197
|
+
state.localAreaPenalty -= C.LOCAL_AREA_PENALTY_AMOUNT * rewardScale;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Apply progress and away-from-goal shaping after a move.
|
|
203
|
+
*
|
|
204
|
+
* @param state - Mutable simulation state for the active run.
|
|
205
|
+
* @param distanceDelta - Positive when the agent moved closer to the goal.
|
|
206
|
+
* @param improved - True when the move improved distance to the goal.
|
|
207
|
+
* @param worsened - True when the move increased distance to the goal.
|
|
208
|
+
* @param rewardScale - Global reward scale used by the shaping terms.
|
|
209
|
+
*/
|
|
210
|
+
export function applyMazeMovementProgressShaping(
|
|
211
|
+
state: SimulationState,
|
|
212
|
+
distanceDelta: number,
|
|
213
|
+
improved: boolean,
|
|
214
|
+
worsened: boolean,
|
|
215
|
+
rewardScale: number,
|
|
216
|
+
): void {
|
|
217
|
+
const currentConfidence = state.actionStats?.maxProb ?? (improved ? 1 : 0.5);
|
|
218
|
+
if (improved) {
|
|
219
|
+
const confidenceScaledBase =
|
|
220
|
+
(C.PROGRESS_REWARD_BASE +
|
|
221
|
+
C.PROGRESS_REWARD_CONF_SCALE * currentConfidence) *
|
|
222
|
+
rewardScale;
|
|
223
|
+
if (state.stepsSinceImprovement > 0) {
|
|
224
|
+
const stepBonus = Math.min(
|
|
225
|
+
state.stepsSinceImprovement * C.PROGRESS_STEPS_MULT * rewardScale,
|
|
226
|
+
C.PROGRESS_STEPS_MAX * rewardScale,
|
|
227
|
+
);
|
|
228
|
+
state.progressReward += stepBonus;
|
|
229
|
+
}
|
|
230
|
+
state.progressReward += confidenceScaledBase;
|
|
231
|
+
state.stepsSinceImprovement = 0;
|
|
232
|
+
const distanceContribution =
|
|
233
|
+
distanceDelta *
|
|
234
|
+
C.DISTANCE_DELTA_SCALE *
|
|
235
|
+
(C.DISTANCE_DELTA_CONF_BASE +
|
|
236
|
+
C.DISTANCE_DELTA_CONF_SCALE * currentConfidence);
|
|
237
|
+
state.progressReward += distanceContribution;
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (worsened) {
|
|
242
|
+
const awayPenalty =
|
|
243
|
+
(C.PROGRESS_AWAY_BASE_PENALTY +
|
|
244
|
+
C.PROGRESS_AWAY_CONF_SCALE * currentConfidence) *
|
|
245
|
+
rewardScale;
|
|
246
|
+
state.progressReward -= awayPenalty;
|
|
247
|
+
state.stepsSinceImprovement++;
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
state.stepsSinceImprovement++;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Apply the per-cell exploration bonus or revisit penalty.
|
|
256
|
+
*
|
|
257
|
+
* @param state - Mutable simulation state for the active run.
|
|
258
|
+
* @param rewardScale - Global reward scale used by the adjustment.
|
|
259
|
+
* @param coordinateScratch - Reused coordinate scratch buffer.
|
|
260
|
+
*/
|
|
261
|
+
export function applyMazeMovementExplorationVisitAdjustment(
|
|
262
|
+
state: SimulationState,
|
|
263
|
+
rewardScale: number,
|
|
264
|
+
coordinateScratch: Int32Array,
|
|
265
|
+
): void {
|
|
266
|
+
const visitsAtThisCell = state.visitsAtCurrent | 0;
|
|
267
|
+
const positiveBonus = C.NEW_CELL_EXPLORATION_BONUS * rewardScale;
|
|
268
|
+
const revisitPenalty = C.REVISIT_PENALTY_STRONG * rewardScale;
|
|
269
|
+
coordinateScratch[0] =
|
|
270
|
+
visitsAtThisCell === 1 ? positiveBonus : -revisitPenalty;
|
|
271
|
+
state.newCellExplorationBonus += coordinateScratch[0];
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Apply the long-horizon global-distance improvement bonus.
|
|
276
|
+
*
|
|
277
|
+
* @param state - Mutable simulation state for the active run.
|
|
278
|
+
* @param encodedMaze - Maze grid used for distance lookup.
|
|
279
|
+
* @param rewardScale - Global reward scale used by the bonus magnitude.
|
|
280
|
+
* @param coordinateScratch - Reused coordinate scratch buffer.
|
|
281
|
+
*/
|
|
282
|
+
export function applyMazeMovementGlobalDistanceImprovementBonus(
|
|
283
|
+
state: SimulationState,
|
|
284
|
+
encodedMaze: number[][],
|
|
285
|
+
rewardScale: number,
|
|
286
|
+
coordinateScratch: Int32Array,
|
|
287
|
+
): void {
|
|
288
|
+
const positionX = state.position[0] | 0;
|
|
289
|
+
const positionY = state.position[1] | 0;
|
|
290
|
+
const currentGlobalDistance = state.hasDistanceMap
|
|
291
|
+
? (state.distanceMap?.[positionY]?.[positionX] ?? Infinity)
|
|
292
|
+
: getMazeMovementDistance(encodedMaze, state.position, state.distanceMap);
|
|
293
|
+
coordinateScratch[0] = currentGlobalDistance as number;
|
|
294
|
+
|
|
295
|
+
const previousGlobalDistance = state.lastDistanceGlobal ?? Infinity;
|
|
296
|
+
if (currentGlobalDistance < previousGlobalDistance) {
|
|
297
|
+
const stagnationSteps = state.stepsSinceImprovement | 0;
|
|
298
|
+
if (stagnationSteps > C.GLOBAL_BREAK_BONUS_START) {
|
|
299
|
+
const bonusSteps = stagnationSteps - C.GLOBAL_BREAK_BONUS_START;
|
|
300
|
+
const uncappedBonus =
|
|
301
|
+
bonusSteps * C.GLOBAL_BREAK_BONUS_PER_STEP * rewardScale;
|
|
302
|
+
const cappedBonus = Math.min(
|
|
303
|
+
uncappedBonus,
|
|
304
|
+
C.GLOBAL_BREAK_BONUS_CAP * rewardScale,
|
|
305
|
+
);
|
|
306
|
+
state.progressReward += cappedBonus;
|
|
307
|
+
}
|
|
308
|
+
state.stepsSinceImprovement = 0;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
state.lastDistanceGlobal = currentGlobalDistance;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Apply repetition and immediate-backtrack penalties.
|
|
316
|
+
*
|
|
317
|
+
* @param state - Mutable simulation state for the active run.
|
|
318
|
+
* @param rewardScale - Global reward scale used by the penalties.
|
|
319
|
+
* @param coordinateScratch - Reused coordinate scratch buffer.
|
|
320
|
+
*/
|
|
321
|
+
export function applyMazeMovementRepetitionAndBacktrackPenalties(
|
|
322
|
+
state: SimulationState,
|
|
323
|
+
rewardScale: number,
|
|
324
|
+
coordinateScratch: Int32Array,
|
|
325
|
+
): void {
|
|
326
|
+
if (state.earlyTerminate) return;
|
|
327
|
+
|
|
328
|
+
const previousAction = state.prevAction;
|
|
329
|
+
const currentAction = state.direction;
|
|
330
|
+
const stagnationSteps = state.stepsSinceImprovement | 0;
|
|
331
|
+
|
|
332
|
+
if (
|
|
333
|
+
previousAction === currentAction &&
|
|
334
|
+
stagnationSteps > C.REPETITION_PENALTY_START
|
|
335
|
+
) {
|
|
336
|
+
const repetitionMultiplier = stagnationSteps - C.REPETITION_PENALTY_START;
|
|
337
|
+
const computedRepetitionPenalty =
|
|
338
|
+
C.REPETITION_PENALTY_BASE * repetitionMultiplier * rewardScale;
|
|
339
|
+
coordinateScratch[0] = -computedRepetitionPenalty;
|
|
340
|
+
state.invalidMovePenalty += coordinateScratch[0];
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (
|
|
344
|
+
previousAction >= 0 &&
|
|
345
|
+
currentAction >= 0 &&
|
|
346
|
+
stagnationSteps > 0 &&
|
|
347
|
+
currentAction === C.OPPOSITE_DIR[previousAction]
|
|
348
|
+
) {
|
|
349
|
+
coordinateScratch[1] = -C.BACK_MOVE_PENALTY * rewardScale;
|
|
350
|
+
state.invalidMovePenalty += coordinateScratch[1];
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Apply entropy-guided shaping based on confidence and perceptual guidance.
|
|
356
|
+
*
|
|
357
|
+
* @param state - Mutable simulation state for the active run.
|
|
358
|
+
* @param rewardScale - Global reward scale used by the penalties and bonuses.
|
|
359
|
+
* @param coordinateScratch - Reused coordinate scratch buffer.
|
|
360
|
+
*/
|
|
361
|
+
export function applyMazeMovementEntropyGuidanceShaping(
|
|
362
|
+
state: SimulationState,
|
|
363
|
+
rewardScale: number,
|
|
364
|
+
coordinateScratch: Int32Array,
|
|
365
|
+
): void {
|
|
366
|
+
if (state.earlyTerminate || !state.actionStats) return;
|
|
367
|
+
|
|
368
|
+
const { entropy, maxProb, secondProb } = state.actionStats;
|
|
369
|
+
const hasLineOfSightGuidance =
|
|
370
|
+
sumVisionGroup(
|
|
371
|
+
state.vision,
|
|
372
|
+
C.VISION_LOS_START,
|
|
373
|
+
C.VISION_GROUP_LEN,
|
|
374
|
+
SHAPING_VISION_SUM_SCRATCH,
|
|
375
|
+
) > 0;
|
|
376
|
+
const hasGradientGuidance =
|
|
377
|
+
sumVisionGroup(
|
|
378
|
+
state.vision,
|
|
379
|
+
C.VISION_GRAD_START,
|
|
380
|
+
C.VISION_GROUP_LEN,
|
|
381
|
+
SHAPING_VISION_SUM_SCRATCH,
|
|
382
|
+
) > 0;
|
|
383
|
+
|
|
384
|
+
if (entropy > C.ENTROPY_HIGH_THRESHOLD) {
|
|
385
|
+
coordinateScratch[0] = -C.ENTROPY_PENALTY * rewardScale;
|
|
386
|
+
state.invalidMovePenalty += coordinateScratch[0];
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const maxMinusSecond = (maxProb ?? 0) - (secondProb ?? 0);
|
|
391
|
+
if (
|
|
392
|
+
(hasLineOfSightGuidance || hasGradientGuidance) &&
|
|
393
|
+
entropy < C.ENTROPY_CONFIDENT_THRESHOLD &&
|
|
394
|
+
maxMinusSecond > C.ENTROPY_CONFIDENT_DIFF
|
|
395
|
+
) {
|
|
396
|
+
coordinateScratch[0] = C.EXPLORATION_BONUS_SMALL * rewardScale;
|
|
397
|
+
state.newCellExplorationBonus += coordinateScratch[0];
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Apply the periodic saturation penalty cycle.
|
|
403
|
+
*
|
|
404
|
+
* @param state - Mutable simulation state for the active run.
|
|
405
|
+
* @param rewardScale - Global reward scale used by the penalties.
|
|
406
|
+
* @param coordinateScratch - Reused coordinate scratch buffer.
|
|
407
|
+
*/
|
|
408
|
+
export function applyMazeMovementSaturationPenaltyCycle(
|
|
409
|
+
state: SimulationState,
|
|
410
|
+
rewardScale: number,
|
|
411
|
+
coordinateScratch: Int32Array,
|
|
412
|
+
): void {
|
|
413
|
+
const saturations = getMazeMovementRunServiceState().saturations;
|
|
414
|
+
if (saturations < C.SATURATION_PENALTY_TRIGGER) return;
|
|
415
|
+
|
|
416
|
+
coordinateScratch[0] = -C.SATURATION_PENALTY_BASE * rewardScale;
|
|
417
|
+
state.invalidMovePenalty += coordinateScratch[0];
|
|
418
|
+
|
|
419
|
+
const period = C.SATURATION_PENALTY_PERIOD;
|
|
420
|
+
if (period > 0 && saturations % period === 0) {
|
|
421
|
+
coordinateScratch[1] = -C.SATURATION_PENALTY_ESCALATE * rewardScale;
|
|
422
|
+
state.invalidMovePenalty += coordinateScratch[1];
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Apply the deep-stagnation termination penalty when appropriate.
|
|
428
|
+
*
|
|
429
|
+
* @param state - Mutable simulation state for the active run.
|
|
430
|
+
* @param coordinateScratch - Reused coordinate scratch buffer.
|
|
431
|
+
* @returns True when the run should terminate early.
|
|
432
|
+
*/
|
|
433
|
+
export function maybeTerminateMazeMovementDeepStagnation(
|
|
434
|
+
state: SimulationState,
|
|
435
|
+
coordinateScratch: Int32Array,
|
|
436
|
+
): boolean {
|
|
437
|
+
const stagnationSteps = state.stepsSinceImprovement | 0;
|
|
438
|
+
if (stagnationSteps <= C.DEEP_STAGNATION_THRESHOLD) {
|
|
439
|
+
return state.earlyTerminate;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const rewardScale = C.REWARD_SCALE;
|
|
443
|
+
try {
|
|
444
|
+
const runningOutsideBrowser = typeof window === 'undefined';
|
|
445
|
+
if (runningOutsideBrowser) {
|
|
446
|
+
coordinateScratch[0] = -C.DEEP_STAGNATION_PENALTY * rewardScale;
|
|
447
|
+
state.invalidMovePenalty += coordinateScratch[0];
|
|
448
|
+
return true;
|
|
449
|
+
}
|
|
450
|
+
} catch {
|
|
451
|
+
coordinateScratch[0] = -C.DEEP_STAGNATION_PENALTY * rewardScale;
|
|
452
|
+
state.invalidMovePenalty += coordinateScratch[0];
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
return state.earlyTerminate;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
export {};
|