Asp Tutorial
Asp Tutorial
Asp Tutorial
tson@cs.nmsu.edu
http://www.cs.nmsu.edu/~tson
October 2005
Answer Set Programming.
Acknowledgement
This tutorial contains some materials from tutorials on answer set programming and on
knowledge representation and logic programming from those provided by
• Chitta Baral, available at www.public.asu.edu/~cbaral.
• Michael Gelfond, available at www.cs.ttu.ued/~mgelfond.
Purpose
Outline
• Foundation of answer set programming: logic programming with answer set semantics
(syntax, semantics, early application).
• Answer set programming: general ideas and examples
• Application of answer set programming in
– Knowledge representation
– Constraint satisfaction problem
– Combinatoric problems
– Reasoning about action and change
– Planning and diagnostic reasoning
• Current issues
• variables: X, Y, Z, etc.
• object constants (or simply constants): a, b, c, etc.
• function symbols: f, g, h, etc.
• predicate symbols: p, q, etc.
• terms: variables, constants, and f (t1, . . . , tn) such that tis are terms.
• atoms: p(t1, . . . , tn) such that tis are terms.
• literals: atoms or an atom preceded by ¬.
• naf-literals: atoms or an atom preceded by not.
• gen-literals: literals or a literal preceded by not.
• ground terms (atoms, literals) : terms (atoms, literals resp.) without variables.
• L – a first order language with its usual components (e.g., variables, constants,
function symbols, predicate symbols, arity of functions and predicates, etc.)
• UL – Herbrand Universe of a language L: the set of all ground terms which can be
formed with the functions and constants in L.
• BL – Herbrand Base of a language L: the set of all ground atoms which can be
formed with the functions, constants and predicates in L.
• Example:
– Consider a language L1 with variables X, Y ; constants a, b; function symbol f of
arity 1; and predicate symbol p of arity 1.
– UL1 = {a, b, f (a), f (b), f (f (a)), f (f (b)), f (f (f (a))), f (f (f (b))), . . .}.
– BL1 = {p(a), p(b), p(f (a)), p(f (b)), p(f (f (a))), p(f (f (b))),
p(f (f (f (a)))), p(f (f (f (b)))), . . .}.
Main Definitions
• ground(r, L): the set of all rules obtained from r by all possible substitution of
elements of UL for the variables in r.
• Example 1 Consider the rule “p(f (X)) ← p(X).” and the language L1. Then
ground(r, L1) will consist of the following rules:
p(f (a)) ← p(a).
p(f (b)) ← p(b).
p(f (f (a))) ← p(f (a)).
p(f (f (b))) ← p(f (b)).
...
• For a program Π:
– ground(Π, L) = r∈Π ground(r, L)
S
Example 2 – Π:
p(a).
p(b).
p(c).
p(f (X)) ← p(X).
– ground(Π):
p(a) ←.
p(b) ← .
p(c) ← .
p(f (a)) ← p(a).
p(f (b)) ← p(b).
p(f (c)) ← p(c).
p(f (f (a))) ← p(f (a)).
p(f (f (b))) ← p(f (b)).
p(f (f (c))) ← p(f (c)).
...
• Meaning of a rule
– A positive rule “a0 ← a1, . . . , an” can be viewed as the disjunction
a0 ∨ ¬a1 ∨ . . . ∨ ¬an which says that if a1, . . . , an are true then a0 is true.
– A rule “a0 ← a1, . . . , an , not an+1, . . . , not an+k ” states that if a1, . . . , an are
true and none of the an+1, . . . , an+k can be proven to be true then a0 is true.
– A constraint “← a1, . . . , an, not an+1, . . . , not an+k ” holds if some ai (1 ≤ i ≤ n)
is false or some aj (n + 1 ≤ j ≤ n + k) is true.
• Knowledge representation using LP-rules
f lies(X) ← bird(X). “if X is a bird, then X flies”
wet grass ← sprinkler on. “The grass is wet if the sprinkler is on”
rain ← humid, hot. “hot and humid causes rain”
love(X, Y ) ← love(Y, X). “Love is a symmetric relationship”
Herbrand Interpretation
Some Examples
Properties of TΠ
• TΠ is monotonic: TΠ(X) ⊆ TΠ (Y ) if X ⊆ Y .
• TΠ has a least fixpoint that can be computed as follows.
1. Let X1 = TΠ(∅) and k = 1
2. Compute Xk+1 = TΠ(Xk ). If Xk+1 = Xk then stops and return Xk .
3. Otherwise, increase k and repeat the second step.
Note: The above algorithm will terminate for positive program Π with finite BΠ.
We denote the least fix point of TΠ with TΠ∞ (∅) or lf p(TΠ ).
Theorem 1 MΠ = lf p(TΠ ).
Theorem 2 For every positive program Π without constraint, MΠ is unique.
More Examples
Entailment
For a program Π and an atom a, Π entails a (with respect to the minimal model
semantics), denoted by Π |= a, iff a ∈ MΠ.
We say that Π entails ¬a (with respect to the minimal model semantics), denoted by
Π |= ¬a, iff a ∈ MΠ .
Example 4 Let
p(f (X)) ← p(X).
Π=
q(a) ← p(X).
p(b).
Π |= ¬q(b), and
Example 5 • Consider the parent-child database with facts of the form p(X, Y )
(X is a parent of Y ). We can define the ancestor relationship, a(X, Y ) (X is an
ancestor of Y ), using the following rules
a(X, Y ) ← p(X, Y ).
Πa =
a(X, Y ) ← p(X, Z), a(Z, Y ).
Given the set of facts I = {p(a, b), p(b, c), p(c, d)}, let Π = Πa ∪ I. We can easily
compute
MΠ = I ∪ {a(X, Y ) | p(X, Y ) ∈ I} ∪ {a(a, c), a(a, d), a(b, d)}
So, Π |= a(a, c) and Π |= a(a, d), i.e., a is an ancestor of c and d; on the other
hand, Π |= ¬a(d, a), i.e., d is not an ancestor of a.
Tran Cao Son 19
Answer Set Programming. Logic Programming and Answer Set Semantics
It can be shown that for every pair of nodes p and q of the graph G,
reachable(p, q) belongs to MΠG iff there exists a path from p to q in the graph G.
Remark 3 Reasoning using positive programs assumes the closed world assumption
(CWA): anything, that cannot be proven to be true, is false.
Detailed Computation
• Consider Π2 = {a ← not b. b ← not a.}. We will show that its has two answer
sets {a} and {b}
S1 = ∅ S2 = {a} S3 = {b} S4 = {a, b}
ΠS2 1 : ΠS2 2 : ΠS2 3 : ΠS2 4 :
a← a←
b← b←
MΠS1 = {a, b} MΠS2 = {a} MΠS3 = {b} MP S4 = ∅
2 2 2
MΠS1 6= S1 MΠS2 = S2 MΠS3 = S3 MΠS4 6= S4
2 2 2 2
NO Y ES Y ES NO
• Assume that our language contains two object constants a and b and consider
Π = {p(X) ← not q(X). q(a) ←}. We show that S = {q(a), p(b)} is an answer set
of Π. We have that ΠS = {p(b) ← q(a) ←} whose minimal model is exactly S. So,
S is an answer set of Π.
Tran Cao Son 22
Answer Set Programming. Logic Programming and Answer Set Semantics
• Π4 = {p ← not p.} We will show that P does not have an answer set.
S1 = ∅, then ΠS4 1 = {p ←} whose minimal model is {p}. {p} =
6 ∅ implies that S1 is
not an answer set of Π4 .
S2 = {p}, then ΠS4 2 = ∅ whose minimal model is ∅. {p} =
6 ∅ implies that S2 is not an
answer set of Π4.
This shows that P does not have an answer set.
In computing answer sets, the following theorem is useful:
Theorem 4 Let Π be a program.
1. Let r be a rule in ground(P ) whose body contains an atom a that does not occur
in the head of any rule in ground(P ). Then, S is an answer set of Π iff S is an
answer set of ground(P ) \ {r}.
2. Let r be a rule in ground(P ) whose body contains a naf-atom not a that does not
occur in the head of any rule in ground(P ). Let r′ be the rule obtained from r by
removing not a. Then, S is an answer set of P iff S is an answer set of
ground(P ) \ {r} ∪ {r′ }.
Remark 6 A program may have zero, one, or more than one answer sets.
• Π1 = {a ← not b.}.
Π1 has a unique answer set {a}.
• Π2 = {a ← not b. b ← not a.}.
The program has two answer sets: {a} and {b}.
• Π3 = {p ← a. a ← not b. b ← not a.}
The program has two answer sets: {a, p} and {b}.
• Π4 = {a ← not b. b ← not c. d ← .}
Answer sets: {d, b}.
• Π5 = {p ← not p.}
No answer set.
• Π6 = {p ← not p, d. r ← not d. d ← not r.}
Answer set {r}.
• Π3 = {a ← not b. b ← not c. d ← .}
Answer sets: {d, b}. Π3 |= b; Π3 |= ¬a; etc.
• Π4 = {p ← not p.}
No answer set. p is unknown.
• Π5 = {p ← not p, d. r ← not d. d ← not r.}
Answer set {r}. Π5 |= r; Π5 |= ¬p; etc.
• Π6 = {p ← not a. p ← not b. a ← not b. b ← not a.}
Two answer sets: {p, a} and {p, b}. So, Π6 |= p but Π6 6|= a and Π6 6|= ¬a?
(likewise b).
• Π7 = {q ← not r. r ← not q. p ← not p. p ← not r.}
One stable model: {p, q}. So, Π7 |= p and Π7 |= q.
• A set of atoms S is closed under a program Π if for all rules of the form
a0 ← a1, . . . , am , not am+1, . . . , not an.
in Π, {a1, . . . , am} ⊆ S and {am+1, . . . , an} ∩ S = ∅ implies that a0 ∈ S.
• A set of atoms S is said to be supported by Π if for all p ∈ S there is a rule of the
form p ← a1, . . . , am , not am+1, . . . , not an.
in Π, such that {a1, . . . , am} ⊆ S and {am+1, . . . , an} ∩ S = ∅.
• A set of atoms S is an answer set of a program Π iff (i) S is closed under Π and (ii)
there exists a level mapping function λ (that maps atoms in S to a number) such that
for each p ∈ S there is a rule in Π of the form p ← a1, . . . , am, not am+1, . . . , not an.
such that {a1, . . . , am} ⊆ S, {am+1, . . . , an} ∩ S = ∅ and λ(p) > λ(ai), for
1 ≤ i ≤ m.
• Note that (ii) above implies that S is supported by Π.
Commonsense Reasoning
Our knowledge
• is often incomplete (it does not contain complete information about the world), and
• contains defaults (rules which have exceptions, also called normative sentences).
• contains preferences between defaults (prefer a conclusion/default).
For this reasons, we often jump to conclusions (ignore what we do not now), and know
to deal with exceptions and preferences.
Example 7 We know
Normally, birds fly.
Normally, computer science students can program.
Normally, students work hard.
Normally, things do not change.
Normally, students do not watch TV.
Normally, the speed limit on highways is 70 mph.
Normally, it is cold in December.
From this, we make conclusions such as Tweety flies if we know that Tweey is a bird;
Monica can program if she is a computer science student; etc.
Representing Defaults
Answer set of Πb : ?
r1
: f lies(X) ← bird(X), not ab(r1, X)
: ← penguin(X)
r2 bird(X)
Πb =
r3 : ab(r1, X) ← penguin(X)
r4 : penguin(tweety) ←
: ←
r bird(tim)
5
Answer set of Πb :
Defaults – Example
We can check that Πa entails that sam is a lion but not a cat while john is a cat
which is a lion.
• The Boeing 747, concord, and FA21314 are airplanes. Airplanes normally fly
unless they are out of order. FA21314 is out of order.
r1 : f lies(X) ← airplane(X), not ab(X)
r2 : ab(X) ← out of order(X)
r3 : airplane(boeing 747) ←
Πairplanes =
r4 : airplane(concord) ←
r5 : airplane(f a21324) ←
r6 : out of order(f a21324) ←
To add expressiveness and make logic programming a more suitable language for
knowledge representation and reasoning, additional features and constructors are
introduced:
• classical negation: instead of atoms, literals are used in the rule
l0 ← l1, . . . , ln , not ln+1, . . . , not ln+k
where li is a literal (an atom a or its negation ¬a). This will allow us to represent and
reason with negative information.
• epistemic disjunction: rule is allowed to have epistemic disjunction in the head
l0 or . . . or lm ← lm+1, . . . , lm+n , not lm+n+1, . . . , not lm+n+k
where or is a epistemic disjunction. This rule states that if lm+1, . . . , lm+n are true
and there is no reason for believing that the lm+n+1, . . . , lm+n+k are true then at least
one of the l0, . . . , lm is believed to be true.
• nested expression: allowing not not l.
Tran Cao Son 36
Extensions of Logic Programming
Answer Set Programming. and Computing Answer Sets
• weighted-atom:
l{l0 = w0, . . . , lk = wk , not lk+1 = wk+1 , . . . , not lk+n = wk+n }u
where li’s are a literal, wi are integers, and l and u are two integers. This atom is true
with respect to a set of literals S if
0≤j≤k k+1≤j≤k+n
l≤ wj + wj ≤ u
X X
lj ∈S lj 6∈S
r1
: f lies(X) ← bird(X), not ab(r1, X)
: ← penguin(X)
r2 bird(X)
Πb = r
3
: ab(r1, X) ← penguin(X)
r4 : penguin(tweety) ←
: ←
r bird(tim)
5
• In the bird example, “Penguins do not fly” is more intuitive than “Normally, penguins
do not fly.” So, r3 of Πb should be changed to
r3′ : ¬f lies(X) ← penguin(X).
Doing so will make the program Πb inconsistent! Obviously, r1 does not account for
the class of birds who do not fly. We should change the rule r1 to
r1′ : f lies(X) ← bird(X), not ab(r1, X), not ¬f lies(X)
The new program, Π′b = Πb \ {r1, r3} ∪ {r1′ , r3′ } will correctly answer the same
questions such as “does Tim flies” and “does Tweety fly?”
Tran Cao Son 38
Extensions of Logic Programming
Answer Set Programming. and Computing Answer Sets
• Complexity: The problem of determining the existence of an answer set for finite
propositional programs (programs without function symbols) is NP-complete. For
programs with disjunctions, function symbols, etc. it is much higher.
A consequence of this property is that there exists no polynomial-time algorithm for
computing answer sets.
Special cases: answer sets of positive programs without function symbols can be
computed in polynomial time in the size of the program.
A good survey on the complexity and expresiveness of different classes of logic
programs can be found in
– Chitta Baral. Knowledge representation, reasoning and declarative problem
solving, Cambridge University Press, 2003.
• Answer set solvers: Programs that compute answer sets of (finite and grounded)
logic programs. Two main approaches:
Tran Cao Son 41
Extensions of Logic Programming
Answer Set Programming. and Computing Answer Sets
Main Idea
Graph Coloring
3 3
1 1
Coloring
4 4
2 2
5 5
Problem: Given a (bi-directed) graph and three colors red, green, and yellow. Find a
color assignment for the nodes of the graph such that no edge of the graph connects two
nodes of the same color.
Graph Coloring
• Checking for a solution: needs to make sure that no edge connects two nodes of the
same color. This can be represented by a constraint:
← edge(X, Y ), color(X, C), color(Y, C). (6)
%% prog1
%% representing the graph
node(1). node(2). node(3). node(4). node(5).
%% constraint checking
:- edge(X,Y), color(X,C), color(Y,C).
Try with lparse prog1 | smodel 1 or lparse prog1 | smodel 0 and see
the result.
Correctness of prog1
removed). This implies that the minimal model of ΠSG cannot contain any element of C.
This implies that S cannot be an answer set of ΠG because it cannot be equal the
minimal model of ΠSG . Again, this contradicts the assumption that S is an answer set of
ΠG. Hence, this case cannot happen.
Case 3: S contains all elements of C. This is similar to the second case, i.e., ΠSG does
not contain any rule whose head is a member of C, and hence, S would not be an
answer set of ΠG.
The three cases show that for each node k, S contains 1-and-only-1 member of the set
{color(k, red), color(k, green), color(k, yellow)}.
2. Now we need to show that the color mapping specified by the answer set S is a
solution to the coloring problem.
Let 1, . . . , n be the nodes of the graph and c1, . . . , cn be the color such that
color(i, ci) ∈ S for i = 1, . . . , n. We need to show that if edge(i, j) belongs to G then
ci 6= cj . Again, we prove by contradiction. Let assume that there is an edge (p, q) in G
and cp = cq . This means that the body of the rule (6) for edge(p, q), color(p, cp), and
color(q, cq ) is satisfied. This means that S is not an answer set of ΠG, i.e., our
assumption contradicts the fact that S is an answer set of ΠG. Thus, our asssumption is
incorrect, i.e., we have proved that for every edge (i, j) of G, ci 6= cj . This shows that S
corresponds to a solution of the 3-coloring problem for G.
Theorem 6If the 3-coloring problem for G has a solution M then ΠG has an
answer set correcsponds to M .
Proof. Let 1, . . . , n be the nodes of the graph. Consider a solution for the 3-coloring
problem for G. Let c1, . . . , cn be the color of the node i = 1, . . . , n, respectively. We will
show that the set of atoms
S = {node(i) | i = 1, . . . , n} ∪ {edge(i, j) | (i, j) is an edge of G}∪
{color(i, ci) | i = 1, . . . , n}
is an answer set of ΠG.
Let us compute ΠSG . We can see that ΠSG consists of the following rules:
• the rules defining the graph, i.e., the rules node(1) ← .... node(n) ← (nodes of the
graph) and the rules edge(i, j) ← if (i, j) is an edge of G.
• for each node i, one rule of the form color(i, ci ) ← node(i) which comes from one of
the rules (3)-(5).
• for each edge (i, j), three constraints:
← edge(i, j), color(i, red), color(j, red)
← edge(i, j), color(i, yellow), color(j, yellow)
← edge(i, j), color(i, green), color(j, green)
We need to show that S is the minimal model of ΠSG that does not violates any of the
constraints.
Let X0 = {node(i) | i = 1, . . . , n} ∪ {edge(i, j) | (i, j) is an edge of G}.
Obviously, TΠS (∅) = X0 and
G
TΠS (X0) = S, and TΠS (S) = S. (*)
G G
Furthermore, because for every edge (i, j), ci 6= cj , we can conclude that there exists no
edge (i, j) in G and a color C ∈ {red, green, yellow} such that S contains color(i, C)
and color(j, C). This is equivalent to say that the constraints in ΠSG are satisfied by S.
Together with (*), we conclude that S is an answer set of ΠG.
n-Queens
Q Q
Q Q
Q Q
Q Q
n-Queens
• Representation: the chess board can be represented by a set of cells cell(i, j) and
the size n.
• Solution: Each cell is assigned a number 1 or 0. cell(i, j) = 1 means that a queen is
placed at the position (i, j) and cell(i, j) = 0 if no queen is placed at the position (i, j)
• Generating a possible solution:
– cell(i, j) is either true or false
– select n cells, each on a column, assign 1 to these cells.
• Checking for the solution: ensures that no queen is attacked
%% prog2
%% representing the board, using n as a constant
col(1..n). % n column
row(1..n). % n row
%% generating solutions
1 {cell(I,J) : row(J)}:- col(I).
Covering with 1
by 2 tiles
Problem: Given a slightly damaged chess board (n × n). Find a covering of the board
using m 1 × 2-tiles so that all the good squares on the board are covered. If no such
covering exists, report there is no solution.
• Problem: Given a slightly damaged chess board (n × n). Find a covering of the
board using m 1 × 2-tiles so that all the good squares on the board are covered. If no
such covering exists, report there is no solution.
• Representation:
• the board
− dimension
− damaged/good cells
• the tiles with their coverage
As with N-queens problem, we can use the two predicates row() and col() to
represent the possible board cells
We can use bad(i, j) to indicate that the cell (i, j) is damaged We can use the
predicate cell(i, j, t) to represent the information that the cell (i, j) is covered by the
tile t
bad(1,1). bad(3,2).
:- col(I),row(J),tile(T1),tile(T2),neq(T1,T2),cell(I,J,T1),cell(I,J,T2).
Making it Shorter
col(1..n). row(1..n). tile(1..ntiles). %represenation
#domain tile(T;T1;T2).
#domain col(I;I1;I2).
#domain row(J;J1;J2).
bad(1,1). bad(3,2).
K-clique
• Problem: Given a number k and a graph G. We say that G has a clique of size k if
there is a set of k different vertices (nodes) in G such that each pair of vertices from
this set is connected through an egde.
• Representation:
• Graph (node() and edge())
• Clique (clique(N )) to say that node N belongs to the clique if clique(N ) is true;
otherwise it does not belong to the clique.
• Generating a solution: Selecting k nodes – this is equivalent to assigning k atoms
of the set
{clique(1), . . . , clique(n)}
the truth value true. This can be achieved by the rule
k{clique(N ) : node(N )}k.
Tran Cao Son 68
Answer Set Programming. Examples
• Checking for a solution: if every pair of the selected nodes is connected then this
is a solution; otherwise it is not a solution. This means that there exists no pair (I, J)
such that clique(I) and clique(J) are true but edge(I, J) is not true.
← clique(I), clique(J), I 6= J, not edge(I, J).
% prog4
node(1..5). % nodes
#domain node(N1;N2).
Example 13 There are three items a, b, and c. Customer 1 wants a for $US 2 and b
for $US 4. Customer 2 wants a for $US 1 and c for $US 6. Who should win?
The problem has two variables: b1 and b2; s1 = {a, b} and s2 = {b, c}. s1 ∩ s2 6= ∅.
This means that we have a constraint ¬(b1 ∧ b2), i.e., we cannot sell to both 1 and 2.
Then, the constraint to maximize Σni=1bipbi makes us decide that 2 wins, i.e, we should
sell to 2.
• for each allowed combination co which allows vi1 , ldots, vij to take a value c∗i1 , . . . , c∗ij ,
ΠP contains the following three rules:
– constraint(co) ←
– satisf ied(co) ← vi1 (c∗i1 ), . . . , vij (c∗ij )
– ← constraint(co), not satisf ied(co).
which have the final effect of requiring vil to be assigned to the value cij .
Tran Cao Son 74
Answer Set Programming. Examples
• for each prohibited combination co that disallows vi1 , ldots, vij to take a value
c∗i1 , . . . , c∗ij , ΠP contains the constraint:
← v1(c∗1 ), . . . , vn (c∗n).
NOTE: We simplify the problem a little bit here. A constraint co might place
conditions on only few variables – not all. Also, a variable might occur in more than
one allowed combination.
Consider the story: Matt – a turkey – is walking along the road. Jimmy – a hunter – is
coming from the opposite direction. He takes out a loaded gun and shoots at Matt.
There are several questions that arise given the above story:
• Is Matt still alive?
• Is the gun still loaded?
• Is Jimmy walking?
• Does Jimmy have the same number of guns?
• etc.
Problems:
• the frame problem – compact representation of what does not change after the
execution of an action.
• the ramification problem – reasoning about indirect effects of actions.
We also know that if Matt is hit by the gun, he will be dead and if he is dead he cannot
not continue walking.
The Language A
A is a high-level action language for representing and reasoning about actions and
change. It has a simple and independent semantics based on transition system. It is
introduced in
• M. Gelfond and V. Lifschitz: “Representing Actions and Change by Logic Programs”,
Journal of Logic Programming, vol. 17, Num. 2,3,4, pp. 301–323, 1993.
Several extensions of A have been proposed. We will use AL, which is introduced in
• C. Baral, M. Gelfond: “Reasoning agents in Dynamic Domains.” Logic Based
Artificial Intelligence , Edited By J. Minker, Kluwer 2000
Tran Cao Son 80
Answer Set Programming. Reasoning about Actions and Changes
In AL, an action theory is defined over two disjoint sets, a set of fluents (a fluent is a
property whose value changes over time) and a set of actions, and is a set of
propositional propositions of the form
a causes f if p1, . . . , pn (7)
f if p1, . . . , pn (8)
initially f (9)
where f and pi’s are fluent literals (a fluent literal is either a fluent g or its negation ¬g,
written as neg(g)) and a is an action. (7), referred as dynamic law, represents the
(conditional) effect of action a. (8) is a static law which represents the relationship
between fluents. Propositions of the form (9), also called v-propositions, are used to
describe the initial situation. An action theory is given by a pair (D, I) where D consists
of propositions of the form (7)-(9) and I consists of propositions of the form (9). D and
I will be called the domain description and initial state, respectively. We assume that
for each fluent f , initially f or initially ¬f belongs to I but not both.
Example 14 (Story) • To say that initially, the turkey is walking and not dead,
we write
initially ¬dead and initially walking
• Initially, the gun is loaded
initially loaded
• Shooting causes the turkey to be dead if the gun is loaded can be expressed by
Semantics
load shoot
dead, -walking,
-loaded
load
-dead, walking,
-loaded
shoot
shoot
load
-dead, -walking,
loaded
-dead, walking,
shoot loaded
load
-dead, -walking,
-loaded load
shoot
Each node represents a state: a consistent and complete set of fluent literals satisfying
all the static laws in Dy . (Picture)
• Each node represents a state: a consistent and complete set of fluent literals satisfying
all the static laws in Dy .
• A fluent literal l holds in a state s if l ∈ s.
• Each directed link between two states s1 and s2 represents a transition from s1 to s2
due to the execution of the action (the label of the link). s2 is called a possible next
state.
• Transition function Φ specifies the set of possible next states:
Φ(a, s) = {s′ | s′ is a possible next state}.
For example,
Φ(shoot, s1) = {s2}
where s1 = {loaded, walking, ¬dead} and s2 = {¬loaded, ¬walking, dead}.
• Φ is extended to define Φ̂ by
– Φ̂([], s) = {s}
– Φ̂([a1, . . . , an], s) = s′∈Φ(a1 ,s) Φ̂([a2, . . . , an], s′ ).
S
Given an action theory (D, I), we are often interest in the following questions/problems:
• Projection: What will be true/false after the execution of the sequence of action
a1, . . . , am from the initial state? Or, whether
(D, I) |= f after a1, . . . , an (11)
holds for a given fluent literal f .
• Planning: Which sequence of actions will changes the world from the initial state into
a state that satisfies a given fluent literal f ? Or, find a sequence of action a1, . . . , am
such that f will be true after the execution of the sequence of action a1, . . . , am from
the initial state.
Remark 10 We can easily generalize the result to answer questions with respect to a
fluent formula.
Given an action theory (D, I) and a sequence of actions α = [a1, . . . , an], we want to
know what is true and what is false after the execution of α from the initial state. In
other words, for every fluent f , we want to know whether
(D, I) |= f after a1, . . . , an
holds or not.
We will compute |= using logic programming. For each action theory (D, I), we define a
program π(D, I) as follows. The language of π(D, I) contains the following predicates:
• holds(F, T ): this states that the fluent literal F holds at time T .
• occ(A, T ): action A occurs at time moment T .
• time(T ): denotes the time moment T .
Tran Cao Son 86
Answer Set Programming. Reasoning about Actions and Changes
Remark 11 π(D, I) also contains some rules that make the encoding more compact.
(See example).
Let π n be the set of rules of π(D, I) in which the time variable takes the value from 0 to
n. Let π = π n ∪ {occ(a1, 0), . . . , occ(an , n − 1)}. The following can be proven:
Theorem 5 For every action theory (D, I), a sequence of actions a1, . . . , an, and a
fluent f ,
(D, I) |= f after a1, . . . , an
iff
π |= holds(f, n),
i.e., holds(f, n) belongs to every answer set of the program π.
% Defining actions
action(load). action(shoot).
% Static law
holds(neg(walking), T) :- holds(dead, T).
Tran Cao Son 89
Answer Set Programming. Reasoning about Actions and Changes
• In the program π(D, I), for each proposition (16), we add a rule
possible(A, T ) ← holds(p1, T ), . . . , holds(pn, T ).
Furthermore, we add a constraint
← occ(A, T ), not possible(A, T )
to π(D, I) which forbids A to occur when its executability condition is not satisfied.
π(D, I) can be used in the same way for projection as before.
c
b a
In this domain we have 3 blocks a, b, c. Each block is either on the table or on top of
another block. They form tower like b and c (a tower of 2 blocks), and a (a tower of one
block). The top block of a tower can be moved to the table or on top of another tower.
To move a block, that is not on top of a tower, we need to move all the blocks above it.
Furthermore, we are interested in building a new tower or finding out where a block is
located, whether a block is on the table, on top of some other block, etc.
We will write on(X, Y ) to denote that block X is on block Y ; We will also use t as a
constant to denote the table. We write move(X, Y ) to represent the action that moves
X on top of Y .
Tran Cao Son 93
Answer Set Programming. Reasoning about Actions and Changes
c
b a
% Defining fluents
fluent(on(X,Y)):- block(X), block(Y), neq(X,Y).
fluent(on(X,t)):- block(X).
% Defining actions
% Static law
holds(neg(on(X,Y)), T):- time(T),
block(X), block(Y), neq(X,Y),
block(Z), neq(X,Z), neq(Y,Z), holds(on(X,Z), T).
% Executability condition
possible(move(X,t),T):- block(X), time(T), holds(clear(X), T).
Tran Cao Son 97
Answer Set Programming. Reasoning about Actions and Changes
holds(on(X,t), T+1) :-
block(X), block(Y), neq(X,Y),
time(T),
holds(on(X,t), T),
not holds(neg(on(X,t)), T+1).
We then can use Smodels to answer query like
(Db, Ib ) |= on(a, c) ∧ on(c, t) after [move(c, t), move(a, c)]
Planning
Given a planning problem hD, I, ϕi, answer set planning solves it by translating it into a
logic program Π(D, I, ϕ) that has two components:
• The program π(D, I): this program describes the action theory (D, I).
• The set of rules for describing the goal and generating action occurrences:
– for each f ∈ ϕ, Π(D, I, ϕ) contains the rule
← not holds(f, length). (17)
which guarantees that f holds at the time moment length
– a rule of the form
1{occ(A, T ) : action(A)}1 ← time(T ), T < length. (18)
which states that at any moment of time, one and only one action must occur. We
add the condition T < length to not allow actions to occur at the time length.
Remark 13 Correctness of Π(D, I, ϕ) can be found in
• Tran Cao Son, Chitta Baral, Tran Hoai Nam, and Sheila McIlraith,
“Domain-Dependent Knowledge in Answer Set Planning,” ACM TOCL.
• Suppose that we are interested in the planning problem (Dy , Iy , dead), we need to add
the following rules to this program:
a
c b
b a c
Initial Goal
we add the following rule to the program:
1 {occ(A,T) : action(A) } 1 :- time(T), T < length.
:- not goal(length).
% effects of crossing
holds(at(M+P,N+Q,L1),T+1):-
time(T),
number(M), number(N), location(L),
number(P), number(Q), action(cross(M,N,L)),
occ(cross(M,N,L), T), location(L1), neq(L,L1),
holds(at(P,Q,L1), T).
holds(boat_at(L1),T+1):-
time(T),
number(M), number(N), location(L),
action(cross(M,N,L)), location(L1), neq(L,L1),
occ(cross(M,N,L), T).
holds(neg(boat_at(L)),T+1):-
time(T),
number(M), number(N), location(L),
action(cross(M,N,L)),
occ(cross(M,N,L),T).
holds(at(P-M,Q-N,L),T+1):-
time(T),
number(M), number(N), location(L),
number(P), number(Q), action(cross(M,N,L)),
occ(cross(M,N,L), T),
holds(at(P,Q,L), T).
% executability condition
possible(cross(I,J,L), T):-
time(T),
number(I), number(J), location(L),
number(P), number(Q), action(cross(I,J,L)),
holds(boat_at(L), T),
holds(at(P, Q, L), T), P>=I, Q>=J.
% initial condition
holds(at(3,3,l1),0).
holds(at(0,0,l2),0).
holds(boat_at(l1),0).
% constraint
:- number(I), number(J), location(L), I+J <= 2, I+J>0,
time(T), occ(cross(I,J,L), T), not possible(cross(I,J,L), T).
:- not_good.
% goal
:- not goal(length).
Diagnosis
Reference
• Chitta Baral, Sheila McIlraith, and Tran Cao Son. Formulating diagnostic problem
solving using an action language with narratives and sensing, Proceedings of the
International Conference on the Principles of Knowledge Representation and
Reasoning (KRR’00), 2000, pages 311-322.
Consider the following narrative
• 9am: John arrived at work and turned the light on. As usual, the light went on and
John started his daily work.
• 12 pm: John turned off the light and went to lunch.
• 1pm: John turned on the light when he got back from lunch. The light did not go on.
• 1:15pm: The company’s electrician arrived. He replaced the bulb and turned on the
light, the light did not go on. He then checked the fuses and replaced one which was
blown. The light is back.
and
(o1)
turn on occurs at s0
(o2) turn off occurs at s1
(o3) turn on between s2, s3
(o4) s0 precedes s1
(o5) s1 precedes s2
OBS =
(o6) s2 precedes s3
(o7) ¬light on at s0
(o8) light on at s1
(o9) ¬light on at s2
(o10) ¬light on at s3
Answer the question: when a system needs a diagnosis and what are the diagnoses.
• generating candidate diagnoses based on an incomplete history of events that have
occurred and observations that have been made.
• in the event of multiple candidate diagnoses, performing actions to enable observations
that will discriminate candidate diagnoses. The selection of a particular action is often
biased towards confirming the most likely diagnosis, or the one that is easiest to test.
• generating (possibly with conditional) plans, comprising both world-altering actions
and sensing actions, to discriminate candidate diagnoses.
• updating the space of diagnoses in the face of changes in the state of the world, and in
the face of new observations.
Elements of Π – Part 1
% define situations
sit(s0). sit(s1). sit(s2). sit(s3).
% actions
action(turn_on). action(turn_off). action(break(bulb)).
% fluents
fluent(light_on). fluent(ab(bulb)).
% literal
literal(F):- fluent(F). literal(neg(F)):- fluent(F).
contrary(F, neg(F)):- fluent(F). contrary(neg(F), F):- fluent(F).
Elements of Π – Part 2
Effects of actions (similar to what has been done in the part on reasoning about
actions/planning).
holds(light_on, T+1):- time(T),
occ(turn_on, T), holds(neg(ab(bulb)), T).
% inertial axiom
Elements of Π – Part 3
% fluent observations
% action observations
between(s0,s1,turn_on). between(s2,s3,turn_on).
Elements of Π – Part 4
Satisfying all the observations
% generating situation order each situation happens at a time moment
1 {happens(S, T): time(T) } 1 :- sit(S).
Elements of Π – Part 4
Satisfying all the observations
% fluent observations
holds(F, T):- time(T), literal(F), sit(S), at(F, S), happens(S, T).
Theoretical Issues
Implementation Issues
• Development of answer set solvers: Current answer set solvers have restrictions on
the type of atoms that can be considered (e.g., ground programs, no recursive
aggregates, etc.) Providing new answer set solvers that can deal with more expressive
programs is the goal of developers of new answer set solvers. Some attempts can be
found in
– T. Eiter, M. Fink, H. Tompits, S. Woltran: Strong and Uniform Equivalence in
Answer-Set Programming: Characterizations and Complexity Results for the
Non-Ground Case. AAAI 2005: 695-700.
– T. Eiter, W. Faber, M. Fink, G. Pfeifer, S. Woltran: Complexity of Answer Set
Checking and Bounded Predicate Arities for Non-ground Answer Set
Programming. Answer Set Programming 2003
– T. Dell’Armi, W. Faber, G. Ielpa, N. Leone, S. Perri, G. Pfeifer: System
Description: DLV with Aggregates. LPNMR 2004: 326-330
– I. Elkabani, E. Pontelli, T. C. Son: SmodelsA - A System for Computing Answer
Sets of Logic Programs with Aggregates. LPNMR 2005: 427-431
Tran Cao Son 125
Answer Set Programming. Current Issues