Knowledge Representation for the Semantic Web Lecture 7: Answer Set - - PowerPoint PPT Presentation

knowledge representation for the semantic web lecture 7
SMART_READER_LITE
LIVE PREVIEW

Knowledge Representation for the Semantic Web Lecture 7: Answer Set - - PowerPoint PPT Presentation

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers Knowledge Representation for the Semantic Web Lecture 7: Answer Set Programming II Daria Stepanova partially based on slides by Thomas Eiter D5: Databases and


slide-1
SLIDE 1

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Knowledge Representation for the Semantic Web Lecture 7: Answer Set Programming II

Daria Stepanova

partially based on slides by Thomas Eiter

D5: Databases and Information Systems Max Planck Institute for Informatics

WS 2017/18

1 / 39

slide-2
SLIDE 2

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Unit Outline

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

2 / 39

slide-3
SLIDE 3

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Strong Negation

❼ Default negation “not a” means “a cannot be proved (derived)

using rules,” and that a is false by default (believed to be false).

❼ ❼ ❼ ❼

3 / 39

slide-4
SLIDE 4

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Strong Negation

❼ Default negation “not a” means “a cannot be proved (derived)

using rules,” and that a is false by default (believed to be false).

❼ Strong negation ¬a (also −a) means that a is (provably) false ❼ Both default and strong negation can be used in ASP ❼ ❼

3 / 39

slide-5
SLIDE 5

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Strong Negation

❼ Default negation “not a” means “a cannot be proved (derived)

using rules,” and that a is false by default (believed to be false).

❼ Strong negation ¬a (also −a) means that a is (provably) false ❼ Both default and strong negation can be used in ASP

“At a railroad crossing, cross the rails if no train approaches.” We may encode this scenario using one of the following two rules: walk ← at(X), crossing(X), not train approaches(X). (r1) walk ← at(X), crossing(X),−train approaches(X). (r2)

❼ r1 fires if there is no information that a train approaches. ❼ r2 fires if it is explictly known that no train approaches.

3 / 39

slide-6
SLIDE 6

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Constraints

❼ Constraints are rules with empty head which exclude invalid models.

← q1, . . . , qm, not r1, . . . , not rn. kills answer sets that

❼ contain q1, . . . , qm, and ❼ do not contain r1, . . . , rn.

4 / 39

slide-7
SLIDE 7

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Constraints

❼ Constraints are rules with empty head which exclude invalid models.

← q1, . . . , qm, not r1, . . . , not rn. kills answer sets that

❼ contain q1, . . . , qm, and ❼ do not contain r1, . . . , rn.

An equivalent version of the above rule is with a fresh predicate p: p ← q1, . . . , qm, not r1, . . . , not rn, not p.

4 / 39

slide-8
SLIDE 8

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Constraints

❼ Constraints are rules with empty head which exclude invalid models.

← q1, . . . , qm, not r1, . . . , not rn. kills answer sets that

❼ contain q1, . . . , qm, and ❼ do not contain r1, . . . , rn.

An equivalent version of the above rule is with a fresh predicate p: p ← q1, . . . , qm, not r1, . . . , not rn, not p. Example: adjacent nodes cannot be colored with the same color ⊥ ← edge(X , Y ), colored(X , Z), colored(Y , Z).

4 / 39

slide-9
SLIDE 9

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Disjunction

The use of disjunction is natural

❼ to express indefinite knowledge: female(X) ∨ male(X) ← person(X). broken(left arm, robot1) ∨ broken(right arm, robot1). ❼

5 / 39

slide-10
SLIDE 10

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Disjunction

The use of disjunction is natural

❼ to express indefinite knowledge: female(X) ∨ male(X) ← person(X). broken(left arm, robot1) ∨ broken(right arm, robot1). ❼ to express a “guess” and to create non-determinism.

  • k(C) ∨ −ok(C) ← component(C).

5 / 39

slide-11
SLIDE 11

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Minimality

❼ Semantics: disjunction is minimal (different from classical logic):

a ∨ b ∨ c.

❼ ❼

6 / 39

slide-12
SLIDE 12

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Minimality

❼ Semantics: disjunction is minimal (different from classical logic):

a ∨ b ∨ c. Minimal models: {a}, {b}, and {c}.

❼ ❼

6 / 39

slide-13
SLIDE 13

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Minimality

❼ Semantics: disjunction is minimal (different from classical logic):

a ∨ b ∨ c. Minimal models: {a}, {b}, and {c}.

❼ Actually subset minimal:

a ∨ b. a ∨ c.

6 / 39

slide-14
SLIDE 14

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Minimality

❼ Semantics: disjunction is minimal (different from classical logic):

a ∨ b ∨ c. Minimal models: {a}, {b}, and {c}.

❼ Actually subset minimal:

a ∨ b. a ∨ c. Minimal models: {a} and {b, c}.

6 / 39

slide-15
SLIDE 15

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Minimality

❼ Semantics: disjunction is minimal (different from classical logic):

a ∨ b ∨ c. Minimal models: {a}, {b}, and {c}.

❼ Actually subset minimal:

a ∨ b. a ∨ c. Minimal models: {a} and {b, c}. a ∨ b. a ← b.

6 / 39

slide-16
SLIDE 16

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Minimality

❼ Semantics: disjunction is minimal (different from classical logic):

a ∨ b ∨ c. Minimal models: {a}, {b}, and {c}.

❼ Actually subset minimal:

a ∨ b. a ∨ c. Minimal models: {a} and {b, c}. a ∨ b. a ← b. Models: {a} and {a, b}, but only {a} is minimal.

6 / 39

slide-17
SLIDE 17

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Minimality

❼ Semantics: disjunction is minimal (different from classical logic):

a ∨ b ∨ c. Minimal models: {a}, {b}, and {c}.

❼ Actually subset minimal:

a ∨ b. a ∨ c. Minimal models: {a} and {b, c}. a ∨ b. a ← b. Models: {a} and {a, b}, but only {a} is minimal.

❼ But minimality is not necessarily exclusive:

a ∨ b. b ∨ c. a ∨ c.

6 / 39

slide-18
SLIDE 18

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Minimality

❼ Semantics: disjunction is minimal (different from classical logic):

a ∨ b ∨ c. Minimal models: {a}, {b}, and {c}.

❼ Actually subset minimal:

a ∨ b. a ∨ c. Minimal models: {a} and {b, c}. a ∨ b. a ← b. Models: {a} and {a, b}, but only {a} is minimal.

❼ But minimality is not necessarily exclusive:

a ∨ b. b ∨ c. a ∨ c. Minimal models: {a, b}, {a, c}, and {b, c}.

6 / 39

slide-19
SLIDE 19

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Extended Logic Programs with Disjunctions

Extended Logic Programs An extended disjunctive logic program (EDLP) is a finite set of rules a1 ∨ · · · ∨ ak ← b1, . . . , bm, not c1, . . . , not cn (k, m, n ≥ 0) (1) where all ai, bj, cl are atoms or strongly negated atoms.

❼ ❼

❼ ❼ ❼

7 / 39

slide-20
SLIDE 20

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Extended Logic Programs with Disjunctions

Extended Logic Programs An extended disjunctive logic program (EDLP) is a finite set of rules a1 ∨ · · · ∨ ak ← b1, . . . , bm, not c1, . . . , not cn (k, m, n ≥ 0) (1) where all ai, bj, cl are atoms or strongly negated atoms. Semantics:

❼ Stable models (answer sets) of EDLPs are defined similarly as for

LPs, viewing −p as a new predicate.

❼ ❼ ❼

7 / 39

slide-21
SLIDE 21

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Extended Logic Programs with Disjunctions

Extended Logic Programs An extended disjunctive logic program (EDLP) is a finite set of rules a1 ∨ · · · ∨ ak ← b1, . . . , bm, not c1, . . . , not cn (k, m, n ≥ 0) (1) where all ai, bj, cl are atoms or strongly negated atoms. Semantics:

❼ Stable models (answer sets) of EDLPs are defined similarly as for

LPs, viewing −p as a new predicate.

❼ Differences:

❼ I must not contain atoms p(c1, . . . , cn), −p(c1, . . . , cn) (consistency) ❼ I is a model of ground rule (1), if either {b1, . . . , bm} I or

{a1, . . . , ak, c1, . . . , cn} ∩ I = ∅.

7 / 39

slide-22
SLIDE 22

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Extended Logic Programs with Disjunctions

Extended Logic Programs An extended disjunctive logic program (EDLP) is a finite set of rules a1 ∨ · · · ∨ ak ← b1, . . . , bm, not c1, . . . , not cn (k, m, n ≥ 0) (1) where all ai, bj, cl are atoms or strongly negated atoms. Semantics:

❼ Stable models (answer sets) of EDLPs are defined similarly as for

LPs, viewing −p as a new predicate.

❼ Differences:

❼ I must not contain atoms p(c1, . . . , cn), −p(c1, . . . , cn) (consistency) ❼ I is a model of ground rule (1), if either {b1, . . . , bm} I or

{a1, . . . , ak, c1, . . . , cn} ∩ I = ∅.

❼ Condition “M is the least model of P M” is replaced by “M is a

minimal model of P M” (P M may have multiple minimal models).

7 / 39

slide-23
SLIDE 23

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example

Let P be the following program: man(dilbert). single(X) ∨ husband(X) ← man(X).

❼ ❼

❼ ❼

8 / 39

slide-24
SLIDE 24

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example

Let P be the following program: man(dilbert). single(X) ∨ husband(X) ← man(X).

❼ As P is “not ”-free, grnd(P)M = grnd(P) for every M. ❼

❼ ❼

8 / 39

slide-25
SLIDE 25

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example

Let P be the following program: man(dilbert). single(X) ∨ husband(X) ← man(X).

❼ As P is “not ”-free, grnd(P)M = grnd(P) for every M. ❼ Answer sets:

❼ M1 = {man(dilbert), single(dilbert)}, and ❼ M2 = {man(dilbert), husband(dilbert)}.

8 / 39

slide-26
SLIDE 26

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

ASP Paradigm

9 / 39

slide-27
SLIDE 27

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

ASP Paradigm

General idea: stable models are solutions!

Reduce solving a problem instance I to computing stable models of a LP Problem Instance I ProgramP Encoding: Model(s) Solution(s) ASP Solver

  • 1. Encode I as a (non-monotonic) logic program P, such that

solutions of I are represented by models of P

  • 2. Compute some model M of P, using an ASP solver
  • 3. Extract a solution for I from M.

Variant: Compute multiple models (for multiple / all solutions)

10 / 39

slide-28
SLIDE 28

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

ASP Paradigm (cont’d)

Compared to SAT solving, ASP offers more features:

❼ transitive closure ❼ negation as failure ❼ predicates and variables ❼ ❼

11 / 39

slide-29
SLIDE 29

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

ASP Paradigm (cont’d)

Compared to SAT solving, ASP offers more features:

❼ transitive closure ❼ negation as failure ❼ predicates and variables

Generic problem solving by separating the

❼ specification of solutions (“logic” PS) ❼ concrete instance of the problem (data D)

Program P

PS

Encoding: Program P

D

Encoding:

ASP Solver Model(s) Solution(s) PS Spec. Problem D Data

11 / 39

slide-30
SLIDE 30

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

The “Guess and Check” Methodology

Important element of ASP: Guess and Check methodology (or Generate-and-Test [Lifschitz, 2002]).

  • 1. Guess: use unstratified negation or disjunctive heads to create

candidate solutions to a problem (program part G), and

  • 2. Check: use further rules and/or constraints to test candidate

solution if it is a proper solution for our problem (program part C).

❼ ❼

12 / 39

slide-31
SLIDE 31

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

The “Guess and Check” Methodology

Important element of ASP: Guess and Check methodology (or Generate-and-Test [Lifschitz, 2002]).

  • 1. Guess: use unstratified negation or disjunctive heads to create

candidate solutions to a problem (program part G), and

  • 2. Check: use further rules and/or constraints to test candidate

solution if it is a proper solution for our problem (program part C). From another perspective:

❼ G: defines the search space ❼ C: prunes illegal branches.

Further discussion in [Eiter et al., 2000], [Leone et al., 2006], [Janhunen and Niemel¨ a, 2016], [Gebser and Schaub, 2016] (+ additional component for computing optimal solutions).

12 / 39

slide-32
SLIDE 32

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: 3-Coloring

Problem specification PPS (compact encoding) g(X) ∨ r(X) ∨ b(X) ← node(X)

  • Guess

← b(X), b(Y ), edge(X, Y ) ← r(X), r(Y ), edge(X, Y ) ← g(X), g(Y ), edge(X, Y )    Check

13 / 39

slide-33
SLIDE 33

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: 3-Coloring

Problem specification PPS (compact encoding) g(X) ∨ r(X) ∨ b(X) ← node(X)

  • Guess

← b(X), b(Y ), edge(X, Y ) ← r(X), r(Y ), edge(X, Y ) ← g(X), g(Y ), edge(X, Y )    Check Data PD: Graph G = (V, E) PD = {node(v) | v ∈ V } ∪ {edge(v, w) | (v, w) ∈ E}. Correspondence: 3-colorings ⇋ models: v ∈ V is colored with c ∈ {r, g, b} iff c(v) is in the model of PPS ∪ PD.

13 / 39

slide-34
SLIDE 34

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: 3-Coloring (cont’d)

  • a
  • b
  • c

PD = {node(a), node(b), node(c), edge(a, b), edge(b, c), edge(a, c)}

14 / 39

slide-35
SLIDE 35

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: 3-Coloring (cont’d)

  • a
  • b
  • c

PD = {node(a), node(b), node(c), edge(a, b), edge(b, c), edge(a, c)}

  • a
  • b
  • c
  • a
  • b
  • c
  • a
  • b
  • c
  • a
  • b
  • c
  • a
  • b
  • c
  • a
  • b
  • c

14 / 39

slide-36
SLIDE 36

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Hamiltonian Path/Cycle

Input: a directed graph represented by node( ) and edge( , ) and a starting node start( ). Problem: find a path beginning at the starting node which visits all nodes of the graph exactly once.

15 / 39

slide-37
SLIDE 37

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Hamiltonian Path/Cycle

Input: a directed graph represented by node( ) and edge( , ) and a starting node start( ). Problem: find a path beginning at the starting node which visits all nodes of the graph exactly once. inPath(X, Y ) ∨ outPath(X, Y ) ← edge(X, Y ).

  • Guess

← inPath(X, Y ), inPath(X, Y1), Y = Y1. ← inPath(X, Y ), inPath(X1, Y ), X = X1. ← node(X), not reached(X).        Check reached(X) ← start(X). reached(X) ← reached(Y ), inPath(Y, X).    Auxiliary Predicate

15 / 39

slide-38
SLIDE 38

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Hamiltonian Path/Cycle

Input: a directed graph represented by node( ) and edge( , ) and a starting node start( ). Problem: find a path/cycle1 beginning at the starting node which visits all nodes of the graph exactly once. inPath(X, Y ) ∨ outPath(X, Y ) ← edge(X, Y ).

  • Guess

← inPath(X, Y ), inPath(X, Y1), Y = Y1. ← inPath(X, Y ), inPath(X1, Y ), X = X1. ← node(X), not reached(X). ← not start reached.        Check reached(X) ← start(X). reached(X) ← reached(Y ), inPath(Y, X). start reached ← start(Y ), inPath(X, Y ).    Auxiliary Predicate

1The encoding for the Hamilthonian cycle contains red lines along with green ones. 15 / 39

slide-39
SLIDE 39

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Hamiltonian Path/Cycle (cont’d)

  • a
  • b
  • c
  • d

PD = {node(a), node(b), node(c), node(d), edge(a, b), edge(a, c), edge(c, b), edge(b, c), edge(b, d), edge(d, c), edge(d, a), edge(a, d), start(a)}

16 / 39

slide-40
SLIDE 40

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Hamiltonian Path/Cycle (cont’d)

  • a
  • b
  • c
  • d

PD = {node(a), node(b), node(c), node(d), edge(a, b), edge(a, c), edge(c, b), edge(b, c), edge(b, d), edge(d, c), edge(d, a), edge(a, d), start(a)}

  • a
  • b
  • c
  • d
  • a
  • b
  • c
  • d
  • a
  • b
  • c
  • d
  • a
  • b
  • c
  • d

16 / 39

slide-41
SLIDE 41

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Course Assignment

Input: information about members and courses of a computer science (CS) dept cs Problem:

❼ assign courses to members of the CS dept ❼ teachers must like the assigned course ❼ each member must teach 1-2 courses

17 / 39

slide-42
SLIDE 42

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Course Assignment

Input: information about members and courses of a computer science (CS) dept cs Problem:

❼ assign courses to members of the CS dept ❼ teachers must like the assigned course ❼ each member must teach 1-2 courses

PD: member(sam, cs). member(bob, cs). member(tom, cs).

17 / 39

slide-43
SLIDE 43

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Course Assignment

Input: information about members and courses of a computer science (CS) dept cs Problem:

❼ assign courses to members of the CS dept ❼ teachers must like the assigned course ❼ each member must teach 1-2 courses

PD: member(sam, cs). member(bob, cs). member(tom, cs). course(java, cs). course(ai, cs). course(c, cs). course(logic, cs).

17 / 39

slide-44
SLIDE 44

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Course Assignment

Input: information about members and courses of a computer science (CS) dept cs Problem:

❼ assign courses to members of the CS dept ❼ teachers must like the assigned course ❼ each member must teach 1-2 courses

PD: member(sam, cs). member(bob, cs). member(tom, cs). course(java, cs). course(ai, cs). course(c, cs). course(logic, cs). likes(sam, java). likes(sam, c). likes(tom, ai). likes(bob, java). likes(bob, ai). likes(tom, logic).

17 / 39

slide-45
SLIDE 45

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Course Assignment (cont’d)

Problem specification PPS: % assign a course to a teacher who likes it, by default teach(X, Y ) ←member(X, cs), course(Y, cs), likes(X, Y ), not − teach(X, Y ). % determine when a course should not be assigned to a teacher −teach(X, Y ) ←member(X, cs), course(Y, cs), teach(X1, Y ), X1 = X.

18 / 39

slide-46
SLIDE 46

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Course Assignment (cont’d)

Problem specification PPS: % assign a course to a teacher who likes it, by default teach(X, Y ) ←member(X, cs), course(Y, cs), likes(X, Y ), not − teach(X, Y ). % determine when a course should not be assigned to a teacher −teach(X, Y ) ←member(X, cs), course(Y, cs), teach(X1, Y ), X1 = X. % check each cs member has some course has course(X) ←member(X, cs), teach(X, Y ). ←member(X, cs), not has course(X).

18 / 39

slide-47
SLIDE 47

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Course Assignment (cont’d)

Problem specification PPS: % assign a course to a teacher who likes it, by default teach(X, Y ) ←member(X, cs), course(Y, cs), likes(X, Y ), not − teach(X, Y ). % determine when a course should not be assigned to a teacher −teach(X, Y ) ←member(X, cs), course(Y, cs), teach(X1, Y ), X1 = X. % check each cs member has some course has course(X) ←member(X, cs), teach(X, Y ). ←member(X, cs), not has course(X). % check each cs member has at most 2 courses ←teach(X, Y1), teach(X, Y2), teach(X, Y3), Y1 = Y2, Y1 = Y3, Y2 = Y3.

18 / 39

slide-48
SLIDE 48

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Programming Techniques

19 / 39

slide-49
SLIDE 49

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Programming Techniqes

❼ With the “guess and check paradigm”, one may use different

techniques to solve particular tasks E.g.,

❼ choice of exactly one element from a set ❼ computing maximum / minimum values (use double negation) ❼ testing a property for all elements in a set (iteration over a set) ❼ testing a co-NP hard property (saturation) ❼ modularization

❼ We do not discuss here saturation (see [Eiter et al., 2009])

Note: extensions of ASP allow to test properties of a set / choose elements elegantly

20 / 39

slide-50
SLIDE 50

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Selecting One Element from a Set

❼ Task: given a set, defined by predicate p(X), select exactly one

element from it (if nonempty).

❼ More general variant: p(

X, Y ), where X = X1, . . . , Xn,

  • Y = Y1, . . . , Ym, select for each

X exactly one Y (if possible)

❼ Implicitly, already done in the above course assignment problem

21 / 39

slide-51
SLIDE 51

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Selecting One Element from a Set

❼ Task: given a set, defined by predicate p(X), select exactly one

element from it (if nonempty).

❼ More general variant: p(

X, Y ), where X = X1, . . . , Xn,

  • Y = Y1, . . . , Ym, select for each

X exactly one Y (if possible)

❼ Implicitly, already done in the above course assignment problem

Select one element from a set: Normal rule encoding sel( X, Y ) ← p( X, Y ), not − sel( X, Y ). −sel( X, Y ) ← p( X, Y ), sel( X, Z), Yi = Zi. i = 1, . . . , m where Z = Z1, . . . , Zm,

21 / 39

slide-52
SLIDE 52

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Selecting One Element from a Set (cont’d)

Example: Course assignment

❼ p(X, Y ) is member(Y, cs), course(X, cs), likes(Y, X) and

sel(X, Y ) is teach(Y, X).

❼ could define an auxiliary rule

p(X, Y ) ← member(Y, cs), course(X, cs), likes(Y, X) Select one element from a set: Disjunctive rule encoding sel( X, Y ) ← p( X, Y ), not − sel( X, Y ). −sel( X, Y ) ∨ −sel( X, Z) ← p( X, Y ), p( X, Z), Yi = Zi. i = 1, . . . , m

22 / 39

slide-53
SLIDE 53

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Selecting One Element from a Set (cont’d)

Example: Course assignment

❼ p(X, Y ) is member(Y, cs), course(X, cs), likes(Y, X) and

sel(X, Y ) is teach(Y, X).

❼ could define an auxiliary rule

p(X, Y ) ← member(Y, cs), course(X, cs), likes(Y, X) Select one element from a set: Disjunctive rule encoding sel( X, Y ) ← p( X, Y ), not − sel( X, Y ). −sel( X, Y ) ∨ −sel( X, Z) ← p( X, Y ), p( X, Z), Yi = Zi. i = 1, . . . , m In some answer set solvers, special syntax is available (see ASP-Core2): 1{sel( X, Y ) : p( X, Y )}1 ← p( X, Z)

22 / 39

slide-54
SLIDE 54

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Use of Double Negation

Defining a predicate p in terms of its negation −p Greatest Common Divisor — Euclid-style

% base case

gcd(X, X, X) ← int(X), X > 1.

% subtract smaller from larger number

gcd(D, X, Y ) ← X < Y, gcd(D, X, Y1), Y = Y1 + X. gcd(D, X, Y ) ← X > Y, gcd(D, X1, Y ), X = X1 + Y. This is not easy to come up with and needs more care in Prolog.

23 / 39

slide-55
SLIDE 55

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Use of Double Negation (cont’d)

Greatest Common Divisor — ASP-style

% Declare when D divides a number N.

divisor(D, N) ← int(D), int(N), int(M), N = D ∗ M.

% Declare common divisors

cd(T, N1, N2) ← divisor(T, N1), divisor(T, N2).

% Single out non-maximal common divisors T

−gcd(T, N1, N2) ← cd(T, N1, N2), cd(T1, N1, N2), T < T1.

% Apply double negation: take non non-maximal divisor

gcd(T, N1, N2) ← cd(T, N1, N2), not − gcd(T, N1, N2).

24 / 39

slide-56
SLIDE 56

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Iteration over a Set

❼ Testing a property, say Prop, for all elements of a set S without negation ❼ This may be needed in some contexts:

❼ in combination with other techniques, e.g., saturation (see [Eiter et al., 2009]), or ❼ if negation could lead to undesired behavior (e.g., cyclic negation). ❼ ❼ ❼

❼ ❼

25 / 39

slide-57
SLIDE 57

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Iteration over a Set

❼ Testing a property, say Prop, for all elements of a set S without negation ❼ This may be needed in some contexts:

❼ in combination with other techniques, e.g., saturation (see [Eiter et al., 2009]), or ❼ if negation could lead to undesired behavior (e.g., cyclic negation). ❼ walk through all elements of set S, from the first to the last element; ❼ check whether property Prop holds up to the current element y

⇔ holds for y and holds up to for x, where y is the successor of x;

❼ when Prop holds up to the last element, it holds for all elements of S

❼ ❼

25 / 39

slide-58
SLIDE 58

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Iteration over a Set

❼ Testing a property, say Prop, for all elements of a set S without negation ❼ This may be needed in some contexts:

❼ in combination with other techniques, e.g., saturation (see [Eiter et al., 2009]), or ❼ if negation could lead to undesired behavior (e.g., cyclic negation). ❼ walk through all elements of set S, from the first to the last element; ❼ check whether property Prop holds up to the current element y

⇔ holds for y and holds up to for x, where y is the successor of x;

❼ when Prop holds up to the last element, it holds for all elements of S

❼ Note: this is a form of finite induction ❼ Use an enumeration of S with predicates first( ), succ( , ), last( )

❼ Easy for static S, more involved for dynamically computed S

25 / 39

slide-59
SLIDE 59

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Hamiltonian Path 2/Reachability

❼ Variant: no use of negation in checking that all nodes are reached

(do not immediately kill stable model candidate): ← node(X), not reached(X).

❼ Check that all nodes of the graph are reached via the selected edges

(inPath(X, Y )) by iteration (S = nodes of the graph)

❼ Use

❼ all reached upto( ) ❼ all reached

❼ Supply in the input an enumeration of the nodes via

first( ), succ( , ), last( )

❼ Alternative: build the enumeration dynamically in the progam, using

e.g. string comparison.

26 / 39

slide-60
SLIDE 60

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Hamiltonian Path 2 (cont’d)

inPath(X, Y ) ∨ outPath(X, Y ) ← edge(X, Y ).

  • Guess

← inPath(X, Y ), inPath(X, Y1), Y = Y1. ← inPath(X, Y ), inPath(X1, Y ), X = X1.

  • Check

reached(X) ← start(X). reached(X) ← reached(Y ), inPath(Y, X).

  • Auxiliary Predicates

all reached upto(X) ← first(X), reached(X). all reached upto(X) ← all reached upto(Y ), succ(Y, X), reached(X). all reached ← last(X), all reached upto(X).        reached = nodes

27 / 39

slide-61
SLIDE 61

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Hamiltonian Path 2 (cont’d)

  • a
  • b
  • c
  • d

PD = {edge(a, b), edge(a, c), edge(c, b), edge(b, c), edge(b, d), edge(d, c), edge(d, a), edge(a, d), first(a), succ(a, b), succ(b, c), succ(c, d), last(d), start(a)}

28 / 39

slide-62
SLIDE 62

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Hamiltonian Path 2 (cont’d)

  • a
  • b
  • c
  • d

PD = {edge(a, b), edge(a, c), edge(c, b), edge(b, c), edge(b, d), edge(d, c), edge(d, a), edge(a, d), first(a), succ(a, b), succ(b, c), succ(c, d), last(d), start(a)} all reached

  • a
  • b
  • c
  • d

all reached

  • a
  • b
  • c
  • d

all reached

  • a
  • b
  • c
  • d

all reached

  • a
  • b
  • c
  • d

28 / 39

slide-63
SLIDE 63

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Example: Hamiltonian Path 2 (cont’d)

Some path guesses not reaching all nodes from a: all reached upto(c)

  • a
  • b
  • c
  • d

all reached upto(c)

  • a
  • b
  • c
  • d

all reached upto(a)

  • a
  • b
  • c
  • d

all reached upto(b)

  • a
  • b
  • c
  • d

all reached upto(a)

  • a
  • b
  • c
  • d

all reached upto(c)

  • a
  • b
  • c
  • d

29 / 39

slide-64
SLIDE 64

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Modularization

❼ Do not reinvent the wheel: reuse solutions to basic problems. ❼ Program Splitting: syntactic means to

❼ develop larger programs by combining parts, and to ❼ compute answer sets layer by layer (by composition).

❼ ❼

30 / 39

slide-65
SLIDE 65

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Modularization

❼ Do not reinvent the wheel: reuse solutions to basic problems. ❼ Program Splitting: syntactic means to

❼ develop larger programs by combining parts, and to ❼ compute answer sets layer by layer (by composition).

Program splitting Suppose (ground) P can be split into P = P1 ∪ P2, such that every atom A that occurs in P1 (“bottom part”) occurs in P2 (“top part”) only in rule bodies (i.e., A is “defined” entirely in P1). Then AS(P) =

M∈AS(P1) AS(P2 ∪ M).

AS(P) = set of answer sets of P

❼ Examples: ”stratified” programs, like GCD; guess&check ❼ Versions of ASP with modules, macros etc. are available

30 / 39

slide-66
SLIDE 66

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Answer Set Solvers

31 / 39

slide-67
SLIDE 67

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Answer Set Solvers

(see also http://en.wikipedia.org/wiki/Answer_set_programming)

asperix www.info.univ-angers.fr/pub/claire/asperix/ assat assat.cs.ust.hk/ clasp 1 potassco.sourceforge.net/#clasp/ cmodels www.cs.utexas.edu/users/tag/cmodels/ dlv 2 www.dbai.tuwien.ac.at/proj/dlv/ asptools research.ics.aalto.fi/software/asp/ me-asp www.mat.unical.it/ricca/me-asp/

  • miga

www.kr.tuwien.ac.at/research/systems/omiga smodels www.tcs.hut.fi/Software/smodels/ wasp www.mat.unical.it/ricca/wasp/ xasp xsb.sourceforge.net/, distributed with XSB ....

1 + claspD, clingo, clingcon etc. (http://potassco.sourceforge.net/) 2 + dlvhex, dlvDB, dlt, dlv-complex, onto-dlv etc.

❼ ❼ ❼ ❼

32 / 39

slide-68
SLIDE 68

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Answer Set Solvers

(see also http://en.wikipedia.org/wiki/Answer_set_programming)

asperix www.info.univ-angers.fr/pub/claire/asperix/ assat assat.cs.ust.hk/ clasp 1 potassco.sourceforge.net/#clasp/ cmodels www.cs.utexas.edu/users/tag/cmodels/ dlv 2 www.dbai.tuwien.ac.at/proj/dlv/ asptools research.ics.aalto.fi/software/asp/ me-asp www.mat.unical.it/ricca/me-asp/

  • miga

www.kr.tuwien.ac.at/research/systems/omiga smodels www.tcs.hut.fi/Software/smodels/ wasp www.mat.unical.it/ricca/wasp/ xasp xsb.sourceforge.net/, distributed with XSB ....

1 + claspD, clingo, clingcon etc. (http://potassco.sourceforge.net/) 2 + dlvhex, dlvDB, dlt, dlv-complex, onto-dlv etc.

❼ Many ASP solvers are available (mostly function-free programs) ❼ clasp was first ASP solver competitive to top SAT solvers ❼ another state-of-the-art solver is dlv ❼

32 / 39

slide-69
SLIDE 69

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Evaluation Approaches

❼ Different methods and evaluation approaches:

❼ resolution-based ❼ forward chaining ❼ lazy grounding AsperiX, Omiga ❼ translation-based (see below) ❼ meta-interpretation

33 / 39

slide-70
SLIDE 70

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Evaluation Approaches

❼ Different methods and evaluation approaches:

❼ resolution-based ❼ forward chaining ❼ lazy grounding AsperiX, Omiga ❼ translation-based (see below) ❼ meta-interpretation

Predominant solver approach intelligent grounding + model search (solving)

33 / 39

slide-71
SLIDE 71

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

2-Level Architecture

  • 1. Intelligent grounding

Given a program P, generate a (subset) of grnd(P) that has the same models

❼ ❼ ❼ ❼ ❼

34 / 39

slide-72
SLIDE 72

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

2-Level Architecture

  • 1. Intelligent grounding

Given a program P, generate a (subset) of grnd(P) that has the same models

  • 2. Solving: Model search

More complicated than in SAT/CSP Solving:

❼ candidate generation (classical model) ❼ model checking (stability, foundedness!) ❼ ❼ ❼

34 / 39

slide-73
SLIDE 73

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

2-Level Architecture

  • 1. Intelligent grounding

Given a program P, generate a (subset) of grnd(P) that has the same models

  • 2. Solving: Model search

More complicated than in SAT/CSP Solving:

❼ candidate generation (classical model) ❼ model checking (stability, foundedness!) ❼ for SAT, model checking is feasible in logarithmic space ❼ for normal propositional programs, model checking is PTime-complete ❼ for disjunctive propositional programs, model checking is co-NP-complete

34 / 39

slide-74
SLIDE 74

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Intelligent Grounding

❼ Grounding is a hard problem

bit(0). bit(1). (2) p(X1, ..., Xn) ← bit(X1), ..., bit(Xn). (3)

❼ ❼ ❼

❼ ❼

❼ ❼ ❼

35 / 39

slide-75
SLIDE 75

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Intelligent Grounding

❼ Grounding is a hard problem

bit(0). bit(1). (2) p(X1, ..., Xn) ← bit(X1), ..., bit(Xn). (3)

❼ In the worst case, grounding time is exponential in the input size ❼ ❼

❼ ❼

❼ ❼ ❼

35 / 39

slide-76
SLIDE 76

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Intelligent Grounding

❼ Grounding is a hard problem

bit(0). bit(1). (2) p(X1, ..., Xn) ← bit(X1), ..., bit(Xn). (3)

❼ In the worst case, grounding time is exponential in the input size ❼ Getting the “right” rules is difficult, already for positive programs ❼

❼ ❼

❼ ❼ ❼

35 / 39

slide-77
SLIDE 77

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Intelligent Grounding

❼ Grounding is a hard problem

bit(0). bit(1). (2) p(X1, ..., Xn) ← bit(X1), ..., bit(Xn). (3)

❼ In the worst case, grounding time is exponential in the input size ❼ Getting the “right” rules is difficult, already for positive programs ❼ Efficient grounding is at the heart of current systems

❼ dlv’s grounder (built-in); ❼ lparse (smodels), gringo (clasp)

❼ ❼ ❼

35 / 39

slide-78
SLIDE 78

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Intelligent Grounding

❼ Grounding is a hard problem

bit(0). bit(1). (2) p(X1, ..., Xn) ← bit(X1), ..., bit(Xn). (3)

❼ In the worst case, grounding time is exponential in the input size ❼ Getting the “right” rules is difficult, already for positive programs ❼ Efficient grounding is at the heart of current systems

❼ dlv’s grounder (built-in); ❼ lparse (smodels), gringo (clasp)

❼ Special techniques used:

❼ ❼ ❼

35 / 39

slide-79
SLIDE 79

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Intelligent Grounding

❼ Grounding is a hard problem

bit(0). bit(1). (2) p(X1, ..., Xn) ← bit(X1), ..., bit(Xn). (3)

❼ In the worst case, grounding time is exponential in the input size ❼ Getting the “right” rules is difficult, already for positive programs ❼ Efficient grounding is at the heart of current systems

❼ dlv’s grounder (built-in); ❼ lparse (smodels), gringo (clasp)

❼ Special techniques used:

❼ “safe rules” (dlv): each variable in a rule occurs in the body in an

unnegated atom with non-built-in predicate (exception: X = c)

❼ ❼

35 / 39

slide-80
SLIDE 80

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Intelligent Grounding

❼ Grounding is a hard problem

bit(0). bit(1). (2) p(X1, ..., Xn) ← bit(X1), ..., bit(Xn). (3)

❼ In the worst case, grounding time is exponential in the input size ❼ Getting the “right” rules is difficult, already for positive programs ❼ Efficient grounding is at the heart of current systems

❼ dlv’s grounder (built-in); ❼ lparse (smodels), gringo (clasp)

❼ Special techniques used:

❼ “safe rules” (dlv): each variable in a rule occurs in the body in an

unnegated atom with non-built-in predicate (exception: X = c)

❼ domain-restriction (smodels) ❼

35 / 39

slide-81
SLIDE 81

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Intelligent Grounding

❼ Grounding is a hard problem

bit(0). bit(1). (2) p(X1, ..., Xn) ← bit(X1), ..., bit(Xn). (3)

❼ In the worst case, grounding time is exponential in the input size ❼ Getting the “right” rules is difficult, already for positive programs ❼ Efficient grounding is at the heart of current systems

❼ dlv’s grounder (built-in); ❼ lparse (smodels), gringo (clasp)

❼ Special techniques used:

❼ “safe rules” (dlv): each variable in a rule occurs in the body in an

unnegated atom with non-built-in predicate (exception: X = c)

❼ domain-restriction (smodels) ❼ deductive db methods: semi-naive evaluation, magic sets, . . .

35 / 39

slide-82
SLIDE 82

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Solving: Model Search

❼ Applied to ground programs. ❼

❼ ❼ ❼ ❼ ❼ ❼

36 / 39

slide-83
SLIDE 83

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Solving: Model Search

❼ Applied to ground programs. ❼ Early solvers (e.g. smodels, dlv): native methods

❼ inspired by Davis-Putnam-Logemann Loveland (DPLL) for SAT ❼ 3 basic operations: decision, propagate, backtrack ❼ special propagation for ASP, e.g., ❼ dlv: must-be-true propagation (supportedness), . . .

b:− not a. a:− not b. c:− not c, a. not a a c not c not b b not b c not c b

❼ ❼

36 / 39

slide-84
SLIDE 84

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Solving: Model Search

❼ Applied to ground programs. ❼ Early solvers (e.g. smodels, dlv): native methods

❼ inspired by Davis-Putnam-Logemann Loveland (DPLL) for SAT ❼ 3 basic operations: decision, propagate, backtrack ❼ special propagation for ASP, e.g., ❼ dlv: must-be-true propagation (supportedness), . . .

b:− not a. a:− not b. c:− not c, a. not a a c not c not b b not b c not c b

❼ important: heuristics (which atom/rule is next?) ❼ chronological backtrack-search improved by backjumping and look-back heuristics

36 / 39

slide-85
SLIDE 85

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Solving: Model Search

❼ Applied to ground programs. ❼ Early solvers (e.g. smodels, dlv): native methods

❼ inspired by Davis-Putnam-Logemann Loveland (DPLL) for SAT ❼ 3 basic operations: decision, propagate, backtrack ❼ special propagation for ASP, e.g., ❼ dlv: must-be-true propagation (supportedness), . . .

b:− not a. a:− not b. c:− not c, a. not a a c not c not b b not b c not c b

❼ important: heuristics (which atom/rule is next?) ❼ chronological backtrack-search improved by backjumping and look-back heuristics

❼ Stability check: unfounded sets, reductions to UNSAT (disj. ASP)

36 / 39

slide-86
SLIDE 86

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

ASP Solving Approaches

❼ Predominant to date: modern SAT techniques (clause driven

conflict learning, CDCL)

❼ Export of techniques from ASP to SAT (optimization issues) ❼ Genuine conflict-driven ASP solvers

❼ clasp, wasp

❼ Translation based solving: to

❼ SAT: assat, cmodels, lp2sat (multiple SAT solver calls) ❼ SAT modulo theories (SMT) aspmt ❼ Mixed Integer Programming (CPLEX backend)

❼ Cross translation: intermediate format to ease cross translation

❼ SAT modulo acyclicity ❼ interconnect graph based constraints with clausal constraints ❼ can postpone choice of the target format to last step solver).

❼ Portfolio solvers

❼ claspfolio: combines variants of clasp ❼ ME-ASP: multi-engine portfolio ASP solver

37 / 39

slide-87
SLIDE 87

More about Logic Programs ASP Paradigm Programming Techniques Answer Set Solvers

Summary

  • 1. More about logic programs

❼ Strong negation, disjunction

  • 2. The answer set programming paradigm

❼ The guess and check methodology

  • 3. Programming techniques

❼ Element selection ❼ Use of double negation ❼ Iteration over a set ❼ Modularization

  • 4. Answer set solvers

❼ Intelligent grounding and solving

38 / 39

slide-88
SLIDE 88

References I

  • T. Eiter, W. Faber, N. Leone, and G. Pfeifer.

Declarative problem-solving using the DLV system. In J. Minker, editor, Logic-Based Artificial Intelligence, pages 79–103. Kluwer Academic Publishers, 2000. Thomas Eiter, Giovambattista Ianni, and Thomas Krennwallner. Answer set programming: A primer. In Sergio Tessaris and Enrico Franconi et al., editors, Reasoning Web, Fifth International Summer School 2008, Bressanone Italy, August 30–September 4, 2009, Tutorial Lectures, number 5689 in LNCS, pages 40–110. Springer, 2009. Martin Gebser and Torsten Schaub. Modeling and language extensions. AI Magazine, 37(3):33–44, 2016.

slide-89
SLIDE 89

References II

Tomi Janhunen and Ilkka Niemel¨ a. The answer set programming paradigm. AI Magazine, 37(3):13–24, 2016. Nicola Leone, Gerald Pfeifer, Wolfgang Faber, Thomas Eiter, Georg Gottlob, Simona Perri, and Francesco Scarcello. The DLV System for Knowledge Representation and Reasoning. ACM Transactions on Computational Logic, 7(3):499–562, July 2006. Vladimir Lifschitz. Answer Set Programming and Plan Generation. Artificial Intelligence, 138:39–54, 2002.