This is the seventeenth part of the ILP series. For your convenience you can find other parts in the table of contents in Part 1 – Boolean algebra

Hi all. Today we are going to solve another riddle, called Lonpos 505 Puzzle. It is very similar to the riddle from the previous part so you might want to read it first.

# Game

In the game we are given a set of 12 elements with different shapes:

We are also give a starting position of elements:

Our goal is to add missing elements in a way that the board is filled completely:

Let’s begin.

# Variables

Our board has 55 fields, we have 12 elements. For every field position and for every element we need variables representing placement of the element on the board, so we have the following binary variables:

Just like in the previous part, variable being equal to one means that the following field is occupied by the element.

# Shapes

Since every element has different shape, we will need a different set of constraints for different elements. For instance, element A has four pieces and can be placed in one out of eight positions (4 rotations on a plane and two rotations of the sides, see the pciture), whereas element K can be placed in exactly one way.

So we do the following:

For every position , for every element , and for every rotation of this element we need to choose variables representing placement of this element on the plane. For instance: let’s say that we want to place second rotation of element A in the position . We would need to consider the following variables:

Next, we calculate conjunction of these variables. If it is true it means that all these fields are occupied by element one. This means:

means element , rotation , position .

We calculate these variables for every rotation of every element placed on every possible position on the board.

# Exactly one rotation of an element

Since we have multiple rotations for almost every element, we need to make sure that at most one rotation for given position is selected:

What’s more, we need to select exactly one placement of an element, which means that we need to select exactly one rotation for particular element:

# Exactly one element in a position

If we would try to solve the problem right now, we would end up with exactly one rotation of all elements placed on the board, however, these elements would probably land all in the same place. We need to make sure that for every position there is exactly one piece of elements there:

# Starting position

If we want to solve the riddle for a given starting position, all we need to do is set subset of variables to one, as described in instruction.

# Summary

As we can see, the implementation looks a bit simpler than in previous part. However, this problem is much more difficult than previous one because we have many more variables and much more difficult constraints. During my tests I was able to solve the problem easily when 8 elements were already placed on the board, however, I didn’t get any solution in an hour for a problem with only 4 elements placed on the board as a starting position. You can verify for yourself that this problem is not as easy as it looks like.

Bonus chatter: Lonpos 500 Puzzle is very interesting game. It also has version in 3D space — try to implement it using ILP.