This paper was converted on www.awesomepapers.org from LaTeX by an anonymous user.
Want to know more? Visit the Converter page.

Automated Aggregator — Rewriting with the Counting Aggregate

Michael Dingess and Miroslaw Truszczynski Research supported by National Science Foundation grant 1707371Department of Computer Science, University of Kentucky, United States [email protected], [email protected]
Abstract

Answer set programming is a leading declarative constraint programming paradigm with wide use for complex knowledge-intensive applications. Modern answer set programming languages support many equivalent ways to model constraints and specifications in a program. However, so far answer set programming has failed to develop systematic methodologies for building representations that would uniformly lend well to automated processing. This suggests that encoding selection, in the same way as algorithm selection and portfolio solving, may be a viable direction for improving performance of answer-set solving. The necessary precondition is automating the process of generating possible alternative encodings. Here we present an automated rewriting system, the Automated Aggregator or AAgg, that given a non-ground logic program, produces a family of equivalent programs with complementary performance when run under modern answer set programming solvers. We demonstrate this behavior through experimental analysis and propose the system’s use in automated answer set programming solver selection tools.

1 Introduction

Developers of answer set programming (ASP) solutions often face situations where individual constraints of a problem or even the problem as a whole can be expressed in several syntactically different but semantically equivalent ways. Picking the right representation is crucial to designing these solutions because, given an instance, certain representations perform better (often much better) than others when processed with modern ASP grounders and solvers. However, techniques for selecting a particular representation are often ad hoc and tailored to the needs of the particular application, and require significant programming expertise from the programmer.

About a decade ago, Gebser et al. presented a set of ”rules-of-thumb” used by their team in manual tuning of ASP solutions [7]. These rules include suggestions on program rewritings that often result in substantial performance gains. This was verified experimentally by Gebser et al., with all rewritings used in their experiments generated manually [7]. Buddenhagen and Lierler studied the impact of these rewritings on an ASP-based natural language processor and reported orders of magnitude gains in memory and time consumption as a result of some program transformations they executed manually [3]. Because of these promising results, researchers proposed to automate the task of program rewriting. Bichler et al. investigated rewritings of long rules guided by the tree decomposition of graphs built of program rules (a form of join optimization) [2]. Hippen and Lierler proposed a method of applying projection to rewrite rules based on estimates of the size of the ground program [9].

These projects demonstrated that automated program rewritings may lead to programs performing better than the original ones when run under current ASP solvers such as gringo/clasp [6]. However, the effects of rewritings are not uniformly the same. In fact, depending on the actual instance they are run with, rewritten programs may perform worse that the original ones. This is a problem because a non-uniform behavior makes the process of selecting the uniformly best encoding ill defined. Nevertheless, automated rewritings potentially can significantly improve the state-of-the-art of ASP solving. Namely, the non-uniform behavior of programs obtained by rewriting opens a possibility of using families of equivalent alternative programs in algorithm selection and portfolio solving [14, 15, 10]. In fact, a recent study of suite of six encodings of the Hamiltonian cycle problem shows that one can train performance prediction models to select, given an instance, a program from the suite to run on that instance, guaranteeing a much better overall performance on large sets of instances than that of each of the six programs alone [13].

In this work, we focused on rewritings in which rules using counting based on explicit naming of a required number of objects are rewritten with the use of a counting aggregate. We designed a software, Automated Aggregator (AAgg), for automating such rewritings into several equivalent forms. We studied the software’s applicability and effectiveness. In our experiments, we applied the software to programs submitted to past ASP Competitions. We found that while many of them already included aggregate expressions and AAgg did not detect any rules to which it could apply, in several cases, it was applicable! For those cases, we studied the performance of the original program and the rewritings produced by AAgg. The results showed that depending on an instance, rewritings produced by our software often performed better than the original programs. In other words, the family of encodings generated by AAgg (the original program and its rewritings) showed a complementary performance on both the instances used in the ASP Competitions and on instances which we generate ourselves. These results show that AAgg can be used as a tool for generating collections of encodings to be used in algorithm selection and portfolio solving. A systematic experimental verification of this claim will be the subject of a future work.

2 Aggregate Equivalence Rewriting

In this section we describe the aggregate equivalence rewriting, its input and output forms. Currently, we support one input form and three output forms. The input form is a rule that expresses a constraint that there are bb different objects with a certain property by explicitly introducing bb variables to name these bb objects. The output forms model the same property by relying, in some way, on the counting aggregate. For each of the rewritings we establish its correctness and experimentally study its performance. The correctness follows from Theorems A.3 and A.5, presented and proved in the appendix.

2.1 Preliminaries

We follow the ASP-Core-2 Input Language Format [4]. We consider rules of the form headbody.head\leftarrow body. The head may consist of a single literal or be empty, the latter representing a contradiction, making the rule a constraint. The body may contain one or more literals or be empty, which constitutes a fact. Literals are composed of an atom, which may be preceded by notnot. Negative literals include notnot; positive literals do not. Atoms have the form p(t_1,,t_k)p(t_{\_}1,\dots,t_{\_}k), where pp is a predicate symbol of arity kk and each t_it_{\_}i is a term, that is, a constant, variable, or expression of the form f(t_1,,t_k)f(t_{\_}1,\dots,t_{\_}k) where ff is a function symbol of arity k>0k>0 and each t_it_{\_}i is a term.111AAgg also accepts other term expressions following the definition used by clingo [6].

Atoms may also take the form of an aggregate expression. In this work, we focus on counting aggregates, that is, expressions of the form:

s_1_1#count{𝐭_1:𝐋_1;;𝐭_n:𝐋_n}_2s_2s_{\_}1\prec_{\_}1\#count\{{\it\bf t}_{\_}1:{\it\bf L}_{\_}1{\bf;}\ldots{\bf;}{\it\bf t}_{\_}n:{\it\bf L}_{\_}n\}\prec_{\_}2s_{\_}2 (1)

In (1), 𝐭_i{\it\bf t}_{\_}i and 𝐋_i{\it\bf L}_{\_}i form an aggregate element, which is a non-empty tuple of terms and literals, respectively. The count operation simply counts the number of unique term tuples 𝐭_i{\it\bf t}_{\_}i whose corresponding condition 𝐋_i{\it\bf L}_{\_}i holds. The result of the count function is compared by the comparison predicates _1\prec_{\_}1 and _2\prec_{\_}2 to the terms s_1s_{\_}1 and s_2s_{\_}2. These comparison predicates may be one of {<,,=,}\{<,\leq,=,\neq\}. One or both of these comparisons can be omitted [6].

2.2 Input Forms

The aggregate equivalence rewriting takes as input rules of the form:

H_1ibF(X_i,𝐘),_1i<jbX_iX_j,G.H\leftarrow\bigwedge_{\_}{1\leq i\leq b}F(X_{\_}i,{\bf Y}),\bigwedge_{\_}{1\leq i<j\leq b}X_{\_}i\neq X_{\_}j,\ G. (2)

where

  • HH is the head of a rule (HH may be empty making the rule a constraint)

  • FF is a predicate of arity 1+|𝐘|1+\bf{|Y|}

  • X_1,,X_bX_{\_}1,\ldots,X_{\_}b are variables, all in the same position in FF

  • 𝐘\bf{Y} is a comma-separated list of variables, identical in variables and variable positions for all FF in the rule

  • GG is the remaining body of the rule, possibly empty,

and the following hold true:

  • b2b\geq 2

  • HH, GG, and 𝐘{\bf Y} have no occurrences of variables X_1,,X_bX_{\_}1,\ldots,X_{\_}b

  • The terms _1i<jbX_iX_j\bigwedge_{\_}{1\leq i<j\leq b}X_{\_}i\neq X_{\_}j may instead be a continuous chain of comparisons:

    _1ib1X_i<X_i+1\bigwedge_{\_}{1\leq i\leq b-1}X_{\_}i<X_{\_}{i+1} or _1ib1X_i>X_i+1\bigwedge_{\_}{1\leq i\leq b-1}X_{\_}i>X_{\_}{i+1}.

Note that X_iX_{\_}i’s need not be in the first position in FF, so long as they are all in the same position in FF and the other variables in FF (if any) are identical in all occurrences of FF in the rule. Additionally, some other forms logically equivalent to _1i<jbX_iX_j\bigwedge_{\_}{1\leq i<j\leq b}X_{\_}i\neq X_{\_}j are also acceptable. For instance, the condition X_1X_2X_{\_}1\neq X_{\_}2 may be expressed as X_1+aX_2+aX_{\_}1+a\neq X_{\_}2+a, for some integer aa.

2.3 Output Forms

The form of the output depends on the size of 𝐘{\bf Y}. When |𝐘|=0|{\bf Y}|=0, the output form is:

Hb#count{X:F(X)},G.H\leftarrow b\leq\#count\{X:F(X)\},G. (3)

where

  • HH, bb, FF, and GG are as above

  • Aggregate element X:F(X)X:F(X) follows the form 𝐭𝐞𝐫𝐦:𝐥𝐢𝐭𝐞𝐫𝐚𝐥{\it\bf term}:{\it\bf literal} as defined above. The prior, XX, is a tuple of one term, which in this case is a variable. The second, F(X)F(X), is a literal.

When |𝐘|>0|{\bf Y}|>0, we perform projection to project out the variable XX from FF. This gives us an output form consisting of two rules:

Hb#count{X:F(X,𝐘)},F(𝐘),G.F(𝐘)F(X,𝐘).\displaystyle\begin{split}H&\leftarrow b\leq\#count\{X:F(X,{\bf Y})\},F^{\prime}({\bf Y}),G.\\ F^{\prime}({\bf Y})&\leftarrow F(X,{\bf Y}).\end{split} (4)

where

  • HH, bb, FF, 𝐘{\bf Y}, and GG are as above

  • Aggregate element X:F(X,𝐘)X:F(X,{\bf Y}) follows the form 𝐭𝐞𝐫𝐦:𝐥𝐢𝐭𝐞𝐫𝐚𝐥{\it\bf term}:{\it\bf literal} as above

  • FF^{\prime} is a new predicate symbol of arity equal to the size of Y, that is, equal to the arity of FF minus one; introducing FF^{\prime} ensures that variables in 𝐘\mathbf{Y} are universally quantified.

The correctness of this rewriting, follows from the results presented in the appendix (Theorem A.3). Specifically, we show there that if a program is obtained from another program by rewriting one of its rules in the way described above, then both programs have the same answer sets (modulo atoms F(𝐲)F^{\prime}(\mathbf{y}), if FF^{\prime} is introduced).

2.3.1 Alternative Output Forms

Two alternative, logically equivalent output forms are also available, each derived from the output form presented above. First, we observe that bFb\leq F is logically equivalent to the negation of F<bF<b. We can then restate the original literal as follows:

not#count{X:F(X,𝐘)}<b.not\ \#count\{X:F(X,{\bf Y})\}<b. (5)

Second, we note that the input language we consider permits integer-only arithmetic. Consequently, the expression nota<bnot\ a<b for some integers aa and bb is equivalent to the conjunctive expression:

¬(a=)¬(a=+1)¬(a=b2)¬(a=b1)\neg(a=-\infty)\wedge\neg(a=-\infty+1)\wedge\ldots\wedge\neg(a=b-2)\wedge\neg(a=b-1).

Additionally, the result of the count never returns a negative number. Therefore, we can restate the aggregate literal in this alternative output form as a conjunction of aggregate literals having the form:

not#count{X:F(X,𝐘)}=0,not#count{X:F(X,𝐘)}=1,,not#count{X:F(X,𝐘)}=b1.\begin{split}&not\ \#count\{X:F(X,{\bf Y})\}=0,\\ &not\ \#count\{X:F(X,{\bf Y})\}=1,\\ &\ldots,\\ &not\ \#count\{X:F(X,{\bf Y})\}=b-1.\end{split} (6)

Note that, due to the precise semantics of logic programs, the equivalence of these two alternative logic forms relies on additional assumptions about the input program (it has to be splittable) and the rule to be rewritten. Theorem A.5 provides conditions under which the rewriting is guaranteed to be correct. These conditions are checked by our software and only when they hold, the software proceeds with rewriting into an alternative form (5) or (6), as selected by the user.

3 The Automated Aggregator System

We now present the Automated Aggregator222The system and all encodings, test instances, and driver programs can be found online at:
https://drive.google.com/drive/folders/1lqRsy9HGIDHvyXPvkc8zKt1-xvbwcAp?usp=sharing
(AAgg) software system for performing the Aggregate Equivalence rewriting. The software provides an automated way to detect rules within a given program following the input format (2) and rewrite those rules into an equivalent output format (3/4), (5), or (6).

3.1 Usage

The software relies on the clingo Python module provided by the Potassco suite [8]. The module is written in Python 2.7. As such, Python 2.7 is required to run the Automated Aggregator system. Installation information is provided in the software’s README file. The Automated Aggregator is invoked as follows:

  python  aagg/main.py
          [-h,--help] [-o,--output FILENAME] [--no-rewrite]
          [--no-prompt] [--use-anonymous-variable]
          [--aggregate-form ID] [-d,--debug] [-r,--run-clingo]
          [encoding_1 encoding_2 ...]

The -h flag lists the help options. The encoding(s) are the filename(s) of the input encoding(s), and the output is the desired name for the output file. If no output filename is given, one is generated based on the first input filename given. When a candidate rule is discovered, the user is shown the proposed rewriting and prompted for confirmation. If the --no-rewrite flag is given, no prompts are given and no rewriting is performed. If the --no-prompt flag is given, no prompts are given and rewriting is performed where possible. The ID supplied to the --aggregate-form argument informs the program which aggregate form to use when performing rewrites: its values 11, 22, and 33 correspond to aggregate forms (3/4), (5), and (6), respectively. The -d debug flag directs the application to operate with verbosity, printing details during the rewriting candidate discovery process and printing some statistics after the application’s conclusion. The --r run-clingo flag directs the application to run the resulting program through clingo after any rewritings are performed.

Finally, the --use-anonymous-variable flag indicates an additional modification of the output form to be performed. It uses the anonymous variable ‘_’ in place of the variable XX in the output forms listed above, with some additional modifications of the rule to ensure the transformation is correct. Specifically, we replace the aggregate element X:F(X,𝐘)X:F(X,{\bf Y}) with F(_,𝐘):F(_,𝐘)F(\_,{\bf Y}):F(\_,{\bf Y}) rather than with _:F(_,𝐘)\_:F(\_,{\bf Y}), because the latter is not a valid gringo syntax. We mention this option for completeness sake, since it is available in our implementation. However, we found that the programs generated when using and when not using the anonymous variable are identical after grounding, so we neither discuss it further nor use these rewritings in our experiments.

By default all boolean flags are disabled and the aggregate-form ID is set to 11 indicating output form (3). At least one input encoding filename must be specified.

3.2 Methodology

The methodology used for discovering whether a rule is a candidate for the aggregate equivalence rewriting is as follows. The given logic program(s) are parsed by the clingo module, generating an abstract syntax tree for each rule. Each such tree is passed to a transformer class for preprocessing. After preprocessing, some information is gathered from the program as a whole; specifically, predicate dependencies are determined, which in turn determine when output forms (5) and (6) are appropriate for a given rule (see the appendix for more details). Rules are then passed individually to an equivalence transformer class for processing. After processing, and if the requested rewriting is possible and confirmed by the user, the rewritten form of the rule is returned. Otherwise the original rule is returned. All returned rules, rewritten or not, are collected and output to the desired output file location. Optionally, the resulting program is then run using clingo.

When a rule is passed to the equivalence transformer for processing, it first undergoes a process of exploration, which traverses the rule’s abstract syntax tree, recording comparison literals between two variables as well as other pertinent information found along the way. These comparison literals are scrutinized to determine whether a subset of the comparisons follows the form given in (2) or some equivalent format as detailed in the section 2.2. The process also identifies those variables that play the role of X_1,,X_bX_{\_}1,\ldots,X_{\_}b as in (2). The rule is then analyzed to determine whether there are bb occurrences of some positive literal of predicate FF with the arguments X_iX_{\_}i and 𝐘\bf{Y}, where each X_iX_{\_}i, 1ib1\leq i\leq b, exists at least once in the set of occurrences, and 𝐘\bf{Y} is the same for each occurrence and contains no variables X_1,,X_bX_{\_}1,\ldots,X_{\_}b. Let us call this set of bb literals combined with the corresponding comparisons following the form given in (2) or equivalent, the rule’s counting literals. Similarly, we denote the variables playing the role of the variables X_iX_{\_}i as given in (2) as the rule’s counting variables.

After gathering counting literals and counting variables, the equivalence transformer verifies that the counting variables are not used within literals anywhere in the rule excluding literals within the set of counting literals. If this verification fails or if any of the constraints for the counting literals cannot be satisfied or if no such set of counting variables can be obtained, then the rule is not fit for rewriting. As a result, no rewriting is performed on the rule and the original rule is returned.

In the other case, if the verification succeeds (the counting literals and the counting variables satisfy all the required constraints), then we proceed as follows. If the requested output form is (3/4), then we check whether |𝐘|>0|\mathbf{Y}|>0 and rewrite the rule accordingly using (3) or (4). If the requested output form is (5) or (6), then the predicate dependency condition related to splitting must be verified (see the appendix for details). If it holds, then we check whether |𝐘|>0|\mathbf{Y}|>0 and rewrite the rule into the desired form (5) or (6), adding the second of the two rules in (4) when |𝐘|>0|\mathbf{Y}|>0.

Note that the user is first prompted for confirmation for each rewritten rule before the rule is added to the final program. If the user denies a rewriting, then the original rule is used.

3.3 Limitations

Here we discuss the limitations of the Automated Aggregator in its current form.

  1. 1.

    The rewriting is one-directional. The software will only rewrite rules from the form (2) into rules of the forms (3/4), (5), and (6). As it stands, the system will not rewrite rules given in any of the forms (3/4), (5), or (6) into rules of any of other form.

  2. 2.

    In the cases when multiple rewritings are possible for a single rule, only one rewriting will be detected and performed. To illustrate, if the form given in (2) occurs twice in one rule over a disjoint set of variables and predicates, where both sets of counting literals consider the other set as part of GG and all conditions hold, then only the set of counting literals with the highest number of counting variables will be used for rewriting. If both sets contain the same number of counting variables, then one is chosen based on the order in which the comparisons using those variables occur in the rule.

  3. 3.

    There are some cases in which the software will not recognize a valid rewriting when one exists. These cases mostly lie in the many obscure ways of representing a chain of comparisons equivalent to that in (2). However, there are no known cases in which an incorrect rewriting will be proposed when no valid rewriting is possible. More details are given in the software’s README document.

4 Experimental Analysis

The Automated Aggregator system has been made available for download online.333https://drive.google.com/drive/folders/1lqRsy9HGIDHvyXPvkc8zKt1-xvbwcAp?usp=sharing Encodings with which the application was tested, their corresponding instances, and (Python) scripts for driving such tests, are included there too.

4.1 Automated Aggregator in Practice

The Automated Aggregator system was applied to logic programs in gringo syntax submitted to the 2009, 2014, and 2015 Answer Set Programming Competitions. Of the 58 encodings given to the application, five contained rules which were candidates for the rewriting described. Table 1 lists the encoding problem names and the ASP Competitions for which they were developed.

Table 1: Problem Domains of ASP Competition Encodings with Rules for Rewriting
ASP Competition Year Problem Domain of Encodings
2009 Golomb Ruler & Wire Routing
2014 Steiner Tree
2015 Steiner Tree & Graceful Graphs

Additionally, to provide an example of the functionality of AAgg, the system was applied to an encoding for the Hamiltonian Cycle problem. The original program is shown in Figure 1. The rewritten program, as output by AAgg, is shown in Figure 2. We see that the first two constraints in the program are both rewritten with the necessary projection performed as in (4). Experimental results for these encodings when applied to a generated set of hard instances are given in the following section.

 

node(X) :- edge(X,Y).
node(X) :- edge(Y,X).

{ hc(X,Y) } :- edge(X,Y).
:- hc(X,Y), hc(X,Z), Y!=Z.
:- hc(X,Y), hc(Z,Y), X!=Z.

reach(X,Y) :- hc(X,Y).
reach(X,Y) :- hc(X,Z), reach(Z,Y).

:- node(X), node(Y), not reach(X,Y).
Figure 1: Example Hamiltonian Cycle problem encoding. Original version.

 

 

node(X) :- edge(X,Y).
node(X) :- edge(Y,X).

{ hc(X,Y) } :- edge(X,Y).
:- 2 <= #count{ Y : hc(X,Y) }, hc_project_Z(X).
hc_project_Z(X) :- hc(X,Y).
:- 2 <= #count { X : hc(X,Y) }, hc_project_Z1(Y).
hc_project_Z1(Y) :- hc(X,Y).

reach(X,Y) :- hc(X,Y).
reach(X,Y) :- hc(X,Z), reach(Z,Y).

:- node(X), node(Y), not reach(X,Y).
Figure 2: Example Hamiltonian Cycle problem encoding. Rewritten version.

 

4.2 Experimental Results

Results were gathered by systematically grounding and solving each instance-encoding pair within families of encodings for each problem type. The data sets of instances we used are available together with the AAgg tool (see the url listed earlier).444We thank Daniel Houston and Liu Liu for providing us with the data sets of instances for the Latin Square and the Hamiltonian Cycle problems, respectively. The data sets for the remaining problems were generated by software tools we developed for the purpose. These tools and descriptions of instance sets used in experiments can be found at the AAgg site. Each encoding in each family of encodings was run for each instance. The total grounding plus solving time of instance-encoding pairs were recorded and compared.

The precise encodings used as input to the AAgg software for gathering results are listed in Table 2. Two output encodings are generated for each input encoding and together they form the encoding family for that problem domain. The two output forms used were those shown in equations (3/4) and (6); previous experiments showed extreme similarity between forms (3/4) and (5), so (5) was left out to preserve machine time. The machine used for testing contained an Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz with 16GB RAM.

Table 2: Sources of Encodings Used for Experimental Results
Source of Encoding Problem Domain of Encodings
ASP Competition 2009 Wire Routing
ASP Competition 2015 Steiner Tree & Graceful Graphs
Home-Brewed Latin Squares& Hamiltonian Cycle555We are thankful to Daniel Houston and Liu Liu for supplying the Latin Squares and Hamiltonian Cycle instance sets, respectively.
Table 3: Result Statistics by Problem Domain
Encoding Wins Exclusive Wins Wins by 20% Wins by 50%
Wire Routing Input Encoding 122 (58.4%) 34 (27.8%) 115 (92.0%) 97 (77.6%)
AAgg Output Form (3/4) 50 (24.0%) 26 (52.0%) 48 (87.3%) 38 (69.1%)
AAgg Output Form (6) 37 (17.7%) 13 (35.1%) 39 (90.7%) 23 (53.5%)
Steiner Tree Input Encoding 130 (33.9%) 0 (0%) 1 (0.8%) 0 (0%)
AAgg Output Form (3/4) 123 (32.1%) 0 (0%) 4 (3.3%) 0 (0%)
AAgg Output Form (6) 130 (33.9%) 0 (0%) 6 (4.6%) 0 (0%)
Graceful Graphs Input Encoding 212 (37.3%) 62 (29.2%) 182 (85.8%) 128 (60.4%)
AAgg Output Form (3/4) 97 (17.1%) 23 (23.7%) 82 (84.5%) 58 (59.8%)
AAgg Output Form (6) 259 (45.6%) 51 (19.7%) 229 (88.4%) 169 (63.7%)
Latin Squares Input Encoding 5611 (77.7%) 5 (0.1%) 4435 (79.0%) 2003 (35.7%)
AAgg Output Form (3/4) 1432 (19.8%) 0 (0%) 655 (45.7%) 86 (6.0%)
AAgg Output Form (6) 176 (2.4%) 0 (0%) 64 (36.4%) 7 (4.0%)
Hamiltonian Cycle Input Encoding 72 (28.7%) 47 (65.3%) 59 (81.9%) 34 (47.2%)
AAgg Output Form (3/4) 102 (40.6%) 69 (67.7%) 79 (77.5%) 52 (51.0%)
AAgg Output Form (6) 77 (30.7%) 45 (58.4%) 64 (83.1%) 44 (57.1%)

Result statistics are shown in Table 3. Data is grouped by problem domain. The first line of each grouping shows statistics for the input encoding. The second line shows statistics for the encoding output by the AAgg software using the first line as input and the output rule form (3/4) as the selected output form. The third and final line of each grouping shows statistics for the encoding output by the AAgg software again using the first line as input but now selecting the output form (6) as the chosen output form.

A win is when an encoding grounds and solves for an instance in the shortest amount of time as compared with the other two encoding in its grouping. The percentage value beside the win count is the proportion of instances which that encoding won relative to the number of instances in the instance set for which at least one of the three encodings terminated within the total time limit set for grounding and solving. This time limit was set to 200 seconds in all experiments, except those with encodings of the Hamiltonian Cycle problem that used a timeout value of 400 seconds due to the relative difficulty of the instance set (in all problem domains except for the Hamiltonian Cycle domain, the number of instances for which no encoding computed an answer within the time limit was very small; for the Hamiltonian Cycle problem, even with the increased time limit, it was significant).

An exclusive win is when the encoding is the only encoding in its grouping to find a solution (or determine there is no solution) for an instance while both of the other two encodings failed to do so within the time limit. The column shows the number and the percentage of wins which were exclusive wins for the encoding. The next column shows the number and the percentage of wins by a margin of at least 20% (this is when the best encoding runs at least 20% faster than the second one; in case the of exclusive wins, we count a win as by at least 20%, if it is faster by at least 20% than the time limit used). The data in the last column shows the numbers and percentages of wins by at least 50% (it is to be interpreted similarly to the data in the previous column).

Results indicate that in some cases, the rewriting can provide complementary performance of encodings. Specifically, the results for the Wire Routing, Graceful Graphs, and Hamiltonian Cycle problems support the claim. This is shown first by the fact that for each problem, each of the encodings scores a significant proportion of wins, and that among those wins a significant proportion are exclusive wins, and an even greater proportion (about 50% or more) are wins by at least 50%. This means that about half of the times when an encoding outperforms the two other encodings, it outperforms both by a factor of at least two.

The Steiner Tree results indicate that sometimes the rewriting produces little to no effect at all. While each encoding for the problem registers a similar proportion of wins, there are very few instances (just eleven out of 283) when the best encoding outperforms the other two by 20% or more, and no instances when the best encoding would outperform the other two by 50% or more. The Latin Squares results are mixed in the sense that for the most part the original encoding works best. However, one of the rewritings registers almost 20% of wins. Moreover, about 45% of those wins are by 20% or more and 6% by 50% or more.

In summary, we see that the rewriting can provide programs that perform complementary to the original. This complementarity is perhaps somewhat surprising, because aggregates are assumed to lead to better performance. Our results show that the picture is more complicated and whether rewriting with aggregates yields better performance is instance-dependent. It is also interesting to note that for some encodings replacing simple counting, like that used in the Latin Square encodings (no two identical elements in a row or column), with the count aggregates does not lead to substantial improvements. Finally, for some problems (in our experiments for the Steiner Tree problem) where complementary behavior does emerge, the differences in performance are relatively small.

5 Future Work

As detailed in Section 3.3, the Automated Aggregator software can be improved. Extending the software so that it rewrites rules by eliminating the counting aggregate by inverting the current rewriting seems to be a potentially most beneficial direction. Indeed, our results show that introducing aggregates does not guarantee uniformly improved performance. This gives reason to think that removing the counting aggregate, that is, rewriting it in an explicit way, has a potential of generating encodings that on many instances may perform better. Just like the present version of the AAgg system, this could yield collections of encodings of complementary strengths. Moreover, this form of rewriting would be quite widely applicable, as the counting aggregate is commonly used.

Expanding the software to detect and perform multiple aggregate equivalence rewritings on a single rule and to detect more obscure forms of representations are two other directions for improvement. While necessary to ensure the software has a possibly broad scope of applicability, we do not expect these extensions to have a major practical impact due to low frequency with which such less intuitive and convoluted forms of modeling are found in programs.

Automated rewriting has been studied before. The Projector system [9] and the lpopt system [2] are two notable examples. That earlier work sought to develop rewritings improving the performance over the original ones. That goal is in general difficult to meet; both systems were shown to offer gains, but the rewritten encodings are not always performing better. Our goal was different. We aimed at rewritings of varying relative performance depending on input instances. With the AAgg system we showed that even a rather simple rewriting consisting of introducing the count aggregate often leads to families of encodings with complementary strengths (areas of superior performance). This opens a way for using machine learning to develop models in support of effective encoding selection or encoding portfolio solving [13]. In such work, to generate promising collections of encodings, one could use the AAgg system, with extensions mentioned above, but also other program rewriting software such as Projector and lpopt which, as noted, while they do yield good encodings, often better than the original one, they do not perform uniformly better.

References

  • [1]
  • [2] Manuel Bichler, Michael Morak & Stefan Woltran (2016): lpopt: A Rule Optimization Tool for Answer Set Programming. In Manuel V. Hermenegildo & Pedro López-García, editors: Proceedings of the 26th International Symposium on Logic-Based Program Synthesis and Transformation, LOPSTR 2016, Selected Papers, Lecture Notes in Computer Science 10184, Springer, pp. 114–130, 10.1007/978-3-319-63139-4_7.
  • [3] Matthew Buddenhagen & Yuliya Lierler (2015): Performance Tuning in Answer Set Programming. In Francesco Calimeri, Giovambattista Ianni & Miroslaw Truszczynski, editors: Proceedings of the 13th International Conference on Logic Programming and Nonmonotonic Reasoning, LPNMR 2015, Lecture Notes in Computer Science 9345, Springer, pp. 186–198, 10.1007/978-3-319-23264-5_17.
  • [4] Francesco Calimeri, Wolfgang Faber, Martin Gebser, Giovambattista Ianni, Roland Kaminski, Thomas Krennwallner, Nicola Leone, Marco Maratea, Francesco Ricca & Torsten Schaub (2020): ASP-Core-2 Input Language Format. Theory Pract. Log. Program. 20(2), pp. 294–309, 10.1017/S1471068419000450.
  • [5] Paolo Ferraris, Joohyung Lee, Vladimir Lifschitz & Ravi Palla (2009): Symmetric Splitting in the General Theory of Stable Models. In Craig Boutilier, editor: Proceedings of the 21st International Joint Conference on Artificial Intelligence, IJCAI 2009, pp. 797–803. Available at http://ijcai.org/Proceedings/09/Papers/137.pdf.
  • [6] M. Gebser, R. Kaminski, B. Kaufmann, M. Lindauer, M. Ostrowski, J. Romero, T. Schaub & S. Thiele (2015): Potassco User Guide. Available at https://github.com/potassco/guide/releases/.
  • [7] Martin Gebser, Roland Kaminski, Benjamin Kaufmann & Torsten Schaub (2011): Challenges in Answer Set Solving. In Marcello Balduccini & Tran Cao Son, editors: Logic Programming, Knowledge Representation, and Nonmonotonic Reasoning - Essays Dedicated to Michael Gelfond on the Occasion of His 65th Birthday, Lecture Notes in Computer Science 6565, Springer, pp. 74–90, 10.1007/978-3-642-20832-4_6.
  • [8] Martin Gebser, Roland Kaminski, Benjamin Kaufmann & Torsten Schaub (2019): Multi-shot ASP solving with clingo. Theory Pract. Log. Program. 19(1), pp. 27–82, 10.1017/S1471068418000054.
  • [9] Nicholas Hippen & Yuliya Lierler (2019): Automatic Program Rewriting in Non-Ground Answer Set Programs. In José Júlio Alferes & Moa Johansson, editors: Proceedings of the 21th International Symposium on Practical Aspects of Declarative Languages, PADL 2019, Lecture Notes in Computer Science 11372, Springer, pp. 19–36, 10.1007/978-3-030-05998-9_2.
  • [10] Holger H. Hoos, Marius Lindauer & Torsten Schaub (2014): claspfolio 2: Advances in Algorithm Selection for Answer Set Programming. Theory Pract. Log. Program. 14(4-5), pp. 569–585, 10.1017/S1471068414000210.
  • [11] Yuliya Lierler (2019): Strong Equivalence and Program’s Structure in Arguing Essential Equivalence Between First-Order Logic Programs. In José Júlio Alferes & Moa Johansson, editors: Proceedings of 21th International Symposium on Practical Aspects of Declarative Languages, PADL 2019, Lecture Notes in Computer Science 11372, Springer, pp. 1–18, 10.1007/978-3-030-05998-9_1.
  • [12] Vladimir Lifschitz & Hudson Turner (1994): Splitting a Logic Program. In Pascal Van Hentenryck, editor: Proceedings of the 11th International Conference on Logic Programming, ICLP 1994, MIT Press, pp. 23–37.
  • [13] Liu Liu & Miroslaw Truszczynski (2019): Encoding Selection for Solving Hamiltonian Cycle Problems with ASP. In Bart Bogaerts, Esra Erdem, Paul Fodor, Andrea Formisano, Giovambattista Ianni, Daniela Inclezan, Germán Vidal, Alicia Villanueva, Marina De Vos & Fangkai Yang, editors: Proceedings of the 35th International Conference on Logic Programming, ICLP 2019, Technical Communications, EPTCS 306, pp. 302–308, 10.4204/EPTCS.306.35.
  • [14] John R. Rice (1976): The Algorithm Selection Problem. Advances in Computers 15, pp. 65–118, 10.1016/S0065-2458(08)60520-3.
  • [15] Lin Xu, Frank Hutter, Holger H. Hoos & Kevin Leyton-Brown (2008): SATzilla: Portfolio-based Algorithm Selection for SAT. J. Artif. Intell. Res. 32, pp. 565–606, 10.1613/jair.2490.

Appendix A Correctness of Rewritings

Let us consider the following program rule (HH may be \bot):

H_1ibF(X_i,𝒁,𝒁),Q(𝑿),G(𝒁,𝒁′′).H\leftarrow\bigwedge_{\_}{1\leq i\leq b}F(X_{\_}i,\bm{Z},\bm{Z^{\prime}}),Q(\bm{X}),G(\bm{Z^{\prime}},\bm{Z^{\prime\prime}}). (7)

where FF is a predicate, 𝑿\bm{X} a tuple of variables X_1,,X_bX_{\_}1,\ldots,X_{\_}b, Q(𝑿)Q(\bm{X}) is a list of literals over variables X_1,,X_bX_{\_}1,\ldots,X_{\_}b, and 𝒁,𝒁\bm{Z},\bm{Z^{\prime}} and 𝒁′′\bm{Z^{\prime\prime}} are three pairwise disjoint tuples of variables, and disjoint with 𝑿\bm{X}, and HH contains none of X_iX_{\_}i.

Let PP be a program containing rule (7) and let PP^{\prime} be the program obtained from PP by replacing that rule with the two rules

H_1ibF(X_i,𝒁,𝒁),Q(𝑿),G(𝒁,𝒁′′),F(𝒁).F(𝒁)F(X,𝒁,𝒁).\displaystyle\begin{split}H&\leftarrow\bigwedge_{\_}{1\leq i\leq b}F(X_{\_}i,\bm{Z},\bm{Z^{\prime}}),Q(\bm{X}),G(\bm{Z^{\prime}},\bm{Z^{\prime\prime}}),F^{\prime}(\bm{Z}).\\ F^{\prime}(\bm{Z})&\leftarrow F(X,\bm{Z},\bm{Z^{\prime}}).\end{split} (8)

where FF^{\prime} is a predicate not occurring in PP.

Theorem A.1.

The programs PP and PP^{\prime} have the same answer sets modulo ground atoms of the form F(𝐳)F^{\prime}(\bm{z}).

Proof.

(Sketch) Consider a ground instance rr of rule (7) and let aa be the first atoms in the body of rr (that is, a ground instance of the atom F(X_1,𝒁,𝒁)F(X_{\_}1,\bm{Z},\bm{Z^{\prime}})). In PP^{\prime} there are ground rules rr^{\prime} and r′′r^{\prime\prime} obtained from (8) using the same variable instantiation as that used to produce rr. Clearly, rr contributes to the reduct of 𝑔𝑟𝑜𝑢𝑛𝑑(P)\mathit{ground}(P) if and only if rr^{\prime} and r′′r^{\prime\prime} contribute to the reduct of 𝑔𝑟𝑜𝑢𝑛𝑑(P)\mathit{ground}(P^{\prime}). Moreover, if they do, rr “fires” in the least model computation if and only if rr^{\prime} fires in the least model computation. It follows that an interpretation II of PP is an answer set of PP if and only if IJI\cup J is an answer set of PP^{\prime}, where JJ consists of all atoms F(𝒛)F^{\prime}(\bm{z}) such that F(x,𝒛,𝒛)IF(x,\bm{z},\bm{z^{\prime}})\in I, for some constant xx and a tuple of constants 𝒛\bm{z^{\prime}}. ∎

Next, we recall the following theorem proved by Lierler [11].

Theorem A.2.

Let HH be an atom (HH may be \bot), GG a list of literals, XX a variable, 𝐙\bm{Z} a tuple of variables, each different from XX and each with at least one occurrence in a literal in GG, and FF a predicate symbol of arity 1+|𝐙|1+|\bm{Z}|. If bb is an integer, and X_1,,X_bX_{\_}1,\ldots,X_{\_}b are variables without any occurrence in HH and GG, then the logic program rule

H_1ibF(X_i,𝒁),_1i<jbX_iX_j,G.H\leftarrow\bigwedge_{\_}{1\leq i\leq b}F(X_{\_}i,\bm{Z}),\bigwedge_{\_}{1\leq i<j\leq b}X_{\_}i\neq X_{\_}j,G. (9)

is strongly equivalent to the logic program rule

Hb#count{X:F(X,𝒁)},G.H\leftarrow b\leq\#count\{X:F(X,\bm{Z})\},G. (10)

where \bigwedge is used to represent a sequence of expressions separated by commas.

Combining the two results leads to the following result that proves the correctness of the first rewriting implemented by our tool AAgg.

Theorem A.3.

Let PP be a program containing a rule rr of the form

H_1ibF(X_i,𝒁,𝒁),_1i<jbX_iX_j,G(𝒁,𝒁′′).H\leftarrow\bigwedge_{\_}{1\leq i\leq b}F(X_{\_}i,\bm{Z},\bm{Z^{\prime}}),\bigwedge_{\_}{1\leq i<j\leq b}X_{\_}i\neq X_{\_}j,\ \ G(\bm{Z^{\prime}},\bm{Z^{\prime\prime}}). (11)

under the same assumptions about variable tuples 𝐗,𝐙,𝐙\bm{X},\bm{Z},\bm{Z^{\prime}} and 𝐙′′\bm{Z^{\prime\prime}} as before. If 𝐙\bm{Z} is empty, let PP^{\prime} be PP. Otherwise, let PP^{\prime} be obtained from PP by replacing rr with the rules (8), adjusting the first of them to contain _1i<jbX_iX_j\bigwedge_{\_}{1\leq i<j\leq b}X_{\_}i\neq X_{\_}j in place of Q(𝐗)Q(\bm{X}). Let us call the first of these two rules rr (reusing the name of the original rule). Finally, let P′′P^{\prime\prime} be obtained from PP^{\prime} by replacing rr with the corresponding rule (10). Then, the programs PP and PP^{\prime} have the same answer sets (in the case, when 𝐙\bm{Z} is not empty, the same answer sets modulo atoms F(𝐳)F^{\prime}(\bm{z})).

Proof.

Clearly, rule rr is a special case of a rule of the form (7). By A.1, programs PP and PP^{\prime} have the same answer sets (when 𝒁\bm{Z}, is not empty, the same answer sets modulo atoms F(𝒛)F^{\prime}(\bm{z})). The rule rr in PP^{\prime} is of the form (9) required by Theorem A.2. Applying this theorem shows that programs PP^{\prime} and P′′P^{\prime\prime} have the same answer sets and the assertion follows.
 

Once a program has a rule of the form (10), we can often modify it further by exploiting alternative encodings of the aggregate expressions. In particular, under some assumptions about the structure of the program, we can replace a rule (10) by

Hnot#count{X:F(X,𝒁)}<b,G.H\leftarrow not\>\#count\{X:F(X,\bm{Z})\}<b,G. (12)

where we assume that the variable XX is not a variable in 𝒁\bm{Z}, and that all variables in 𝒁\bm{Z} appear in GG.

We recall that a partition (P_b,P_t)(P_{\_}b,P_{\_}t) of a program PP is a splitting of PP if no predicate appearing in the head of a rule from P_tP_{\_}t appears in P_bP_{\_}b [12, 5]. A well-known result on splitting states that answer set of programs that have a splitting can be described in terms of answer sets of programs that form the splitting.

Theorem A.4.

Let PP be a logic program and let (P_b,P_t)(P_{\_}b,P_{\_}t) be a splitting of PP. For every answer set I_bI_{\_}b of P_bP_{\_}b, every answer set of the program P_tI_bP_{\_}t\cup I_{\_}b is an answer set of PP. Conversely, for every answer set II of PP, there is an answer set I_bI_{\_}b of P_bP_{\_}b such that II is an answer set of P_tI_bP_{\_}t\cup I_{\_}b.

This result implies that in programs that have a splitting, a rule in P_tP_{\_}t containing in its body an aggregate expression involving only predicates appearing in P_bP_{\_}b can be replaced by a rule in which this aggregate expression is replaced by any of its (classically) equivalent forms. We formally state this result for the case of rules of the form (10).

Theorem A.5.

Let PP be a logic program and let (P_b,P_t)(P_{\_}b,P_{\_}t) be a splitting of PP. If P_tP_{\_}t contains a rule of the form (10) and FF appears in P_bP_{\_}b, then PP and the program PP^{\prime} obtained from PP by replacing the rule (10) by the rule (12) have the same answer sets.

Proof.

(Sketch) Let P_tP_{\_}t^{\prime} be the program obtained from P_tP_{\_}t by replacing the rule (10) by the rule (12). It is clear that (P_b,P_t)(P_{\_}b,P_{\_}t^{\prime}) is a splitting of PP^{\prime}. Let II be an answer set of PP. By A.4, there is an answer set I_bI_{\_}b of P_bP_{\_}b such that II is an answer set of P_tI_bP_{\_}t\cup I_{\_}b. In particular, II is an answer set of the program I_b𝑔𝑟𝑜𝑢𝑛𝑑(P_t)I_{\_}b\cup\mathit{ground}(P_{\_}t). Let QQ be the program obtained by simplifying the bodies of the rules in 𝑔𝑟𝑜𝑢𝑛𝑑(P_t)\mathit{ground}(P_{\_}t) as follows. If a conjunct cc in the body of a rule in 𝑔𝑟𝑜𝑢𝑛𝑑(P_t)\mathit{ground}(P_{\_}t) involves only atoms from the Herbrand base HB(P_b)HB(P_{\_}b) of P_bP_{\_}b, we remove cc if I_bcI_{\_}b\models c, and we remove the rule, if I_b⊧̸cI_{\_}b\not\models c. Because atoms from HB(P_b)HB(P_{\_}b) do not appear in the heads of the rules in 𝑔𝑟𝑜𝑢𝑛𝑑(P_t)\mathit{ground}(P_{\_}t), II is an answer set of QI_bQ\cup I_{\_}b.

We denote by QQ^{\prime} the program obtained by the same simplification process from 𝑔𝑟𝑜𝑢𝑛𝑑(P_t)\mathit{ground}(P_{\_}t^{\prime}). From the definition of P_tP_{\_}t^{\prime} it follows that Q=QQ^{\prime}=Q (indeed, the only difference between P_tP_{\_}t and P_tP_{\_}t^{\prime} is in the bodies of some rules, in which an aggregate built entirely from the atoms in HB(P_b)HB(P_{\_}b) is replaced by a classically equivalent one; thus, both expressions evaluate in the same way under I_bI_{\_}b and the contribution of the corresponding rules to QQ and QQ^{\prime} in each case is the same). Consequently, II is an answer set of QI_bQ^{\prime}\cup I_{\_}b. Because atoms from HB(P_b)HB(P_{\_}b) do not appear in the heads of the rules in 𝑔𝑟𝑜𝑢𝑛𝑑(P_t)\mathit{ground}(P_{\_}t^{\prime}), II is an answer set of 𝑔𝑟𝑜𝑢𝑛𝑑(P_t)I_b\mathit{ground}(P_{\_}t^{\prime})\cup I_{\_}b and, because (P_b,P_t)(P_{\_}b,P_{\_}t^{\prime}) is a splitting of PP^{\prime}, also an answer set of PP^{\prime}. A similar argument shows that answer sets of PP^{\prime} are also answer sets of PP. ∎

It is clear that the same argument applies to other similar rewritings, for instance, to the one that replaces the rule (9) by the rule

H_0i<b1noti=#count{X:F(X,𝒁)},G.H\leftarrow\bigwedge_{\_}{0\leq i<b-1}not\>i=\#count\{X:F(X,\bm{Z})\},G. (13)

where, as before, we assume that the variable XX is not a variable in 𝒁\bm{Z}, and that all variables in 𝒁\bm{Z} appear in GG.

Theorem A.5 establishes the correctness of the two remaining rewritings implemented by the tool AAgg. The tool is designed to check for splitting and allows the rewriting to take place only if the conditions of A.5 hold.