train-kit / test / tabular.test.mjs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Tabular Q: the Bellman step, hand-checked.
import test from "node:test";
import assert from "node:assert/strict";
import { ensureRow, qUpdate } from "../dist/tabular.js";

test("ensureRow zero-fills on first sight and is stable after", () => {
  const m = { v: 1, game: "toy", episodes: 0, updatedAt: 0, q: {} };
  const row = ensureRow(m, "s0", 3);
  assert.deepEqual(row, [0, 0, 0]);
  row[1] = 5;
  assert.deepEqual(ensureRow(m, "s0", 3), [0, 5, 0], "same row object");
});

test("qUpdate matches the hand-computed step, including terminals", () => {
  const m = { v: 1, game: "toy", episodes: 0, updatedAt: 0, q: { s1: [2, 8] } };
  const opts = { alpha: 0.5, gamma: 0.9, nActions: 2 };
  qUpdate(m, { g: "toy", s: "s0", a: 0, r: 1, s2: "s1", done: false }, opts);
  // q = 0 + 0.5*(1 + 0.9*8 - 0) = 4.1
  assert.ok(Math.abs(m.q.s0[0] - 4.1) < 1e-12);
  qUpdate(m, { g: "toy", s: "s0", a: 1, r: -2, s2: null, done: true }, opts);
  // terminal: q = 0 + 0.5*(-2 + 0 - 0) = -1
  assert.equal(m.q.s0[1], -1);
});

static mirror of HEAD · about · clone: git clone https://git.ardegazu.ro/train-kit.git