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
step(): runs one step. It returns nothing for an ordinary model, and a promise to wait for when a rule is a small model chooses.reset(): starts again from the same config. With the same each run it starts the same run again.state(): the step, the space, the names of the kinds, every agent (id,kind,x,y,opinion,energy,resting,unhappy) and the links as pairs of positions in that list. On a gridxis the column andythe row; on a plane they are the position; with connections only they are a place from 0 to 1 for drawing.metrics():step,population,counts(each kind),mixed,spread,unhappy,changedandended.neighbours(id): the ids of the agents it notices.dispose()frees a small model.endedis true once an ending element has stopped the run.
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.
| Section | Elements | Numbers, with defaults |
|---|---|---|
space | a grid of cells · an open plane · connections only | width, height: 20 by 20 cells, or 600 by 400 |
edges | wrap round · bounce back · fall out | bounce back when left out. Fall out removes an agent that reaches the border, so its kind's count falls. |
kinds of agent | one 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) |
noticing | the eight cells around · the four cells around · anyone within a distance · those it is connected to | distance: 30, for a plane |
moving | stays put · wanders · moves to an empty cell when unhappy · goes towards a place · keeps away from others | one 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 |
connections | a ring · at random · to the nearest | a ring: reach 2, shortcuts 0 (the chance a link is rewired to anywhere); at random: chance 0.1; to the nearest: count 3 |
rule | a list of: too few like me, so I move · copy the majority · meet halfway · pass it on · rest when tired · a small model chooses | too 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. |
turns | one by one, in a new random order each step · one by one, in the same order each step · all at once | All at once: every agent decides from the picture of the step before, then all act. |
watching | the picture · a count of each kind over time · how mixed the neighbourhoods are · how far opinions are spread | Only a tool reads this; metrics() always gives every number. |
ending | runs until stopped · stops after a number of steps · stops when nothing changes | steps: 100. Nothing changing means no agent changed place, kind or opinion in the step. |
chance | the same each run · different each run | seed: 1 |
How the elements work
- Kinds are also states. copy the majority and pass it on change an agent's kind, so the count of each kind over time shows an opinion or a rumour spreading.
- Too few like me, so I move says an agent is unhappy when the share of its neighbours of its own kind is below the threshold, and an agent with no neighbours is content. It only decides who is unhappy: moves to an empty cell when unhappy, under
moving, moves them. - Copy the majority takes the kind most neighbours have, and keeps its own on a tie. Meet halfway moves an opinion by
pullof the way to the mean of the neighbours whose opinion is withintolerance. Pass it on turns an agent into thespreadskind with chancechancefor each neighbour that already is. - Rest when tired is a behaviour tree of one selector: if the agent is tired (energy below
tired) it rests, gainingrecoverya step, until it hasrested; otherwise it moves asmovingsays and spendsspend. - Connections only has no places: a ring is drawn round a circle and other connections are scattered over a square, and to the nearest measures on that square. Agents there can only stay put.
- Seeds. With the same each run, everything AgentJS decides by chance (the order of a random scheduler, a wandering push, a link formed at random) comes from one small seeded generator,
seedRandom(seed)andrandom(), both exported. Without a seed it isMath.random. An untrained small model's weights come from a fixed seed of their own.
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.