AgentJS
AgentJS is a TypeScript library for agent-based models: simulations in which many small agents each follow simple rules and something larger shows up. It runs in the browser or in Node.js, and draws with p5.js when you want to see the model run.
You write your own agents. The library supplies the places they live in (a grid, or a continuous space), the schedulers that give each agent its turn, networks of connections, behaviour trees, and a layer for agents run by small trained models. There are no analytics or cookies on this site.
Install
From 1.1.0, which adds NSMnum, the package is @prayas_abhinav/agentjs-core; version 1.0.1 stays on npm under the old name, agentjs-core. To build it from the repository:
git clone https://github.com/prayasabhinav/AgentJS.git
cd AgentJS
npm ci
npm run build:framework # writes dist/index.es.js, the library as one module
For a browser page without a build step, copy lib/agentjs.es.js from this site, which is that same build. lib/README.md says how it was made.
Quick start
Walkers in a continuous space, run in Node.js. Save it as walkers.mjs in the repository folder after building, and change the import to ./dist/index.es.js; in a project that has installed the package, the import below is the one to use.
import { MovingAgent, ContinuousSpace, RandomScheduler } from '@prayas_abhinav/agentjs-core';
class Walker extends MovingAgent {
constructor(id, space) {
super(id, {}, { x: 250, y: 250 }, { maxSpeed: 2 });
this.space = space;
}
step() {
this.wander(1); // a push in a random direction
super.step(); // apply friction and the top speed, then move
this.space.moveAgent(this, this.position); // the space wraps the walker at the edges
}
}
const space = new ContinuousSpace({
width: 500,
height: 500,
boundaryType: 'periodic',
enableSpatialIndex: true,
maxObjectsPerNode: 10,
maxTreeDepth: 6,
});
const scheduler = new RandomScheduler();
for (let i = 0; i < 100; i++) {
const walker = new Walker(`walker-${i}`, space);
space.addAgent(walker);
scheduler.addAgent(walker);
}
for (let step = 0; step < 200; step++) scheduler.step();
const away = space.getAllAgents().filter((walker) => space.distance(walker.position, { x: 250, y: 250 }) > 50);
console.log(`after ${scheduler.getCurrentStep()} steps, ${away.length} of ${space.getAgentCount()} walkers are more than 50 from the start`);
It prints a line such as after 200 steps, 60 of 100 walkers are more than 50 from the start. The same model, drawn, is the first example below.
The pattern is the same everywhere. An agent class has a step() method. An environment (ContinuousSpace or Grid2D) holds the agents and answers questions about where they are. A scheduler holds the agents and calls each one's step() once per scheduler.step(). Drawing is separate and only reads the model. The guide goes through it in more detail.
Examples
Each one runs in the page. Under it is its explanation and its full source, which is the file the page runs.
- 1Walkers in a continuous space
Points wander over a space with joined edges; the space reports who is near whom. ContinuousSpace, MovingAgent
- 2Two kinds on a grid
Two kinds of dweller move when too few neighbours are like them, and the grid sorts itself. Grid2D, BaseAgent
- 3Opinions along connections
Points on a ring move their opinions towards those they are connected to. NetworkManager
- 4Points with a behaviour tree
Each point runs its own tree: rest when tired, go to the marker when near, otherwise wander. BehaviorTree
- 5NSMnum with untrained choosers
Two agent types each get a small model of their own, and the untrained models answer from random weights. NSMnumTable, TensorFlowChooser
NSMnum
An NSMnum is a small model that answers only with a number: the place of one action in a list, or none. It writes no text and does not act; your code checks the answer and decides what runs. In AgentJS each agent type can have its own. The method is called ungli, and the parts of an NSMnum are listed on the NSMnum page of SYNTAX. The last example shows it running with untrained models, whose answers come from random weights and mean nothing; a real one is trained on the keys of the person it works for.
More
- Guide: how a model is put together, in the browser and in Node.js.
- Models as plain objects:
runModel(config)builds a whole model from JSON. - Settings: what
ConfigurationManagerdoes and does not do. - API reference, generated from the source.
- Repository, README, changelog, licence. To report a problem or suggest a change, open an issue or a pull request there.