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
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
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:
The type parameter can take one of the above constants or Environment.LAYOUT_USER_DEFINED. Please note that if the number of agents exceeds the number of cells, a runtime error will occur.
You can also set initial agent location using this method:
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
The neighborhood model will affect the result of the following method:
Agent[] getNeighbors() - returns the array of agents in the cells that are counted as neighbors
and also the functions send() and deliver() if 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: