1 CUNY Brooklyn College & Graduate Center. 2 Institute of Software, Chinese Academy of Sciences. 3 Department of Computing Science, University of Alberta.
Compiling Answer Set Programs into Event-Driven Action Rules Neng-Fa Zhou1 , Yi-Dong Shen2 and Jia-Huai You3 1
CUNY Brooklyn College & Graduate Center Institute of Software, Chinese Academy of Sciences Department of Computing Science, University of Alberta 2
3
Abstract. This paper presents a compilation scheme, called ASP2AR, for translating ASP into event-driven action rules. For an ASP program, the generated program maintains a partial answer set as a pair of sets of tuples (called IN and OU T ) and propagates updates to these sets using action rules. To facilitate propagation, we encode each set as a finite-domain variable and treat additions of tuples into a set as events handled by action rules. Like GASP and ASPeRiX, ASP2AR requires no prior grounding of programs. The preliminary experimental results show that ASP2AR is an order of magnitude faster than GASP and is much faster than Clasp on benchmarks that require heavy grounding.
1
Introduction
Most ASP systems such as SModels, ASSAT, CModels, ASPPS, DLV, and Clasp rely on a grounder to transform a given program into a propositional one before computing consistent models called answer sets. This two-phase computation has been considered problematic because the grounding process may take an exponential time (in the size of the non-ground program) and the resulting grounded program may be too large to be stored and processed effectively. The NPDatalog system [2] does not ground programs as these systems, but it translates a source program into a constraint program in OPL which performs complete grounding using iterators. Recently, a bottom-up iterative approach has been proposed for ASP in which grounding takes place in the computation process [4]. The main idea of this approach is to iteratively apply rule propagation and nondeterministic rule selection until an answer set is found. Two systems, namely, GASP [5] and ASPeRiX [3], have been developed based on this approach. Both systems are interpreters. While these systems outperform the cutting-edge ASP systems such as Clasp on some benchmarks for which grounding is very expensive, they are not as competitive in general. This paper describes a compiler, called ASP2AR, for translating ASP into event-driven action rules (AR) [6]. For an ASP program, predicates are divided into stratified and unstratified parts. The stratified part is evaluated using
tabling and those in the unstratified part are translated into AR to maintain the current interpretation. Like in GASP and ASPeRiX, two disjoint tuple sets, called IN and OU T , are used to represent the current interpretation. To facilitate propagation, we represent the IN and OU T sets for each predicate as two finite-domain variables. In this way, additions of tuples into IN and OU T sets can be treated as events and handled by action rules. When a fixpoint is reached after propagation, a tuple of a negative literal that is neither in IN or OU T is selected and assumed to be true or false. This step is called labeling. Labeling a tuple triggers further propagation. This step is repeated until no further tuple can be selected. Our approach differs from the bottom-up iterative approach used in GASP and ASPeRiX in that it labels negative literals, not rule instances. The answer set semantics requires that every tuple in an answer set must be justified. When a tuple is labeled false, we can safely add it into OU T . When a tuple is labeled true, however, we cannot just add it into IN because it may not be producible by any rule instance. For this reason, we use action rules to ensure that the tuple is supported by at least one rule instance. Our system is compiler-based. It does not have the interpretation overhead as seen in GASP and ASPeRiX. Compared with propositional programs generated by a grounder, programs generated by our compiler are very small. The preliminary experimental results show that ASP2AR is an order of magnitude faster than GASP and is much faster than Clasp on benchmarks that require heavy grounding; and on the benchmarks that can be grounded easily, however, ASP2AR is still not as competitive as Clasp.
2
The Procedure for Computing Answer Sets
Given a program, we assume that all the predicates that are not dependent on unstratified negation have been completely evaluated using tabling. Figure 1 presents the procedure for computing answer sets when the program contains unstratified negation. Three sets named IN , OU T , and P IN are used to maintain the current partial answer set: IN contains tuples that are known to be true, OU T contains tuples that are known to be false, and P IN contains tuples that have been assumed to be true. The pair hIN, OU T i is said to be inconsistent if there exists a tuple that is contained in both IN and OU T , and complete if for any tuple it is included in either IN or OU T . A tuple of a positive literal is said to be true if it is included in IN , a tuple of a negative literal is said to be true if it is included in OU T , and a tuple is said to be unknown if it is included in neither IN nor OU T . In the beginning, OU T and P IN are empty and IN contains all the tuples obtained from the evaluation of the stratified part. The procedure repeatedly applies propagation and labeling until an answer set is found or a failure is reported. 2
compute(){ initialize IN , OUT , and P IN ; propagate(); while (hIN, OUT i is consistent but not complete) { choose an unknown tuple t of a negative literal; labeling: OUT = OUT ∪{t} ⊕ P IN = P IN ∪{t}; propagate(); }; if (hIN, OUT i is consistent and every tuple in P IN is also in IN ) output IN as an answer set; } propagate(){ do { for (each rule H:-B in the program) { right-to-left: For each instance of B that is true, add H into IN ; left-to-right: For each instance of H in OUT , ensure no instance of B is true; } } while (IN or OUT is updated); seek-support: For each tuple in P IN but not IN , ensure it is producible; }
Fig. 1. The procedure for computing answer sets.
2.1
Right-to-left propagation
For each rule “H:−B”, the right-to-left propagation adds H into IN for each instance of B that is true. Since all rules are range restricted, H is guaranteed to be ground for each ground instance of B. Let B = “B1 , . . . , Bn ”. The conjunction of literals is true if all the literals Bi (i=1,...,n) are true. Recall that a ground instance of a positive literals is true if it is included in IN , and a ground instance of a negative literals is true if it is included in OU T . The right-to-left propagation essentially conducts joins of the known relations of the literals B1 , . . ., Bn . 2.2
Left-to-right propagation
For each rule “H:−B”, once a ground instance of H is added into OU T , the leftto-right propagation ensures that no instance of B is true. Let B =”B1 , . . . , Bn ”. If instances of any n − 1 literals are found to be true, then the instance of the remaining one literal must be false. If the remaining literal is positive, then all of its ground instances are added into OU T and further propagation will ensure that they will never be produced. If the remaining literal is negative, then all of its ground instances are added into P IN , and further propagation will ensure that for each such an instance there exists at least one rule instance that can produce it. Note that a tuple added into P IN is still unknown until it is added into IN afterwards. 2.3
Seek-support
For each tuple in P IN but not in IN , the seek-support propagation ensures that it has at least one support, i.e., a rule instance, that can produce it. An instance 3
of a positive literal is supported if (1) it is in IN ; (2) it is in P IN and has a support; or (3) it is not in P IN and it is unknown. An instance of a negative literal is supported if (1) it is in OU T ; or (2) it is unknown. A rule instance “H:−B1 , . . . , Bn ” is a support of H if every Bi (i=1,...,n) is supported. 2.4
Labeling
In labeling, an unknown tuple t of a negative literal is chosen and the labeling step nondeterministically adds t into OU T or P IN . When a tuple is labeled false, it is safely added into OU T . After this, propagation will ensure that the tuple can never be produced (left-to-right). When a tuple is labeled true, however, it is added into P IN , not IN . After this, propagation will ensure that there exists at least one rule instance that can produce it (seek-support).
3
Translation from ASP into AR
The AR (Action Rules) language, which was initially designed for programming constraint propagators [6], is used as the target language for compiling ASP. An action rule takes the following form: “H, G, {E}=>B” where H (called the head) is an atomic formula that represents a pattern for agents, G (called the guard) is a conjunction of conditions on the agents, E (called event patterns) is a non-empty disjunction of patterns for events that can activate the agents, and B (called action) is a sequence of arbitrary subgoals. In general, a predicate can be defined with multiple action rules. Consider the following example: p(X),{dom_any(X,E)} => writeln(E). go :- X in 1..4, p(X), X #\= 1, X #\= 3. The event dom any(X,E) is posted whenever an element E is excluded from the domain of X. The query go outputs two lines, 1 in the first line and 3 in the second line. The stratified part of a given program is completely evaluated using tabling. After this, each remaining predicate must have a known finite domain. We encode each tuple of a predicate as an unique integer and use an integer domain variable to represent each of the IN and OU T sets for the predicate. Initially, the domain contains all the encoded integers for the tuples to denote the empty set. Each time a tuple is added into a set, the encoded integer of the tuple is excluded from the domain. Dummies are added into the domains so that no domain variable will be instantiated. This representation is compact since each element in a domain is encoded as a bit. Furthermore, exclusions of domain elements are treated as dom any events that can be handled using action rules. For the sake of simplicity of presentation, we assume the existence of the event 4
tuple added(S, p(A1, ..., An )), which is posted whenever a tuple p(A1 , ..., An ) is added into the set S. Consider the ASP rule “p(X,Z) :-q(X,Y),r(Y,Z).” To do right-to-left propagation, we create an agent named agent q to watch additions of tuples into INq. Once a tuple q(X,Y) has been added into INq, we conduct join of q(X,Y) with each of the tuples that have been already added into INr. Also, we create another agent named agent q r to watch future additions of tuples into INr. The following defines agent q and agent q r. The predicate add tuple(INp,p(X1,...,Xn)) adds the tuple p(X1,...,Xn) into the set INp. agent_q(INp,INq,INr),{tuple_added(INq,q(X,Y))} => foreach(r(Y,Z) in INr, add_tuple(INp,p(X,Z))), agent_q_r(INp,X,Y,INr). agent_q_r(INp,X,Y,INr),{tuple_added(INr,r(Y,Z))} => add_tuple(INp,p(X,Z)).
Negative literals are treated in the same way as positive literals except that OU T sets are used instead of IN sets. The compiled program is improved by indexing agents. For example, for the conjunction “q(X,Y),r(Y,Z)”, after a tuple q(X,Y) has been observed, an agent is created to watch only upcoming tuples of r/2 whose first argument is the same as Y. By indexing agents, redundant join operations are avoided. The left-to-right propagation rule is encoded similarly. Consider again the above ASP rule. To ensure that p(X,Z) is false, an agent is created to watch q(X,Y). For each such a tuple, r(Y,Z) is added into OUTr. Another agent is created to watch r(Y,Z). For each such a tuple, q(X,Y) is added into OUTq. The seek-support rule is encoded in the following way. To ensure that p(X,Z) is producible, an agent is created to watch additions of tuples into OUTq and OUTr. Each time a tuple is added into OUTq or OUTr, the agent ensures that there exists a tuple q(X,Y) that is true or unknown (i.e., not in OUTq) and a tuple r(Y,Z) that is true or unknown. Aggregates are easily handled. For example, the cardinality constraint 1{. . .}1 is handled as follows: once a tuple of the relation is added into IN , all unknown tuples are added into OU T ; and once all tuples but one are added into OU T , then this remaining tuple is added into IN .
4
Performance Evaluation
B-Prolog version 7.5 has been enhanced with a library of predicates for compiling ASP. Table 1 compares hand-compiled ASP2AR with Clasp (version 1.3.5) [1] and GASP [5] on CPU times for four benchmarks. ASP2AR is an order of magnitude faster than GASP. The first two programs, p2 and squares, are known to require expensive grounding [5]. For them, ASP2AR considerably outperforms Clasp. The other two programs, queens and color, do not require expensive grounding, and ASP2AR is still not comparable with Clasp. Note that 5
Table 1. CPU time (seconds, Windows-XP, 1.4 GHz CPU, 1G RAM). Benchmark ASP2AR Clasp GASP p2 0.27 12.29 14.90 square 0.02 32.17 0.65 queens(50) 0.98 0.156 n/a color 3.76 0.09 >3600
the labeling strategy used has a great impact on the performance. In ASP2AR, a relation with the fewest unknown tuples is labeled first. This strategy is similar to the first-fail principle used in CLP(FD). No direct comparison was conducted with ASPeRiX since ASPeRiX does not support aggregates which are used in three of the benchmarks.
5
Conclusion
This paper has presented ASP2AR, a scheme for compiling ASP into action rules. The preliminary results show that ASP2AR is competitive for programs that require heavy grounding, and the grounding-before-solving approach is competitive for programs that require light grounding. One future project is to develop a solver that amalgamates these two approaches. Since AR is used as a common intermediate language for both CLP(FD) and ASP, it will be easy to closely integrate these two languages. Another future project is to introduce CLP(FD) features such as global constraints into ASP.
Acknowledgements Neng-Fa Zhou is supported by NSF (No.1018006), Yi-Dong Shen is supported by NSFC (No. 60970045), and Jia-Huai You is supported by NSERC (No. 90718009).
References 1. Martin Gebser, Roland Kaminski, Benjamin Kaufmann, Max Ostrowski, Torsten Schaub, and Sven Thiele. A User’s Guide to gringo, clasp, clingo and iclingo. Technical report, University of Potsdam, 2008. 2. Sergio Greco, Cristian Molinaro, Irina Trubitsyna, and Ester Zumpano. NP Datalog: A logic language for expressing search and optimization problems. TPLP, 10(2):125– 166, 2010. 3. Claire Lef`evre and Pascal Nicolas. A first order forward chaining approach for answer set computing. In LPNMR, pages 196–208, 2009. 4. Lengning Liu, Enrico Pontelli, Tran Cao Son, and Miroslaw Truszczynski. Logic programs with abstract constraint atoms: The role of computations. In ICLP, pages 286–301, 2007. 5. Alessandro Dal Pal` u, Agostino Dovier, Enrico Pontelli, and Gianfranco Rossi. Answer set programming with constraints using lazy grounding. In ICLP, pages 115– 129, 2009. 6. Neng-Fa Zhou. Programming finite-domain constraint propagators in action rules. TPLP, 6(5):483–508, 2006.
6