Create a GridEnvironment
with the given width
and height
.
An array of the renderers associated with this Environment
.
An Environment
can have multiple renderers, usually one to render
the Agent
s spatially and others for data visualization,
such as a LineChartRenderer
, Histogram
, etc.
This property will always equal the number of tick cycles that
have passed since the Environment
was created. If you call
tick
so that it goes forward multiple time steps, it will
increase the time
by that value (not by just 1
, even though
you only called tick
once).
const environment = new Environment();
environment.time; // returns 0
environment.tick();
environment.time; // returns 1
environment.tick(3);
environment.time; // returns 4
Add an Agent
to this Environment
. Once this is called,
the Agent
's environment
property
will automatically be set to this Environment
.
Whether to rebalance if there is a KDTree
(defaults to true)
Remove all agents from the environment.
Decrement a numeric piece of data associated with this Agent
(decreasing its value by 1). This method is synchronous —
it immediately decreases the value (to asynchronously decrease it,
the rule function should instead return a new value.
agent.set('x', 50);
agent.decrement('x');
agent.get('x'); // returns 49
If the second parameter n
is included, decrements by that amount.
agent.set('x', 50);
agent.decrement('x', 10);
agent.get('x'); // returns 40
If the value has not yet been set, calling this method sets it to -1
(or to -n
).
Fill every cell of the grid with an agent and set that agent's position to its x/y coordinate.
Retrieve an arbitrary piece of data associated by name.
If the data has not been set
, returns null
.
Retrieve the agent at the specified cell coordinate.
Get an agent in the environment by its ID.
Get an array of all the agents in the environment.
Retrieve the cell at the specified coordinate.
Get all cells of the environment, in a flat array.
Retrieve all the data associated with this Agent
at once.
agent.set('x', 3);
agent.set('color', 'blue');
agent.set('active', false);
agent.getData();
// returns {
// x: 3,
// color: 'blue',
// active: false
// }
Find a random open cell in the GridEnvironment.
The coordinate of the open cell.
increment a numeric piece of data associated with this Agent
(increasing its value by 1). This method is synchronous —
it immediately increases the value (to asynchronously increase it,
the rule function should instead return a new value.
agent.set('x', 50);
agent.increment('x');
agent.get('x'); // returns 51
If the second parameter n
is included, decrements by that amount.
agent.set('x', 50);
agent.increment('x', 10);
agent.get('x'); // returns 60
If the value has not yet been set, calling this method sets it to 1
(or to n
).
loop
is like tick
, but the callback is invoked with every
cell coordinate, not every agent.
The callback is invoked with arguments x
, y
, and agent
(if there is one at that cell coordinate).
Pass a function to cache and use the return value within the same environment tick.
The function to memoize.
The return value of the function that was passed.
Get the neighbors of an agent within a certain radius. Depending on the third parameter, retrieves either the von Neumann neighborhood (https://en.wikipedia.org/wiki/Von_Neumann_neighborhood) or the Moore neighborhood (https://en.wikipedia.org/wiki/Moore_neighborhood).
the agent whose neighbors to retrieve
how far to look for neighbors
whether to use the Moore neighborhood or von Neumann (defaults to von Neumann)
Remove an agent from the environment.
For GridEnvironments, removeAgentAt
takes x
and y
values
and removes the Agent
(if there is one) at that cell coordinate.
Remove an agent from the environment by its ID.
Set a piece of data associated with this agent. Name should be a string while value can be any valid type. Alternatively, the first parameter can be an object, which merges the current data with the new data (adding new values and overwriting existing). Ex. agent.set('x', 5); agent.set('color', 'red');
Get an array of data associated with agents in the environment by key.
Calling environment.stat('name')
is equivalent to calling
environment.getAgents().map(agent => agent.get('name'));
By default, calling this will calculate the result at most once per time cycle, and return the cached value on subsequent calls (until the next time cycle, when it will recalculate).
The key for which to retrieve data.
Whether or not to cache the result.
Array of data associated with agent.get(key)
across all agents.
environment.addAgent(new Agent({ name: "Alice" }));
environment.addAgent(new Agent({ name: "Bob" }));
environment.addAgent(new Agent({ name: "Chaz" }));
environment.stat('name'); // returns ['Alice', 'Bob', 'Chaz']
Given two pairs of cell coordinates, swap the agents at those cells. If both are empty, nothing happens. If one is empty and the other has an agent, this is equivalent to moving that agent to the new cell coordinate.
Override/extend Environment.tick
to include the
GridEnvironment
's cells.
Generated using TypeDoc
A
GridEnvironment
is the deprecated version of a cellular automata. It's now recommended that you use a standardEnvironment
with aTerrain
. This class will be removed entirely in v0.6.0.In a
GridEnvironment
with anASCIIRenderer
,Agent
s are rendered using their"value"
data (a single character).since 0.4.0
0.0.10