0

I'm going crazy with this problem that I have to solve with Numberjack, that is a library in python for CSP. We have n x m squares with colored sides. These squares must be arranged in a n x m grid in such a way that the squares' adjacent sides are of the same color. The square can be rotated and shifted. An example:

enter image description here

I thought about using 4 matrix (one for nord, one for sud, one for west and one for east side ) and a number for a color. Nord(i,j), West(i,j), East(i,j), Sud(i,j) describe the square i,j on the grid. Which constraints i have to consider?

Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39
  • What is a shift? Is that a swap with a neighbor? – Erwin Kalvelagen Apr 05 '18 at 17:04
  • Yes, exactly a swap with a neighbor @Erwin Kalvelagen – Lorenzo Pisaneschi Apr 05 '18 at 21:07
  • Precisely I have to develop a program where I can only swap the squares and another one where I can swap and rotate too. – Lorenzo Pisaneschi Apr 05 '18 at 21:20
  • Can you clarify: do you (a) have a heap of coloured tiles that you want to assemble into a colour-matched grid, or (b) already have the tiles arranged in a grid, and you want to compute a series of swap/rotates to transform this into a colour-matched grid? – jschimpf Apr 06 '18 at 08:08
  • Sure! I have an heap of coloured tiles and I want to assemble them into a grid, using numberjack. I have not to find an algorithm that solve this problem, this is the solver's task (Mistral 2 for example); my guess is to find a representation for the tiles, give the tiles as an input for my model function and find the correct constrains in such a way that my solver can solve the problem. – Lorenzo Pisaneschi Apr 06 '18 at 09:07

1 Answers1

0

As this sounds like homework, let me just outline one model that works:

Describe each given type of tile as a quintuple (Type,N,E,S,W), where Type is an identifier for this type of tile, and N,E,S,W are the colours on the north, east, south and west side. For each type of tile, have four such tuples for the different rotations.

Represent the grid with n x m similar tuples (Type,N,E,S,W), where Type ranges over the tile type identifiers, and N,E,S,W over the colours.

You then need three types of constraints, all of which should be easily representable in Numberjack (and other finite-domain style solvers):

  • every grid tuple matches one of the tile descriptor tuples
  • adjacent colours match
  • the grid contains the right number of tiles of each type
jschimpf
  • 4,904
  • 11
  • 24
  • How can I create the grid? Like a matrix ? In this case, it is not allowed in numberjack to create matrix with tuples :( – Lorenzo Pisaneschi Apr 06 '18 at 18:48
  • This is only conceptual. You can use five matrices of integer variables instead, four of which you had already proposed in your question. – jschimpf Apr 06 '18 at 23:43