Skip to main content

A Tutorial on Hybrid Answer Set Solving with clingo

  • Chapter
  • First Online:
Reasoning Web. Semantic Interoperability on the Web (Reasoning Web 2017)

Part of the book series: Lecture Notes in Computer Science ((LNISA,volume 10370))

Included in the following conference series:

Abstract

Answer Set Programming (ASP) has become an established paradigm for Knowledge Representation and Reasoning, in particular, when it comes to solving knowledge-intense combinatorial (optimization) problems. ASP’s unique pairing of a simple yet rich modeling language with highly performant solving technology has led to an increasing interest in ASP in academia as well as industry. To further boost this development and make ASP fit for real world applications it is indispensable to equip it with means for an easy integration into software environments and for adding complementary forms of reasoning.

In this tutorial, we describe how both issues are addressed in the ASP system clingo. At first, we outline features of clingo’s application programming interface (API) that are essential for multi-shot ASP solving, a technique for dealing with continuously changing logic programs. This is illustrated by realizing two exemplary reasoning modes, namely branch-and-bound-based optimization and incremental ASP solving. We then switch to the design of the API for integrating complementary forms of reasoning and detail this in an extensive case study dealing with the integration of difference constraints. We show how the syntax of these constraints is added to the modeling language and seamlessly merged into the grounding process. We then develop in detail a corresponding theory propagator for difference constraints and present how it is integrated into clingo’s solving process.

T. Schaub—Affiliated with the School of Computing Science at Simon Fraser University, Burnaby, Canada, and the Institute for Integrated and Intelligent Systems at Griffith University, Brisbane, Australia.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

eBook
USD 16.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 16.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    Alternatively, this could also be done before instantiation.

  2. 2.

    More elaborate forms of aggregates can be obtained by explicitly using function (eg. #count) and relation symbols (eg. ).

  3. 3.

    The ground routine takes a list of pairs as argument. Each such pair consists of a subprogram name (e.g. base or acid) and a list of actual parameters (e.g. [] or [42]).

  4. 4.

    The planning horizon is the maximum number of steps a planner takes into account when searching for a plan.

  5. 5.

    In order to construct atoms, symbolic terms, or function terms, respectively, the clingo API function Function has to be used. Hence, the expression p(a,b) actually stands for Function("p", [Function("a"), Function("b")]).

  6. 6.

    In our case, \(n=2\) would be sufficient.

  7. 7.

    Alternatively, this can be invoked by #include \(\texttt {<}\) incmode >

  8. 8.

    The Python as well as a Lua implementation can be found in examples/ clingo/iclingo in the clingo distribution.

  9. 9.

    These names have no general, predefined meaning; their meaning emerges from their usage in the associated script (see below).

  10. 10.

    For simplicity, we consider normalized difference constraints rather than general ones of form \(x_1-x_2\circ k\).

  11. 11.

    This distinction is analogous to that between head and input atoms, defined via rules or #external directives [20], respectively.

  12. 12.

    See [18] for different ways of associating theories with nogoods.

  13. 13.

    Unit propagation extends an assignment with literals complementary to the ones missing in unit nogoods.

  14. 14.

    For brevity, we below drop the qualification Propagator and use its function names unqualified.

  15. 15.

    Program literals are also used in the aspif format (see Appendix A).

  16. 16.

    Note that clasp’s preprocessor might associate a positive or even negative solver literal with multiple atoms.

  17. 17.

    Depending on the configuration of clasp, threads can communicate with each other. For example, some of the recorded nogoods can be shared. This is transparent from the perspective of theory propagators.

  18. 18.

    More general settings are discussed in [26] and made available at https://potassco.org/clingo.

  19. 19.

    The assignment maintains the decision level; it is incremented for each decision made and decremented for each decision undone while backjumping; initially, the decision level is zero.

  20. 20.

    A solver literal might be associated with multiple edges (see Footnote 16).

  21. 21.

    In one solving step, the clingo API guarantees that a (grounded) theory atom is associated with exactly one solver literal. Theory grounded in later solving steps can be associated with fresh solver literals though.

  22. 22.

    Here we assume that the user supplied a valid theory atom. A propagator for production should check validity and provide proper error messages.

  23. 23.

    Python’s setdefault function is used to update the mappings. Depending on whether the given key already appears in the dictionary, the function either retrieves the associated value or inserts and returns the second argument.

  24. 24.

    The optional arguments tag and lock of add_nogood can be used to control the scope and lifetime of recorded nogoods. Furthermore, if a propagator adds nogoods that are not necessarily violated, function control.propagate can be invoked to trigger unit propagation.

  25. 25.

    Note that in each solution all tasks are executed as early as possible. This is no coincidence and actually guaranteed by the algorithm implemented in the Graph class.

  26. 26.

    ASP Intermediate Format.

  27. 27.

    http://www.tcs.hut.fi/Software/smodels.

References

  1. Balduccini, M., Lierler, Y.: Constraint answer set solver EZCSP and why integration schemas matter. CoRR, abs/1702.04047 (2017)

    Google Scholar 

  2. Banbara, M., Gebser, M., Inoue, K., Ostrowski, M., Peano, A., Schaub, T., Soh, T., Tamura, N., Weise, M.: aspartame: solving constraint satisfaction problems with answer set programming. In: Calimeri, F., Ianni, G., Truszczynski, M. (eds.) LPNMR 2015. LNCS (LNAI), vol. 9345, pp. 112–126. Springer, Cham (2015). doi:10.1007/978-3-319-23264-5_10

    Chapter  Google Scholar 

  3. Banbara, M., Kaufmann, B., Ostrowski, M., Schaub, T.: Clingcon: the next generation. Theory and Practice of Logic Programming (2017, To appear)

    Google Scholar 

  4. Baral, C.: Knowledge Representation, Reasoning and Declarative Problem Solving. Cambridge University Press, New York (2003)

    Book  MATH  Google Scholar 

  5. Barrett, C., Sebastiani, R., Seshia, S., Tinelli, C.: Satisfiability modulo theories. In: Biere et al. [7], Chap. 26, pp. 825–885

    Google Scholar 

  6. Bartholomew, M., Lee, J.: System aspmt2smt: computing ASPMT theories by SMT solvers. In: Fermé, E., Leite, J. (eds.) JELIA 2014. LNCS (LNAI), vol. 8761, pp. 529–542. Springer, Cham (2014). doi:10.1007/978-3-319-11558-0_37

    Google Scholar 

  7. Biere, A., Heule, M., van Maaren, H., Walsh, T. (eds.) Handbook of Satisfiability. Frontiers in Artificial Intelligence and Applications, vol. 185. IOS Press (2009)

    Google Scholar 

  8. Cabalar, P., Kaminski, R., Ostrowski, M., Schaub, T.: An ASP semantics for default reasoning with constraints. In: Kambhampati, R. (ed.) Proceedings of the Twenty-Fifth International Joint Conference on Artificial Intelligence (IJCAI 2016), pp. 1015–1021. IJCAI/AAAI Press (2016)

    Google Scholar 

  9. Carro, M., King, A. (eds.): Technical Communications of the Thirty-second International Conference on Logic Programming (ICLP 2016). Open Access Series in Informatics (OASIcs), vol. 52 (2016)

    Google Scholar 

  10. Cotton, S., Maler, O.: Fast and flexible difference constraint propagation for DPLL(T). In: Biere, A., Gomes, C.P. (eds.) SAT 2006. LNCS, vol. 4121, pp. 170–183. Springer, Heidelberg (2006). doi:10.1007/11814948_19

    Chapter  Google Scholar 

  11. Crawford, J., Baker, A.: Experimental results on the application of satisfiability algorithms to scheduling problems. In: Hayes-Roth, B., Korf, R. (eds.) Proceedings of the Twelfth National Conference on Artificial Intelligence (AAAI 1994), pp. 1092–1097. AAAI Press (1994)

    Google Scholar 

  12. Dodaro, C., Ricca, F., Schüler, P.: External propagators in wasp: preliminary report. In: Proceedings of the Twenty-Third International Workshop on Experimental Evaluation of Algorithms for Solving Problems with Combinatorial Explosion (RCRA 2016), vol. 1745, pp. 1–9. CEUR Workshop Proceedings (2016)

    Google Scholar 

  13. Drescher, C., Walsh, T.: A translational approach to constraint answer set solving. Theory Pract. Logic Program. 10(4–6), 465–480 (2010)

    Article  MathSciNet  MATH  Google Scholar 

  14. Eiter, T., Ianni, G., Krennwallner, T.: Answer set programming: a primer. In: Tessaris, S., Franconi, E., Eiter, T., Gutierrez, C., Handschuh, S., Rousset, M.-C., Schmidt, R.A. (eds.) Reasoning Web 2009. LNCS, vol. 5689, pp. 40–110. Springer, Heidelberg (2009). doi:10.1007/978-3-642-03754-2_2

    Chapter  Google Scholar 

  15. Gebser, M., Grote, T., Kaminski, R., Schaub, T.: Reactive answer set programming. In: Delgrande, J.P., Faber, W. (eds.) LPNMR 2011. LNCS (LNAI), vol. 6645, pp. 54–66. Springer, Heidelberg (2011). doi:10.1007/978-3-642-20895-9_7

    Chapter  Google Scholar 

  16. Gebser, M., Kaminski, R., Kaufmann, B., Lindauer, M., Ostrowski, M., Romero, J., Schaub, T., Thiele, S.: Potassco User Guide, 2nd edn. University of Potsdam (2015)

    Google Scholar 

  17. Gebser, M., Kaminski, R., Kaufmann, B., Ostrowski, M., Schaub, T., Thiele, S.: Engineering an incremental ASP solver. In: Garcia de la Banda, M., Pontelli, E. (eds.) ICLP 2008. LNCS, vol. 5366, pp. 190–205. Springer, Heidelberg (2008). doi:10.1007/978-3-540-89982-2_23

    Chapter  Google Scholar 

  18. Gebser, M., Kaminski, R., Kaufmann, B., Ostrowski, M., Schaub, T., Wanko, P.: Theory solving made easy with clingo 5. In: Carro and King [9], pp. 2:1–2:15

    Google Scholar 

  19. Gebser, M., Kaminski, R., Kaufmann, B., Schaub, T.: Answer Set Solving in Practice, Synthesis Lectures on Artificial Intelligence and Machine Learning. Morgan and Claypool Publishers (2012)

    Google Scholar 

  20. Gebser, M., Kaminski, R., Kaufmann, B., Schaub, T.: Clingo = ASP + control: Preliminary report. In: Leuschel, M., Schrijvers, T. (eds.) Technical Communications of the Thirtieth International Conference on Logic Programming (ICLP 2014). Theory and Practice of Logic Programming, arXiv:1405.3694v1, Online Supplement (2014). http://arxiv.org/abs/1405.3694v1

  21. Gebser, M., Kaminski, R., Obermeier, P., Schaub, T.: Ricochet robots reloaded: a case-study in multi-shot ASP solving. In: Eiter, T., Strass, H., Truszczyński, M., Woltran, S. (eds.) Advances in Knowledge Representation, Logic Programming, and Abstract Argumentation. LNCS (LNAI), vol. 9060, pp. 17–32. Springer, Cham (2015). doi:10.1007/978-3-319-14726-0_2

    Chapter  Google Scholar 

  22. Gebser, M., Kaufmann, B., Schaub, T.: Conflict-driven answer set solving: from theory to practice. Artif. Intell. 187–188, 52–89 (2012)

    Article  MathSciNet  MATH  Google Scholar 

  23. Gelfond, M., Kahl, Y.: Knowledge Representation, Reasoning, and the Design of Intelligent Agents: The Answer-Set Programming Approach. Cambridge University Press (2014)

    Google Scholar 

  24. Gelfond, M., Lifschitz, V.: The stable model semantics for logic programming. In: Kowalski, R., Bowen, K. (eds.) Proceedings of the Fifth International Conference and Symposium of Logic Programming (ICLP 1988), pp. 1070–1080. MIT Press (1988)

    Google Scholar 

  25. Gelfond, M., Lifschitz, V.: Classical negation in logic programs and disjunctive databases. New Gener. Comput. 9, 365–385 (1991)

    Article  MATH  Google Scholar 

  26. Janhunen, T., Kaminski, R., Ostrowski, M., Schaub, T., Schellhorn, S., Wanko, P.: Clingo goes linear constraints over reals and integers: A preliminary study (2017, In preparation)

    Google Scholar 

  27. Janhunen, T., Liu, G., Niemelä, I.: Tight integration of non-ground answer set programming and satisfiability modulo theories. In: Cabalar, P., Mitchell, D., Pearce, D., Ternovska, E. (eds.) Proceedings of the First Workshop on Grounding and Transformation for Theories with Variables (GTTV 2011), pp. 1–13 (2011)

    Google Scholar 

  28. Li, C., Manyà, F.: MaxSAT. In: Biere et al. [7], Chap. 19, pp. 613–631

    Google Scholar 

  29. Lierler, Y., Susman, B.: SMT-based constraint answer set solver EZSMT (system description). In: Carro and King [9], pp. 1:1–1:15

    Google Scholar 

  30. Lifschitz, V.: Introduction to answer set programming. Unpublished draft (2004)

    Google Scholar 

  31. Liu, G., Janhunen, T., Niemelä, I.: Answer set programming via mixed integer programming. In: Brewka, G., Eiter, T., McIlraith, S. (eds.) Proceedings of the Thirteenth International Conference on Principles of Knowledge Representation and Reasoning (KR 2012), pp. 32–42. AAAI Press (2012)

    Google Scholar 

  32. Marques-Silva, J., Lynce, I., Malik, S.: Conflict-driven clause learning SAT solvers. In: Biere et al. [7], Chap. 4, pp. 131–153

    Google Scholar 

  33. Mellarkod, V., Gelfond, M., Zhang, Y.: Integrating answer set programming and constraint logic programming. Ann. Math. Artif. Intell. 53(1–4), 251–287 (2008)

    Article  MathSciNet  MATH  Google Scholar 

  34. Nieuwenhuis, R., Oliveras, A., Tinelli, C.: Solving SAT and SAT modulo theories: from an abstract Davis-Putnam-Logemann-Loveland procedure to DPLL(T). J. ACM 53(6), 937–977 (2006)

    Article  MathSciNet  MATH  Google Scholar 

  35. Oikarinen, E., Janhunen, T.: Modular equivalence for normal logic programs. In: Brewka, G., Coradeschi, S., Perini, A., Traverso, P. (eds.) Proceedings of the Seventeenth European Conference on Artificial Intelligence (ECAI 2006), pp. 412–416. IOS Press (2006)

    Google Scholar 

  36. Redl, C.: The dlvhex system for knowledge representation: recent advances (system description). Theory Pract. Logic Program. 16(5–6), 866–883 (2016)

    Article  MathSciNet  Google Scholar 

  37. Roussel, O., Manquinho, V.: Pseudo-Boolean and cardinality constraints. In: Biere et al. [7], Chap. 22, pp. 695–733

    Google Scholar 

  38. Simons, P., Niemel, I., Soininen, T.: Extending and implementing the stable model semantics. Artif. Intell. 138(1–2), 181–234 (2002)

    Article  MathSciNet  MATH  Google Scholar 

  39. Syrjänen, T.: Lparse 1.0 user’s manual (2001)

    Google Scholar 

  40. Tamura, N., Taga, A., Kitagawa, S., Banbara, M.: Compiling finite linear CSP into SAT. Constraints 14(2), 254–272 (2009)

    Article  MathSciNet  MATH  Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Torsten Schaub .

Editor information

Editors and Affiliations

A Intermediate Language

A Intermediate Language

To accommodate the richer input language, a more general grounder-solver interface is needed. Although this could be left internal to clingo 5, it is good practice to explicate such interfaces via an intermediate language. This also allows for using alternative downstream solvers or transformations.

Unlike the block-oriented smodels format, the aspif Footnote 26 format is line-based. Notably, it abolishes the need of using symbol tables in smodels’ formatFootnote 27 for passing along meta-expressions and rather allows gringo5 to output information as soon as it is grounded. An aspif file starts with a header, beginning with the keyword asp along with version information and optional tags:

figure ak

where \(v_m,v_n,v_r\) are non-negative integers representing the version in terms of major, minor, and revision numbers, and each \(t_i\) is a tag for \(k\ge 0\). Currently, the only tag is incremental, meant to set up the underlying solver for multi-shot solving. An example header is given in line 1 of Listings 1.13a and 1.14. The rest of the file is comprised of one or more logic programs. Each logic program is a sequence of lines of aspif statements followed by a 0, one statement or 0 per line, respectively. Positive and negative integers are used to represent positive or negative literals, respectively. Hence, 0 is not a valid literal.

Let us now briefly describe the format of aspif statements and illustrate them with a simple logic program in Listing 1.13 as well as the result of grounding a subset of Listing 1.6 in Listing 1.14.

figure al

Rule statements have form

figure am

in which head H has form

figure an

where \(h \in \{\texttt {0},\texttt {1}\}\) determines whether the head is a disjunction or choice, \(m \ge 0\) is the number of head elements, and each \(a_i\) is a positive literal.

Body B has one of two forms:

  • Normal bodies have form

    figure ao

    where \(n \ge 0\) is the length of the rule body, and each \(l_i\) is a literal.

  • Weight bodies have form

    figure ap

    where l is a positive integer to denote the lower bound, \(n \ge 0\) is the number of literals in the rule body, and each \(l_i\) and \(w_i\) are a literal and a positive integer.

All types of ASP rules are included in the above rule format. Heads are disjunctions or choices, including the special case of singular disjunctions for representing normal rules. As in the smodels format, aggregates are restricted to a singular body, just that in aspif cardinality constraints are taken as special weight constraints. Otherwise, a body is simply a conjunction of literals.

The three rules in Listing 1.13a are represented by the statements in lines 2–4 of Listing 1.13b. For instance, the four occurrences of 1 in line 2 capture a rule with a choice in the head, having one element, identified by 1. The two remaining zeros capture a normal body with no element. For another example, lines 2–7 of Listing 1.14 represent the four facts in lines 1 and 2 of Listing 1.7 along with the ones (comprising theory atoms) in line 6 of Listing 1.7.

Minimize statements have form

figure aq

where p is an integer priority, \(n \ge 0\) is the number of weighted literals, each \(l_i\) is a literal, and each \(w_i\) is an integer weight. Each of the above expressions gathers weighted literals sharing the same priority p from all #minimize directives and weak constraints in a logic program. As before, maximize statements are translated into minimize statements.

Projection statements result from #project directives and have form

figure ar

where \(n \ge 0\) is the number of atoms, and each \(a_i\) is a positive literal.

Output statements result from #show directives and have form

figure as

where \(n \ge 0\) is the length of the condition, each \(l_i\) is a literal, and \(m\ge 0\) is an integer indicating the length in bytes of string s (where s excludes byte ‘\0’ and newline). The output statements in lines 5–7 of Listing 1.13b print the symbolic representation of atom a, b, or c, whenever the corresponding atom is true. For instance, the string ‘a’ is printed if atom ‘1’ holds. Unlike this, the statements in lines 8–11 of Listing 1.14 unconditionally print the symbolic representation of the atoms stemming from the four facts in lines 1 and 2 of Listing 1.7.

External statements result from #external directives and have form

figure at

where a is a positive literal, and \(v \in \{0,1,2,3\}\) indicates free, true, false, and release.

Assumption statements have form

figure au

where \(n\ge 0\) is the number of literals, and each \(l_i\) is a literal. Assumptions instruct a solver to compute stable models such that \(l_1,\dots ,l_n\) hold. They are only valid for a single solver call.

Heuristic statements result from #heuristic directives and have form

figure av

where \(m\in \{0,\dots ,5\}\) stands for the (m+1)th heuristic modifier among level, sign, factor, init, true, and false, a is a positive literal, k is an integer, p is a non-negative integer priority, \(n \ge 0\) is the number of literals in the condition, and the literals \(l_i\) are the condition under which the heuristic modification should be applied.

Edge statements result from #edge directives and have form

figure aw

where u and v are integers representing an edge from node u to node v, \(n \ge 0\) is the length of the condition, and the literals \(l_i\) are the condition for the edge to be present.

Let us now turn to the theory-specific part of aspif. Once a theory expression is grounded, gringo 5 outputs a serial representation of its syntax tree. To illustrate this, we give in Listing 1.14 the (sorted) result of grounding all lines of Listing 1.6 related to difference constraints, viz. lines 2/3, 11, 15/16, and 19, as well as lines 1 and 13.

Theory terms are represented using the following statements:

(1)
(2)
(3)

where \(n \ge 0\) is a length, index u is a non-negative integer, integer w represents a numeric term, string s of length n represents a symbolic term (including functions) or an operator, integer t is either -1, -2, or -3 for tuple terms in parentheses, braces, or brackets, respectively, or an index of a symbolic term or operator, and each \(u_i\) is an integer for a theory term. Statements (1), (2), and (3) capture numeric terms, symbolic terms, as well as compound terms (tuples, sets, lists, and terms over theory operators).

Fifteen theory terms are given in lines 12–26 of Listing 1.14. Each of them is identified by a unique index in the third spot of each statement. While lines 12–20 stand for primitive entities of type (1) or (2), the ones beginning with represent compound terms. For instance, line 21 and 22 represent end(1) or start(1), respectively, and line 23 corresponds to end(1)-start(1).

figure ax

Theory atoms are represented using the following

(4)
(5)
(6)

where \(n \ge 0\) and \(m \ge 0\) are lengths, index v is a non-negative integer, a is a positive literal or 0 for directives, each \(u_i\) is an integer for a theory term, each \(l_i\) is an integer for a literal, integer p refers to a symbolic term, each \(v_i\) is an integer for a theory atom element, and integer g refers to a theory operator. Statement (4) captures elements of theory atoms and directives, and statements (5) and (6) refer to the latter.

For instance, line 27 captures the (single) theory element in ‘{ end(1)-start(1) }’, and line 29 represents the theory atom ‘&diff { end(1)-start(1) }<=200’.

Comments have form

figure ay

where s is a string not containing a newline.

The aspif format constitutes the default output of gringo 5. With clasp 3.2, ground logic programs can be read in both smodels and aspif format.

Rights and permissions

Reprints and permissions

Copyright information

© 2017 Springer International Publishing AG

About this chapter

Cite this chapter

Kaminski, R., Schaub, T., Wanko, P. (2017). A Tutorial on Hybrid Answer Set Solving with clingo . In: Ianni, G., et al. Reasoning Web. Semantic Interoperability on the Web. Reasoning Web 2017. Lecture Notes in Computer Science(), vol 10370. Springer, Cham. https://doi.org/10.1007/978-3-319-61033-7_6

Download citation

  • DOI: https://doi.org/10.1007/978-3-319-61033-7_6

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-319-61032-0

  • Online ISBN: 978-3-319-61033-7

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics