2

I am new at Answer Set Programming and trying to encode a problem into ASP. I think it is a simple question. Here is the code;

events(1..3).
sequence(A,B,C) :- events(A;B;C), A!=B, A!=C, B!=C.

As you see, there is a sequence which consists of events. In this case its length is 3. However I want it to be decided by the user. For example;

  • If the user wants it to be length 3, then

gringo asp.lp --const n=3 | clasp

code should be look like;

events(1..3).
sequence(A,B,C) :- events(A;B;C), A!=B, A!=C, B!=C.
  • If the user wants it to be length 4, then

gringo asp.lp --const n=4 | clasp

code should be look like;

events(1..4).
sequence(A,B,C,D) :- events(A;B;C;D), A!=B, A!=C, A!=D, B!=C, B!=D, C!=D.

How could I do that? Thanks,

false
  • 10,264
  • 13
  • 101
  • 209
genclik27
  • 323
  • 1
  • 7
  • 18
  • I don't think this is possible with gringo alone. Maybe you can try writing a lua script. However, I'm curious what is the problem you are trying to solve? Do you really need an atom for every permutation? – dspyz Oct 24 '13 at 19:21

1 Answers1

0

Indeed, using some nifty recursion, you can get lisp-style lists of every possible permutation.

Here's my program for it:

top(n).
num(1..T) :- top(T).

partial_pattern(nil, 0).
unused(N,nil) :- num(N).
partial_pattern(cons(N,P),K+1) :- num(N), partial_pattern(P,K), unused(N,P).
unused(M,cons(N,P)) :- partial_pattern(cons(N,P),_), N!=M,unused(M,P).

pattern(P) :- partial_pattern(P,T), top(T).

#show pattern/1.
dspyz
  • 5,280
  • 2
  • 25
  • 63