Agents can be instantiated with or without data. Instantiating
with data is equivalent to creating an Agent and immediately
calling Agent.set to add data.
// instantiates an Agent without data
const a = new Agent();
// instantiates an Agent with data
const b = new Agent({
x: 50,
y: 100
});
An Agent can only belong to a single Environment. When
environment.addAgent(agent); is called, this is value is updated
to point to that Environment.
const environment = new Environment();
const agent = new Agent();
agent.environment; // returns `null`
environment.addAgent(agent);
agent.environment === environment; // returns `true`
Agents are automatically assigned a unique ID when they are created.
This can be useful when you need to refer to a specific Agent, and
they can be retrieved using their ID from their Environment by calling
environment.getAgentById(id);
const agent = new Agent();
const id = agent.id; // returns "59B4F928-46C8-..." (for example)
Until v0.5.14, this was the preferred way to add behavior to Agents.
Now, the preferred method is by setting the Agent's "tick" value (i.e. agent.set({ tick: function(agt) { ... }})).
This method will still be allowed until v0.7.0.
Adds a rule (a function taking an Agent as a callback or a Rule object) that may be run with every Environment.tick.
It is possible to add more than one rule to an Agent, although it
is generally easier to write a longer function or to break it apart
into multiple functions.
// adds a rule that *synchronously* increments the Agent's "x" value
agent.addRule(function(agt) {
agent.increment('x');
});
// adds a rule that *asynchronously* increments the Agent's "x" value
agent.addRule(function(agt) {
return {
x: agt.get('x') + 1
};
});
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).
Like Agent.addRule, this method is deprecated and the
recommended way is to now call
agent.set('queue', function(agt) { ... });
Calling this method enqueues a function to be executed
asynchronously (at the end of the Environment's tick cycle).
This is useful if a 'cleanup pass' should be performed between
time steps to adjust Agent data.
Below, the Agent sets its "x" value to 30 whenever it is
activated during the Environment's tick cycle. After all of that
cycle's Agents have been activated, this Agent sets its "x"
value to 20. So if any other Agent references its "x" value
during a tick cycle after it has been activated, it will return 30,
but in between tick cycles it will return 20.
agent.addRule(agt => {
agt.set("x", 30);
agt.enqueue(a => {
a.set("x", 20);
});
});
Any additional parameters passed to the enqueued function will be remembered and passed through when the function is executed.
Retrieve an arbitrary piece of data associated by name.
If the data has not been set, returns null.
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
// }
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).
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');
Generated using TypeDoc
This class puts the
Agentin 'agent-based modeling.' More specifically, anAgentrepresents an individual unit of data and its associated behaviors.0.0.5