From 428062dccb362627b4251945632c75d9db8f93f7 Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 10 Nov 2023 11:24:56 -0600 Subject: refactor senses to use world instead of lattice only --- src/world/agent.js | 4 ++-- src/world/sense.js | 6 +++--- src/world/sense.test.js | 12 ++++++------ src/world/world.js | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/world/agent.js b/src/world/agent.js index d91481b..576ebc4 100644 --- a/src/world/agent.js +++ b/src/world/agent.js @@ -4,8 +4,8 @@ import { sense_read } from './sense.js'; import { proposal_merge } from './proposal.js'; -export function agent_decide(lattice, agent, senses, actions) { - const inputs = senses.map(s => sense_read(lattice, agent, s)).flat(); +export function agent_decide(world, agent, senses, actions) { + const inputs = senses.map(s => sense_read(world, agent, s)).flat(); const [result, state] = agent.net.compute(inputs, agent.state); const new_agent = { ...agent, state }; diff --git a/src/world/sense.js b/src/world/sense.js index 47329b7..9b5c7d4 100644 --- a/src/world/sense.js +++ b/src/world/sense.js @@ -8,10 +8,10 @@ */ -export function sense_read(lattice, agent, sense) { - const result = sense.read(lattice, agent); +export function sense_read(world, agent, sense) { + const result = sense.read(world, agent); if (result.length !== sense.size) { throw new Error(`Expected result of size ${sense.size}, but got ${result.length} instead.`); } - return sense.read(lattice, agent); + return result; } diff --git a/src/world/sense.test.js b/src/world/sense.test.js index 1ef7bce..27ee2b5 100644 --- a/src/world/sense.test.js +++ b/src/world/sense.test.js @@ -4,30 +4,30 @@ import { sense_read } from './sense.js'; test("basic sense works", () => { const flag_sense = { size: 1, - read: (lattice, agent) => { + read: (world, agent) => { const {x, y} = agent; - return [ lattice[y-1][x].type === 'flag' ? 1.0 : 0.0 ] + return [ world.lattice[y-1][x].type === 'flag' ? 1.0 : 0.0 ] }, }; const lattice = [[ { type: 'flag' } ]]; const agent = { x: 0, y: 1 }; - expect(sense_read(lattice, agent, flag_sense)).toEqual([1.0]); + expect(sense_read({lattice}, agent, flag_sense)).toEqual([1.0]); }); test("senses throw if the size is incorrect", () => { const flag_sense = { size: 2, - read: (lattice, agent) => { + read: (world, agent) => { const {x, y} = agent; - return [ lattice[y-1][x].type === 'flag' ? 1.0 : 0.0 ] + return [ world.lattice[y-1][x].type === 'flag' ? 1.0 : 0.0 ] }, } const lattice = [[ { type: 'flag' } ]]; const agent = { x: 0, y: 1 }; - expect(() => sense_read(lattice, agent, flag_sense)).toThrow(); + expect(() => sense_read({lattice}, agent, flag_sense)).toThrow(); }); diff --git a/src/world/world.js b/src/world/world.js index 87d0aed..e1d984d 100644 --- a/src/world/world.js +++ b/src/world/world.js @@ -8,7 +8,7 @@ export function world_update(world, postprocess=[]) { const intermediate_lattice = lattice_apply(world.lattice, lattice_props); const decisions = world.agents - .map(a => agent_decide(world.lattice, agent, world.senses, world.actions)) + .map(a => agent_decide(world, agent, world.senses, world.actions)) .reduce( ([agents, props], [agent, prop]) => [[...agents, agent], [...props, prop]], [[], []] -- cgit v1.2.1