Options
All
  • Public
  • Public/Protected
  • All
Menu

Class GridEnvironment

A GridEnvironment is the deprecated version of a cellular automata. It's now recommended that you use a standard Environment with a Terrain. This class will be removed entirely in v0.6.0.

In a GridEnvironment with an ASCIIRenderer, Agents are rendered using their "value" data (a single character).

deprecated

since 0.4.0

since

0.0.10

Hierarchy

Index

Constructors

constructor

  • Create a GridEnvironment with the given width and height.

    Parameters

    • width: number = 2
    • height: number = 2

    Returns GridEnvironment

Properties

_cellHashes

_cellHashes: string[]

cells

cells: Map<string, Cell>

height

height: number

renderers

renderers: AbstractRenderer[] = ...

An array of the renderers associated with this Environment. An Environment can have multiple renderers, usually one to render the Agents spatially and others for data visualization, such as a LineChartRenderer, Histogram, etc.

time

time: number = 0

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
since

0.1.4

width

width: number

Methods

addAgent

  • addAgent(agent: Agent, rebalance?: boolean): void
  • Add an Agent to this Environment. Once this is called, the Agent's environment property will automatically be set to this Environment.

    since

    0.0.5

    Parameters

    • agent: Agent
    • rebalance: boolean = true

      Whether to rebalance if there is a KDTree (defaults to true)

    Returns void

addAgentAt

  • addAgentAt(x_?: number, y_?: number, agent?: Agent): Agent
  • For GridEnvironments, addAgent takes x and y values and automatically adds a Agent to that cell coordinate.

    since

    0.1.0

    Parameters

    • x_: number = 0
    • y_: number = 0
    • agent: Agent = ...

    Returns Agent

    The agent that was added at the specified coordinate.

clear

  • clear(): void
  • Remove all agents from the environment.

    since

    0.1.3

    Returns void

decrement

  • decrement(name: string, n?: number): void
  • 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).

    since

    0.0.8

    Parameters

    • name: string
    • n: number = 1

    Returns void

fill

  • fill(): void
  • Fill every cell of the grid with an agent and set that agent's position to its x/y coordinate.

    since

    0.0.5

    Returns void

get

  • get(name: string): any
  • Retrieve an arbitrary piece of data associated by name. If the data has not been set, returns null.

    since

    0.0.5

    Parameters

    • name: string

    Returns any

getAgentAt

  • getAgentAt(x_: number, y_: number): Agent
  • Retrieve the agent at the specified cell coordinate.

    since

    0.1.0

    Parameters

    • x_: number
    • y_: number

    Returns Agent

getAgentById

  • getAgentById(id: string): Agent
  • Get an agent in the environment by its ID.

    since

    0.1.3

    Parameters

    • id: string

    Returns Agent

getAgents

  • Get an array of all the agents in the environment.

    since

    0.0.5

    Returns Agent[]

getCell

  • getCell(x_: number, y_: number): Cell
  • Retrieve the cell at the specified coordinate.

    since

    0.1.0

    Parameters

    • x_: number
    • y_: number

    Returns Cell

getCells

  • getCells(): Cell[]
  • Get all cells of the environment, in a flat array.

    since

    0.1.0

    Returns Cell[]

getData

  • getData(): Data
  • 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
    // }
    
    since

    0.1.0

    Returns Data

getRandomOpenCell

  • getRandomOpenCell(): Cell
  • Find a random open cell in the GridEnvironment.

    since

    0.0.7

    Returns Cell

    The coordinate of the open cell.

increment

  • increment(name: string, n?: number): void
  • 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).

    since

    0.0.8

    Parameters

    • name: string
    • n: number = 1

    Returns void

loop

  • loop(callback?: Function): void
  • 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).

    since

    0.0.5

    Parameters

    • callback: Function = ...

    Returns void

memo

  • memo(fn: Function, key?: string): any
  • Pass a function to cache and use the return value within the same environment tick.

    since

    0.3.14

    // Within the same time cycle, this function will only be called once.
    // The cached value will be used on subsequent calls.
    const blueAgents = environment.memo(() => {
      return environment.getAgents().filter(a => a.get('color') === 'blue');
    });
    

    Parameters

    • fn: Function

      The function to memoize.

    • Optional key: string

    Returns any

    The return value of the function that was passed.

neighbors

  • neighbors(agent: Agent, radius?: number, moore?: boolean): Agent[]

removeAgent

  • removeAgent(agent: Agent, rebalance?: boolean): void
  • Remove an agent from the environment.

    since

    0.0.8

    Parameters

    • agent: Agent
    • rebalance: boolean = true

    Returns void

removeAgentAt

  • removeAgentAt(x_?: number, y_?: number): void
  • For GridEnvironments, removeAgentAt takes x and y values and removes the Agent (if there is one) at that cell coordinate.

    since

    0.1.0

    Parameters

    • x_: number = 0
    • y_: number = 0

    Returns void

removeAgentById

  • removeAgentById(id: string): void
  • Remove an agent from the environment by its ID.

    since

    0.1.3

    Parameters

    • id: string

    Returns void

set

  • set(name: string | Data, value?: any): void
  • 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');

    since

    0.0.5

    Parameters

    • name: string | Data
    • Optional value: any

    Returns void

stat

  • stat(key: string, useCache?: boolean): any[]
  • 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).

    since

    0.3.14

    Parameters

    • key: string

      The key for which to retrieve data.

    • useCache: boolean = true

      Whether or not to cache the result.

    Returns any[]

    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']
    

swap

  • swap(x1_: number, y1_: number, x2_: number, y2_: number): void
  • 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.

    since

    0.0.7

    Parameters

    • x1_: number
    • y1_: number
    • x2_: number
    • y2_: number

    Returns void

tick

  • tick(opts?: number | TickOptions): void
  • Override/extend Environment.tick to include the GridEnvironment's cells.

    override

    Parameters

    • Optional opts: number | TickOptions

    Returns void

use

  • use(helper: EnvironmentHelper): void
  • Use a helper with this environment. A helper can be one of:

    since

    0.1.3

    Parameters

    • helper: EnvironmentHelper

    Returns void

Generated using TypeDoc