The 2D discrete space is a rectangular array of cells fully or partially occupied by the agents. There can be at most one agent in one cell.
AnyLogic support for this kind of space includes agent distribution over the cells, moving to a neighboring cell or jumping to arbitrary cell, finding out who are the agent’s neighbors (with respect to the neighborhood model), finding empty cells, and other useful services.Demo model: Schelling Segregation
To set the environment space type to discrete
- In the Projects view, click the environment agent type where your agents live (e.g. Main). This shows the agent types properties in the Properties view.
- Open the Space and network section of the properties.
- In the list of agents, select agent populations that you want to place in this environment.
- Choose Discrete option from the group of options Space type.
- Below, specify the space dimensions: number of Columns and Rows in the discrete space.
The Width and Height fields will then be used to visualize the space. For example, if the space has 100x100 cells and its width and height are 500x500, then each cell will have the size of 5x5 pixels when displayed.
You can use the following functions to get information about the space and cells dimensions:
- double spaceHeight() — Returns the height of environment in discrete space.
- double spaceWidth() — Returns the width of environment.
- double spaceCellHeight() — Returns the height of the cell.
- double spaceCellWidth() — Returns the width of the cell.
- int spaceColumns() — Returns the number of columns in the space.
- int spaceRows() — Returns the number of rows.
Location of an agent in the discrete space is defined by two integer coordinates (row,column).
The initial location can be defined specified at the agent type.
To define the initial layout of agents
- In the section Space and network of the upper level agent Properties choose the Layout type and, unless you have chosen the User-defined layout, select the Apply on startup check box.
- If you have selected the User-defined layout, then you can specify Row and Column fields in the Initial location section of the agent population Properties.
The User-defined layout type assumes you will take care of the agent locations yourself in e.g. On startup code of the Main agent. The standard discrete layouts supported by AnyLogic are:
You can change the layout type dynamically while the model is running using the environment API, for example:
environment.setLayoutType( type ); // this will just set the default type of layout, the agents will not get relocated environment.applyLayout(); // this will relocate the agents according to the layout type currently set.
The type parameter can take one of the above constants or Environment.LAYOUT_USER_DEFINED.
You can also set initial agent location using the following method:
void setCell(int r, int c) — Puts the agent into a given cell. It is only used to initialize the agent location. Agent is put into the given cell of discrete 2D environment (it is assumed the cell is empty, and the agent has not yet been assigned a cell).
r — the row of the cell
c — the column of the cell.
The agent’s presentation will be animated at the position of its current cell relative to its origin. This means that if in design time the agent’s presentation was placed at (50,100) on the container (e.g. Main diagram) and if the current cell of the agent in row 10 and column 20 and the cell dimension is 5 x 5 pixels, the agent will be shown at ( 50 10*5, 100 20 *5), i.e. at (100, 200). Therefore if you have designed some graphical background for your space, it makes sense to put the embedded agent presentation at the top left corner of it.
You can use one of two alternative neighborhood models: Euclidean or Moore. Neighborhood model determines the way the agent neighbors are defined. In Moore model the neighbors are counted in all 8 cells. In Euclidean model the neighbors are counted in 4 cells to the north, south, east, and west only.
An agent can find out who is in the cells nearby or who lives in a particular cell:
- Agent getAgentAtCell( int r, int c ) — returns the agent that occupies a given cell, or null if the cell is empty
- Agent getAgentNextToMe( CellDirection dir ) — returns the agent in a neighboring cell in the specified direction (one of the following: NORTH, SOUTH, EAST, WEST, NORTHEAST, NORTHWEST, SOUTHEAST, SOUTHWEST).
To set the neighborhood model
- In the section Space and network of the upper level agent Properties choose the appropriate Neighborhood type.
The neighborhood model will affect the result of the Agent getNeighbors() method, which returns the array of agents in the cells that are counted as neighbors — as well as the functions send() and deliver(), provided they are called with ALL_NEIGHBORS or RANDOM_NEIGHBOR parameter (see Agent communication).
If you need additional information to be associated with each cell (such as altitude, vegetation, etc.), you may define a two-dimensional array (line double altitude) in the environment agent and let the agents read and write to it as they are simulated.
Should you need a different space functionality for your model, you can build it either on top of this standard discrete space or you can implement your own space. Example:
- If you wish to have a torus space instead of a rectangular one, you can override the corresponding functions for moving and getting neighbors.
How can we improve this article?