# Project Euler 280

Project Euler 280 is an interesting problem involving probability and combinatorics:

There is a 5×5 grid. An ant starts in the center square of the grid and walks randomly. Each step, the ant moves to an adjacent square (but not off the grid).

In each of the five bottom-most squares, there is a seed. When the ant reaches such a square, he picks up the seed (if he isn’t already carrying one).

When the ant reaches a square in the top-most row while carrying a seed, he drops off the seed (if there isn’t already a seed there).

This ‘game’ ends when all five seeds are in the five squares of the top-most row.

The problem asks for the expected (average) number of turns it would take to get from the initial to the ending position. It requires six digits of precision.

### The Monto Carlo Approach

Perhaps the easiest way to tackle this problem is with a Monto Carlo simulation. This uses a computer to actually run the game many times, using random number generators.

This is my straight-forward Monto Carlo implementation in Java:

import java.util.*;

public class Main{
public static void main(String[] args){
Random r = new Random();
int stepsum = 0;
int tries = 0;
while(true){
stepsum += new Simulation(r).simulate();
tries ++;
System.out.println((double) stepsum / (double) tries);
}
}
}

class Simulation{

static final int[] init = {
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
1,1,1,1,1
};
State state;
Random rand;
int steps;

Simulation(Random rand){
state = new State(init, 12, false);
this.rand = rand;
steps = 0;
}

int simulate(){
while(!done())
step();
return steps;
}

boolean done(){
int[] b = state.board;
return b==1 && b==1 && b==1 && b==1 && b==1;
}

boolean step(){
steps ++;

int antX = state.ant % 5;
int antY = state.ant / 5;
int dir;

while(true){
// 0:N 1:S 2:E 3:W
dir = rand.nextInt(4);
if(antY==0 && dir==0) continue;
if(antY==4 && dir==1) continue;
if(antX==4 && dir==2) continue;
if(antX==0 && dir==3) continue;
break;
}

switch(dir){
case 0: antY--; break;
case 1: antY++; break;
case 2: antX++; break;
case 3: antX--; break;
}

int oldAnt = state.ant;
state.ant = 5*antY + antX;

if(state.carrying){
state.board[oldAnt]--;
state.board[state.ant]++;
}

if(antY == 0 && state.board[state.ant] == 1 && state.carrying){
// drop off
state.carrying = false;
}

if(antY == 4 && state.board[state.ant] == 1 && !state.carrying){
// pick up
state.carrying = true;
}

return true;
}
}

class State{
// 25 board.
int[] board;

int ant;
boolean carrying;

State(int[] board, int ant, boolean carrying){
this.board = board.clone();
this.ant = ant;
this.carrying = carrying;
}

State(State s){
this(s.board, s.ant, s.carrying);
}

public boolean equals(Object o){
State s = (State) o;
return Arrays.equals(s.board, board) && s.ant == ant;
}

public int hashCode(){
return Arrays.hashCode(board) + ant;
}
}


Running this program will produce something close to the final result. But this will not nearly be accurate enough to actually solve the problem: running this for a few hours would only produce two or three digits of precision, while six is required.

It’s not exactly impossible, as on the forums Rodinio claims to have run a similar Monto Carlo simulation on a computer cluster, giving enough precision to start making guesses for the last digits. To get so many digits on a Monto Carlo simulation requires an astronomical amount of tries.

Obviously this is not the most efficient approach.

### Introducing the Markov Chain

A more efficient solution can be implemented using Markov Chains.

A Markov Chain solves the problem of discrete random processes. What is a Markov Chain, and how is it used?

Let’s say we have a finite group of states, $S = \{ s_1, s_2, \cdots , s_n \}$. Make a matrix $M$, of size $n * n$.

On each step (or move) of the process, the probability of going from state $s_i$ to $s_j$ is $M_{ij}$.

The process starts on an initial state. From each state there is a certain probability of going to every other state.

Notice that in a Markov Chain, what happens next does not depend on what has happened before. The only data is the current state you’re on.

I’ll explain all this with a very simple example: On a 1×3 game board, Bob (the happy face) starts at square 1. Each step, he randomly moves to an adjacent square with equal probability. The game ends when he reaches square 3.

Of course the only time he has a ‘choice’ in which square to move to is when he’s on square 2 (half-half chance of moving to 1 and 3). If he’s already on square 1, he has a 100% chance of moving to square 2, and if he’s on square 3, well, the game is over.

We can represent this game like this: Another way of showing the above would be something like this: This is the matrix, of course: $\left[ \begin{array}{ccc} 0 & 1 & 0 \\ \frac{1}{2} & 0 & \frac{1}{2} \\ 0 & 0 & 1 \end{array} \right]$

This type of matrix is called a Stochastic matrix. What’s special about this matrix is that each row of the matrix adds up to 1.

A state that can only return to itself is called an absorbing state. If in the matrix, $M_{ii} = 1$ for any value of $i$, then state $i$ is an absorbing state.

Here, state 3 is an absorbing state.

The next step is very important: determining the time until absorption.

From our matrix, take out all of the absorbing rows, and all of the left columns to make it a square matrix: It seems that by doing this we are losing information. But we’re actually not. We are treating each absorbing state as the same; and since each row originally added up to 1, the probability of going to the absorbing state is simply 1 minus the sum of the rest of the row.

This form is considered the canonical form of the stochastic matrix. Our matrix in canonical form looks like this: $\left[ \begin{array}{cc} 0 & 1 \\ \frac{1}{2} & 0 \end{array} \right]$

Next we subtract it from the identity matrix. $\left[ \begin{array}{cc} 1 & 0 \\ 0 & 1 \end{array} \right] - \left[ \begin{array}{cc} 0 & 1 \\ \frac{1}{2} & 0 \end{array} \right] = \left[ \begin{array}{cc} 1 & -1 \\ -\frac{1}{2} & 1 \end{array} \right]$

Next we invert this matrix and get: $\left[ \begin{array}{cc} 1 & -1 \\ -\frac{1}{2} & 1 \end{array} \right]^{-1} = \left[ \begin{array}{cc} 2 & 2 \\ 1 & 2 \end{array} \right]$

The expected absorption time from state $s_n$ is simply the sum of row $n$ in the above matrix.

So because we started on state 1, the expected length of the game is 2+2 or 4.

All of this seems like magic, because I haven’t given any proof for this. If you really want to prove that this method works, you can get a textbook about Markov chains.

“Markov Chains” by J.R.Norris (Cambridge 1997) is a decent textbook if you want to know more about this. I would recommend this as a less dense introduction to Markov chains.

### An attempt using Markov Chains

Now that we know some basics of Markov chains, we need to apply it to solve the problem.

If you attach a Set or something to the Monto Carlo simulation program, you would find that there are 10270 distinct states for this problem. If we count the five final states as the same state, then we have 10266 states.

This is exactly the same as what we’ve just done. Except instead of a 3*3 matrices, we’re now working with matrices with tens of thousands of rows and columns. Woo hoo.

You can probably see here that this is going to be a problem. A 10265*10265 (final state not counted) matrix contains over 100 million elements. Most of these elements will be zero. If we store all of this data in double types, that’s 800mb of memory used, just for the matrix.

It’s easy to underestimate the size of this problem. But there’s a few optimizations we can make.

We could save some time and space by subtracting from the identity matrix at the same time as filling up the matrix.

Inverting a 10265*10265 matrix is no small task either. Instead there’s a simple optimization to avoid inverting such a huge matrix:

Remember that we want to find the inverse of the matrix, and get the sum of all the elements in the first row. Let $M$ be our matrix and $M'$ be the inverse.

What’s important here is that finding the sum of all the rows of a matrix is the same as multiplying by a column of ones. This produces a one-columned matrix, each cell representing the sum of the corresponding row.

If $C$ is the column of ones, and $S$ is the sum matrix (what we want), then $M' * C = S$. We can also write that as $M * S = C$.

See, this way we don’t have to compute the inverse. We just have to solve the above equation for $S$.

To avoid implementing the standard matrix solve function, I’ll be using the JAMA library for matrix manipulation.

Finally, the source code for my implementation of what I just discussed:

import java.util.*;
import Jama.*;

public class Main{

// For a state, maps to all possible states from it.
// All next-states have the same probability.
static Map<State,List<State>> states = new LinkedHashMap<State,List<State>>();

public static void main(String[] args){

// Keep the time.
long startTime = System.currentTimeMillis();

// Initialize the state map.

// To construct the matrix, we need to map all the states to a position
// in the matrix (an integer). Because our map is already ordered, we
// use its position in the map as the position in the matrix. In order
// to avoid doing a linear search through the keyset to find its position,
// we cache its position in a map.

Set<State> keySet = states.keySet();
Map<State,Integer> positionMap = new HashMap<State,Integer>();

// Set up position map.
Iterator<State> iterator = keySet.iterator();
int pos = 0;
while(iterator.hasNext()){
positionMap.put(iterator.next(), pos);
pos++;
}

// Finally we can begin constructing our matrix.
// This takes up about 900mb of memory.
double[][] matrix = new double[states.size()][states.size()];

// Fill up the matrix.
Collection<List<State>> values = states.values();
Iterator<List<State>> rowIterator = values.iterator();

for(int row=0; row<matrix.length; row++){

// Fill up one row.
List<State> thisRow = rowIterator.next();

// What to fill with?
double fill = 1.0 / thisRow.size();

// An optimization: why not do the subtraction step here
// instead of subtracting from identity matrix?
matrix[row][row] = 1;

for(State st : thisRow){
// The final state doesn't count.
if(st.equals(State.FINAL_STATE)) continue;

int col = positionMap.get(st);
matrix[row][col] = -fill;
}
}

// Finishing up.
Matrix bigMatrix = new Matrix(matrix);
Matrix onesColumn = new Matrix(states.size(), 1, 1);
Matrix sums = bigMatrix.solve(onesColumn);

System.out.println(sums.get(0,0));
System.out.println(System.currentTimeMillis() - startTime + "ms");

}

// Returns a list of possible continuation states from the current one.
static List<State> nextStates(State state){

// Current and changing position of the ant
int antX = state.ant % 5;
int antY = state.ant / 5;

// Whether it can go into each of the four directions (N,S,E,W respectively).
boolean[] possibleDirs = new boolean;
Arrays.fill(possibleDirs, true);

// Take out some directions if it's in the corner.
if(antY == 0) possibleDirs = false; // Can't go north
if(antY == 4) possibleDirs = false; // Can't go south
if(antX == 4) possibleDirs = false; // Can't go east
if(antX == 0) possibleDirs = false; // Can't go west

// Construct a list of continuations.
List<State> nextStates = new ArrayList<State>();

// Loop through the four directions.
for(int i=0; i<4; i++){

// Cannot go this direction.
if( !(possibleDirs[i])) continue;

int newAntX = antX;
int newAntY = antY;

// Modify direction.
switch(i){
case 0: newAntY--; break;
case 1: newAntY++; break;
case 2: newAntX++; break;
case 3: newAntX--; break;
}

// Start constructing new state object.
int oldAnt = state.ant; // old ant position
int newAnt = 5*newAntY + newAntX;
int[] board = state.board.clone();
boolean carrying = state.carrying;

// Carrying a seed. Notice that a square can contain
// two seeds at once (but not more); seeds are indistinguishable
// so we just need to keep track of the number of seeds
// on each square.
if(carrying){
board[oldAnt] --;
board[newAnt] ++;
}

// Drop off the seed.
if(newAntY == 0 && board[newAnt] == 1 && carrying)
carrying = false;

// Pick up a new seed.
if(newAntY == 4 && board[newAnt] == 1 && !carrying)
carrying = true;

// Treat the five done positions the same.
if(donePosition(board))

}

return nextStates;
}

// Recursively add all continuation states.

if(states.containsKey(state)) return;

List<State> nexts = nextStates(state);
states.put(state, nexts);

// Recurse (but not if we've reached the final state).
for(State next : nexts)
if( !(next.equals(State.FINAL_STATE)))

}

// Is the board in the done position?
static boolean donePosition(int[] b){
return b==1 && b==1 && b==1 && b==1 && b==1;
}
}

class State{

static final State INIT_STATE = new State(
new int[]{
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
1,1,1,1,1
}, 12, false);

// Consider all final states the same; there is
// no ant position.
static final State FINAL_STATE = new State(
new int[]{
1,1,1,1,1,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0
}, -1, true);

// 25 board.
int[] board;

int ant;
boolean carrying;

State(int[] board, int ant, boolean carrying){
this.board = board;
this.ant = ant;
this.carrying = carrying;
}

State(State s){
this(s.board, s.ant, s.carrying);
}

public boolean equals(Object o){
State s = (State) o;
return Arrays.equals(s.board, board) && s.ant == ant && s.carrying == carrying;
}

public int hashCode(){
return Arrays.hashCode(board) + ant;
}

// For debugging mostly.
public String toString(){
StringBuilder ret = new StringBuilder("\n");
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
int pos = 5*i + j;
if(ant == pos) ret.append("#");
else ret.append(board[pos] >= 1? "+" : "-");
}
ret.append("\n");
}
return ret.toString();
}
}


Running it: So my program takes about 19 minutes to run, using up over 2GB of ram. The construction of the matrix only takes about 2 seconds, and the rest of the time is used on solving the huge matrix.

JAMA is probably not the fastest matrix library; the running time might be cut down a bit if we use libraries designed for sparse matrices.

But to get this to run in under a minute, a completely different approach would be needed. For now, though, I’m pretty happy with getting it in 19 minutes.