问题
I'm totally new to Clingo (and logic programming) and I'm looking for the best way to implement the following basic constraints:
Q1. I have a predicate selected(T)
where T ranges from 1 to N=5; how can I specify that exist at least one T such that selected(T) ?
Q2. I have a binary predicate wrap(E,T)
where E, T range from 1 to M, N; how can I specify that for each E exist at least one T such that wrap(E,T) ?
Q3. How can I specify that if selected(a)
OR selected(b)
then selected(c)
must be false
I actually use two lines of code, but probably there is a better way:
:- selected(c), selected(a) .
:- selected(c), selected(b) .
Q4. How can I specify that if a certain condition C(A) is true then the two unary predicates p1(A) and p2(A) must have the same value?
I actually use two lines of code:
p1(A) :- C(A), p2(A)
:- C(A), not p2(A), p1(A)
Q5. What book do you suggest for a good introduction to Answer Set Programming?
回答1:
Q1:
%%%% Config
#const n = 5.
%%%% Code
% selected is not an empty predicate
1 { selected(1..n) }.
Q2:
%%%% Config
%% N
#const n = 5.
%% M
#const m = 4.
%%%% Helpers
%% Ts
t(1..n).
%% Es
e(1..m).
%%%% Code
% there is an E, then there must be at least one
% wrap(E,T) where T is from t
1 { wrap(E, T) : t(T) } :- e(E).
Q3:
:- selected(c), selected(a;b).
Q4: I actually thought
:- c(A), p1(A)!=p2(A).
would work, but apparently not. I think clearest way to write it would be
:- c(A), not p1(A), p2(A).
:- c(A), p1(A), not p2(A).
though I'm not happy with the use of 'not'. Maybe someone who has used ASP recently remembers a better solution. Alternatively you could extend p1 and p2 to binary predicates and write
:- c(A), p1(A,P1), p2(A,P2), P1!=P2.
Q5: Unfortunately in my opinion there is no good book for ASP. That said the absolutely best one I've read is "Answer Set Solving in Practice" by the usual suspects Gebser, Kaminski, Kaufmann, Schaub. When reading materials related to the potassco collection you'll need to adjust between gringo 3 and gringo 4 language differences, which the official manual does not explain well, and neither does any other resource I've seen.
EDIT: Now there is a new manual for the Potassco tools with up to date syntax for clasp 3 and gringo/clingo 4.5, find it here: http://sourceforge.net/projects/potassco/files/guide/2.0/
来源:https://stackoverflow.com/questions/28317924/newbie-questions-on-answer-set-programming