# Using the Polygons package for dynamic and repeated games¶

In this notebook, we introduce the Polygons class and show how it can be used to compute equilibrium sets of repeated games.

## Set up¶

Currently the package is not available through the Julia package distribution system. To download and use the package (and the convex hull routines one which it depends), remove the comment markers (#) and run the following in an interactive Julia session:

In [1]:
#Pkg.clone("https://github.com/squipbar/Polygons.jl")
#Pkg.clone("https://github.com/cc7768/CHull2d.jl")


Note that you do not need to do this every time. Once you have it, re-downloading will yield errors.

## Defining polygon objects¶

The Polygon class stores a polygon object is two ways. First, as a collection of vertices, pts. This is not a terribly helpful representation, mathematically speaking, and is useful mostly for visualizing the object. The second representation is the normal-distance representation of the sides of the polygon. The is a mathematially useful object. If $G$ is an $n\times2$ matrix of vector normals $g_i$ of the polygon faces, and $m$ is a length-$n$ vector of distances, then the point $z\in\mathbb R^2$ is inside the polygon if:

$$g_i z \le m_i \ \forall \ i = 1, \ldots, n$$

Ths representation is stored inside the Polygon class via the terms dirs (for $G$) and dists (for $m$). Except where explicitly noted below, polygons are assumed to be convex.

Let's define our first polygons:

In [2]:
using Polygons

In [3]:
Z = [ 1 -1; 1 1; -1 1; -1 -1 ]
G = [ 1 0; 0 1; -1 0; 0 -1 ]
m = [ 1, 1, 1, 1 ]
a = Polygon( Z, G, m ) ;
b = Polygon( pts=Z ) ;
c = Polygon( dirs=G, dists=m ) ;


Because the class constructors standardize the polygon representation (points and normals are ordered anti-clockwise starting at lowest point, normals are unit-length), then we can throw badly-formed representations at the constructors and still preserve equality.

In [4]:
badZ = Z[ [1, 3, 4, 2], : ]

Out[4]:
Polygons.Polygon(4x2 Array{Float64,2}:
1.0  -1.0
1.0   1.0
-1.0   1.0
-1.0  -1.0,4x2 Array{Float64,2}:
1.0   0.0
0.0   1.0
-1.0   0.0
0.0  -1.0,[1.0,1.0,1.0,1.0])

So d and a are the same thing (sadly the == operator doesn't work as well as hoped here). But we can eyeball the two to check that everything is ok:

In [5]:
a

Out[5]:
Polygons.Polygon(4x2 Array{Float64,2}:
1.0  -1.0
1.0   1.0
-1.0   1.0
-1.0  -1.0,4x2 Array{Float64,2}:
1.0   0.0
0.0   1.0
-1.0   0.0
0.0  -1.0,[1.0,1.0,1.0,1.0])

Plotting a polygon is easy. Just use the polyPlot function:

In [6]:
polyPlot(a)

Out[6]:

Look - it's a square. Awesome!

We can add a vector to a polygon as follows

In [7]:
f = a + [ 1, 1 ] ;


Scalar multiplication will expand the set along rays through the origin

In [8]:
g = 1.5 * a ;


Becuase polyPlot also accepts polygon arrays, we can easily compare multiple polygon objects in the same plot

In [9]:
polyPlot( [a, f, g] )

Out[9]:

## Approximate set sums¶

### Theory¶

The polygon package also allows us to compute approximate set sums of polygons. Denote by $Z_1$ and $Z_2$ the vertices of two polygons $P_1$ and $P_2$. If $H$ is a $m \times 2$ matrix of search directions, the extremal points of the two polygons in the search direction $h_i$ are given by the maximal projection of the vertices onto $h_i$:

$$m^1_i = max Z_1 h_i' \qquad\qquad\qquad m^2_i = max Z_2 h_i'$$

Let $m^j=(m_i^j)$ be the vector of these projections and $m = m^1 + m^2$. Then we can form a Judd-Yeltekin-Conklin outer aproximation to the set sum as the set with faces with normals given by the rows of $H$ and distances $m$:

\begin{aligned} P &= \{ v_1 + v_2 | v_1 \in P_1, \ v_2 \in P_2 \} \\ &\subseteq \{ z | h_i.z \le m_i \ \forall i \} \end{aligned}

We can also connect the vertices which maximize the projected distance vectors to form an inner approximation to the setsum. Let $\hat z_i^j$ be the vertex of polygon $j$ which achieves the maximum distance in the direction $h_i$. Then if $\hat P$ is the polygon defined by the vertices $\hat z_i = z_i^1 + z_i^2$:

$$\hat P \subseteq P$$

Note that we could also just take the convex hull of all the possible point sums of the vertices $Z_1$ and $Z_2$ and recover a poterntially larger inner approximation. But this is, in general, a fairly slow algorithm. Projecting onto thevectors in $H$ is faster, as it involves less sorting.

### Practice¶

So, let's try these methods:

In [10]:
h = Polygon( pts=[ 0 1 ; 1 0 ; -1 0 ; 0 -1 ] )
exactdirs = [ a.dirs ; h.dirs ]
# The union of the face directions.  Will give an exact result
exact = setSum( a, h, exactdirs )
# The exact set sum
apxdirs = [ .1 1; .2 .8 ; -1 1 ; -.7 -.3 ; .4 -.6; .6 -.4  ]
outer = setSum( a, h, apxdirs ) ;
inner = setSum( a, h, apxdirs, false ) ;
polyPlot( [ a, h, exact, inner, outer ])

Out[10]:

The polygons a and h are in red and blue in the centre of the diagram. The exact set sum is in black, and is achieved when the search directions are the same as the union of nomals of the two sets. The cyan and green polygons defin the inner and outer approximations to this. Here, they are quite inaccuracte as there are few search directions and the differ in general from the true normals. If we increase the number of search directions then we will get much better approximations.

In [11]:
N = 40
theta = 2 * pi * rand( N )
manydirs = zeros( N, 2 )
for i in 1:N
manydirs[i,:] = [ cos(theta[i]), sin(theta[i]) ]
end
manyouter = setSum( a, h, manydirs ) ;
manyinner = setSum( a, h, manydirs, false ) ;
polyPlot( [ exact, manyinner, manyouter ])

Out[11]:

In this case, the inner approximation is the exact soluttion, as it picks out all the vertices perfectly

Not discussed here, but useful functions to know about: setsum can apply to arrays, weightedSum can perform a weighted set sum, and deeDoop removes duplicate and near-duplicate points from a polygon.

## Convex hull¶

The polygon package also provides a routine to generate a convex hull using the Graham Scan method

In [26]:
using Gadfly
using Colors
N = 40
srand(42)
manypts = randn( N, 2 )
chull( manypts )
ch = Polygon( pts = chull( manypts ) )
chplot = ch.pts[ [ 1:end; 1], : ]
plot( layer( x=manypts[:,1], y=manypts[:,2], Geom.point ), layer( x=chplot[:,1], y=chplot[:,2],  Geom.path,
Theme(default_color=colorant"red") ) )

Out[26]:

## Cropping polygons¶

Cropping polygons is a fundamental operation when calculating incentive-compatilbility with a fixed deviating payoff (when the deviating payoff is not fixed, we need to use linear programming or other methods to find the deviating payoff). The crop function does this - chopping the polyon in either the x or y direction as required.

In [27]:
chr = crop( ch, 1, .5, true )
chdown = crop( ch, 2, .5, false )
polyPlot( [chr, chdown, ch])

Out[27]:

## Unions¶

The union function forms the convex union of two polygons (or an array of polygons).

In [28]:
polyPlot( [ exact, ch, union( [ exact, ch ] ) ] )

Out[28]: