diff options
author | sanine <sanine.not@pm.me> | 2023-11-10 11:24:56 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-10 11:24:56 -0600 |
commit | 428062dccb362627b4251945632c75d9db8f93f7 (patch) | |
tree | 27d50a332414ccd105997827102ff0a59403f58e | |
parent | 1b92f501c40eded57d66946ff717e49a5362f7c3 (diff) |
refactor senses to use world instead of lattice only
-rw-r--r-- | src/world/agent.js | 4 | ||||
-rw-r--r-- | src/world/sense.js | 6 | ||||
-rw-r--r-- | src/world/sense.test.js | 12 | ||||
-rw-r--r-- | 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]], [[], []] |