Advertisement
dvulakh

costas.clingo

Jan 25th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 1.33 KB | None | 0 0
  1. % Raphael Finkel 10/2008, 6/2018
  2.  
  3. fullRange(1..m) . % the ruler is a permutation of numbers 1 .. m
  4. subRange(1..m-1) . % differences in array index
  5. diff(1.. 2*m-1) .  % differences in array value, offset by m
  6. position(1..m) .
  7.  
  8. % #domain fullRange(Row) . % Row is index, Col is value
  9. % #domain fullRange(Row1) . % Row is index, Col is value
  10. % #domain fullRange(Col) . % Row is index, Col is value
  11. % #domain fullRange(Col1) . % Row is index, Col is value
  12. % #domain subRange(Delta) .
  13. % #domain diff(Diff) .
  14. % #domain position(Pos) .
  15.  
  16. % exactly one value in each index
  17. 1 {setting(Row, Col_):fullRange(Col_)} 1 :- fullRange(Row) .
  18.  
  19. % exactly one index for each value (no duplicates allowed)
  20. 1 {setting(Row_, Col):fullRange(Row_)} 1 :- fullRange(Col) .
  21.  
  22. % difference between values at Row and at Row+Delta is Diff
  23. delta(Delta, Row, Diff) :-
  24.     Row + Delta <= m , setting(Row, Col), Row+Delta == Row1,
  25.     setting(Row1, Col1),
  26.     Diff == m + Col1 - Col,
  27.     fullRange(Row;Row1), subRange(Delta) .
  28. :- delta(Delta, Row, Diff), Delta + Row > m . % uniqueness
  29. % not necessary, but seems to speed up clasp:
  30.     1 {delta(Delta, Row, Diff_):diff(Diff_)} 1 :-
  31.         Row + Delta <= m ,
  32.         subRange(Delta), fullRange(Row) .
  33.  
  34. % constraint on differences
  35. 0 {delta(Delta, Row_, Diff):fullRange(Row_)} 1 :-
  36.     Delta < m-1 ,
  37.     subRange(Delta), fullRange(Row), diff(Diff).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement