Search code examples
logicplanninganswer-set-programmingclingo

Compare cardinality of multiple sets and get specific value from member of greatest set


I am using clingo to solve flood-it problems. I use the predicate frontier([CELL], [COLOR], [TIMESTEP]) to keep track of all cells that are neighbors of the flood. The set of frontiers could look something like this:

frontier(c(1,3),2,3) frontier(c(2,1),2,3) frontier(c(2,2),3,3) frontier(c(2,3),3,3) frontier(c(3,1),3,3) frontier(c(3,2),3,3) frontier(c(4,1),3,3)

We can split this set in two subsets. One where each color value is 2 or 3 respectively. What I need is basically two things:

  1. Determine which subset is bigger, i.e. if there are more cells with color value 2 or 3 (BTW the number of colors is not fixed, thus a solution has to be generic)
  2. Get the color value of a member of the biggest set

How can I compare the cardinalities of n (n>=2) sets in predicate logic?

Thank you in advance!


Solution

  • I found an answer which is more domain (i.e. clingo) specific than general.

    What I initially do is count the number of cells that are of color C:

    frontier_subset_size(C,N) :- color(C), N = #count{ X : frontier(X,C) }.
    

    Then I filter the biggest set(s) using the #max aggregate:

    max_subset_color(C) :- frontier_subset_size(C,N), N = #max{ M : frontier_subset_size(_,M) }.
    

    This works as desired for this specific problem.

    Yet I would like to know how to do that in pure predicate logic.