Skip to content

Commit

Permalink
Examples of ASP clause generator added.
Browse files Browse the repository at this point in the history
  • Loading branch information
vale1410 committed May 12, 2021
1 parent 67f2d68 commit cb94780
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
26 changes: 26 additions & 0 deletions examples/asp_clause_generation_examples/color.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
% GRAPH COLORING
%
% Intuitive readings for predicates:
%
% edge(X,Y) = There is a directed edge from X to Y
% node(X) = X is a node
% color(X,C) = Node X is colored with color C

#const n = 3.

%%% DOMAIN RULES %%%

node(X) :- edge(X,Y).
node(Y) :- edge(X,Y).

%%% CLAUSES DEFINING SOLUTIONS %%%

color(X,C) : C = 1..n :- node(X).

-color(X,C) | -color(Y,C) :- edge(X,Y), C = 1..n.

%%% PROJECT SOLUTIONS %%%

#hide.
#show color/2.
#show -color/2.
40 changes: 40 additions & 0 deletions examples/asp_clause_generation_examples/queens.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
% THE n-QUEENS PROBLEM
%
% Intuitive readings for predicates:
%
% coord(X) = X is a valid coordinate value
% dir(R,C) = Define coordinate differences for adjacent cells
% target(X,Y,R,C) = (X,Y) and (X+R,Y+C) are adjacent
% queen(X,Y) = There is a queen at position (X,Y)
% attack(X,Y,R,C) = Position (X,Y) is attacked from direction (-R,-C)

#const n = 4.

%%% DOMAIN RULES %%%

coord(1..n).

dir(0,-1). dir(-1,-1). dir(-1,0). dir(-1,1).

target(X,Y,R,C) :- coord(X;Y;X+R;Y+C), dir(R,C).

%%% CLAUSES DEFINING SOLUTIONS %%%

attack(X+R,Y+C,R,C) | -queen(X,Y) :- target(X,Y,R,C).
attack(X+R,Y+C,R,C) | -attack(X,Y,R,C) :- target(X,Y,R,C), target(X-R,Y-C,R,C).

-attack(X+R,Y+C,R,C) | queen(X,Y) |
attack(X,Y,R,C) : target(X-R,Y-C,R,C) :- target(X,Y,R,C).

-queen(X+R,Y+C) | -attack(X+R,Y+C,R,C) :- target(X,Y,R,C).

queen(X,1) | attack(X,1,0,-1) : target(X,2,0,-1) :- coord(X).
queen(1,Y) | attack(1,Y,-1,0) : target(2,Y,-1,0) :- coord(Y).

%%% PROJECT SOLUTIONS %%%

#hide.
#show queen/2.
#show -queen/2.
#show attack/4.
#show -attack/4.

0 comments on commit cb94780

Please sign in to comment.