I'm trying to implement a reduction from the skyscrapper latin-square problem to SAT.
The skyscrapper problem is the same of Latin-sqaure but in addition, the skyscraper puzzle asks the player to place buildings of varying & unique heights on a grid so as to satisfy clues given on the grid’s edges. These “edge clues” tell the player how many buildings are visible from the standpoint of a given clue’s placement along the board’s edge. (full explanation)
So in order to solve this problem using SAT solver, I tried a similar approach of solving a suduko using SAT (which I already did). So first off all I defined n^3 binary variables, X_i,j,k which indicates that in the i,j cell the value k is true (so in the i,j cell we have a skyscrapper with a height of k)
I added the following constarints in the form of a CNF:
- each cell contains only 1 true var
- each row contains exactly 1 of each value
- each column contains exacly 1 of each value
Now I'm having troubles figuring out which constraints should I add for the clues and the heights. First I thought about the option of adding every possible order for each clue given, for example If from the left I can see 3 skyscrappers ( n = 4) so the possibilities are: [[2 ,3 ,4, 1],[1,3,4,2]] but I think it would be O(n!) in total...
I implemnted Suduko solver using ILP so I read about solving this problem usin ILP (integer linear programming). I found this link which describes how to do that. But still I can't find the equivalent for the height constarint they added in the ILP situation that will fit in the SAT solver.
Thanks a lot!
There are probably many many approaches, but one that comes to mind can look like the following.
For each clue
C_i
, you introduce new helper variables:A clue than consists in posting the correct cardinality (different approaches; you chose how)
Now the question is how to constrain
NV
:NV_i_1 = true
is always validNV_i_j = true <-> there exists a HIGHER value EARLIER
It's growing fast, but i recommend the following approach (which should be no problem for those sizes i saw in your linked pages; although SAT is a less natural approach here than CP for example):
For
N=5
, constrained by your all-diff constraint, there areC(N, 2) = 10
symmetry-free unique pairs:You will now use these pairs in regards to:
Assuming, your decision-variables are that of some tensor of rank 3 (row position, column position, value in unary encoding) with dimensions (N, N, N):
NV_i_j = true <-> there exists a HIGHER value EARLIER
can be expressed as (we assume a clue active on some column here):Now there is only one thing left (and i won't show it):
NV
constraints:clue_n