Movement in Discrete Space

In discrete space, one agent occupies one cell. You can move agents 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.

Moving and jumping

When you move an agent to some specific cell, make sure this cell is not occupied; otherwise, you may receive an error.

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.

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.

Swapping agents between cells

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. 

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.

Related topics

Discrete space

Agent movement overview

Agent movement speed

Agent animation rotation

Agent action on arrival