Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions examples/skill-create-env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { describe, expect, it } from 'vitest';
import { IgnitionEnv, validateTrainingEnv } from '@ignitionai/core';
import { CartPoleEnv, GridWorldEnv, MountainCarEnv } from '@ignitionai/environments';
import {
TRAINING_BUDGET_STEPS,
TRAINING_SEEDS,
createInvalidTrainingEnv,
LineWorldEnv,
runLineWorldTraining,
scriptedStep,
spaceKind,
} from './skill-create-env';

/**
* Budget and seeds declared before execution:
* - budget = TRAINING_BUDGET_STEPS (200 env steps / seed)
* - seeds = TRAINING_SEEDS (7, 11, 19)
* Measurements are printed; this file does not assert convergence.
*/
describe('skill-create-env LineWorld', () => {
it('passes TrainingEnv validation and wraps in IgnitionEnv', () => {
const world = new LineWorldEnv();
expect(() => validateTrainingEnv(world)).not.toThrow();
expect(() => new IgnitionEnv(world)).not.toThrow();
expect(spaceKind(world.actionSpace())).toBe('discrete n=2');
expect(spaceKind(world.observationSpace())).toBe('box shape=1');
expect(world.observe()).toEqual([0]);
});

it('rejects an invalid env with the real validation error', () => {
const invalid = createInvalidTrainingEnv();
expect(() => validateTrainingEnv(invalid)).toThrowError(
'[IgnitionAI] TrainingEnv must implement done() as a function',
);
expect(
() => new IgnitionEnv(invalid as never),
).toThrowError('[IgnitionAI] TrainingEnv must implement done() as a function');
});

it('reports terminated when the 1D goal is reached', async () => {
const world = new LineWorldEnv(3, 20);
world.position = 1;
const result = await scriptedStep(world);
expect(result).toMatchObject({
observation: [1],
reward: 1,
terminated: true,
truncated: false,
});
expect(world.position).toBe(0);
});

it('reports truncated when the step limit hits before the goal', async () => {
const world = new LineWorldEnv(5, 1);
const result = await scriptedStep(world);
expect(result.terminated).toBe(false);
expect(result.truncated).toBe(true);
expect(result.reward).toBe(-0.01);
expect(world.position).toBe(0);
});

it('runs the declared qtable budget on three seeds and records raw returns', async () => {
expect(TRAINING_SEEDS).toHaveLength(3);
const measurements = [];
for (const seed of TRAINING_SEEDS) {
const measurement = await runLineWorldTraining(seed, TRAINING_BUDGET_STEPS);
measurements.push(measurement);
expect(measurement.completedSteps).toBe(TRAINING_BUDGET_STEPS);
expect(measurement.terminatedEpisodes + measurement.truncatedEpisodes).toBe(
measurement.episodeReturns.length,
);
}
// Raw measurements (failures included). Not a convergence claim.
console.log(JSON.stringify({ budgetSteps: TRAINING_BUDGET_STEPS, measurements }, null, 2));
});

it('reference envs still satisfy TrainingEnv validation', () => {
expect(() => validateTrainingEnv(new GridWorldEnv())).not.toThrow();
expect(() => validateTrainingEnv(new CartPoleEnv())).not.toThrow();
expect(() => validateTrainingEnv(new MountainCarEnv())).not.toThrow();
});
});
191 changes: 191 additions & 0 deletions examples/skill-create-env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import {
IgnitionEnv,
type ActionSpace,
type BoxSpace,
type DiscreteSpace,
type MultiDiscreteSpace,
type ObservationSpace,
type StepResult,
type TrainingEnv,
} from '@ignitionai/core';
import { IgnitionEnvTFJS } from '@ignitionai/backend-tfjs';

/**
* Declared before any training run.
*
* Budget: 200 environment steps per seed (manual `step()`, not the async loop).
* Seeds: 7, 11, 19. Algorithm: qtable. No convergence claim without measurement.
*/
export const TRAINING_BUDGET_STEPS = 200;
export const TRAINING_SEEDS = [7, 11, 19] as const;
export const TRAINING_OVERRIDES = {
lr: 0.3,
epsilon: 1,
epsilonDecay: 0.99,
minEpsilon: 0.05,
stateBins: 8,
stateLow: [0],
stateHigh: [1],
} as const;

/** 1D line: start at 0, goal at `length - 1`, discrete left/right actions. */
export class LineWorldEnv implements TrainingEnv {
actions = ['left', 'right'];
position = 0;
stepCount = 0;
readonly length: number;
readonly maxSteps: number;

constructor(length = 5, maxSteps = 12) {
this.length = length;
this.maxSteps = maxSteps;
}

observe(): number[] {
return [this.position / (this.length - 1)];
}

step(action: number | number[]): void {
const a = typeof action === 'number' ? action : action[0];
if (a === 1) {
this.position = Math.min(this.length - 1, this.position + 1);
} else {
this.position = Math.max(0, this.position - 1);
}
this.stepCount += 1;
}

reward(): number {
if (this.terminated()) return 1;
return -0.01;
}

done(): boolean {
return this.terminated() || this.truncated();
}

terminated(): boolean {
return this.position === this.length - 1;
}

truncated(): boolean {
return !this.terminated() && this.stepCount >= this.maxSteps;
}

reset(): void {
this.position = 0;
this.stepCount = 0;
}

actionSpace(): DiscreteSpace {
return { type: 'discrete', n: this.actions.length };
}

observationSpace(): BoxSpace {
return { type: 'box', low: [0], high: [1], shape: [1] };
}
}

/** Missing `done()` — rejected by `validateTrainingEnv`. */
export function createInvalidTrainingEnv(): unknown {
return {
actions: ['left', 'right'],
observe(): number[] {
return [0];
},
step(): void {},
reward(): number {
return 0;
},
reset(): void {},
};
}

export function spaceKind(space: ActionSpace | ObservationSpace): string {
switch (space.type) {
case 'discrete':
return `discrete n=${(space as DiscreteSpace).n}`;
case 'box':
return `box shape=${(space as BoxSpace).shape.join('x')}`;
case 'multidiscrete':
return `multidiscrete nvec=${(space as MultiDiscreteSpace).nvec.join(',')}`;
default: {
const unexpected: never = space;
throw new Error(`unhandled space: ${JSON.stringify(unexpected)}`);
}
}
}

function mulberry32(seed: number): () => number {
let state = seed >>> 0;
return () => {
state += 0x6d2b79f5;
let t = state;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}

export interface TrainingRunMeasurement {
seed: number;
budgetSteps: number;
completedSteps: number;
terminatedEpisodes: number;
truncatedEpisodes: number;
episodeReturns: number[];
}

export async function runLineWorldTraining(
seed: number,
budgetSteps: number = TRAINING_BUDGET_STEPS,
): Promise<TrainingRunMeasurement> {
const rng = mulberry32(seed);
const originalRandom = Math.random;
Math.random = rng;
const world = new LineWorldEnv();
const runner = new IgnitionEnvTFJS(world);
try {
runner.train('qtable', { ...TRAINING_OVERRIDES });
runner.stop();
const episodeReturns: number[] = [];
let episodeReturn = 0;
let terminatedEpisodes = 0;
let truncatedEpisodes = 0;
let completedSteps = 0;
for (let i = 0; i < budgetSteps; i++) {
const result: StepResult = await runner.step();
completedSteps += 1;
episodeReturn += result.reward;
if (result.terminated || result.truncated) {
if (result.terminated) terminatedEpisodes += 1;
if (result.truncated) truncatedEpisodes += 1;
episodeReturns.push(episodeReturn);
episodeReturn = 0;
}
}
return {
seed,
budgetSteps,
completedSteps,
terminatedEpisodes,
truncatedEpisodes,
episodeReturns,
};
} finally {
runner.stop();
runner.agent?.dispose?.();
Math.random = originalRandom;
}
}

/** Scripted always-right agent for episode-end semantics (real `IgnitionEnv.step`). */
export async function scriptedStep(world: TrainingEnv): Promise<StepResult> {
const runner = new IgnitionEnv(world);
runner.agent = {
getAction: async () => 1,
remember() {},
train: async () => {},
};
return runner.step();
}
Loading
Loading