From e0838d091c33281fe8d707b51811985a09d4d090 Mon Sep 17 00:00:00 2001 From: Michael Peters Date: Thu, 29 Aug 2024 16:07:58 -0700 Subject: [PATCH] add smoke test for neat-snake-brain.ts --- src/index.tsx | 1 + src/site/snake/neat-snake-brain.ts | 2 +- src/test/test-neat-snake-brain.ts | 29 +++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/test/test-neat-snake-brain.ts diff --git a/src/index.tsx b/src/index.tsx index 3b540a0..7014905 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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'); diff --git a/src/site/snake/neat-snake-brain.ts b/src/site/snake/neat-snake-brain.ts index 5d83cb2..3586494 100644 --- a/src/site/snake/neat-snake-brain.ts +++ b/src/site/snake/neat-snake-brain.ts @@ -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 }, })); diff --git a/src/test/test-neat-snake-brain.ts b/src/test/test-neat-snake-brain.ts new file mode 100644 index 0000000..6bc6fa3 --- /dev/null +++ b/src/test/test-neat-snake-brain.ts @@ -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);