Body: <p>I am trying to understand how ASP (answer set programming) works. Hopefully I am asking this in the right place, otherwise I'm sorry. To be more specific, I am using Clingo, the ASP programming language of my university, but I hope somebody will still be able to help me out.
  I am trying to write a programm that finds the orthogonal neighbours of each of the cells in a 4x4 grid. This is what I have written so far:</p>
  <pre><code>row(1..4).
  column(1..4).
  cell(X,Y) :- row(X), column(Y).
  
  {neighbor(X-1,Y); neighbor(X+1, Y); neighbor(X, Y-1); neighbor(X, Y+1)} = 4 :- cell(X,Y). 
  </code></pre>
  <p>This way, I get the following solution:</p>
  <pre><code>row(1) row(2) row(3) row(4) column(1) column(2) column(3) column(4) cell(1,1) cell(2,1) cell(3,1) cell(4,1) cell(1,2) cell(2,2) cell(3,2) cell(4,2) cell(1,3) cell(2,3) cell(3,3) cell(4,3) cell(1,4) cell(2,4) cell(3,4) cell(4,4) neighbor(1,2) neighbor(1,0) neighbor(2,1) neighbor(0,1) neighbor(2,2) neighbor(2,0) neighbor(1,1) neighbor(3,1) neighbor(3,2) neighbor(3,0) neighbor(4,1) neighbor(4,2) neighbor(4,0) neighbor(5,1) neighbor(1,3) neighbor(0,2) neighbor(2,3) neighbor(3,3) neighbor(4,3) neighbor(5,2) neighbor(1,4) neighbor(0,3) neighbor(2,4) neighbor(3,4) neighbor(4,4) neighbor(5,3) neighbor(1,5) neighbor(0,4) neighbor(2,5) neighbor(3,5) neighbor(4,5) neighbor(5,4)
  SATISFIABLE
  </code></pre>
  <p>This is not what I wanted because I don't want neighbours that are &quot;outside&quot; the 4x4 grid, like neighbour(0,3), for example. (Any neighbour that has 0 or 5 as coordinates, actually.)
  I tried writing the following line as constraint:</p>
  <pre><code>:- neighbour(0, Y).
  </code></pre>
  <p>However, this doesn't work and yields &quot;unsatisfiable&quot;.
  Could anybody explain why this doesn't work?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-11-26T11:38:50.320Z
Comments.Text: I think there's a typo in your post. neighbour(0, 3) would not be outside 4x4, right? Anyway, I think your question is still on-topic, but barely. Programming questions are generally off-topic here, but you're trying to understand the program, so it's more a conceptual question.
Comments.UserDisplayName: nbro
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-11-26T12:44:20.007Z
Comments.Text: It is outside the grid, because I used the values from 1 to 4 as coordinates. Where should I post the question to be on-topic? Programming stack-exchange?
Comments.UserDisplayName: LarenEmpty
Tags: <logic><computer-programming><answer-set-programming>
Title: Program in Answer Set Programming - constraints?
UserDisplayName: LarenEmpty
ThreadText: # Program in Answer Set Programming - constraints?
  
  (Asked by: username_0 on 2024-11-25)
  
  I am trying to understand how ASP (answer set programming) works. Hopefully I am asking this in the right place, otherwise I'm sorry. To be more specific, I am using Clingo, the ASP programming language of my university, but I hope somebody will still be able to help me out.
  I am trying to write a programm that finds the orthogonal neighbours of each of the cells in a 4x4 grid. This is what I have written so far:
  
  ```
  row(1..4).
  column(1..4).
  cell(X,Y) :- row(X), column(Y).
  
  {neighbor(X-1,Y); neighbor(X+1, Y); neighbor(X, Y-1); neighbor(X, Y+1)} = 4 :- cell(X,Y). 
  
  ```
  
  This way, I get the following solution:
  
  ```
  row(1) row(2) row(3) row(4) column(1) column(2) column(3) column(4) cell(1,1) cell(2,1) cell(3,1) cell(4,1) cell(1,2) cell(2,2) cell(3,2) cell(4,2) cell(1,3) cell(2,3) cell(3,3) cell(4,3) cell(1,4) cell(2,4) cell(3,4) cell(4,4) neighbor(1,2) neighbor(1,0) neighbor(2,1) neighbor(0,1) neighbor(2,2) neighbor(2,0) neighbor(1,1) neighbor(3,1) neighbor(3,2) neighbor(3,0) neighbor(4,1) neighbor(4,2) neighbor(4,0) neighbor(5,1) neighbor(1,3) neighbor(0,2) neighbor(2,3) neighbor(3,3) neighbor(4,3) neighbor(5,2) neighbor(1,4) neighbor(0,3) neighbor(2,4) neighbor(3,4) neighbor(4,4) neighbor(5,3) neighbor(1,5) neighbor(0,4) neighbor(2,5) neighbor(3,5) neighbor(4,5) neighbor(5,4)
  SATISFIABLE
  
  ```
  
  This is not what I wanted because I don't want neighbours that are "outside" the 4x4 grid, like neighbour(0,3), for example. (Any neighbour that has 0 or 5 as coordinates, actually.)
  I tried writing the following line as constraint:
  
  ```
  :- neighbour(0, Y).
  
  ```
  
  However, this doesn't work and yields "unsatisfiable".
  Could anybody explain why this doesn't work?
  
  **Tags:** logic, computer-programming, answer-set-programming
  
  > **Comments:**
  > **username_1**: I think there's a typo in your post. neighbour(0, 3) would not be outside 4x4, right? Anyway, I think your question is still on-topic, but barely. Programming questions are generally off-topic here, but you're trying to understand the program, so it's more a conceptual question.  
  > **username_0**: It is outside the grid, because I used the values from 1 to 4 as coordinates. Where should I post the question to be on-topic? Programming stack-exchange?  
  
  ---
  
  ## 0 answers
  
  