Skip to content

Commit

Permalink
Add Answer Set Programming examples
Browse files Browse the repository at this point in the history
Pulled from clingo (MIT) and from Draco (BSD 3 Clause)
  • Loading branch information
nickswalker committed Dec 30, 2024
1 parent 7346ece commit ac28ec3
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
43 changes: 43 additions & 0 deletions samples/Answer Set Programming/15puzzle-encoding.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <incmode>.

% if set to one, permits consecutive moves in one time step
% this will not provide optimal plans but usually finds solutions much faster
#const consecutive = 0.

#program base.

% neighborhood relation
d(1,0;0,1;-1,0;0,-1).
n((X,Y),(X+DX,Y+DY)) :- dim((X,Y)), dim((X+DX,Y+DY)), d(DX,DY).

% positions at time step zero
at(0,(X,Y),N) :- in(X,Y,N).

#program step(t).

% guess moves
1 { move(t,A,B) : n(A,B) } 1 :- dim(B), not at(t-1,B,_).
0 { move(t,A,B) : n(A,B) } 1 :- move(t,B,_), consecutive == 1.

% check moves
:- 2 { move(t,A,B) }, dim(A).

% state transition
at(t,A,N) :- at(t-1,A,N), not move(t,A,_).
at(t,B,N) :- at(t-1,A,N), move(t,A,B).

% some redundant constraints
:- move(t,A,_), not at(t-1,A,_).
:- move(t,A,B), move(t-1,B,A), consecutive != 1.

#program check(t).

% check domain knowledge
:- not 1 { not at(t,A,_) : dim(A) } 1.
:- 2 { at(t,A,N) }, dim(A).

% check goal
:- go(X,Y,N), not at(t,(X,Y),N), query(t).

#show move/3.
#show at/3.
34 changes: 34 additions & 0 deletions samples/Answer Set Programming/apt.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
% implementation of APT

#include "asp/define.lp".
#include "asp/generate.lp".
#include "asp/hard.lp".

:~ type(E,quantitative), channel(E,x), priority(E,P). [1@P,E]
:~ type(E,quantitative), channel(E,y), priority(E,P). [1@P,E]
:~ type(E,quantitative), channel(E,size), priority(E,P). [2@P,E]
:~ type(E,quantitative), channel(E,color), priority(E,P). [3@P,E]

:~ type(E,ordinal), channel(E,x), priority(E,P). [1@P,E]
:~ type(E,ordinal), channel(E,y), priority(E,P). [1@P,E]
:~ type(E,ordinal), channel(E,color), priority(E,P). [2@P,E]
:~ type(E,ordinal), channel(E,size), priority(E,P). [3@P,E]

:~ type(E,nominal), channel(E,x), priority(E,P). [1@P,E]
:~ type(E,nominal), channel(E,y), priority(E,P). [1@P,E]
:~ type(E,nominal), channel(E,color), priority(E,P). [2@P,E]
:~ type(E,nominal), channel(E,shape), priority(E,P). [3@P,E]
:~ type(E,nominal), channel(E,size), priority(E,P). [4@P,E]

:- channel(_,text).
:- channel(_,detail).
:- channel(_,row).
:- channel(_,column).

% don't bin, use log, or aggregate
:- bin(_).
:- log(_).
:- aggregate(_,_).

#show mark/1.
#show channel/2.
25 changes: 25 additions & 0 deletions samples/Answer Set Programming/onmodel-py.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#script (python)

import clingo

def main(prg):
def on_model(m):
print("shown")
print(" positive: " + ", ".join(map(str, m.symbols(shown=True))))
print(" negative: " + ", ".join(map(str, m.symbols(shown=True, complement=True))))
print("atoms")
print(" positive: " + ", ".join(map(str, m.symbols(atoms=True))))
print(" negative: " + ", ".join(map(str, m.symbols(atoms=True, complement=True))))
print("terms")
print(" positive: " + ", ".join(map(str, m.symbols(terms=True))))
print(" negative: " + ", ".join(map(str, m.symbols(terms=True, complement=True))))

prg.ground([("base", [])])
prg.solve(on_model=on_model)

#end.

{a}.
b :- a.
#show c : a.
#show b/0.

0 comments on commit ac28ec3

Please sign in to comment.