Skip to main content

Model Class

import { Model } from "https://vimtaai.github.io/agent/lib/index.js";

The Model class represents a multi-agent model, or a simulation. A Model contains Field objects in a grid and Agent objects can be added to them. Updating the properties of Field and Agent object within a model with a Timer object can be used to create a simulation.

Constructor

new Model(properties: object)

Creates a new Model, sets its initial properties from the given object, and inserts it into the document. You can specify the initial width, height, and scale properties of the model in the properties parameter, as well as the parentElement for the Model that defaults to document.body.

Default for properties: { width: 100, height: 100, scale: 5, parentElement: document.body }

Example

new Model();
new Model({ width: 100, height: 100, scale: 5 });
important

When creating a new Model, it will be automatically inserted into the end of the document. If you want to insert your Model to another element, pass the reference to the desired parent element in parentElement field of the properties argument.

Properties

model.width: number

The width of the Model in Field units.

Default: 100

model.height: number

The height of the Model in Field units.

Default: 100

model.scale: number

The zoom level of the Model, the size of a Field in pixels.

Default: 5

model.wrapHorizontal: boolean

Whether the Model wraps horizontally.

Default: false

model.wrapVertical: boolean

Whether the Model wraps vertically.

Default: false

model.fields: array read-only

The array of Field objects in the Model in row-major order.

model.agents: array read-only

The array of Agent objects of the Model in the order they were added.

model.centerX: number read-only

The X coordinate of the center of the Model.

model.centerY: number read-only

The Y coordinate of the center of the Model.

model.center: object read-only

The coordinates of the center of the Model in { x, y } format.

model.randomX: number read-only

A random X coordinate of the Model.

model.randomY: number read-only

A random Y coordinate of the Model.

model.randomFieldX: number read-only

The X coordinate of a random Field of the Model.

model.randomFieldY: number read-only

The Y coordinate of a random Field of the Model.

model.randomField: Field read-only

A random Field of the Model.

model.randomAgent: Agent read-only

A random Agent of the Model.

Methods

model.update()

Forces the Model to re-render.

model.addAgent(agent: Agent)

Adds an Agent to the Model.

model.removeAgent(agent: Agent)

Removes an Agent from the Model.

model.clearDrawing()

Clears all drawings from the Model.

model.clearAgents()

Removes all Agent objects from the Model.

Events

agentclick

Fires an AgentEvent when an Agent is clicked.

  • AgentEvent.target: the Agent object that was clicked.
  • AgentEvent.agent: the AgentEvent that was clicked.

fieldclick

Fires an FieldEvent when a Field is clicked.

  • FieldEvent.target: the Field object that was clicked.
  • FieldEvent.field: the FieldEvent that was clicked.