add smoke test for neat-snake-brain.ts

This commit is contained in:
Michael Peters 2024-08-29 16:07:58 -07:00
parent 72a761eb3d
commit e0838d091c
3 changed files with 31 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import './test/test-network';
import './test/test-ioset';
import './test/test-neat';
import './test/test-neat-brain';
import './test/test-neat-snake-brain';
import './test/test-util';
const main = document.querySelector('main');

View File

@ -50,7 +50,7 @@ const _BASE_GENOME_SNAKE_BRAIN_NEAT_EDGES = completeBipartiteEdges(
],
[OUTPUTS.U, OUTPUTS.D, OUTPUTS.L, OUTPUTS.R],
);
const BASE_GENOME_SNAKE_BRAIN_NEAT = _BASE_GENOME_SNAKE_BRAIN_NEAT_EDGES.map((e, idx) => ({
export const BASE_GENOME_SNAKE_BRAIN_NEAT = _BASE_GENOME_SNAKE_BRAIN_NEAT_EDGES.map((e, idx) => ({
...e,
data: { innovation: -_BASE_GENOME_SNAKE_BRAIN_NEAT_EDGES.length + idx, weight: 0, enabled: true },
}));

View File

@ -0,0 +1,29 @@
import { vec2 } from '../site/snake/game-engine';
import { mutate } from '../site/snake/neat';
import { BASE_GENOME_SNAKE_BRAIN_NEAT, NEATSnakeBrain } from '../site/snake/neat-snake-brain';
import { addTest, assert } from './tests';
function testThink() {
const mc = {
mutate_rate: 0.25,
assign_rate: 0.1,
assign_mag: 1.0,
perturb_mag: 0.1,
new_node_rate: 0.2,
new_connection_rate: 0.1,
};
const genome = mutate(BASE_GENOME_SNAKE_BRAIN_NEAT, mc);
const brain = NEATSnakeBrain.fromGenome(genome);
const sgs = {
dead: false,
snake: [vec2(2, 2), vec2(2, 3), vec2(2, 4)],
apple: vec2(2, 6),
};
const move = brain.think(sgs);
assert(move !== 'dead');
assert(move !== vec2(0, -1));
}
addTest(testThink);