In discrete space, one agent occupies one cell. You can move an agent from its cell to another specific cell: to the next cell in some direction, or to a random cell under condition that it is empty.
The columns and rows define cells of the discrete space. Sometimes you might want to get the current location of your agent before you proceed to moving it.
- int getR() — returns the row of the agent’s current cell.
- int getC() — returns the column of the agent’s current cell.
AnyLogic offers the following API for moving agents in discrete space.
When you move an agent to some specific cell, make sure this cell is not occupied; otherwise, you may receive an error.
Function | Description |
---|---|
void jumpToCell( int r, int c ) |
Puts the agent to the cell with the specified coordinates, raises a runtime error if the cell is occupied. r - the row of the cell c - the column of the cell |
void moveToNextCell( CellDirection dir ) |
Puts the agent into a neighboring cell in the specified direction. The direction parameter dir may take the following values depending on the type of neighborhood, Moore or Euclidean: |
The term “pseudo-randomly” in the methods described below means the following: for performance purposes, the algorithm of finding an empty cell may pick among the set of empty cells with not always equal probabilities. The less populated is the space, the more fair is the choice.
Function | Description |
---|---|
int[] findRandomEmptyCell() | Returns coordinates {row,column} of a pseudo-randomly located empty cell, or null if all cells are occupied. |
boolean jumpToRandomEmptyCell() | Puts the agent into a pseudo-randomly located empty cell, returns false if all cells are occupied. |
These functions allow you to move agents to the cells that are already occupied. The main condition still remains relevant: one agent occupies one cell. If you use regular movement functions to interchange two agents in their cells, you will need one more empty cell, and the process will take three steps: move the first agent to an empty cell, move the other agent to the cell that has become available, move the first agent to the second agent's cell. Using the functions described below, you can run that operation in one easy step.
Function | Description |
---|---|
void swapWithCell(int r, int c) | Swaps this agent with an agent at the cell with the given row and column. That cell may be empty; in this case it simply moves the agent there. |
void swapWithNextCell(CellDirection dir) |
Swaps the agent with an agent at the adjacent cell in a given direction. dir — the direction (NORTH, SOUTH, ..., NORTHEAST, ...). |
void swapWithAgent(Agent anotherAgent) |
Swaps the cell location of this agent with another agent. anotherAgent — agent to swap cell location with. It should be located in the same environment. |
-
How can we improve this article?
-