From 984f07a24d3d377c13358fc5533c0eda383858f5 Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 10 Nov 2023 19:51:02 -0600 Subject: refactor: actions take an additional world parameter --- src/simulation/actions.js | 8 +++--- src/simulation/actions.test.js | 64 +++++++++++++++++++++--------------------- src/world/agent.js | 2 +- src/world/agent.test.js | 6 ++-- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/simulation/actions.js b/src/simulation/actions.js index aaaf1d8..0f1a767 100644 --- a/src/simulation/actions.js +++ b/src/simulation/actions.js @@ -5,7 +5,7 @@ const threshold = 0.5; const move_forward = { size: 1, - propose: (agent, head) => { + propose: (world, agent, head) => { if (head[0] > threshold) { const dx = { n: 0, e: 1, s: 0, w: -1 }[agent.flags.orientation]; const dy = { n: -1, e: 0, s: 1, w: 0 }[agent.flags.orientation]; @@ -24,7 +24,7 @@ const move_forward = { const move_backward = { size: 1, - propose: (agent, head) => { + propose: (world, agent, head) => { if (head[0] > threshold) { const dx = { n: 0, e: 1, s: 0, w: -1 }[agent.flags.orientation]; const dy = { n: -1, e: 0, s: 1, w: 0 }[agent.flags.orientation]; @@ -44,7 +44,7 @@ const move_backward = { const turn_left = { size: 1, - propose: (agent, head) => { + propose: (world, agent, head) => { if (head[0] > threshold) { const orientation = { n: 'w', e: 'n', s: 'e', w: 's' }[agent.flags.orientation]; return [{ @@ -62,7 +62,7 @@ const turn_left = { const turn_right = { size: 1, - propose: (agent, head) => { + propose: (world, agent, head) => { if (head[0] > threshold) { const orientation = { n: 'e', e: 's', s: 'w', w: 'n' }[agent.flags.orientation]; return [{ diff --git a/src/simulation/actions.test.js b/src/simulation/actions.test.js index d039f8a..04111f1 100644 --- a/src/simulation/actions.test.js +++ b/src/simulation/actions.test.js @@ -16,23 +16,23 @@ test("move forward", () => { const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } }; const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } }; - expect(move_forward.propose(n, [1])).toEqual([ + expect(move_forward.propose(null, n, [1])).toEqual([ { agent_changes: [{ agent_id: 0, x: 0, y: -1 }] }, ]); - expect(move_forward.propose(s, [1])).toEqual([ + expect(move_forward.propose(null, s, [1])).toEqual([ { agent_changes: [{ agent_id: 0, x: 0, y: 1 }] }, ]); - expect(move_forward.propose(e, [1])).toEqual([ + expect(move_forward.propose(null, e, [1])).toEqual([ { agent_changes: [{ agent_id: 0, x: 1, y: 0 }] }, ]); - expect(move_forward.propose(w, [1])).toEqual([ + expect(move_forward.propose(null, w, [1])).toEqual([ { agent_changes: [{ agent_id: 0, x: -1, y: 0 }] }, ]); - expect(move_forward.propose(n, [0])).toEqual([]); - expect(move_forward.propose(s, [-1])).toEqual([]); - expect(move_forward.propose(e, [0])).toEqual([]); - expect(move_forward.propose(w, [-1])).toEqual([]); + expect(move_forward.propose(null, n, [0])).toEqual([]); + expect(move_forward.propose(null, s, [-1])).toEqual([]); + expect(move_forward.propose(null, e, [0])).toEqual([]); + expect(move_forward.propose(null, w, [-1])).toEqual([]); }); @@ -42,23 +42,23 @@ test("move backward", () => { const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } }; const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } }; - expect(move_backward.propose(n, [1])).toEqual([ + expect(move_backward.propose(null, n, [1])).toEqual([ { agent_changes: [{ agent_id: 0, x: 0, y: 1 }] }, ]); - expect(move_backward.propose(s, [1])).toEqual([ + expect(move_backward.propose(null, s, [1])).toEqual([ { agent_changes: [{ agent_id: 0, x: 0, y: -1 }] }, ]); - expect(move_backward.propose(e, [1])).toEqual([ + expect(move_backward.propose(null, e, [1])).toEqual([ { agent_changes: [{ agent_id: 0, x: -1, y: 0 }] }, ]); - expect(move_backward.propose(w, [1])).toEqual([ + expect(move_backward.propose(null, w, [1])).toEqual([ { agent_changes: [{ agent_id: 0, x: 1, y: 0 }] }, ]); - expect(move_backward.propose(n, [0])).toEqual([]); - expect(move_backward.propose(s, [-1])).toEqual([]); - expect(move_backward.propose(e, [0])).toEqual([]); - expect(move_backward.propose(w, [-1])).toEqual([]); + expect(move_backward.propose(null, n, [0])).toEqual([]); + expect(move_backward.propose(null, s, [-1])).toEqual([]); + expect(move_backward.propose(null, e, [0])).toEqual([]); + expect(move_backward.propose(null, w, [-1])).toEqual([]); }); @@ -68,23 +68,23 @@ test("turn_left", () => { const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } }; const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } }; - expect(turn_left.propose(n, [1])).toEqual([ + expect(turn_left.propose(null, n, [1])).toEqual([ { agent_changes: [{ agent_id: 0, flags: { orientation: 'w' } }] }, ]); - expect(turn_left.propose(s, [1])).toEqual([ + expect(turn_left.propose(null, s, [1])).toEqual([ { agent_changes: [{ agent_id: 0, flags: { orientation: 'e' } }] }, ]); - expect(turn_left.propose(e, [1])).toEqual([ + expect(turn_left.propose(null, e, [1])).toEqual([ { agent_changes: [{ agent_id: 0, flags: { orientation: 'n' } }] }, ]); - expect(turn_left.propose(w, [1])).toEqual([ + expect(turn_left.propose(null, w, [1])).toEqual([ { agent_changes: [{ agent_id: 0, flags: { orientation: 's' } }] }, ]); - expect(turn_left.propose(n, [0])).toEqual([]); - expect(turn_left.propose(s, [-1])).toEqual([]); - expect(turn_left.propose(e, [0])).toEqual([]); - expect(turn_left.propose(w, [-1])).toEqual([]); + expect(turn_left.propose(null, n, [0])).toEqual([]); + expect(turn_left.propose(null, s, [-1])).toEqual([]); + expect(turn_left.propose(null, e, [0])).toEqual([]); + expect(turn_left.propose(null, w, [-1])).toEqual([]); }); @@ -94,23 +94,23 @@ test("turn_right", () => { const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } }; const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } }; - expect(turn_right.propose(n, [1])).toEqual([ + expect(turn_right.propose(null, n, [1])).toEqual([ { agent_changes: [{ agent_id: 0, flags: { orientation: 'e' } }] }, ]); - expect(turn_right.propose(s, [1])).toEqual([ + expect(turn_right.propose(null, s, [1])).toEqual([ { agent_changes: [{ agent_id: 0, flags: { orientation: 'w' } }] }, ]); - expect(turn_right.propose(e, [1])).toEqual([ + expect(turn_right.propose(null, e, [1])).toEqual([ { agent_changes: [{ agent_id: 0, flags: { orientation: 's' } }] }, ]); - expect(turn_right.propose(w, [1])).toEqual([ + expect(turn_right.propose(null, w, [1])).toEqual([ { agent_changes: [{ agent_id: 0, flags: { orientation: 'n' } }] }, ]); - expect(turn_right.propose(n, [0])).toEqual([]); - expect(turn_right.propose(s, [-1])).toEqual([]); - expect(turn_right.propose(e, [0])).toEqual([]); - expect(turn_right.propose(w, [-1])).toEqual([]); + expect(turn_right.propose(null, n, [0])).toEqual([]); + expect(turn_right.propose(null, s, [-1])).toEqual([]); + expect(turn_right.propose(null, e, [0])).toEqual([]); + expect(turn_right.propose(null, w, [-1])).toEqual([]); }); diff --git a/src/world/agent.js b/src/world/agent.js index 576ebc4..3faa3c8 100644 --- a/src/world/agent.js +++ b/src/world/agent.js @@ -15,7 +15,7 @@ export function agent_decide(world, agent, senses, actions) { const tail = result.slice(action.size); const props = action - .propose(new_agent, head) + .propose(world, new_agent, head) .reduce( (acc, proposal) => proposal_merge(acc, proposal), proposals diff --git a/src/world/agent.test.js b/src/world/agent.test.js index 5d5828f..d10a43a 100644 --- a/src/world/agent.test.js +++ b/src/world/agent.test.js @@ -13,13 +13,13 @@ test("simple agent decisions", () => { const senses = []; const actions = [ - { size: 1, propose: (agent, head) => [{ agent_changes: [{ agent_id: 3, flags: { act1: head[0] } }] }] }, - { size: 1, propose: (agent, head) => [{ agent_changes: [{ agent_id: 3, flags: { act2: head[0] } }] }] }, + { size: 1, propose: (world, agent, head) => [{ agent_changes: [{ agent_id: 3, flags: { act1: head[0] } }] }] }, + { size: 1, propose: (world, agent, head) => [{ agent_changes: [{ agent_id: 3, flags: { act2: head[0] } }] }] }, ]; expect(agent_decide(lattice, agent, senses, actions)).toEqual([ { ...agent, state: 'state' }, - actions.map((a, idx) => a.propose(null, [idx])).flat(), + actions.map((a, idx) => a.propose(null, null, [idx])).flat(), ]); }); -- cgit v1.2.1