AgentJS agent-based modelling in the browser

Two kinds on a grid

A grid of two kinds of dweller, each moving when too few of its neighbours are like it, sorts itself into clusters nobody asked for.

starting

kind Akind B

Each dweller is a BaseAgent that belongs to kind A or kind B. It looks at the cells around its own, which Grid2D returns as a Moore neighbourhood (the eight cells around it), and counts how many of the occupied ones hold its own kind.

If that share is below the tolerance on the slider, the dweller moves to a random empty cell; otherwise it stays. At a tolerance of 40% nobody demands to be in the majority, yet the grid ends up in large single-kind patches. Move the slider and watch the patches change. A few dwellers also move each step for no reason, so the grid never quite settles.

The grid allows one dweller to a cell (allowMultipleOccupancy: false), so getEmptyCells and placeAgentAtCell do the moving.

Classes used

Grid2D (getCellNeighbors, getAgentsAtCell, getAgentGridCoordinate, getEmptyCells, placeAgentAtCell), BaseAgent, RandomScheduler.

Source

This is example.js, the file this page runs.

Loading the source…