answer-set-programmingclingo

Find lowest element of predicates of the size 2 and ensure that there is only one lowest element?


I am working on a piece of encoding that finds the cell with the lowest coordinates in a grid. This cell will be used as a starting point for a path. Cells are defined with predicate cell(X,Y).

isLower(X,Y,F,G) :- cell(X,Y), cell(F,G), X<=F, Y<=G.   

first(X,Y) :- cell(X,Y), not black(X,Y), cell(F,G), not black(F,G), cell(A,B), not black(A,B), isLower(X,Y,F,G), not isLower(A,B,X,Y).

I want to have only one first cell. My problem is that ASP starts giving me a bunch of cells, because first(X,Y) is a predicate and ASP will try to produce as many as it can.

How do I tell the program that it is supposed to find only the lowest element (i.e. the cell with the smallest coordinates)? How can I force it to be only ONE cell?


Solution

  • It is still unclear what exactly you want to achieve, but in case that cell/2 are facts, i would recommend a rule like:

    first(X',Y') :- (X',Y') = #min {(X,Y) : cell(X,Y)}.