Algorithm Search⌘K

Techniquecore

Answer set programming

Also called ASP.

Solves a search problem by writing a logic program whose stable models are exactly its solutions and calling a solver to find them.

Answer set programming (ASP) is a declarative way to solve hard search problems. You write a logic program in which each stable model is one solution, and a solver computes the stable models. Niemelä (1999) and Marek and Truszczyński (1999) proposed it, and the name refers to the answer sets of Gelfond and Lifschitz. The method fits the problems in NP, because deciding whether a finite propositional normal program has a stable model is NP-complete.

In plain words

You do not write steps that find the answer. You describe what an answer looks like. First you say which choices are open, for example "each node gets one colour". Then you say which choices are wrong, for example "two neighbours must not get the same colour". A solver then finds the sets of choices that break no rule. Each such set is one answer. If you want more answers, the solver lists them, and if you want the best one, you add a cost and the solver finds the cheapest. The description is short and close to the problem statement, so it is easy to change. The work is in the solver, which uses the same search methods as modern SAT solvers.

When it applies

ASP fits a problem when these conditions hold.

  • The solutions are finite sets of facts over a finite domain, and a proposed solution can be checked by rules. This is the class NP for normal programs, and Σ2p\Sigma_2^p with disjunctive programs.
  • The constraints are logical and combinatorial, such as colourings, assignments, schedules, paths and plans of bounded length.
  • Recursive definitions are needed, such as reachability. Stable models give these their least fixed point meaning, which a plain SAT encoding must add by hand.
  • Default reasoning or closed world assumptions are part of the problem: an atom that no rule derives is false.

ASP does not fit well when the domain is large and numeric, such as real-valued optimization, or when the program has a huge ground instance, because the grounder builds all relevant rule instances before the search.

The template

A typical program has four parts over a set of facts that describe the instance (Lifschitz 2002; Gebser, Kaminski, Kaufmann and Schaub 2012).

  1. Instance. Facts such as v(1)v(1), e(1,2)e(1, 2).
  2. Generate. Choice rules state the open choices, for example 1 {c(X,r),c(X,g),c(X,b)} 1←v(X)1\ \{c(X, r), c(X, g), c(X, b)\}\ 1 \leftarrow v(X).
  3. Define. Normal rules derive auxiliary atoms, for example reachability r(Y)←r(X),in(X,Y)r(Y) \leftarrow r(X), \mathit{in}(X, Y).
  4. Test. Constraints ←B\leftarrow B reject the candidates in which BB holds, for example ←e(X,Y),c(X,C),c(Y,C)\leftarrow e(X, Y), c(X, C), c(Y, C).

An optional fifth part states a cost to minimize, as in the optimization statements of smodels (Simons, Niemelä and Soininen 2002) or the weak constraints of DLV (Leone et al. 2006).

The solver runs in two phases. The grounder replaces variables by constants and produces a propositional program. The solver searches for stable models of it, with smodels-style propagation or conflict-driven nogood learning. The input language of clingo writes the colouring program as follows.

v(1..4).
e(1,2). e(2,3). e(3,4). e(4,1). e(1,3).
1 { c(X,r); c(X,g); c(X,b) } 1 :- v(X).
:- e(X,Y), c(X,C), c(Y,C).

Classic examples

  • Graph colouring. For the graph with vertices 1,2,3,41, 2, 3, 4 and edges 12,23,34,41,1312, 23, 34, 41, 13, the program above has 66 answer sets, one for each proper 3-colouring. Vertices 11 and 33 take two different colours, and 22 and 44 both take the third colour. Without the edge 1313 the graph is a 4-cycle and the program has 1818 answer sets. See graph colouring.
  • Hamiltonian cycle. Choose a set of arcs with {in(X,Y)}←e(X,Y)\{\mathit{in}(X, Y)\} \leftarrow e(X, Y) over both directions of each edge, forbid two arcs out of or into one vertex, define r(Y)←in(1,Y)r(Y) \leftarrow \mathit{in}(1, Y) and r(Y)←r(X),in(X,Y)r(Y) \leftarrow r(X), \mathit{in}(X, Y), and add ←not r(X)\leftarrow \mathit{not}\ r(X) for each vertex. For the graph above the answer sets are the two directions of the cycle 1,2,3,41, 2, 3, 4. The recursive definition of rr is essential: the completion of the program also accepts the arcs 12,21,34,4312, 21, 34, 43, where r(3)r(3) and r(4)r(4) support each other, but no stable model contains them. See Hamiltonian cycle.
  • Planning. With a fixed horizon TT, choose actions at each step, define the state at the next step with effect rules and frame rules such as h(F,T+1)←h(F,T),not ab(F,T)h(F, T+1) \leftarrow h(F, T), \mathit{not}\ \mathit{ab}(F, T), and constrain the goal at the last step. Lifschitz (2002) describes encodings of this kind.
  • SAT. For each variable xx, the pair x←not xˉx \leftarrow \mathit{not}\ \bar{x}, xˉ←not x\bar{x} \leftarrow \mathit{not}\ x generates the two values, and each clause becomes a constraint. The stable models are the satisfying assignments, which is the reduction in the NP-completeness proof.

Variations

  • Optimization. Minimize statements and weak constraints rank the answer sets by cost. Solvers search for a model and then for a better one, until none exists.
  • Disjunctive ASP. Rules with disjunctive heads express problems at the second level of the polynomial hierarchy, such as 2-QBF; existence is Σ2p\Sigma_2^p-complete.
  • Aggregates and weights. Cardinality and weight constraints, and aggregates such as count and sum, express numeric conditions without auxiliary atoms (Simons, Niemelä and Soininen 2002).
  • Brave and cautious reasoning. Instead of listing models, ask whether an atom is in some answer set or in all of them. For normal programs these tasks are NP-complete and coNP-complete.
  • Abductive reasoning. Abductive logic programs declare some atoms as hypotheses, and with a choice rule over them an ASP solver computes explanations.

Pitfalls

  • The grounding is too large. A rule with kk variables over cc constants can have ckc^k instances. Reduce the number of variables per rule, and give each variable a domain atom in the positive body. The grounding page gives a family where the ground program grows exponentially.
  • Unsafe variables. Every variable of a rule must occur in a positive body atom. A rule such as p(X)←not q(X)p(X) \leftarrow \mathit{not}\ q(X) is rejected by grounders, because XX has no domain.
  • A choice with no test. A choice rule without constraints permits every subset. The program {a,b,c}←\{a, b, c\} \leftarrow has 88 answer sets, not 11.
  • Constraints do not derive atoms. A constraint ←not a\leftarrow \mathit{not}\ a removes the models without aa; it does not make aa true. If no rule can derive aa, the program has no answer set.
  • Positive loops are not support. Atoms that only support each other through a positive cycle are false in every stable model, as in the Hamiltonian cycle example. Encodings that rely on such loops for truth, and translations to SAT that use only the completion, give wrong results.
  • Symmetric answer sets. Swapping colour names gives another answer set of the colouring program, so the 66 answer sets above are 11 colouring up to the names. Counting or enumerating without symmetry breaking can take much longer than finding one model.
  • Closed world. An atom that no rule derives is false, so a missing fact in the instance changes the answer without any warning.

Exercises

  1. Write a program whose answer sets are the independent sets of size at least 22 of a graph given by facts v(X)v(X) and e(X,Y)e(X, Y). Hint: a choice rule over in(X)\mathit{in}(X), a constraint on edges, and a cardinality constraint in a constraint body.
  2. Show that the program {a}←\{a\} \leftarrow, b←ab \leftarrow a, ←not b\leftarrow \mathit{not}\ b has exactly one answer set and find it.
  3. In the Hamiltonian cycle program, remove the rules for rr and the constraints ←not r(X)\leftarrow \mathit{not}\ r(X). Describe the answer sets that are added, in terms of cycles in the graph.
  4. Encode the N-queens problem for N=4N = 4 with one choice rule and constraints, and count its answer sets by hand. Answer: 22.
  5. Give a program with variables whose ground instance over cc constants has at least c3c^3 rules, and rewrite it with the same answer sets and O(c2)O(c^2) rules.

References

  1. Ilkka Niemelä, Logic programs with stable model semantics as a constraint programming paradigm, Annals of Mathematics and Artificial Intelligence 25(3-4), 241 to 273 (1999), the proposal to use stable models as a constraint programming method for NP search problems
  2. Victor W. Marek and Mirosław Truszczyński, Stable models and an alternative logic programming paradigm, in Krzysztof R. Apt, Victor W. Marek, Mirosław Truszczyński and David S. Warren (eds.), The Logic Programming Paradigm: a 25-Year Perspective, Springer, 375 to 398 (1999), the proposal of stable logic programming, where solutions are models and not answers to queries
  3. Vladimir Lifschitz, Answer set programming and plan generation, Artificial Intelligence 138(1-2), 39 to 54 (2002), the description of answer set programming as generate and test, and its use for planning
  4. Martin Gebser, Roland Kaminski, Benjamin Kaufmann and Torsten Schaub, Answer Set Solving in Practice, Morgan and Claypool (2012), the modeling chapters, the grounding chapter (Chapter 4) and the solving chapters
  5. Patrik Simons, Ilkka Niemelä and Timo Soininen, Extending and implementing the stable model semantics, Artificial Intelligence 138(1-2), 181 to 234 (2002), the smodels system, choice rules, cardinality and weight constraints, and optimization statements
  6. Martin Gebser, Benjamin Kaufmann and Torsten Schaub, Conflict-driven answer set solving, from theory to practice, Artificial Intelligence 187-188, 52 to 89 (2012), the conflict-driven solver clasp
  7. 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) (2006), the DLV system for disjunctive programs and weak constraints

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • five exercises