# Addressing All Eight Directions in the Worksheet

This tutorial explains how to refer to agents in all eight positions relative to a specific agent.

## Overview

A common question from students is, “What about the other directions?” This usually occurs when introducing diffusion and hill climbing computational thinking patterns. The usual approach is to consider the directions up, down, right, and left, and to ignore the diagonal directions, both when calculating diffusion of agent attributes, as well as performing the hill climbing decision making. It is possible, however, to reference all eight directions, both in performing calculations and decision making.

## Movement Directions

The AgentSheets worksheet is based on a rectilinear, or Cartesian, coordinate system. For any cell, there are eight neighboring cells: up, down, left, right, up-left, up-right, down-left, and down-right. Many AgentSheets conditions and actions have a directional reference, as shown in the images below of a partial view of the Actions palette, and the result of clicking on the arrow of one of the actions:

For most conditions and actions, simply referencing the appropriate directional arrow is sufficient to address the appropriate worksheet cell. The exceptions are the “Is” attribute condition and the “Set” attribute action. The operands in both require specifying in language the desired direction. For example, if one refers to the “abc” attribute of the cell above, the reference would be stated: abc[up]

The analogous references for the opposite and orthogonal directions are [down], [left], and [right].

There are no word references for the diagonal directions, however. Instead, it is necessary to use two-dimensional matrix notation for the relative row and column being referenced. In order to understand this, it is necessary to know that the origin, or starting point of an AgentSheets worksheet, is the upper left corner. Thus, a row below a given cell’s row is a row number that is greater than the current cell’s row number, and a column to the right of a given cell’s column is a column number greater than that cell’s column number. Similarly, rows to the left and columns above the current cell position are lower numbers, respectively, than the current cell’s row and column numbers. Cells in the same row/column as a given cell have a relative position of zero – no offset – with respect to the given cell’s row/column number. Finally, the matrix notation for the relative position of an adjacent cell is specified by its relative row and column offset, in that order. Below is a diagram of the relative positions.

Below are some examples of relative references to agent attribute “abc” and the appropriate notation using the above diagram:

Refer to                                Notation
Cell above                            abc[up] or abc[-1,0]
Cell up and right                   abc[-1,1]
Cell down and left                 abc[1,-1]
Cell right                              abc[right] or abc[0,1]

## Example Project

A sample project that demonstrates relative references to agent attributes can be found at:

As noted in the description provided when running the applet, viewing the agent attribute window (using the pull-down menu to the right of the worksheet), will permit examining the agent attributes of the background cells as well as the pink “mover” cell, which always assumes the values of the agent attributes of the cell into which it is moving. The description below provides details of the behavior implementation.

The background cell agent uses the “When creating new agent” method to initialize its agent attributes called “row”, “column”, and “height”. Note the references to the cells above and to the left that create a sequential numbering down and to the right of the “row” and “column” attributes, respectively. The “height” attribute is a calculation that gives a unique value for each cell because of the different multipliers.

Next, examine the behavior of the “mover” agent, which is controlled by keyboard input. The next two images show the complete behavior. Notice the use of the matrix notation to refer to “height” attribute, which is unique to every cell. However, it is possible to use the word notations (up, down, etc.) to assign the “row” and “column” values, since the value of the “row” attribute is the same for all cells in the row next to the current cell’s row (above or below). Similarly, the value of the “column” attribute is the same for all cells in the column next to the current cell’s column (right or left). Thus, even though the movement may be up and right, it is still possible to assign the new value to “row” from the cell immediately up, for example. However, since the value of “height” is unique to each cell, one must use the matrix notation to assign that value.

To observe what happens, when running the simulation, open the Agent Attributes window from the Tools menu in AgentSheets or using the pull-down menu on the right side of the worksheet when running the browser applet. Click on a background (blue) cell next to the “mover” agent to observe its attributes. Then click on the “mover” agent and move the agent using one of the appropriate keys (see below) to see its attributes change to match the destination cells.

The keyboard keys for movement are:

Key                           Direction
Up Arrow                    Immediately up
Down Arrow                Immediately down
Right Arrow                Immediately right
Left Arrow                  Immediately left
“Q” key                      Up and to the left
“E” key                      Up and to the right
“A” key                      Down and to the left
“D” key                      Down and to the right

## Example Game Project Using Diffusion and Hill Climbing in all Eight Directions

A game project, “Circle The Cat”, references all eight relative positions in the implementation of diffusion and hill climbing. The example project can be found at:

There is a little more complication than usual in this game because it uses a recursive technique to force diffusion to happen faster than would normally occur after the player makes a move. This provides the “cat” agent with a better view of the game board status, making its move more intelligent. There is an explanation of the “loopcount” simulation property to control this in the text description on the web page when the applet is loaded. However, the purpose of mentioning the game here is to examine the use of the matrix notation for diffusion and hill climbing computations. Below is the background behavior showing the diffusion calculation:

The complete diffusion calculation is not visible in the window of the “Set” action:

.125*(out[-1,-1]+out[-1,1]+out[1,-1]+out[1,1]+out[up]+out[down]+out[left]+out[right])

In simpler diffusion calculations, the average of the four orthogonal cells’ attributes (up, down, right, and left) is typically used, and the average coefficient is 0.25, or 1/4. Here, because there are eight directions of the “out” agent attribute to be averaged, use of matrix notation is required, and the coefficient is 0.125, or 1/8. By averaging all of the eight surrounding cells, a more accurate value of the diffused attribute occurs, which, in this case, gives the “cat” agent the best possible choice of direction to pursue.

Similarly, the “cat” agent examines all eight possible directions to determine the best choice. Below is the decision behavior of the “cat” agent for four of the directions. There is analogous behavior for the other four directions. Notice the matrix notation when referring to the cells diagonally next to the “cat” agent.

## Final Note: Vertical References to Attributes

Unlike the arrow directions in conditions and actions, it is possible to refer to the attributes of
agents at the “top” or “bottom” of a stack, using those two word references. However, it is not possible to reference by relative position, such as one below or one above. Addressing agents in a vertical direction might be important in simulations in which agents stack. This might be the case in simulating an accumulation of material, for example, an avalanche simulation.