Flocc
    Preparing search index...

    Class Agent

    This class puts the Agent in 'agent-based modeling.' More specifically, an Agent represents an individual unit of data and its associated behaviors.

    0.0.5

    Hierarchy (View Summary)

    Implements

    • DataObj
    Index

    Constructors

    • 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
      });

      Parameters

      • data: Data = {}

      Returns Agent

    Properties

    environment: Environment = null

    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;
    id: string = ...

    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)

    Methods

    • 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
      };
      });

      Parameters

      • rule: Function | Rule
      • ...args: any[]

      Returns void

      since version 0.5.14

      0.0.5

    • 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).

      Parameters

      • name: string
      • n: number = 1

      Returns void

      0.0.8

    • Emit an event to the environment's event bus.

      Type Parameters

      • T = any

      Parameters

      • type: string

        Event type

      • data: T

        Event payload

      Returns void

      // In tick function
      agent.set('tick', (a) => {
      if (a.get('health') < 10) {
      a.emit('agent:lowHealth', { health: a.get('health') });
      }
      });

      0.6.0

    • 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.

      Parameters

      • rule: Function
      • ...args: any[]

      Returns void

      since version 0.5.14

      0.0.5

    • Retrieve an arbitrary piece of data associated by name. If the data has not been set, returns null.

      Parameters

      • name: string

      Returns any

      0.0.5

    • 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
      // }

      Returns Data

      0.1.0

    • 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).

      Parameters

      • name: string
      • n: number = 1

      Returns void

      0.0.8

    • Subscribe to an event type. The handler receives the agent as the first argument and the event object as the second.

      Type Parameters

      • T = any

      Parameters

      • type: string

        Event type to listen for

      • handler: (agent: Agent, event: FloccEvent<T>) => void

        Function to call when event is emitted

      Returns () => void

      Unsubscribe function

      agent.on('food:nearby', (agent, event) => {
      agent.set('target', event.data.location);
      agent.set('state', 'foraging');
      });

      0.6.0

    • Schedule this agent to tick at a specific environment time. Only has an effect if the environment uses a scheduler that supports explicit scheduling (e.g., PriorityScheduler).

      Parameters

      • time: number

        The environment time at which to tick

      Returns void

      agent.set('tick', (a) => {
      // Do something...

      // Schedule next tick at time 150
      a.scheduleAt(150);
      });

      0.6.0

    • Schedule this agent to tick after a delay (relative to current time). Shorthand for agent.scheduleAt(environment.time + delay).

      Parameters

      • delay: number

        Number of ticks to wait before next activation

      Returns void

      agent.set('tick', (a) => {
      const energy = a.get('energy');
      a.set('energy', energy - 10);

      // Lower energy = longer wait until next action
      const delay = Math.ceil(100 / energy);
      a.scheduleIn(delay);
      });

      0.6.0

    • 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');

      Parameters

      • name: string | Data
      • Optionalvalue: any

      Returns void

      0.0.5