AgentJS agent-based modelling in the browser

Models as plain objects

runModel(config) builds a running agent-based model from a plain object, so a model can be written as JSON or YAML, or put together by a tool such as taali, without writing model code.

Since 1.2.0. The object's keys are the element types of the agent-based model syntax, and each section's element names one of that type's elements. The numbers an element needs sit beside it, and every number has a default.

import { runModel } from '@prayas_abhinav/agentjs-core';

const model = runModel({
  space: { element: 'a grid of cells', width: 30, height: 30 },
  edges: { element: 'wrap round' },
  'kinds of agent': { element: 'two kinds', kinds: [{ name: 'red', count: 330 }, { name: 'blue', count: 330 }] },
  noticing: { element: 'the eight cells around' },
  moving: { red: { element: 'moves to an empty cell when unhappy' }, blue: { element: 'moves to an empty cell when unhappy' } },
  rule: [{ element: 'too few like me, so I move', threshold: 0.4 }],
  turns: { element: 'one by one, in a new random order each step' },
  chance: { element: 'the same each run', seed: 1 },
});

for (let i = 0; i < 50; i++) model.step();
console.log(model.metrics().mixed);   // the share of neighbours of the same kind, on average

What it returns

The sections and their numbers

A config that names an element the syntax does not have, a section it does not have, or a combination that cannot work (a kind that moves in a space with no places, a rule that needs noticing when nothing is noticed) is refused before anything runs, and the message names what is wrong.

SectionElementsNumbers, with defaults
spacea grid of cells · an open plane · connections onlywidth, height: 20 by 20 cells, or 600 by 400
edgeswrap round · bounce back · fall outbounce back when left out. Fall out removes an agent that reaches the border, so its kind's count falls.
kinds of agentone kind · two kinds · several kinds (3 to 5)kinds: each with name, count, opinion as [lowest, highest] (0 to 1) and energy as [lowest, highest] (0 to 100)
noticingthe eight cells around · the four cells around · anyone within a distance · those it is connected todistance: 30, for a plane
movingstays put · wanders · moves to an empty cell when unhappy · goes towards a place · keeps away from othersone entry for each kind, by its name. strength 0.5, speed 2 (the top speed on a plane); for a place, x and y as a share of the width and height (0.5) and reach, beyond which the agent wanders
connectionsa ring · at random · to the nearesta ring: reach 2, shortcuts 0 (the chance a link is rewired to anywhere); at random: chance 0.1; to the nearest: count 3
rulea list of: too few like me, so I move · copy the majority · meet halfway · pass it on · rest when tired · a small model choosestoo few: threshold 0.4; meet halfway: tolerance 0.3, pull 0.5; pass it on: spreads (a kind's name, the first by default), chance 0.5; rest when tired: tired 20, rested 80, recovery 1.5, spend 0.3; small model: threshold 0, stride 5. Any rule may carry kinds, a list of names, to apply to those only.
turnsone by one, in a new random order each step · one by one, in the same order each step · all at onceAll at once: every agent decides from the picture of the step before, then all act.
watchingthe picture · a count of each kind over time · how mixed the neighbourhoods are · how far opinions are spreadOnly a tool reads this; metrics() always gives every number.
endingruns until stopped · stops after a number of steps · stops when nothing changessteps: 100. Nothing changing means no agent changed place, kind or opinion in the step.
chancethe same each run · different each runseed: 1

How the elements work

A small model chooses

Each kind the rule covers gets its own TensorFlowChooser in an NSMnumTable. It reads four numbers (where the agent is, its opinion, the share of like neighbours) and answers only with the place of one move in a fixed list (up, down, left, right) or none, and code makes the move. The model runModel makes is untrained unless the rule carries weights. An untrained one answers from seeded weights and means nothing. A real one is trained on the keys of the person it works for, with keyingItems, readKeys and keyedRows, and its weights (chooser.getWeights()) are put in the rule. The handle's situations() lists what the model reads for each agent, so a person can key it; taali does this inside its page. Leave moving empty for a kind the model moves.

The page that runs it needs TensorFlow.js; the NSMnum example shows the import map.