# ASP Tutorial - To Use Pp1-78
# ASP Tutorial - To Use Pp1-78
• Christian Anger
• Kathrin Konczak
• Thomas Linke
• Torsten Schaub
1
Resources
Course material
• http://www.cs.uni-potsdam.de/wv/lehre/
• http://www.cs.uni-potsdam.de/~torsten/asp/
Systems
• dlv http://www.dbai.tuwien.ac.at/proj/dlv/
• noMoRe http://www.cs.uni-potsdam.de/~linke/nomore/
• smodels http://www.tcs.hut.fi/Software/smodels/
• plp http://www.cs.uni-potsdam.de/~torsten/plp/
• nlp http://www.cs.uni-potsdam.de/~torsten/nlp/
2
Roadmap
• Modeling
• Applications: Configuration
• Applications: Agents
3
Problem solving versus Programming
Problem Solution
6
Modeling Interpretation
?
Representation - Output
Computation
4
Wanted
For instance,
• Planning,
• Diagnosis,
• Configuration,
• Combinatorics,
• ...
5
A solution
Basic Idea
• Encode problem (class+instance) as a set of rules
• Read off solutions from answer sets of the rules
6
Roots
• Related fields
– Logic Programming (of course)
– Nonmonotonic Reasoning
– Deductive Databases
– Constraint Programming
7
Motivation
Prolog (Programming in logic) is great, it’s almost declarative!
above(X,Y) :- on(X,Y).
above(X,Y) :- on(X,Z),above(Z,Y).
and compare it to
above(X,Y) :- above(Z,Y),on(X,Z).
above(X,Y) :- on(X,Y).
8
Motivation (ctd)
Prolog offers negation as failure via operator not.
For instance,
info(a).
ask(X) :- not info(X).
cannot be captured by
9
Motivation (ctd)
• p :- p yields p ↔ p
• p :- not p yields p ↔ ¬p
10
Answer Set Programming
• Basic idea
• Syntax
• Semantics
• Examples
• Integrity constraints
• An algorithm
11
Answer Set Programming: Basic idea
✘ View rules as constraints on models.
For instance, given a rule
c ← a, not b
then each model X must satisfy
if a ∈ X and b 6∈ X , then c ∈ X .
➥ Models are closed under rules (informally!).
✘ Circumscribe models.
For instance, prefer model
∅ to {a, c} to {a, c, d} .
12
Problem solving in Answer Set Programming
Problem Solution(s)
6
Modeling Interpretation
?
Logic Program - Answer sets
Computation
13
Normal logic programs
• Notation
head (r) = A0
body(r) = {A1 , . . . , Am , not Am+1 , . . . , not An }
body + (r) = {A1 , . . . , Am }
body − (r) = {Am+1 , . . . , An }
14
Answer sets
Basic programs
• A set of atoms X is closed under a basic program Π iff
for any r ∈ Π, head (r) ∈ X whenever body + (r) ⊆ X .
• The smallest set of atoms which is closed under a basic
program Π is denoted by Cn(Π).
Normal programs
• The reduct, ΠX , of a program Π relative to a set X of atoms is
defined by
15
A closer look at ΠX
16
Some “logical” remarks
17
Another “logical” remark
18
A first example
Π = {p ← p, q ← not p}
X Π ΠX Cn(ΠX )
∅ p ← p p ← p {q}
q ← not p q ←
{p} p ← p p ← p ∅
q ← not p
{q} p ← p p ← p {q}
q ← not p q ←
{p, q} p ← p p ← p ∅
q ← not p
19
A second example
Π = {p ← not q, q ← not p}
X Π ΠX Cn(ΠX )
∅ p ← not q p ← {p, q}
q ← not p q ←
{p} p ← not q p ← {p}
q ← not p
{q} p ← not q {q}
q ← not p q ←
{p, q} p ← not q ∅
q ← not p
20
A third example
Π = {p ← not p}
X Π ΠX Cn(ΠX )
∅ p ← not p p ← {p}
{p} p ← not p ∅
21
A closer look at Cn
Inductive characterization
i
S
Theorem Cn(Π) = i≥0 Π ∅
T for any basic program Π.
22
Let’s iterate TΠ
Π = {p ←, q ←, r ← p, s ← q, t, t ← r, u ← v}
TΠ0 ∅ = ∅
TΠ1 ∅ = {p, q} = TΠ TΠ0 ∅ = TΠ (∅)
TΠ2 ∅ = {p, q, r} = TΠ TΠ1 ∅ = TΠ ({p, q})
TΠ3 ∅ = {p, q, r, t} = TΠ TΠ2 ∅ = TΠ ({p, q, r})
TΠ4 ∅ = {p, q, r, t, s} = TΠ TΠ3 ∅ = TΠ ({p, q, r, t})
TΠ5 ∅ = {p, q, r, t, s} = TΠ TΠ4 ∅ = TΠ ({p, q, r, t, s})
TΠ6 ∅ = {p, q, r, t, s} = TΠ TΠ5 ∅ = TΠ ({p, q, r, t, s})
where var (r) stands for the set of all variables occuring in r ;
θ is a (ground) substitution.
Ground Instantiation of Π
24
An example
UΠ = {a, b, c}
r(a, a), r(a, b), r(a, c), r(b, a), r(b, b), r(b, c), r(c, a), r(c, b), r(c, c),
BΠ =
t(a, a), t(a, b), t(a, c), t(b, b), t(b, b), t(b, c), t(c, b), t(c, b), t(c, c)
r(a, b) ← ,
r(b, c)
← ,
ground (Π) = t(a, a) ← r(a, a), t(b, a) ← r(b, a), t(c, a) ← r(c, a),
t(a, b) ← r(a, b), t(b, b) ← r(b, b), t(c, b) ← r(c, b),
t(a, c) ← r(a, c), t(b, c) ← r(b, c), t(c, c) ← r(c, c)
25
Answer sets of programs with Variables
26
Programs with Integrity Constraints
Purpose Integrity constraints eliminate unwanted candidate solutions
27
A first glance at algorithmics
Global parameters: Logic program Π and its set of atoms A:
answersetΠ (X, Y )
1. (X, Y ) ← propagationΠ (X, Y )
2. if (X ∩ Y ) 6= ∅ then fail
3. if (X ∪ Y ) = A then return(X)
4. Select A ∈ A \ (X ∪ Y )
5. answersetΠ (X ∪ {A}, Y )
6. answersetΠ (X, Y ∪ {A})
28
Modeling and Interpreting
Problem Solution(s)
6
Modeling Interpretation
?
Logic Program - Answer sets
Computation
29
Problem 7→ Logic Program
30
n-colorability of graphs
31
3–colorability of graphs
32
n-colorability of graphs
with n = 3
33
n–colorability of graphs
with n = 3
34
Let it run!
35
Here’s the rest!
Answer: 2
Stable Model: colored(3,g) othercolor(2,g) othercolor(1,g) othercolor(3,b)
othercolor(2,b) colored(1,b) othercolor(3,r) colored(2,r) othercolor(1,r)
color(g) color(b) color(r) edge(3,1) edge(2,3) edge(1,2) vertex(3) vertex(2)
vertex(1)
Answer: 3
Stable Model: othercolor(3,g) colored(2,g) othercolor(1,g) colored(3,b)
othercolor(2,b) othercolor(1,b) othercolor(3,r) othercolor(2,r) colored(1,r)
color(g) color(b) color(r) edge(3,1) edge(2,3) edge(1,2) vertex(3) vertex(2)
vertex(1)
Answer: 4
Stable Model: othercolor(3,g) othercolor(2,g) colored(1,g) colored(3,b)
othercolor(2,b) othercolor(1,b) othercolor(3,r) colored(2,r) othercolor(1,r)
color(g) color(b) color(r) edge(3,1) edge(2,3) edge(1,2) vertex(3) vertex(2)
vertex(1)
Answer: 5
Stable Model: othercolor(3,g) colored(2,g) othercolor(1,g) othercolor(3,b)
othercolor(2,b) colored(1,b) colored(3,r) othercolor(2,r) othercolor(1,r)
color(g) color(b) color(r) edge(3,1) edge(2,3) edge(1,2) vertex(3) vertex(2)
vertex(1)
Answer: 6
Stable Model: othercolor(3,g) othercolor(2,g) colored(1,g) othercolor(3,b)
colored(2,b) othercolor(1,b) colored(3,r) othercolor(2,r) othercolor(1,r)
color(g) color(b) color(r) edge(3,1) edge(2,3) edge(1,2) vertex(3) vertex(2)
vertex(1)
False
36
And finally some statistics!
Duration: 0.010
Number of choice points: 5
Number of wrong choices: 5
Number of atoms: 28
Number of rules: 45
Number of picked atoms: 42
Number of forced atoms: 0
Number of truth assignments: 347
Size of searchspace (removed): 9 (0)
37
Basic Methodology
38
Satisfiability
39
Satisfiability
40
n-Queens Problem
A solution to n = 4 :
Q
Q
Q
Q
41
n-Queens in answer set programming
q(X, Y ) gives the legal positions of the queensa
q(X, Y ) ← not¬q(X, Y )
¬q(X, Y ) ← not q(X, Y )
← q(X, Y ), q(X ′ , Y ), X 6= X ′
← q(X, Y ), q(X, Y ′ ), Y 6= Y ′
← q(X, Y ), q(X ′ , Y ′ ), |X − X ′ | = |Y − Y ′ |, X 6= X ′ , Y 6= Y ′
← not hasq(X)
hasq(X) ← q(X, Y )
d(1..queens).
43
n-Queens in answer set programming
(in disjunctive logic programming)
q(X, Y ) ∨ ¬q(X, Y ) ←
← q(X, Y ), q(X ′ , Y ), X 6= X ′
← q(X, Y ), q(X, Y ′ ), Y 6= Y ′
← q(X, Y ), q(X ′ , Y ′ ), |X − X ′ | = |Y − Y ′ |, X 6= X ′ , Y 6= Y ′
← not hasq(X)
hasq(X) ← q(X, Y )
44
n-Queens in answer set programming
(in nested logic programming)
← q(X, Y ), q(X ′ , Y ), X 6= X ′
← q(X, Y ), q(X, Y ′ ), Y 6= Y ′
← q(X, Y ), q(X ′ , Y ′ ), |X − X ′ | = |Y − Y ′ |, X 6= X ′ , Y 6= Y ′
← not hasq(X)
hasq(X) ← q(X, Y )
45
n-Queens (ctd)
(in the smodels language with cardinality constraints)
1 { q(X, Y ) } 1 ← d(X)
1 { q(X, Y ) } 1 ← d(Y )
← q(X, Y ), q(X ′ , Y ′ ), |X − X ′ | = |Y − Y ′ |, X 6= X ′ , Y 6= Y ′
d(1) ←
..
.
d(n) ←
46
n-Queens (ctd)
(in the smodels language with cardinality constraints)
d(1..queens).
47
And the Performance . . . ?
48
Planning
in the Blocksworld
3 6
1 3 5 2 5
2 4 6 1 4
49
Initial Situation
const grippers=2.
const lasttime=3.
block(1..6).
% DEFINE
on(1,2,0).
on(2,table,0).
on(3,4,0).
on(4,table,0).
on(5,6,0).
on(6,table,0).
50
Goal Situation
% TEST
:- not on(3,2,lasttime).
:- not on(2,1,lasttime).
:- not on(1,table,lasttime).
:- not on(6,5,lasttime).
:- not on(5,4,lasttime).
:- not on(4,table,lasttime).
51
Planning in the Blocksworld I
GENERATE
time(0..lasttime).
location(B) :- block(B).
location(table).
% GENERATE
{ move(B,L,T) : block(B) : location(L) } grippers :- time(T),
T<lasttime.
52
Planning in the Blocksworld II
DEFINE
% inertia
on(B,L,T+1) :- on(B,L,T), not neg_on(B,L,T+1),
location(L), block(B), time(T), T<lasttime.
% uniqueness of location
neg_on(B,L1,T) :- on(B,L,T), L!=L1,
block(B), location(L), location(L1), time(T).
53
Planning in the Blocksworld III
TEST
54
The Plan
55
Action description languages
zB. A (Gelfond & Lifschitz, 1990)
represents
% inertia
on(B,L,T+1) :- on(B,L,T), not neg_on(B,L,T+1),
location(L), block(B), time(T), T<lasttime.
56
Configuration
computer(desktop) ←
{ hard-drive(ide), hard-drive(scsi) } ← computer(x)
keyboard(us) ∨ keyboard(german) ← computer(x)
controller(scsi) ← hard-drive(scsi)
← monitor(21in) ∧ graphics(evil)
57
Disjunctive logic programs
• Syntax
• Semantics
• Examples
58
Disjunctive logic programs
• (Generalized) Notation
head (r) = {A1 , . . . , Am }
body(r) = {Am+1 , . . . , An , not An+1 , . . . , not Ao }
body + (r) = {Am+1 , . . . , An }
body − (r) = {An+1 , . . . , Ao }
59
Answer sets
Positive programs
• A set of atoms X is closed under a positive program Π iff
for any r ∈ Π, head (r) ∩ X 6= ∅ whenever body + (r) ⊆ X .
• The set of all ⊆-minimal sets of atoms being closed under a
positive program Π is denoted by min⊆ (Π).
Disjunctive programs
• The reduct, ΠX , of a disjunctive program Π relative to a set X
of atoms is defined by
60
A “positive” example
a ←
Π=
b ;c ← a
• The sets {a, b}, {a, c}, and {a, b, c} are closed under Π.
61
3-colorability revisited
62
An example with variables
a(1, 2) ←
Π =
b(X) ; c(Y ) ← a(X, Y ), not c(Y )
a(1, 2) ←
b(1) ; c(1)
← a(1, 1), not c(1)
ground (Π) = b(1) ; c(2) ← a(1, 2), not c(2)
b(2) ; c(1) ← a(2, 1), not c(1)
b(2) ; c(2) ← a(2, 2), not c(2)
63
An example with variables
a(1, 2) ←
b(1) ; c(1)
← a(1, 1), not c(1)
ground (Π)X = b(1) ; c(2) ← a(1, 2), not c(2)
b(2) ; c(1) ← a(2, 1), not c(1)
b(2) ; c(2) ← a(2, 2), not c(2)
• We get min⊆ (ground (Π)X ) = { {a(1, 2), b(1)}, {a(1, 2), c(2)} }.
64
An example with variables
a(1, 2) ←
b(1) ; c(1)
← a(1, 1), not c(1)
ground (Π)X = b(1) ; c(2) ← a(1, 2), not c(2)
b(2) ; c(1) ← a(2, 1), not c(1)
b(2) ; c(2) ← a(2, 2), not c(2)
65
Nested logic programs
• Syntax
• Semantics
• Examples
66
Nested logic programs
67
Satisfaction relation
• The satisfaction relation X |= F between a set of atoms and a
formula F is defined recursively as follows:
– X |= F if F ∈ X for an atom F
– X |= ⊤,
– X 6|= ⊥,
– X |= (F, G) if X |= F and X |= G,
– X |= (F ; G) if X |= F or X |= G,
– X |= not F if X 6|= F .
68
Reduct
• The reduct F X of a formula F relative to a set X of atoms is
defined recursively as follows:
– FX = F if F is an atom or ⊤ or ⊥,
– (F, G)X = (F X , GX ),
– (F ; G)X = (F X ; GX ),
⊥ if X |= F
– (not F )X =
⊤ otherwise
69
Two examples
• Π1 = {(p ; not p) ←}
– For X = ∅, we get Π∅1 = {(p ; ⊤) ←} and min⊆ (Π∅1 ) = {∅}.
{p} {p}
– For X = {p}, we get Π1 = {(p ; ⊥) ←} and min⊆ (Π1 ) = {{p}}.
• Π2 = {p ← not not p}
– For X = ∅, we get Π∅2 = {p ← ⊥} and min⊆ (Π∅2 ) = {∅}.
{p} {p}
– For X = {p}, we get Π2 = {p ← ⊤} and min⊆ (Π2 ) = {{p}}.
• In general,
– F ← G, not not H is equivalent to F ; not H ← G
– F ; not not G ← H is equivalent to F ← not G, H
– not not not F is equivalent to not F
➥ Intuitionistic Logic HT (Heyting, 1930)
70
Some more examples
71
Implementation
nlp http://www.cs.uni-potsdam.de/~torsten/nlp
72
Language extensions
• Choice rules
• Cardinality constraints
• Weight constraints
73
Choice rules
Syntax
Implementation smodels
74
Cardinality constraints
Implementation smodels
75
n-colorability revisited
with n = 3
76
Further extensions
Weight constraints
Syntax l {A1 : w1 , . . . , Am : wm } u
Informal meaning A weight constraint is satisfied in an answer set
X , if l ≤ ΣAi ∈X wi ≤ u.
Implementation smodels
Optimization
Syntax minimize {A1 : w1 , . . . , Am : wm }
Implementation smodels
77
Further extensions (cont’d)
78
Embedding in nested logic programs
• ← F as ⊥ ← F
For example,
{p, q}c ← (p ; not p), (q ; not q) ←
Π= stands for
← p, q ⊥ ← p, q
79
Embedding in nested logic programs (ctd)
• l {F1 , . . . , Fn } as
_
(Fi1 , . . . , Fil )
1≤i1 <...il ≤n
80
An example
c
{p, q, r} ←
Π= ← 2 {p, q, r}
← {p, q, r} 0
(p ; not p) ←
(q ; not
q) ←
stands for (r ; not r) ←
⊥ ← (p, q) ; (p, r) ; (q, r)
⊥ ← not (p ; q ; r),
The program
1 {p, q, r}c 1 ←
82
A closer look
1 {p, q, r} 2 ←
= (1 {p, q, r}), ({p, q, r} 2) ←
= (p ; q ; r), not (3 {p, q, r}) ←
= (p ; q ; r), not ( p, q, r ) ←
1 {p, q, r}c 2 ←
= 1 {p, q, r} 2, {p, q, r}c ←
= (p ; q ; r), not ( p, q, r ), ((p ; not p), (q ; not q), (r ; not r)) ←
83
A closer look (ctd)
(1 {p, q, r} 2 ←)X
= (1 {p, q, r} 2)X ←
= (p ; q ; r)X , (not ( p, q, r ))X ←
= (p ; q ; r), (not ( p, q, r ))X ←
84
A closer look (ctd)
Consider X = {p, q}
(1 {p, q, r} 2 ←){p,q}
= (p ; q ; r), (not ( p, q, r )){p,q} ←
= (p ; q ; r), ⊤ ←
= (p ; q ; r) ←
85
What about. . . ?
86
Usage in lparse and smodels
In lparse and smodels,
87
Algorithms & Systems: The Smodels approach
• Approximation
• Expansion
• Backtracking search
88
Approximating answer sets
89
Approximating answer sets (ctd)
Second Idea
Iterate
• Replace L by L ∪ Cn(ΠU )
• Replace U by U ∩ Cn(ΠL )
until L and U do not change anymore.
Observations
• At each iteration step
– L becomes larger (or equal)
– U becomes smaller (or equal)
• L ⊆ X ⊆ U is invariant for every answer set X of Π
• If L 6⊆ U , then Π has no answer set!
• If L = U , then L is an answer set of Π.
90
The basic expand algorithm
expand(L, U )
repeat
L′ ← L
U′ ← U
′
L ← L′ ∪ Cn(ΠU )
′ L′
U ← U ∩ Cn(Π )
if L 6⊆ U then return
until L = L′ and U = U ′
☞ Π is a global parameter!
91
Let’s expand!
a←
b ← a, not c
Π=
d ← b, not e
e ← not d
′ ′
L′ Cn(ΠU ) L U′ Cn(ΠL ) U
1 ∅ {a} {a} {a, b, c, d, e} {a, b, d, e} {a, b, d, e}
2 {a} {a, b} {a, b} {a, b, d, e} {a, b, d, e} {a, b, d, e}
3 {a, b} {a, b} {a, b} {a, b, d, e} {a, b, d, e} {a, b, d, e}
92
The basic expand algorithm (ctd)
expand
93
Let’s expand with d !
a←
b ← a, not c
Π=
d ← b, not e
e ← not d
′ ′
L′ Cn(ΠU ) L U′ Cn(ΠL ) U
1 {d} {a} {a, d} {a, b, c, d, e} {a, b, d} {a, b, d}
2 {a, d} {a, b, d} {a, b, d} {a, b, d} {a, b, d} {a, b, d}
3 {a, b, d} {a, b, d} {a, b, d} {a, b, d} {a, b, d} {a, b, d}
94
Let’s expand with “not d” !
a←
b ← a, not c
Π=
d ← b, not e
e ← not d
′ ′
L′ Cn(ΠU ) L U′ Cn(ΠL ) U
1 ∅ {a, e} {a, e} {a, b, c, e} {a, b, d, e} {a, b, e}
2 {a, e} {a, b, e} {a, b, e} {a, b, e} {a, b, e} {a, b, e}
3 {a, b, e} {a, b, e} {a, b, e} {a, b, e} {a, b, e} {a, b, e}
95
The basic smodels algorithm
smodels(L, U )
expand(L, U )
if L 6⊆ U then return
if L = U then exit with L
A ← select(U \ L)
smodels(L ∪ {A}, U )
smodels(L, U \ {A})
96
The basic smodels algorithm (ctd)
97
Outer system architecture
Two-phase implementation
Running smodels:
More info:
• http://www.tcs.hut.fi/Software/smodels/
98
What’s inside real smodels?
expand in smodels
• ...
smodels in smodels
• ...
99
Lookahead
• Given a program Π, an atom A, and sets of atoms L and U
– if expand(L ∪ {A}, U ) yields a conflict, then delete A from U
– if expand(L, U \ {A}) yields a conflict, then add A to L
100
Some remarks on lparse
101
Examples for domain predicates
• Facts
– vertex(1). vertex(2). vertex(3).
– edge(1,2). edge(2,3). edge(3,1).
– color(1). color(2). color(3).
• Non-recursive rules
– two-edge(X,Y) :- edge(X,Z), edge(Z,Y), not edge(X,Y).
• Recursive rules
– path(X,Y) :- edge(X,Y).
– path(X,Y) :- edge(X,Z), path(Z,Y), vertex(Z).
102
Examples for domain-restricted rules
103
Domain-restricted programs
• Rules defining domain predicates have a single answer set.
This answer set can be computed very efficiently through database
techniques.
• Domain-restricted programs are grounded in two-steps:
1. Evaluate domain predicates;
2. Generate for each rule with variables a set of ground instances
that are compatible with the evaluation of domain predicates.
• Also, certain built-ins are supported. For instance,
– d(0..n)
– odd(X+1) :- d(X), X<n, not odd(X).
where n is either explicitly provided in the program by means of
const n=n or supplied through the command line option -c n=n to
lparse.
104
Algorithms & Systems: The noMoRe approach
• Exemplary proceeding
• Formal devices
• Coloring sequences
105
Motivation
106
Examplary proceeding
r1 : penguin(tweety) ←
r2 : bird(tweety) ←
r3 : f lies(tweety) ← bird(tweety), not ¬f lies(tweety)
r4 : ¬f lies(tweety) ← penguin(tweety), not f lies(tweety).
Propagation r~
1 r~
2 Propagation
?
?
Propagation r~
4 - r~
3 Choice
Buildingset
Coloring
Answer
X1 = {penguin(tweety), bird(tweety), f lies(tweety)} extraction
the graph
107
Examplary proceeding
r1 : penguin(tweety) ←
r2 : bird(tweety) ←
r3 : f lies(tweety) ← bird(tweety), not ¬f lies(tweety)
r4 : ¬f lies(tweety) ← penguin(tweety), not f lies(tweety).
r~
1 r~
2
?
?
r~
4 - r~
3
X2 = {penguin(tweety), bird(tweety), ¬f lies(tweety)}
108
Formal devices in a nutshell
Graphs The rule dependency graph (Π, E0 , E1 ) of program Π is a
labeled directed graph with
E0 = {(r, r ′ ) | r, r ′ ∈ Π, head (r) ∈ body + (r ′ )} (0-edges);
E1 = {(r, r ′ ) | r, r ′ ∈ Π, head (r) ∈ body − (r ′ )} (1-edges);
109
Rule dependency graph
(Π, E0 , E1 )
110
An example
r1 : p ←
r2 : b ← p
r3 : f ← b, not f ′
r4 : f ′ ← p, not f
r5 : b ← m
r6 : x ← f, f ′ , not x
111
An example (ctd)
0
r1 - r2
• Consider Π = {r1 , . . . , r6 }, where
r1 : p ← 0 0
r2 : b ← p
?
1 ?
r3 : f ← b, not f ′ r4 - r3
r4 : f ′ ← p, not f 6
r5 : b ← m 0 0 0
r6 : x ← f, f ′ , not x
?
r6 r5
1
• The RDG of Π is graphically given as:
112
Coloring
• Define
113
Coloring (ctd)
C ⊑ C′ if C⊕ ⊆ C⊕ ′ and C⊖ ⊆ C⊖ ′ .
• Accordingly, define
C ⊔ C′ as (C⊕ ∪ C⊕ ′ , C⊖ ∪ C⊖ ′ ) .
114
An example
0
r1~ - r~
2
0 0
• If C is a coloring of ΓΠ ,
we call the pair (ΓΠ , C) a colored RDG . ?
?
1
r4 - r3
6
• For example, “coloring” the RDG
0 0 0
of the previous program Π with
?
C = ({r1 , r2 }, {r6 }) = (C⊕ , C⊖ )
r6~ r5
1
yields the following colored graph.
115
Colorings representing answer sets
where
116
Auxiliary concepts
117
Auxiliary concepts (ctd)
118
An example (ctd)
119
Relation to answer sets
120
Relation to answer sets (ctd)
Let Γ be the RDG of logic program Π and C be a partial coloring of Γ .
we have that
121
Propagation operator P
• Define
PΓ : C → C
as
122
Propagation operator P (ctd)
• Define
PΓ∗ : C → C
where PΓ∗ (C) is
– the ⊑-smallest partial coloring
– containing C and
– being closed under PΓ .
• Alternatively, we have
G
∗
PΓ (C) = P i (C)
i<ω
where
1. P 0 (C) = C and
2. P i+1 (C) = PΓ (P i (C)) for i < ω .
123
An example
r1 : p ← r3 : f ← b, not f ′ r5 : b ← m
r2 : b ← p r4 : f ′ ← p, not f r6 : x ← f, f ′ , not x
124
Propagation operator U
• Alternative definition:
Define UΓ : C → C as
125
Choice operators
126
All operators at a glance
• P and P ∗ :
– deterministic
– provide basic forward propagation
– P ∗ computes the closure under P
– P ∗ amounts to closure under Fitting’s operator (see below)
– P is reflexive, monotonic, and answer set preserving
• U and V :
– deterministic
– allow for detecting unsupported rules
– V is an incremental variant of U
– (PU)∗ amounts to well-founded semantics (see below)
– U is reflexive, idempotent, monotonic, and answer set preserving
127
All operators at a glance (ctd)
• N :
– deterministic
– colors all uncolored rules with ⊖
– Formally: NΓ (C) = (C⊕ , Π \ C⊕ ).
128
Operational answer set characterization I
1. C 0 = (∅, ∅);
3. C n = PΓ (C n );
4. C n = UΓ (C n );
5. C n = C .
129
Operational answer set characterization II
3. C n = C .
130
A coloring sequence according to II
r1 : p ← r3 : f ← b, not f ′ r5 : b ← m
r2 : b ← p r4 : f ′ ← p, not f r6 : x ← f, f ′ , not x
r1j r2j j
x j
x j
x j
x j
x j
x
0- 0- 0- 0-
0 0 0 0 0 0 0 0
? 1 ? ? 1 ? ? 1 ? ? 1 ?
j
r4 - j
r3 j
r4 - r3j j
r4 - j
x j
x - x
j
0 0 6
0 0 0 6
0 0 0 6
0 0 0 6
0
? ? ? ?
r6j r5j (PU)∗
r6j j
x ⊕
CΓ r6j j
x (PU)∗
j
x j
x
1 1 1 1
Γ Γ
7−→ 7−→ 7−→
131
Operational answer set characterization V
3. C n = NΓ (C n−1 );
4. C n = PΓ (C n );
5. C n = C .
133
The noMoRe system
• System architecture
• Extensions
• Future development
134
System architecture
grounder daVinci
135
Computing answer sets
136
The basic coloring strategy of noMoRe
☞ Recall the basic strategy of the noMoRe system:
Let Γ be the RDG of logic program Π and let C be a total coloring of Γ .
Then, C is an admissible coloring of Γ
iff
there exists a sequence (C i )0≤i≤n with the following properties:
3. C n = NΓ (C n−1 );
4. C n = PΓ (C n );
5. C n = C .
pre_color( Col, CP ) :-
facts(Fs),q_facts(QF),
color_all_1(Fs,Col,QF,CP1),
loops_1(L1),
color_all_0(L1,Col,CP1,CP).
138
Implementation: Main algorithm II
139
Implementation: Main algorithm III
☞ Missing predicates
• color_rest(Col) is true iff
all uncolored nodes can be colored ⊖ wrt Col and we obtain an
admissible coloring (see operator-NΓ ).
• color_all_1(Set,Col,InCP,OutCP) is true iff
all nodes in Set can be colored ⊕ wrt Col where OutCP is InCP plus
new possible choices (color_all_0\4 analog).
• facts\1, q_facts\1 and loop_1\1 give the respective rules.
Examples
– q_facts(h :- not b) is true (no positive body)
– loop_1(h :- a, not, b, not h) is true (self-blocking)
• heuristic_choose(Node,InCP,OutCP,Col) is true iff
Node is some heuristically chosen rule out of InCP;
OutCP contains remaining possible choices.
140
• col_1\1 and col_0\1 color a node ⊕ or ⊖, respectively, and
propagate the new coloring. If propagation fails col_1/0 fails!!
141
Implementation: Main algorithm IV
142
Implementation: Propagation I
• No need for global computation of S(Γ, C), S(Γ, C), B(Γ, C) and
B(Γ, C).
• Next possible choices are computed online during propagation.
• Propagation is performed recursively until no more nodes can be
colored by propagation.
143
Implementation: Propagation II
144
1 1
Propagation: prop(⊕ → u) 7→ (⊕ → ⊖)
Condition: no
1 r ⊖
⊕ r′
1 r ⊖
145
0 0
Propagation: prop(⊕ → u) 7→ (⊕ → ⊕)
1 r ′′ ⊖
⊕ ⊕
0
r ′ r 1
r ′′ ⊖
⊕ 1
0
r ′ r
146
1 1
Propagation: prop(⊖ → u) 7→ (⊖ → ⊕)
r ′′ ⊕
r ′′ ⊖
⊖ 0 1
1
r′ r ⊕ 1
1 r r ′′ ⊖
147
0 0
Propagation: prop(⊖ → u) 7→ (⊖ → ⊖)
Condition: r ∈ S(Γ, C)
r ′′ ⊖
⊖ ⊖ 0
0
r′ r 0
r ′′ ⊖
148
Implementation: Backward propagation I
150
1 1
Backward propagation: bprop (⊕ ← u) 7→ (⊕ ← ⊖)
Condition: no
1 r ⊖
⊕ r′
1 r ⊖
151
0 0
Backward propagation: bprop (⊕ ← u) 7→ (⊕ ← ⊕)
Condition: r ′ ∈ B(Γ, C)
r ′′ ⊖
1 0
0
⊕ r ′ r + 0
0
1 r ′′
r ′′ ⊖
152
1 1
Backward propagation: bprop (⊖ ← u) 7→ (⊖ ← ⊕)
Condition: r ′ ∈ S(Γ, C)
⊕
r ′′ ⊖
0 1
1
⊖ r ′ r +
1
r ′′ ⊖
153
0 0
Backward propagation: bprop (⊖ ← u) 7→ (⊖ ← ⊖)
Condition: r ′ ∈ B(Γ, C)
⊖ 1 0 r ⊖
⊖
r′
⊖ 1 0 r ⊖
154
Backward propagation: Jumping
rc re
155
Backward propagation: Experiments
noMoRe smodels
backprop no yes yes with (without)
jumping no no yes lookahead
ham k 7 14335 14335 2945 4814 (34077)
ham k 8 82200 82200 24240 688595 (86364)
ind cir 20 539 317 276 ∗ 276 (276)
ind cir 30 9266 5264 4609 ∗ 4609 (4609)
p1 step4 - 464 176 7 (69)
p2 step6 - 13654 3779 75 (3700)
col4x4 27680 27680 7811 7811 (102226)
col5x5 - - 580985 580985 (2.3 Mil)
queens4 84 84 5 1 (11)
queens5 326 326 13 9 (34)
156
Visualizing color sequences: An example
r1 : b ← p
r2 : f ← b, not f ′
r3 : f′ ← p, not f
r4 : p ←
r5 : w ← b
157
daVinci V2.1 daVinci V2.1 daVinci V2.1
2
2
1
1
1
3
3
3
5
4
4
4
5
5
daVinci V2.1 daVinci V2.1 daVinci V2.1
2
2
2
1
1
1
3
3
3
5
4
4
4
5
5
158
daVinci V2.1 daVinci V2.1 daVinci V2.1
2
2
2
1
1
1
3
3
3
5
4
4
4
5
5
1
1
1
3
3
3
5
4
4
4
5
5
Visualizing color sequences: An example
Extension: Transformations
0 0
v v v v
159
Transformations: An example
r1 : a ← not b. r2 : b ← not a.
r3 : c ← a. r4 : c ← b. r5 : d ← c.
1 ⊕/⊖ ⊖/⊕
⊕/⊖ r1 r2 ⊖/⊕
1
r1 , r3 r2 , r4
0 0
⊕/⊖ r3 r4 ⊖/⊕ 0 0
r5
0 0 ⊕/⊕
r5
⊕/⊕
☞ no normal program with same answer sets corresponds to the
transformed graph.
160
Extension: Syntax
h1 , . . . , hk ← B1 ; . . . ; Bn (1)
161
Extension: Transformations
162
noMoRe ++
• C++ implementation
163
Extensions and future development
• more transformations
164
Classical Negation ¬
• Motivation
• Semantics
• Translation
165
Motivation
• Example
166
Literals
167
Extended logic programs
• Notations
head (r) = L0
body(r) = {L1 , . . . , Lm , not Lm+1 , . . . , not Ln }
body + (r) = {L1 , . . . , Lm }
body − (r) = {Lm+1 , . . . , Ln }
168
Answer sets
• The smallest set of atoms which is logically closed and closed under
a basic program Π is denoted by Cn(Π).
169
Back to normal logic programs
170
Properties of norm
norm(Cn(Π)) = Cn(norm(Π) ∪ Π⊥ )
171
An example
′
p ← q, q
p ← not q p ← not q
p′ ← q, q ′
Π= q ← not p norm(Π) ∪ Π⊥ = q ← not p ∪
′
q ← p, p
p′
¬p
q ′ ← p, p′
172
Preferences
• Motivation
• Semantics
• Compilation
173
Motivation
• etc etc . . .
174
Legal reasoning
The challenge!
“A person wants to find out if her security interest in a certain ship is perfected. She
currently has possession of the ship. According to the Uniform Commercial Code
(UCC, §9-305) a security interest in goods may be perfected by taking possession of
the collateral. However, there is a federal law called the Ship Mortgage Act (SMA)
according to which a security interest in a ship may only be perfected by filing a
financing statement. Such a statement has not been filed. Now the question is
whether the UCC or the SMA takes precedence in this case. There are two known
legal principles for resolving conflicts of this kind. The principle of Lex Posterior gives
precedence to newer laws. In our case the UCC is newer than the SMA. On the other
hand, the principle of Lex Superior gives precedence to laws supported by the higher
authority. In our case the SMA has higher authority since it is federal law.”
(Gordon, 1993)
175
Legal reasoning
Our solution in “ordered logic programming”
176
What type of preference?
• Preferences on rules
(versus preferences on literals
or preferences on answer sets)
• Complexity within NP
(versus complexity beyond NP)
177
How to express preferences?
178
An example
Consider the following ordered logic program (Π, <) with Π = {r1 , r2 , r3 }
r1 : ¬a ← and r3 < r2 .
r2 : b ← ¬a, not c
r3 : c ← not b
179
How to define preferred answer sets?
for statically ordered logic programs
with
r1 = ¬a ←
r2 = b ← ¬a, not c
r3 = c ← not b
181
Implementation
for dynamically ordered logic programs
Idea
Translate a logic program Π with preference information
into a regular logic program T (Π) such that
answers to T (Π) respect the preferences in Π.
Plan
1. Extend the language for expressing preference
2. Add axioms encoding specific preference handling strategies
182
(Dynamically) ordered logic programs
An ordered logic program is an extended logic program over a
propositional language L,
containing the following pairwise disjoint categories:
• a set N of terms serving as names for rules;
• a set A of regular (propositional) atoms of a program; and
• a set A≺ of preference atoms s ≺ t, where s, t ∈ N are names.
183
Towards preferred answer sets
184
Translating ordered logic programs
• ap(nr ),
• bl(nr ),
• ok(nr ), and
• rdy(nr , nr′ ).
Then, the logic program T (Π) over L⋆ contains the following rules,
(shown on the next slide)
185
Translating ordered logic programs (ctd)
186
An(other) example
r1 = ¬a ←
r2 = b ← ¬a, not c
r3 = c ← not b
r4 = n3 ≺ n2 ← not d
187
Properties
Selection
If Y is an answer set of T (Π ∪ {(nr ≺ nr′ ) ←| r < r ′ }),
then Y ∩ L is an answer set of Π.
188
Implementation
plp http://www.cs.uni-potsdam.de/~torsten/plp
189
The example ran through plp
r1 = ¬a ←
r2 = b ← ¬a, not c
r3 = c ← not b
r4 = n3 ≺ n2 ← not d
becomes
neg a.
b :- name(n2), neg a, not c.
c :- name(n3), not b.
(n3 < n2) :- not d.
190
The outcome
neg_a.
b :- ap(n2).
ap(n2) :- ok(n2), neg_a, not c.
bl(n2) :- ok(n2), not neg_a.
bl(n2) :- ok(n2), c.
c :- ap(n3).
ap(n3) :- ok(n3), not b.
bl(n3) :- ok(n3), b.
prec(n3, n2) :- not d.
ok(N) :- name(N), rdy(N, n2), rdy(N, n3).
rdy(N, M) :- name(N), name(M), not prec(N, M).
rdy(N, M) :- name(N), name(M), prec(N, M), ap(M).
rdy(N, M) :- name(N), name(M), prec(N, M), bl(M).
neg_prec(M, N) :- name(N), name(M), prec(N, M).
prec(N, M) :- name(N), name(M), name(O),
prec(N, O), prec(O, M).
false :- a, neg_a. false :- b, neg_b. false :- c, neg_c. false :- d, neg_d.
false :- name(N), name(M), prec(N, M), neg_prec(N, M).
name(n3). name(n2).
191
Computing preferred answer sets
?- lp2dlv(’Examples/example’).
yes
?- dlv(’Examples/example’).
dlv [build BEN/Apr 5 2000 gcc 2.95.2 19991024 (release)]
yes
?- dlv(’Examples/example’,nice).
dlv [build BEN/Apr 5 2000 gcc 2.95.2 19991024 (release)]
{neg_a, b}
yes
?-
192
Sokoban - Initial Information
• initial_at(X,Y)
• square(X,Y)
• initial_box(X,Y)
• target_square(X,Y)
193
Sokoban - Beginning and End
time(1..n).
at(X, Y, 1) :-
initial_at(X, Y).
has_box(X, Y, 1) :-
initial_box(X, Y).
194
Sokoban - Has Box I
195
Sokoban - Has Box II
196
Sokoban - Has Box III
197
Sokoban - Move to
198
Sokoban - Pushing Boxes I
199
Sokoban - Pushing Boxes II
200
Sokoban - Moving Boxes
has_target_route(X, Y) :-
target_square(X, Y).
has_target_route(X, Y) :-
square(X, Y ; X+1, Y; X-1, Y),
has_target_route(X+1, Y).
usw.
201
Sokoban - Reachable
% The worker can reach all places that are not blocked.
reachable(X, Y, I) :-
square(X, Y),
time(I),
at(X, Y, I).
reachable(X+1, Y, I) :-
square(X, Y ; X+1, Y),
time(I),
reachable(X, Y, I),
not has_box(X+1, Y, I).
usw.
202
Sokoban - Has Neighbor
203
Sokoban - Improvements I
204
Sokoban - Improvements II
205
Sokoban - Improvements III
usw.
206
Sokoban - Improvements IV
% Don’t push two boxes adjacent each other along the edge.
:- edge_pair(X_1, Y_1, X_2, Y_2),
time(I),
has_box(X_1, Y_1, I),
has_box(X_2, Y_2, I).
edge_pair(X, Y, X+1, Y) :-
move_square(X, Y ; X+1, Y),
not target_square(X, Y),
not target_square(X+1, Y),
not square(X, Y-1),
not square(X+1, Y-1).
usw.
207
Sokoban - Improvements V
208
Sokoban - Improvements VI
209
Sokoban - Improvements VII
% Treat first two moves special because we know what moves are possible:
possible_box(X, Y, 1) :- initial_box(X, Y).
possible_box(X_2, Y_2, 2) :-
same_segment(X_1, Y_1, X_2, Y_2, Dir),
move_square(X_2, Y_2),
initial_box(X_1, Y_1).
possible_box(X, Y, 2) :-
initial_box(X, Y).
possible_box(X, Y, I) :-
time(I), I >= 3,
move_square(X, Y).
210
What is a configuration problem?
211
Classes of configurations
212
Debian GNU/Linux System
213
Database
214
Debian GNU/Linux System
Database
package(mail-reader1 )
package(mail-reader2 )
package(mail-extension)
package(mail-transport-agent)
depends(mail-reader1 , mail-transport-agent)
depends(mail-reader2 , mail-transport-agent)
depends(mail-extension, mail-reader1 )
conf licts(mail-reader1 , mail-reader2 )
recommends(mail-reader1 , mail-extension)
215
Inference Rules
216
Debian GNU/Linux System
Inference Rules
217
Suitable configurations
CM = {P | in(P ) ∈ M }
218
Debian GNU/Linux System
Logic program
package(mailreader1). depends(mailreader1,mailtransportagent).
package(mailreader2). depends(mailreader2,mailtransportagent).
package(mailextension). depends(mailextension,mailreader1).
package(mailtransportagent). recommends(mailreader1,mailextension).
conflicts(mailreader1,mailreader2).
userinclude(mailreader1).
219
Debian GNU/Linux System
Stable models
220
Debian GNU/Linux System
Logic program
package(mailreader1). depends(mailreader1,mailtransportagent).
package(mailreader2). depends(mailreader2,mailtransportagent).
package(mailextension). depends(mailextension,mailreader1).
package(mailtransportagent). recommends(mailreader1,mailextension).
conflicts(mailreader1,mailreader2).
userinclude(mailreader2).
221
Debian GNU/Linux System
Stable models
222
Debian GNU/Linux System
Logic program
package(mailreader1). depends(mailreader1,mailtransportagent).
package(mailreader2). depends(mailreader2,mailtransportagent).
package(mailextension). depends(mailextension,mailreader1).
package(mailtransportagent). recommends(mailreader1,mailextension).
conflicts(mailreader1,mailreader2).
userinclude(mailextension).
userinclude(mailreader2).
No stable models!
223
Diagnostic Model
224
Diagnostic Model
New atoms
needs-reason(P) mark the packages that are in some way part of the
problem and thus needs an explanation
225
Diagnosis
1. M is a stable model of DM ∪ U ;
PM = {P | needs-reason(P ) ∈ M }; and
226
Modifying configuration models
We want that the diagnosis contains only those packages that have to
be in there so that it will be as small as possible.
Thus, remove rule
227
Missing Packages
Error set
228
Conflicts
Error set
To handle the case where the user wants to leave out a package that
some other package needs, we replace the rule ← in(P ), user -exclude(P )
with
229
Justifications
needs-reason(P ) marks the packages we want to justify.
We add the rules
needs-reason(P ) ← missing(P )
needs-reason(P1 ) ← in-conf lict(P1 , P2 ), package(P1 )
needs-reason(P2 ) ← in-conf lict(P1 , P2 ), package(P2 )
needs-reason(P1 ) ← depends(P1 , P2 ), needs-reason(P2 ), in(P1 )
230
Debian GNU / Linux System
231
Stable Models
User requirements:
userinclude(mailextension). userinclude(mailreader2).
package(mailreader1). depends(mailreader1,mailtransportagent).
package(mailreader2). depends(mailreader2,mailtransportagent).
package(mailextension). depends(mailextension,mailreader1).
package(mailtransportagent). recommends(mailreader1,mailextension).
conflicts(mailreader1,mailreader2).
232
Stable Models
User requirements:
userinclude(mailextension). userinclude(mailreader2).
Stable Model: userinclude(mailreader2) userinclude(mailextension)
needs(mailextension,mailreader1)
in(mailreader1)
in(mailextension)
in(mailtransportagent)
in(mailreader2)
needsreason(mailreader1)
needsreason(mailreader2)
needsreason(mailextension)
userselected(mailreader2)
userselected(mailextension)
inconflict(mailreader1,mailreader2)
233
Debian GNU / Linux System
Diagnosis
Error set:
E = {in-conf lict(mail-reader1 , mail-reader2 )}
Problem set:
P = {mail-reader1 , mail-reader2 , mail-extension}
Explanation
set:
user -selected(mail-reader2 ),
R= user -selected(mail-extension), .
needs(mail-extension, mail-reader1 )
234
Stable Models
User requirements:
userinclude(mailreader2).
E=P =R=∅
235
Configuration of a PC
A computer is configured using the following configuration model:
236
Configuration Rule Language (CRL)
|: ”normal or”
⊕: exclusive or
237
Configuration model of a PC
computer ←
IDEdisk | SCSIdisk | f loppydrive ← computer
GermanlayoutKB ⊕ U KlayoutKB ← computer
P II ⊕ P III ← computer
AT X ⊕ I820 ← computer
SCSIcontroller ← SCSIdisk
← P II, I820
← P III, AT X
gcard ← not gcardinmb
gcardinmb ← AT X
238
Satisfaction
1. If a1 | . . . | al ← b1 , . . . , bm , not c1 , . . . not cn ,
{b1 , . . . , bm } ⊆ C and {c1 , . . . , cn } ∩ C = ∅,
then {a1 , . . . al } ∩ C 6= ∅.
2. If a1 ⊕ . . . ⊕ al ← b1 , . . . , bm , not c1 , . . . not cn ,
{b1 , . . . , bm } ⊆ C and {c1 , . . . , cn } ∩ C = ∅,
then for exactly one a ∈ {a1 , . . . al }, a ∈ C .
239
Computation of configurations
240
Configuration of a PC
computer ←
IDEdisk | SCSIdisk | f loppydrive ← computer
GermanlayoutKB ⊕ U KlayoutKB ← computer
P II ⊕ P III ← computer
AT X ⊕ I820 ← computer
SCSIcontroller ← SCSIdisk
← P II, I820
← P III, AT X
gcard ← not gcardinmb
gcardinmb ← AT X
GermanlayoutKB ←
241
Configuration of a PC
computer ←
IDEdisk | SCSIdisk | f loppydrive ← computer
GermanlayoutKB ⊕ U KlayoutKB ← computer
P II ⊕ P III ← computer
AT X ⊕ I820 ← computer
SCSIcontroller ← SCSIdisk
← P II, I820
← P III, AT X
gcard ← not gcardinmb
gcardinmb ← AT X
GermanlayoutKB ←
computer, IDEdisk, GermanlayoutKB, P III, AT X,
C2 =
SCSIcontroller, gcardinmb
Configuration C2 is not R-valid.
242
Configuration of a PC
computer ←
IDEdisk | SCSIdisk | f loppydrive ← computer
GermanlayoutKB ⊕ U KlayoutKB ← computer
P II ⊕ P III ← computer
AT X ⊕ I820 ← computer
SCSIcontroller ← SCSIdisk
← P II, I820
← P III, AT X
gcard ← not gcardinmb
gcardinmb ← AT X
GermanlayoutKB ←
computer, SCSIdisk, GermanlayboutKB, P II, AT X,
C3 =
SCSIcontroller, gcardinmb
243
Configuration of a PC
C3 |= R
Reduct RC3 :
computer ←
SCSIdisk ← computer
GermanlayoutKB ← computer
P II ← computer
AT X ← computer
SCSIcontroller ← SCSIdisk
← P II, I820
← P III, AT X
gcardinmb ← AT X
GermanlayoutKB ←
M M (RC3 ) = C3 .
Configuration C3 is R-valid.
244
Relationship to logic programming semantics
245
Configuration of a PC
translation of the rules
computer ←
stays unchanged.
246
Configuration of a PC
translation of the rules
f ′ ← not f ′ , f
247
Configuration of a PC
translation of the rules
248
Configuration of a PC
computer.
fp :- not fp, f.
f :- computer, nidedisk, nscsidisk, nfloppydrive.
idedisk :- not nidedisk, computer.
nidedisk :- not idedisk.
scsidisk :- not nscsidisk, computer.
nscsidisk :- not scsidisk.
floppydrive :- not nfloppydrive, computer.
nfloppydrive :- not floppydrive.
f:- computer, ngermanlayoutKB, nuklayoutKB.
germanlayoutKB :- not ngermanlayoutKB, computer.
ngermanlayoutKB :- not germanlayoutKB.
uklayoutKB :- not nuklayoutKB, computer.
nuklayoutKB :- not uklayoutKB.
f :- computer, germanlayoutKB, uklayoutKB.
f:- computer, npII, npIII. f :- computer, natx, ni820.
pII :- not npII, computer. atx :- not natx, computer.
npII :- not pII. natx :- not atx.
pIII :- not npIII, computer. i820 :- not ni820, computer.
npIII :- not pIII. ni820 :- not i820.
f :- computer, pII, pIII. f :- computer, atx, i820.
scsicontroller :- scsidisk.
:- pII, i820.
:- pIII, atx.
gcard :- not gcardinmb.
gcardinmb :- atx.
germanlayoutKB.
hide.
show computer,
idedisk,scsidisk,floppydrive,germanlayoutKB,uklayoutKB,pII,pIII,atx,i820,scsicontroller,gcard,gcardinmb.
249
Configuration of a PC
smodels version 2.27. Reading...done
Answer: 1
Stable Model: gcard i820 pIII germanlayoutKB idedisk computer
Answer: 2
Stable Model: gcard scsicontroller scsidisk i820 pIII germanlayoutKB idedisk computer
Answer: 3
Stable Model: gcard scsicontroller scsidisk i820 pIII germanlayoutKB floppydrive idedisk computer
Answer: 4
Stable Model: gcard i820 pIII germanlayoutKB floppydrive idedisk computer
Answer: 5
Stable Model: gcard i820 pIII germanlayoutKB floppydrive computer
Answer: 6
Stable Model: gcard scsicontroller scsidisk i820 pIII germanlayoutKB floppydrive computer
Answer: 7
Stable Model: gcard scsicontroller scsidisk i820 pIII germanlayoutKB computer
Answer: 8
Stable Model: gcardinmb atx scsicontroller scsidisk pII germanlayoutKB computer
Answer: 9
Stable Model: gcardinmb atx pII germanlayoutKB idedisk computer
Answer: 10
Stable Model: gcardinmb atx scsicontroller scsidisk pII germanlayoutKB idedisk computer
Answer: 11
Stable Model: gcardinmb atx scsicontroller scsidisk pII germanlayoutKB floppydrive idedisk computer
Answer: 12
Stable Model: gcardinmb atx scsicontroller scsidisk pII germanlayoutKB floppydrive computer
Answer: 13
Stable Model: gcardinmb atx pII germanlayoutKB floppydrive computer
Answer: 14
Stable Model: gcardinmb atx pII germanlayoutKB floppydrive idedisk computer
250
References
251
Action languages
• Transition Systems
• Action language A
• Action language C
252
Action signatures
253
Transition systems
1. a set S ,
3. a subset R of S × A × S .
254
Transitions
255
Transition graph
256
Example
Doors
F luents : {closed}
Actions : {opendoor}
s1
s1 = {closed}
States :
s2 = {¬closed}
a
?
s2
T ransitions : hs1 , a, s2 i a
hs2 , a, s2 i
257
Example
F luents : {p, q, r}
Actions : {a, b}
States : s1 = {¬p, ¬q, ¬r}
s2 a - s3
s2 = {p, q, ¬r}
6
s3 = {p, ¬q, r}
b b
s1
T ransitions : hs1 , a, s1 i a
hs1 , b, s2 i
hs1 , b, s3 i
hs2 , a, s3 i
258
Example
F luents : {p, q}
Actions : {a}
States : s1 = {p, q}
s2 = {¬p, q} ? a
? a
s1 s3
s3 = {p, ¬q}
s4 = {¬p, ¬q} 6 6
a a
s2 s4
T ransitions : hs1 , a, s1 i
hs2 , a, s1 i
hs3 , a, s3 i
hs4 , a, s3 i
259
Action language A
A causes L if F
260
Action language A
• V (P, s) = s(P ),
E(A, s) ⊆ s′ ⊆ E(A, s) ∪ s,
261
Example
F luents : {p, q} Actions : {a}
P ropositions : a causes p
States : s1 = {p, q}
s2 = {¬p, q} ? a
? a
s1 s3
s3 = {p, ¬q}
s4 = {¬p, ¬q} 6 6
a a
s2 s4
T ransitions : hs1 , a, s1 i
hs2 , a, s1 i
hs3 , a, s3 i
hs4 , a, s3 i
Effects of a executed in si are {p} for all i ∈ {0, . . . , 4}. E(A, s) ⊆ s′ ⊆ E(A, s) ∪ s holds for all transitions hs, a, s′ i.
262
Example
F luents : {p, q} Actions : {a}
P ropositions : a causes p if q
States : s1 = {p, q}
s2 = {¬p, q} ? a
? a
s1 s3
s3 = {p, ¬q}
s4 = {¬p, ¬q} 6
a
? a
s2 s4
T ransitions : hs1 , a, s1 i
hs2 , a, s1 i
hs3 , a, s3 i
hs4 , a, s4 i
E(a, s1 ) = E(a, s2 ) = {p}, E(a, s3 ) = E(a, s4 ) = ∅. E(A, s) ⊆ s′ ⊆ E(A, s) ∪ s holds for all transitions hs, a, s′ i.
263
Action language C
caused F if G
caused F if G after U
264
Action language C
caused F if G
caused F if G after U
265
Action language C
caused F if G
in D , s satisfies F if s satisfies G,
266
Example
Doors
267
Shortcuts
268
Translation to extended logic programs
Consider timesteps t = 0, . . . , T
¬B ← not B
B ← not ¬B.
269
Example
Doors
default Closed,
OpenDoor causes ¬Closed
stands for
270
Example
Doors
time(0..1).
271
Example
Doors
Answer: 1
Stable Model: closed(0) openDoor(0) -closed(1)
Answer: 2
Stable Model: -closed(0) openDoor(0) -closed(1)
Answer: 3
Stable Model: -closed(0) -openDoor(0) closed(1)
Answer: 4
Stable Model: closed(0) -openDoor(0) closed(1)
272
Example
Monkey& Bananas
Loc(x),
HasBananas, OnBox
273
Example
Monkey& Bananas
275
Example
Monkey& Bananas
276
Example
Monkey& Bananas
Initial situation:
• loc(bananas,l1,0), loc(monkey,l2,0), loc(box,l3,0)
Solution:
• walk(l3,0)
– walk(l3,0) causes loc(monkey,l3,1)
• pushBox(l1,1)
– pushBox(l1,1) causes loc(box,l1,2)
– pushBox(l1,1) causes loc(monkey,l1,2)
• climbOn(2)
– climbOn(2) causes onBox(3)
• graspBananas(3)
– graspBananas(3) causes hasBananas(4)
277
Monkey& Bananas with DLVK
http://www.dbai.tuwien.ac.at/proj/dlv/K/
278
monkey.dl
%% Background Knowledge
object(box).
object(monkey).
object(bananas).
279
monkey.plan
280
monkey.plan
always: caused loc(monkey,L) after walk(L).
caused -loc(monkey,L) after walk(L1), loc(monkey,L), L<>L1.
executable walk(L) if not onBox.
caused loc(monkey,L) after pushBox(L).
caused loc(box,L) after pushBox(L).
caused -loc(monkey,L) after pushBox(L1), loc(monkey,L), L<>L1.
caused -loc(box,L) after pushBox(L1), loc(box,L), L<>L1.
executable pushBox(L) if loc(monkey,L1), loc(box,L1), not onBox.
caused onBox after climbBox.
executable climbBox if not onBox, loc(monkey,L), loc(box,L).
caused hasBananas after graspBananas.
executable graspBananas if onBox, loc(monkey,L), loc(bananas,L).
inertial loc(O,L).
inertial onBox.
inertial hasBananas.
281
monkey.plan
initially: loc(monkey,2).
loc(box,3).
loc(bananas,1).
noConcurrency.
282
Solutions
bash-2.05b$ dlv -FP -N=4 monkeyK.plan monkeyK.dl
DLV [build BEN/May 23 2004 gcc 2.95.4 20011002 (Debian prerelease)]
283
Meta-interpreting Logic Programs
284
Representation
of a logic program
285
Example
bird & penguin
(1) peng.
(2) bird.
(3) ¬f lies ← not f lies, peng.
(4) f lies ← not ¬f lies, bird.
F (Π) : rule(r1 ). head(peng, r1 ).
rule(r2 ). head(bird, r2 ).
rule(r3 ). head(negf lies, r3 ).
pbl(peng, r3 ). nbl(f lies, r3 ).
rule(r4 ). head(f lies, r4 )
pbl(bird, r4 ). nbl(negf lies, r4 ).
and compl(f lies, negf lies).
286
Basic Meta-interpreter program
287
Basic Meta-interpreter program
The positive part of the body is true, if all its literals are in the answer
set.
If there are no positive literals, the positive body is trivially true.
288
Basic Meta-interpreter program
pbl inbetween(X, Y, R) ← pbl(X, R), pbl(Y, R), pbl(Z, R), X < Z, Z < Y.
pbl notlast(X, R) ← pbl(X, R), pbl(Y, R), X < Y.
pbl notf irst(X, R) ← pbl(X, R), pbl(Y, R), Y < X.
289
Basic Meta-interpreter program
The positive body is true up to some positive body literal (wrt the
built-in order):
pos body true upto(R, X) ← pbl(X, R), not pbl notf irst(X, R), in AS(X).
pos body true upto(R, X) ← pos body true upto(R, Y ), pbl(X, R),
in AS(X), Y < X, not pbl inbetween(Y, X, R).
pos body true(R) ← pos body true upto(R, X),
not pbl notlast(X, R).
290
Basic Meta-interpreter program
The negative part of a body is false, if one of its literals is in the answer
set.
291
Meta-interpreter
292
Example
bird & penguin
rule(r1). head(peng,r1).
rule(r2). head(bird,r2).
rule(r3). head(negflies,r3).
pbl(peng,r3). nbl(flies,r3).
rule(r4). head(flies,r4).
pbl(bird,r4). nbl(negflies,r4).
compl(flies,negflies).
293
Crossing a river
294
Abduction
• Π is a logic program,
295
Crossing a river
Explanations
canCross ← boat, not leaking
• Π=
canCross ← boat, leaking, hasBucket
• Observation: canCross
• Explanations:
1. {boat}
2. {boat, hasBucket}
3. {boat, leaking, hasBucket}
296
Running DLV
• canCross.dl
canCross :- boat, not leaking.
canCross :- boat, leaking, hasBucket.
• canCross.hyp
boat.
leaking.
hasBucket.
• canCross.obs
canCross.
297
Running DLV
Computing all explanations:
Diagnosis: boat
298
Further Semantics
• Completion
• Supported Models
• Fitting semantics
• Well-founded semantics
299
Completion
Let Π be a basic normal logic program.
Then, Π∗ is a set of rules obtained from Π as follows:
300
Example
Completion
a ← true
a ←
b ← a
b ← a
∗
c ← a
Π: c ← a Π :
c ← b
c ← b
d ← c, e
d ← c, e
e ← f alse
a ← true a ≡ true
b ← a b ≡ a
Π∗∗ : c ← (a ∨ b) Completion of Π : c ≡ (a ∨ b)
d ← (c ∧ e) d ≡ (c ∧ e)
e ← f alse e ≡ f alse
301
2-valued models
• T ∩ F = ∅,
302
Supported models
303
Example
Supported model
Completion of Π:
a ≡ true
b ≡ a
c ≡ (a ∨ b)
d ≡ (c ∧ e)
e ≡ f alse
304
Including negation as failure
Π:
q ← not p
p ← not x, not q
Completion of Π:
q ≡ ¬p
p ≡ (¬x ∧ ¬q)
x ≡ f alse
305
3-valued models
• T ∩ F = ∅, T ∩ ⊥ = ∅, F ∩ ⊥ = ∅,
306
Fitting Operator
ΦΠ hT, F i = hT ′ , F ′ i
(iii) A ∈ ⊥ otherwise.
307
Example
Π:
a ←
b ← a
c ← not a
d ← b, c
308
Properties of ΦΠ
309
Fitting semantics
Φ0Π = h∅, ∅i
Φi+1
Π = ΦΠ (ΦiΠ )
Φω
Π = ∪i<ω {ΦiΠ }
310
Example
Fitting
r ← s ←
t ← r, s u ← not t
Π=
p ← not p, not q a ← b
b ← a
Φ0Π = h∅, ∅i
Φ1Π = h{r, s}, {q}i
Φ2Π = h{r, s, t}, {q}i
Φ3Π = h{r, s, t}, {q, u}i
Φ4Π = Φ3Π
311
Example
Fitting
a ← b
Π=
b ← a
Φ0Π = h∅, ∅i
Φ1Π = h∅, ∅i
312
Unfounded sets
The greatest unfounded set (of Π wrt hT, F i) is the union of all
unfounded sets (of Π wrt hT, F i).
313
Example
Unfounded sets
a ← b
Π=
b ← a
314
Unfounded sets
UΠ hT, F i = hT ′ , F ′ i
(i) A ∈ T ′ if A ∈ T ,
(iii) A ∈ ⊥ otherwise.
315
Well-founded semantics
0
WΠ = h∅, ∅i
i+1 i i
WΠ = ΦΠ (WΠ ) ∪ UΠ (WΠ )
ω i
WΠ = ∪i<ω WΠ
ω
WΠ is the well-founded model of Π.
There always exists a well-founded model of Π.
If the well-founded model is total, then it is the unique stable model.
The well-founded model is a subset of every stable model.
316
Example
r ← s ← t ← r, s u ← not t
Π=
p ← not p, not q a ← b b ← a
317
Relation to other systems
noMoRe:
Let Γ be the RDG of normal logic program Π.
PΓ∗ ((∅, ∅)) corresponds to Φω
Π (h∅, ∅i), and
UΓ (C) gives us the greatest unfounded set wrt Π and the 3-valued model
obtained from C .
(PU)∗Γ ((∅, ∅)) gives us the well-founded model of Π.
smodels:
expand(∅, A) gives well-founded model of Π, where A is the set of all
atoms in Π
318
Alternating fixpoint characterization
• hlf pAΠ (X), Atm \ CΠ (lf pAΠ (X))i is the well-founded model of Π.
319
Example
Alternating fixpoint characterization
r ← s ← t ← r, s u ← not t
Π=
p ← not p, not q a ← b b ← a
X = {r, s, t}
320
The Cmodels approach
• Completion
• Operation of Cmodels
321
Completion (Recapitulation)
p ← not q
q ← not p
Π: Π∗ = Π
r ← p
r ← q
p ← ¬q p ≡ ¬q
Π∗∗ : q ← ¬p Completion of Π : q ≡ ¬p
r ← (p ∨ q) r ≡ (p ∨ q)
323
Satisfiability
324
Propositional Satisfiability
325
Clausification
Let F be a formula.
Clausify(F )
eliminate all connectives from F except ¬, ∧ and ∨;
distribute ¬ over ∧ and ∨ until it applies to atoms only;
distribute ∨ over ∧ until it applies to literals only;
return the set of conjunctive terms of the resulting formula;
326
Clausification
We have
327
Clausification with new atoms
Clausify⋆ (F, Γ)
if F is a conjunction of clauses C1 ∧ . . . ∧ Ck
then exit with {C1 , . . . , Ck } ∪ Γ;
G ← a minimal (wrt subformulas) non-literal subformula of F ;
u ← a new atom;
F ← result of replacing G in F by u;
Clausify⋆ (F, Γ ∪ Clausify(u ≡ G));
328
Example for Clausify⋆ (F, Γ)
F Γ G u Clausify(u ≡ G)
p ∨ ¬(q → r) ∅ q→r u0 (¬u0 ∨ ¬q ∨ r) ∧ (q ∨ u0 ) ∧ (¬r ∨ u0 )
p ∨ ¬u0 ∅
329
Properties of Clausify⋆
• F and Clausify⋆ (F, ∅) are not equivalent:
330
Example for Clausify⋆ (F, Γ)
F Γ G u Clausify(u ≡ G)
(p1 ∧ q1 ) ∨ (p2 ∧ q2 ) ∅ p1 ∧ q1 u0 (¬u0 ∨p1 )∧(¬u0 ∨q1 )∧
(¬p1 ∨ ¬q1 ∨ u0 )
u0 ∨ (p2 ∧ q2 ) Clausify(u0 ≡ p1 ∧ q1 ) ∪ Γ p2 ∧ q2 u1 (¬u1 ∨p2 )∧(¬u1 ∨q2 )∧
(¬p2 ∨ ¬q2 ∨ u1 )
u0 ∨ u1 Clausify(u1 ≡ p2 ∧ q2 ) ∪ Γ
A simplification step like this may create a new unit clause and that can
make further simplifications possible.
332
Unit Clause Propagation Procedure
UnitPropagation(Γ, U )
while there is a unit clause {L} in Γ
U ← U ∪ {L}
for every clause C ∈ Γ do
if L ∈ C then Γ ← Γ \ {C}
elsif L ∈ C then Γ ← (Γ \ {C}) ∪ {C \ {L}}
end
end
• Γ is a set of clauses;
333
Some remarks
334
Example
Γ U L
{p, ¬p ∨ ¬q, ¬q ∨ r} ∅ p
{¬q, ¬q ∨ r} {p} ¬q
∅ {p, ¬q} no
335
Another Example
For Γ = {p, p ∨ q, ¬p ∨ ¬q, q ∨ r, ¬q ∨ ¬r} apply Unit-Propagation.
Γ U L
{p, p ∨ q, ¬p ∨ ¬q, q ∨ r, ¬q ∨ ¬r} ∅ p
{¬q, q ∨ r, ¬q ∨ ¬r} {p} ¬q
{r} {p, ¬q} r
∅ {p, ¬q, r} no
336
Properties of Unit-Propagation
There are two cases when unit clause propagation alone is sufficient for
solving SAT for a set of clauses Γ0 .
For this, consider the values of Γ and U upon termination of
Unit-Propagation(Γ0 , ∅).
337
Davis-Putnam-Logemann-Loveland Procedure
Observation
For any set of formulas Γ and any formula F ,
the set of models of Γ is the union of the sets of models
of Γ ∪ {F } and Γ ∪ {¬F }.
The DPLL procedure uses this fact to apply unit clause propagation
(even) when Γ does not contain unit clauses.
338
The DPLL algorithm
DPLL(Γ, U )
UnitPropagation(Γ, U )
if ∅ ∈ Γ then return
if Γ = ∅ then exit with a model of U
A ← select(atoms(Γ))
DPLL(Γ ∪ {A}, U )
DPLL(Γ ∪ {A}, U )
339
Some remarks
340
Example
Let Γ = {¬p ∨ q, ¬p ∨ r, q ∨ r, ¬q ∨ ¬r} be a set of clauses and apply DPLL.
2
DPLL(Γ ∪ {p}, U )
6
6
6 UP gives ∅ ∈ Γ hence no model
6 2
6
6 DPLL(Γ ∪ {q}, U )
6 6
Γ = {¬p ∨ q, ¬p ∨ r, q ∨ r, ¬q ∨ ¬r} 6 6 UP gives
DPLL(Γ ∪ {¬p}, U ) 6
6 6
U =∅ 6 Γ=∅
6
6
6 UP gives 6
DPLL(Γ, U ) 6 U = {¬p, q, ¬r} is model
6 6
6
6 Γ = {q ∨ r, ¬q ∨ ¬r} 6
UP leaves Γ and U unchanged 6 6 6 DPLL(Γ ∪ {¬q}, U )
6
6 U = {¬p} 6
6 UP gives
6 6
select q
6
6 6
6 6 Γ=∅
4 4
U = {¬p, ¬q, r} is model
341
Operation of Cmodels
3. forms the program completion and call a SAT solver to find its
models.
342
Simplification
343
Some Definitions
Define
344
Simplification
Head ← Body, F.
Π′
← F.
(2)
′
Π
345
Simplification
← not F.
(3)
′
Π
← F.
Head ← Body. (4)
Π′
346
Example
a ← b, not c.
Π = d ← not a.
c ← not d.
☞ AS(Π) = {{d}} implies Atom+ (Π) = {d} and Atom− (Π) = {a, b, c}.
According to (2) Π is equivalent to
← b.
Π′ = d ← not a.
c ← not d.
☞ Atom+ (Π′ ) = Atom+ (Π) = {d} and Atom− (Π′ ) = Atom− (Π) = {a, b, c}.
347
Example ctd
← b.
Π′′ = d ← not a.
← not d.
← b.
′′′ ← a.
Π =
d←.
← not d.
348
Remark on Simplification
349
Tight Programs
350
Example
p ← not p.
Π = p ← q.
q ← p.
p - q
352
Example
a ← not b, not d.
b ← not a, not c.
c ← d.
d ← c.
f ← c, not f.
c ← not d, not a, not b.
d ← not c, not a, not b.
353
Example
Π Sim(Π) Completion
a ← not b, not d. a ← not b. a ≡ ¬b
b ← not a, not c. b ← not a. b ≡ ¬a
c ← d. ← d. ⊥ ≡ d.
d ← c. ← c. ⊥ ≡ c.
f ← c, not f. ← c.
c ← not d, not a, not b. c ← not a, not b. c ≡ ¬b ∧ ¬a.
d ← not c, not a, not b. d ← not a, not b. d ≡ ¬b ∧ ¬a.
4. forms the program completion and call a SAT solver to find its
models.
355
lparse Programs
A0 ← l {A1 = w1 , . . . , Am = wm },
356
Basic Nested Programs
357
Example: choice rules
{p, q}
r ← p
r ← q
p ← not not p
q ← not not q
r ← p
r ← q
358
Simplification
← F
Head ← L{T ail} (5)
Π′
359
Simplification
Let Π be a lparse program of the form
← not F.
Head ← L{T ail} (6)
Π′
← F.
Head ← L − w{T ail} (7)
Π′
360
Example
{a}
c ← 4{a = 1, b = 1, not d = 2}
☞ AS(Π) = {{a}, ∅} implies Atom+ (Π) = ∅ and Atom− (Π) = {b, c, d}.
According to (5) Π is equivalent to
{a}
←b
c ← 4{a = 1, not d = 2}
361
Example ctd
{a}
←b
c ← 4{a = 1, not d = 2}
is equivalent to
{a}
←b
←d
c ← 2{a = 1}.
362
Example: Translation to Basic Nested Programs
The rule
p ← 3{q = 3, r = 2, s = 2}
p ← aux1
aux1 ← 3{r = 2, s = 2}
p ← q, aux2
aux2 ← 0{r = 2, s = 2}
363
Example ctd
We obtain
p ← aux1
aux1 ← 3{r = 2, s = 2}
p←q
A similar step gives:
p ← aux1
aux1 ← aux3
aux3 ← 3{s = 2}
aux1 ← r, aux4
aux4 ← 1{s = 2}
p←q
☞ aux3 is an abbreviation for 3{s = 2} and
☞ aux4 is an abbreviation for 1{s = 2}.
364
Example ctd
p ← aux1
aux1 ← r, aux4
aux4 ← 1{s = 2}
p←q
p ← aux1
aux1 ← r, aux4
aux4 ← s
p←q
365
SAT for Horn Clauses and
Computation of the least Herbrand Modell
Clauses vs Rules:
The efficient DPLL algorithm for SAT can be further optimized if we
restrict ourself to clauses with “direction”.
☞ We deal with definite programs, Horn clauses or rules.
366
Horn-Klauseln (Erinnerung)
• A (Fakt)
367
Beispiel
A
¬A ∨ B
¬A ∨ ¬B
368
Das Kleinste Herbrand-Modell (Erinnerung)
TP ↑ 1 = {A | (A ← B1 ∧ . . . ∧ Bm ) ∈ P und {B1 , . . . , Bn } ⊆ ∅}
TP ↑ (n + 1) = {A | (A ← B1 ∧ . . . ∧ Bm ) ∈ P und {B1 , . . . , Bn } ⊆ TP ↑ (n)}
369
Algorithmus 1 für definite Programme
Sei P = {r1 , . . . , rn } ein definites Programm, Atm(P) = {A1 , . . . , Am } die
aussagenlogischen Variablen von P , V ~ ein boolscher Vektor der Länge m und ch
eine boolsche Variable.
program a l g o r i t h m 1 ;
begin ch := t ;
f o r each X ∈ Atm(P) do V (X) := f end f o r
f o r each X ∈ Atm(P) s.t. X ∈ P do V (X) := t end f o r
w h i l e ch do
ch := f ;
f o r each (A ← B1 ∧ . . . ∧ Bk ) ∈ P do
if V (B1 ) = t, . . . , V (Bk ) = t and V (A) = f th en
V (A) := t; ch := t;
P := P \ {A ← B1 ∧ . . . ∧ Bk }
end if
end f o r
end w h i l e
end
A B←A
C D ←B∧C
E ←D∧F F ←E
➥ V (A) = V (C) = t
➥ 1. While-Loop: V (B) = t ; P := P \ {B ← A}
➥ 2. While-Loop: V (D) = t ; P := P \ {D ← B ∧ C}
371
Algorithmus 1 für Horn-Klauseln
Sei H = {r1 , . . . , rn } ein definites Programm, Atm(H) = {A1 , . . . , Am } die
aussagenlogischen Variablen, V ~ ein boolscher Vektor der Länge m und ch und cons
boolsche Variablen.
program a l g o r i t h m 1 ;
begin ch := t; cons := t;
f o r each X ∈ Atm(H) do V (X) := f end f o r
f o r each X ∈ Atm(H) s.t. X ∈ H do V (X) := t end f o r
w h i l e ch and cons do ch := f ;
f o r each C ∈ H and ch do
i f C = ¬B1 ∨ . . . ∨ ¬Bk and V (B1 ) = t, . . . , V (Bk ) = t th en cons := f
else
i f C = (A ∨ ¬B1 ∨ . . . ∨ ¬Bk ) and V (B1 ) = t, . . . , V (Bk ) = t and V (A) = f th en
V (A) := t; ch := t;
H := H \ C
end if
end if
end f o r
end w h i l e end 372
Beispiel
A
¬A ∨ B
¬A ∨ ¬B
➥ V (A) = V (B) = f ;
➥ V (A) = t
➥ 2. While-Loop: cons = f ;
☞ Inkonsistenz entdeckt!!
☞ Komplexität O(n2 )
373
Optimierung von Algorithmus 1
☞ Initial werden alle aussagenlogischen Variablen gleich f (false)
gesetzt (wie bisher), aber es wird andere Datenstruktur verwendet.
• eine Klausel C wird bearbeitet, wenn der Wert ihres Zählers 0 ist,
d.h. für alle negativen Literale ¬B ∈ C ist B = t (true)
➥ das positive Literal A ∈ C wird auf t (true) gesetzt sobald der
Zähler von C null ist
374
Algorithmus 2 für Horn-Klauseln
Sei H = {r1 , . . . , rn } ein definites Programm und
Atm(H) = {A1 , . . . , Am } die aussagenlogischen Variablen, die in H vorkommen.
program algorithm2;
type clause = r e c o r d N : 1 . . n ; next : ˆ clause end ;
type lit = record val : b o o l e a n ; clauselist : ˆ clause end ;
type Hornclause = a r r a y [ 1 . . m ] of lit ;
type count = a r r a y [ 1 . . n ] of 0 .. m ;
var H : Hornclause ; queue : q-type ;
num, poslit : count ; cons : b o o l e a n ;
begin
input(H);
init(H, num, poslit, queue, new);
cons := t;
sat(H, num, poslit, queue, cons, new);
i f cons th en
print(assignment);
else
print(unsat);
end 375
Algorithmus 2 Initialisierung
Gegeben: H
Initialisierung von num , poslit und cons ( queue ):
4. setzte cons := t
376
Algorithmus 2 sat
program sat(H, num, poslit, queue, cons, new);
var c1, c2, old, new, next : 1 . . n; \ ∗ H = {r1 , . . . , rn } ∗ \
k : 1 . . m; \ ∗ Atm(H) = {A1 , . . . , Am } ∗ \
begin old := new;
w h i l e queue <> nil and cons do new := 0;
f o r i := 1 t o old and cons do
c1 := pop(queue); next := poslit(c1);
f o r c2 ∈ H[next].clauselist do num[c2] := num[c2] − 1;
i f num[c2] = 0 th en k := poslit[c2];
i f H[k].val 6= 1 th en
i f k <> 0 th en
H[k].val := t; queue := push(c2, queue); new := new + 1;
else cons := f ;
end if end if end if
end f o r end f o r
old := new;
end w h i l e end
377
Beispiel: Initialisierung
c1 : A c2 : B ∨ ¬A ≡ (B ← A)
c3 : C ∨ ¬A ∨ ¬B ≡ (C ← A ∧ B) c4 : ¬B ∨ ¬C ≡ (← B ∧ C)
378
Beispiel: nach 1. While-Loop
c1 : A c2 : B ∨ ¬A ≡ (B ← A)
c3 : C ∨ ¬A ∨ ¬B ≡ (C ← A ∧ B) c4 : ¬B ∨ ¬C ≡ (← B ∧ C)
379
Beispiel: nach 2. While-Loop
c1 : A c2 : B ∨ ¬A ≡ (B ← A)
c3 : C ∨ ¬A ∨ ¬B ≡ (C ← A ∧ B) c4 : ¬B ∨ ¬C ≡ (← B ∧ C)
380
Beispiel: nach 3. While-Loop
c1 : A c2 : B ∨ ¬A ≡ (B ← A)
c3 : C ∨ ¬A ∨ ¬B ≡ (C ← A ∧ B) c4 : ¬B ∨ ¬C ≡ (← B ∧ C)
☞ Use tow counter for each rule, one for support and one for blockage.
383
Notions of Equivalence
Two programs Π1 and Π2 are
384
How to Show Strong Equivalence
385
How to Show Strong Equivalence (ctd.)
Ms (Π1 ) = {({a}, {a}), ({b}, {b}), ({a}, {a, b}), ({b}, {a, b}),
({a, b}, {a, b})}
386
How to Show Uniform Equivalence
387
How to Show Uniform Equivalence (ctd.)
Ms (Π1 ) = {({a}, {a}), ({b}, {b}), ({a}, {a, b}), ({b}, {a, b}),
({a, b}, {a, b})}
388