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

    • 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

    • 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