An Introduction To Answer Set Programming: Paul Vicol October 14, 2015 (Based On Slides by Torsten Schaub)
An Introduction To Answer Set Programming: Paul Vicol October 14, 2015 (Based On Slides by Torsten Schaub)
Paul Vicol
October 14, 2015
(Based on slides by Torsten Schaub)
1
outline
0. My Research
1. Declarative Problem Solving
2. ASP Syntax and Semantics
3. Modeling Problems in ASP
2
my research
3
multi-agent belief change
4
declarative problem solving
5
traditional imperative programming
6
traditional imperative programming
7
declarative problem solving
8
declarative problem solving
9
what is answer set programming?
10
asp systems
11
two paradigms
12
comparison to prolog
Is Prolog Declarative?
edge(1,2).
edge(2,3).
reachable(X,Y) :- edge(X,Y).
reachable(X,Y) :- edge(X,Z), reachable(Z,Y).
• A query:
?- reachable(1,3).
true.
13
comparison to prolog (contd.)
edge(1,2).
edge(2,3).
• Then we get:
?- reachable(1,3).
Fatal Error: local stack overflow.
Prolog ASP
Query Derivation Model Generation
Top-down Bottom-up
Fixed execution order No fixed execution order
Programming Language Modeling Language
15
asp syntax and semantics
16
logic programs
a0 ← a1 , . . . , am , ∼ am+1 , . . . , ∼ an
where each ai ∈ A.
• Rules are a way of expressing constraints on a set of atoms
• “∼” represents default negation
• An atom is assumed to be false until it is proven to be true
17
stable models / answer sets
φ = q ∧ (q ∧ ¬r → p)
• φ has three classical models: {p, q}, {q, r}, and {p, q, r}
• {p, q} represents the model where:
p 7→ 1, q 7→ 1, r 7→ 0
q←
p ← q, ∼ r
• This logic program has one stable model (a.k.a answer set): {p, q}
• A set X of atoms is a stable model of a logic program P if X is a
(classical) model of P and all atoms in X are justified by some rule
in P
18
modeling problems in asp
19
asp solving steps
20
asp solving steps
21
asp modeling language
• Facts
• a.
• person(bill).
• person(alice;bob;james).
• Is shorthand for person(alice). person(bob). person(james).
• num(1..10).
• Is shorthand for num(1). num(2). num(3). num(4). etc.
• Rules
• a :- b.
• reachable(X,Z) :- edge(X,Y), reachable(Y,Z).
22
grounding example
24
asp modeling constructs
• Choice Rules
• 1 { has_property(X,C) : property(C) } 1 :- item(X).
• Integrity Constraints
• :- in_clique(2), in_clique(3), not edge(2,3).
• “It cannot be the case that nodes 2 and 3 are in a clique, and there
is no edge betweeen 2 and 3.”
• Aggregates
• within_budget :- 10 #sum { Amount : paid(Amount) } 100.
• Optimization Statements
• #maximize { 1,X:in_clique(X),node(X) }.
25
n-queens problem
26
n-queens - defining the board
27
n-queens - placing queens
28
n-queens - placing queens
29
n-queens - restricting the number of queens
:- not n { queen(I,J) } n.
$ clingo queens.lp --const n=4
Solving...
Answer: 1
queen(1,1) queen(2,1) queen(3,1) queen(4,1)
30
n-queens - restricting the number of queens
31
n-queens - forbidding attacks
32
n-queens - full program
queens.lp
row(1..n).
col(1..n).
% Generate
n { queen(I,J) : row(I), col(J) } n.
% Test
:- queen(I,J1), queen(I,J2), J1 != J2.
:- queen(I1,J), queen(I2,J), I1 != I2.
:- queen(I,J), queen(II,JJ), (I,J) != (II,JJ), I+J == II+JJ.
:- queen(I,J), queen(II,JJ), (I,J) != (II,JJ), I-J == II-JJ.
#show queen/2.
33
n-queens - solution
34
elaboration tolerance
35
graph 3-colouring problem
36
graph 3-colouring - instance
node(1..6).
edge(1,2). edge(1,3). edge(1,5).
edge(2,3). edge(2,4). edge(2,6).
edge(3,4). edge(3,5). edge(4,5). edge(5,6).
col(red;green;blue).
37
for reference: asp modeling constructs
• Choice Rules
• 1 { has_property(X,C) : property(C) } 1 :- item(X).
• Integrity Constraints
• :- in_clique(2), in_clique(3), not edge(2,3).
• “It cannot be the case that nodes 2 and 3 are in a clique, and there
is no edge betweeen 2 and 3.”
• Aggregates
• within_budget :- 10 #sum { Amount : paid(Amount) } 100.
• Optimization Statements
• #maximize { 1,X:in_clique(X),node(X) }.
38
graph 3-colouring - encoding
39
graph 3-colouring - full program
col.lp
node(1..6).
edge(1,2). edge(1,3). edge(1,5).
edge(2,3). edge(2,4). edge(2,6).
edge(3,4). edge(3,5).
edge(4,5).
edge(5,6).
col(red;green;blue).
1 { node_col(X,C) : col(C) } 1 :- node(X).
:- edge(X,Y), node_col(X,C), node_col(Y,C).
#show node_col/2.
40
graph 3-colouring - running
$ clingo col.lp
Answer: 1
node_col(2,green) node_col(1,blue) node_col(3,red) \
node_col(5,green) node_col(4,blue) node_col(6,red)
SATISFIABLE
Models : 1+
Calls : 1
Time : 0.003s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time : 0.000s
41
graph 3-colouring - interpreting
42
graph 3-colouring - all answer sets
44
xkcd knapsack problem part 1
45
xkcd knapsack problem part 2
food(fruit;fries;salad;wings;mozz_sticks;sampler).
cost(fruit,215).
cost(fries,275).
cost(salad,335).
cost(wings,355).
cost(mozz_sticks,420).
cost(sampler,580).
46
xkcd knapsack problem part 3
food(fruit;fries;salad;wings;mozz_sticks;sampler).
48
efficiency
Is ASP Declarative?
49
efficiency (contd.)
50
performance of n-queens
1 { queen(I,1..n) } 1 :- I = 1..n.
1 { queen(1..n,J) } 1 :- J = 1..n.
:- 2 { queen(D-J,J) }, D = 2..2*n.
:- 2 { queen(D+J,J) }, D = 1-n..n-1.
51