Grid

The functions here are used often when dealing with 2D grids (like in TicTacToe).

Heading

With the turn_heading, turn_left and turn_right functions an agent can turn around in a grid. In a 2D grid the orientations normally are:

  • North: (0,1)
  • South: (0,-1)
  • East: (1,0)
  • West: (-1,0)

In code:


In [2]:
orientations = [(1, 0), (0, 1), (-1, 0), (0, -1)]

We signify a left turn with a +1 and a right turn with a -1.

The functions turn_left and turn_right call turn_heading, which then turns the agent around according to the input.

First the code for turn_heading:


In [3]:
def turn_heading(heading, inc, headings=orientations):
    return headings[(headings.index(heading) + inc) % len(headings)]

We can now use the function to turn left:


In [4]:
print(turn_heading((0, 1), 1))


(-1, 0)

We were facing north and we turned left, so we are now facing west.

Let's now take a look at the other two functions, which automate this process:


In [5]:
def turn_right(heading):
    return turn_heading(heading, -1)

def turn_left(heading):
    return turn_heading(heading, +1)

The first one turns the agent right, so it passes -1 to turn_heading, while the second one turns the agent left, so it passes +1.

Let's see what happens when we are facing north and want to turn left and right:


In [6]:
print(turn_left((0, 1)))
print(turn_right((0, 1)))


(-1, 0)
(1, 0)

When we turn left from north we end up facing west, while on the other hand if we turn right we end up facing east.

Distance

The function returns the Euclidean Distance between two points in the 2D space.


In [6]:
import math

def distance(a, b):
    """The distance between two (x, y) points."""
    return math.hypot((a[0] - b[0]), (a[1] - b[1]))

For example:


In [7]:
print(distance((1, 2), (5, 5)))


5.0

Distance Squared

This function returns the square of the distance between two points.


In [8]:
def distance_squared(a, b):
    """The square of the distance between two (x, y) points."""
    return (a[0] - b[0])**2 + (a[1] - b[1])**2

For example:


In [10]:
print(distance_squared((1, 2), (5, 5)))


25

Vector Clip

With this function we can make sure the values of a vector are within a given range. It takes as arguments three vectors: the vector to clip (vector), a vector containing the lowest values allowed (lowest) and a vector for the highest values (highest). All these vectors are of the same length. If a value v1 in vector is lower than the corresponding value v2 in lowest, then we set v1 to v2. Similarly we "clip" the values exceeding the highest values.


In [5]:
from utils import clip

def vector_clip(vector, lowest, highest):
    """Return vector, except if any element is less than the corresponding
    value of lowest or more than the corresponding value of highest, clip to
    those values."""
    return type(vector)(map(clip, vector, lowest, highest))

For example:


In [6]:
print(vector_clip((-1, 10), (0, 0), (9, 9)))


(0, 9)

The vector we wanted to clip was the tuple (-1, 10). The lowest allowed values were (0, 0) and the highest (9, 9). So, the result is the tuple (0,9).