Solution Enumeration by Optimality in Answer Set Programming

Given a combinatorial search problem, it may be highly useful to enumerate its (all) solutions besides just finding one solution, or showing that none exists. The same can be stated about optimal solutions if an objective function is provided. This work goes beyond the bare enumeration of optimal solutions and addresses the computational task of solution enumeration by optimality (SEO). This task is studied in the context of Answer Set Programming (ASP) where (optimal) solutions of a problem are captured with the answer sets of a logic program encoding the problem. Existing answer-set solvers already support the enumeration of all (optimal) answer sets. However, in this work, we generalize the enumeration of optimal answer sets beyond strictly optimal ones, giving rise to the idea of answer set enumeration in the order of optimality (ASEO). This approach is applicable up to the best k answer sets or in an unlimited setting, which amounts to a process of sorting answer sets based on the objective function. As the main contribution of this work, we present the first general algorithms for the aforementioned tasks of answer set enumeration. Moreover, we illustrate the potential use cases of ASEO. First, we study how efficiently access to the next-best solutions can be achieved in a number of optimization problems that have been formalized and solved in ASP. Second, we show that ASEO provides us with an effective sampling technique for Bayesian networks.


Introduction
In this paper, we address combinatorial problem solving where some solution components are combined to meet problem specific requirements. Such problems are frequent in computer science and computationally hard to solve since the number of solution candidates is usually exponential in the length of a problem instance (Karp 1972). Besides finding solutions for an instance, it is also possible to seek solutions that are optimal as defined by an objective function f mapping solutions to numbers. In fact, combinatorial optimization problems described, e.g., by Korte and Vygen (2006) tend to be computationally harder to solve, since the proof for optimality presumes the exclusion of yet better solutions. However, optimal solutions are not necessarily unique, suggesting the search and enumeration of all optimal solutions, e.g., for the sake of generating alternatives or better understanding the true nature of the objective function f . Besides enumeration, Murty (1968) was interested in the ranking of solutions based on their cost, taking systematically into consideration sub-optimal solutions. While this idea is general by nature, it is applied to a specific problem where cost-optimal assignments are sought based on a cost matrix. If, in addition, a bound k on the number of solutions is introduced, we arrive at the identification of the best k solutions as in the case of the shortest path problem studied by Lawler (1972).
In this work, we adopt the ideas discussed above and concentrate on the enumeration of optimal solutions but also take recursively into consideration sub-optimal but next-best solutions according to f . This gives rise to a systematic procedure that we coin as solution enumeration by optimality (SEO) which can also be understood as sorting solutions by their values obtained from the objective function. Since the underlying solution space is usually worst-case exponential, it is immediate that SEO poses computational challenges in general. For simplicity, we confine our attention to problems and solving paradigms yielding finite solution spaces in this work.
Besides tailoring native search algorithms, one viable way to solve combinatorial (optimization) problems is to describe their solutions in terms of constraints and to use existing solver technology for the search of actual solutions. To this end, well-known approaches are (maximum) Boolean satisfiability (SAT/MaxSAT) (Biere et al. 2009), integer linear programming (ILP), constraint programming (CP) (Rossi et al. 2006), and answer set programming (ASP) (Brewka et al. 2011) with optimization (Simons et al. 2002;Brewka et al. 2003). While the number of solutions is generally unbounded in ILP, MaxSAT, CP, and ASP yield finite solution spaces in their standard use cases, hence enabling the exhaustive enumeration of all solutions in finite time.
As regards systematic solution enumeration, ASP offers somewhat better premises, since a typical ASP encoding aims at a one-to-one correspondence between solutions and answer sets. Gebser et al. (2007a) present dedicated algorithms for answer set enumeration (ASE) that are able to operate in polynomial space and to project answer sets with respect to a user-defined signature. Furthermore, in the presence of an objective function, the enumeration of optimal answer sets (ASO) is similarly feasible, once the respective optimum value of the objective function has been determined. In contrast, the enumeration features of contemporary SAT/MaxSAT solvers are quite limited although the algorithmic ideas are described by Gebser et al. (2009). For this reason, we concentrate on ASP solvers that natively support solution enumeration (SE) and the enumeration of optimal solutions (SO) as discussed above, but not SEO in any systematic way.
Our main goal is to combine both answer set enumeration (ASE) and the enumeration of optimal answer sets (ASO) as the task of enumerating answer sets in the order of optimality as determined by an objective function f . In ASP, such functions are usually pseudo-Boolean expressions that assign integer weights to literals. The outcome, i.e., answer set enumeration by optimality (ASEO) realizes SEO under the view that answer sets bijectively capture the solutions of a problem being solved. In the sequel, we will explore ways to extend ASO procedures such that after enumerating all optimal solutions the search is continued to enumerate next-best solutions, and so on. Basically, this is achieved by performing ASO recursively but a careless implementation of this strategy may jeopardize the desired (polynomial) consumption of space.
When it comes to the enumeration of answer sets S as guided by an objective function f (S), we will explore two mainstream approaches. The first aims at enumerating all answer sets unless the enumeration procedure is interrupted by the user. The second presumes a parameter k that gives the number of answer sets to be enumerated and, thereafter, the best k answer sets S are sought given f (S). The latter approach opens up new possibilities for organizing ASEO when k is small enough for storing intermediate solutions in memory. To realize these two approaches, we will deploy existing enumeration and optimization algorithms of Gebser et al. (2007a) and their implementations in the Clingo system. The main contributions of our work are: 1. The concept of answer set enumeration by optimality (ASEO) as a process where answer sets S are produced in an order determined by an objective function f (S). 2. Relating ASE, ASO, and ASEO from the perspective of computational complexity. 3. The development of basic algorithms that implement ASEO either in limited settings (the best k answer sets) or without limitation (sorting answer sets subject to f ). 4. An experimental evaluation of algorithms using ASP encodings of optimization problems. 5. Demonstrating the potential of ASEO in sampling guided by objective functions f .
It should be stressed that the main ideas of the paper can be generalized for other problem solving paradigms supporting optimization as long as the solution spaces are finite.
This paper is organized as follows. First, we briefly recall some concepts of answer set programming (ASP) and optimization in Section 2, and assess the computational complexity of solution enumeration by optimality (SEO) in the context of ASP (ASEO) in Section 3. Given these premises, we present our mixed algorithms for ASEO in Section 4. The efficiency of the novel ASEO algorithms is then studied in Section 5 by using optimization problems from ASP competitions as benchmarks. Yet further application is established in Section 6: it is shown and experimentally verified that our ASEO algorithms provide a potentially effective method for sampling and approximate inference on Bayesian networks. Section 7 concludes the paper.

Preliminaries
The basic syntax of answer set programming (ASP) (Brewka et al. 2011) is based on rules where h i :s, b i :s, and c i :s are atoms. A constraint is a rule (1) with an empty head (l = 0). A logic program P is a finite set of rules (1). The program P is called normal if l ≤ 1 holds for every rule (1) and disjunctive, otherwise. Given a set of atoms A, the reduct P A of P with respect to A contains a positive rule h 1 | . . . | h l :-b 1 , . . . , b n for each rule (1) of P such that l > 0 and no negated atom c i is in A. Given a logic program P , let At(P ) be the signature of P , i.e., the set of atoms that occur in P . An answer set A ⊆ At(P ) of P (i) violates no constraint of P and (ii) is a subset-minimal set closed under P A , i.e., if b 1 ∈ A, . . . ,b n ∈ A for some rule h 1 | . . . | h l :-b 1 , . . . , b n of P A , then at least one h i is in A. Each program P induces set of answer sets, denoted AS(P ), usually capturing the solutions of a search problem encoded by P . The syntax of ASP has been generalized in various ways as proposed, e.g., by Simons et al. (2002), but many extensions can be translated back into rules of the form (1) using transformations of Bomanson et al. (2014) and Alviano et al. (2015). To cater for optimization problems within ASP, it is possible to introduce objective functions to identify optimal answer sets. For the purposes of this work, an objective function f (A) is defined as a pseudo-Boolean expression where each w i is a non-negative integer weight and each l i is a literal, i.e., either an atom 'a' or its negation 'not a'. If desired, negative weights can also be tolerated but typically reduced back to non-negative ones. Given an answer set A ∈ AS(P ), the value f (A) of (2) at A is where |= denotes the (standard) satisfaction of a literal l i in A: (i) A |= a iff a ∈ A, and (ii) A |= not a iff a ∈ A. An answer set A ∈ AS(P ) is optimal (in the sense of minimization) if f (A ) < f (A) for no A ∈ AS(P ). Maximization can also be supported by transforming f into f suitable for minimization (Simons et al. 2002). Certain applications call for several objective functions f i (2) indexed by a priority level 1 ≤ i ≤ p. The resulting values (3) are interpreted lexicographically, i.e., f 1 (A) is the most important, then f 2 (A), etc. So, given an answer set (candidate) A, its (prioritized) objective value f (A) is essentially a tuple f 1 (A), . . . ,f p (A) .

Complexity Landscape
In the following, we assume some familiarity with basic notions of computational complexity; see, e.g., the book of Papadimitriou (1994) for an account. As regards decision problems, checking the existence of an answer set for a normal (resp. disjunctive) ground logic program given as input is NP-complete (resp. Σ p 2 -complete) as respectively shown by Marek and Truszczyński (1991) and Eiter and Gottlob (1995). However, the task of computing an optimal answer set A for a normal logic program subject to an objective function f forms an FP NP -complete function problem as established by Simons et al. (2002). This means that only polynomially many calls to an NP-oracle are required in the worst case. Indeed, the last call is typically the most demanding one since it excludes the existence of yet better answer sets. Once an optimal answer set A has been determined, the respective optimum value f (A) is naturally also known.
Let us then address the computational complexity of enumerating solutions in an order determined by an objective function f . Our first result concerns solution enumeration in general, i.e., the tasks SE, SO, and SEO, identified earlier. It is assumed that a search problem instance P has some finite representation, ultimately encoded as a string, based on which solutions S can be sought and enumerated, and the value f (S) of the objective function f is computable.

Proposition 1
Given a search problem instance P and an objective function f , (i) the task SE is (constant-time) reducible to SO and SEO and (ii) the task SEO is not reducible to SE nor to SO in general.

Proof
(i) The instance P can be reduced to an instance P, f of SO and SEO by setting a constant value f (S) = c for every solution S of P . Then, both SO and SEO implement SE since the order of solutions is irrelevant and all solutions get enumerated in the end.
(ii) Suppose that the instance P, f of SEO were reducible to an instance P of SE without an objective function. To enumerate the solutions of P , the instance P must possess the same set of solutions. However, since there is no objective function for P under SE, these solutions can be enumerated in any order, not necessarily compatible with f .
Quite similarly, suppose that the instance P, f of SEO is reducible to an instance P , f of SO. Since the latter must yield all solutions to P , the instance P must have the same solutions as P and, in addition, each solution S to P must be optimal given f , i.e., f (S) is constant. Thus, the (optimal) solutions to P can be enumerated in any order, not necessarily compatible with f .
To conclude, reductions are not feasible in general, regardless of computational resources.
On the other hand, if only the enumeration of the best k solutions is of interest, the respective enumeration task may become easier than SEO: fewer solutions have to be enumerated. For sufficiently small values of k, the task may be even easier than SO but harder than SO if k exceeds the number of best solutions. However, the number of optimal solutions is not known in advance, which makes SEO an attractive option for the user. Typical answer set solvers perform SO with k = 1 by default when optimizing answer sets. Next we concentrate on the enumeration of answer sets only and gather evidence that ASEO can be more demanding than ASE or ASO in general. One way to implement ASEO is to use ASO repeatedly as a subroutine and to exclude the solutions already enumerated by a constraint. It is established below that the intermediate instances of ASO remain equally hard enumeration problems in the worst case.
Theorem 1 Given a normal logic program P subject to an objective function f of the form (2) and a nonnegative integer lower bound b for f , the task of computing the next-best answer set A subject to

Proof
The bound b and the objective function f (A) for a candidate answer set A can be incorporated by using a single weight rule r (Simons et al. 2002) that effectively expresses f (A) ≥ b and by translating that rule into normal rules (Bomanson et al. 2014). The translation N (r) is feasible in polynomial time and P ∪N (r) subject to f is an (unbounded) instance of answer-set optimization and, thus, a member in FP NP as shown by Simons et al. (2002). Hardness for FP NP is implied by the bound b = 0 and the original (unbounded) complexity result of Simons et al. (2002).
Proposition 1 does not cover the reducibility of an instance P, f of ASO with respect to ASE and ASEO. Interestingly, if the objective value f (A) = b for a best answer set A were known, reductions would become feasible. By expressing the condition f (A) ≤ b with a weight rule r rewritten as a set N (r) of normal rules (cf. the proof of Theorem 1), the instance P ∪ N (r) of ASE, or the instance P ∪ N (r), f of ASEO, would capture the original ASO problem. The key observation is that such a bound b on f cannot be determined in general in polynomial time.

Proposition 2
The problem ASO is not polytime reducible to ASE nor to ASEO unless P = coNP.

Proof
Let P be a normal program forming an instance of deciding if AS(P ) = ∅ or not. In general, this problem was shown coNP-complete by Marek and Truszczyński (1991). Let us construct a program Q by introducing new atoms a and b as well as rules "a :-not b.", "b :-not a.", and the rules of P conditioned by the atom b. Then {a} ∈ AS(Q) and, if A ∈ AS(P ), then A ∪ {b} ∈ AS(Q). By setting an objective function f (A) = 2 × a + 1 × b, the answer set {a} is optimal only if answer sets of the latter kind do not exist. Towards our goal, let us assume that R is the polytime reduction of Q, f into an ASE problem. We can check in polynomial time if {a} ∈ AS(R) (i.e., whether it should be enumerated by ASE), iff AS(P ) = ∅.
Similarly, let R, g be a polytime reduction of Q, f into an ASEO problem. Again, we can test if {a} ∈ AS(R) in polynomial time, giving rise to two cases. (i) If {a} ∈ AS(R), then Q has answer sets of the form A ∪ {b}, all to be enumerated by ASEO and equally admitted by g, i.e., f (A ∪ {b}) = b is constant. Thus AS(P ) = ∅. (ii) If {a} ∈ AS(R), two subcases arise: is not minimal, then R must have an answer set B = {a} such that g(B) < g ({a}). Without loss of generality, assume that g(B) is minimal. Then B is to be enumerated by ASEO but also {a}, since ASEO will enumerate the inferior answer sets as well.
Then both B and {a} should enumerated by ASO on the input Q, f , a contradiction.

Thus g({a})
is minimal and {a} is to be enumerated by ASEO. Then it must be enumerated by ASO given Q, f excluding all other answer sets. Thus AS(P ) = ∅.
Thus we obtained a (hypothetical) polynomial-time test whether AS(P ) = ∅.

Proposition 3
In the worst case, a (normal) logic program P subject to an objective function f may possess exponentially many answer sets A with different objective values f (A).

Proof
For an illustration, let us consider a concrete normal program P n based on atoms a 1 , . . . ,a n and b 1 , . . . ,b n parameterized by n. The intended objective function f (a 1 , . . . ,a n ) = n i=1 2 i−1 ×a i . Thus assignments to a 1 , . . . ,a n encode distinct values in the range 0, . . . ,2 n − 1 in binary. Using complementary atoms a for the atoms a involved, the rules of P n are for all i = 1, . . . ,n: The rules of P n serve the following purposes. The rules of the first line simply choose truth values for a i :s and b i :s. The rules in the second line check whether the value represented by a i :s is lower than the value represented by b i :s such that lt i takes bits i, . . . ,n into account. Regarding constraints in the third line, they accept only one half of possible values for b i :s given some fixed values of a i :s. Thus P n has 2 n−1 answer sets A corresponding to each (fixed) assignment to the atoms a 1 , . . . ,a n and, in total, 2 n × 2 n−1 = 2 2n−1 answer sets. Given an answer set A of P n , the objective value f (A) is f (a 1 , . . . ,a n ) = n i=1 2 i−1 × a i based on the truth values of a 1 , . . . ,a n in A. Given P n and f , any ASEO algorithm must enumerate all answer sets A with value f (A) = 0 first, all answer sets with value f (A) = 1 next, and so on, until (finally) all answer sets with value f (A) = 2 n − 1 get enumerated. Each subtask in this sequence can be viewed as an ordinary ASO task yielding 2 n−1 assignments for b i :s and those assignments are different for each assignment of a i :s. This is to prevent the learning of nogoods for systematically excluding certain values of b i :s. In this way, P n maximally exercises the underlying enumeration procedure, although finding a single answer set is yet easy.

Algorithms for Answer Set Enumeration by Optimality
Propositions 1-3 support the view that ASEO can be strictly more demanding task than ASE and ASO. This, however, does not prevent us from using ASE and ASO as subroutines when realizing ASEO in practice. Therefore, we implement our ASEO algorithms on top of a stateof-the-art ASP solver Clingo (Gebser et al. 2008) treated as a black box for running ASO and ASE subtasks for systematic answer set enumeration. Clingo implements an efficient search for answer sets, as witnessed by ASP competitions (see, e.g., the report of Calimeri et al. (2016)), deploying conflict-driven nogood learning (CDNL) algorithms devised by Gebser et al. (2007b). However, sub-optimal answer sets cannot be readily enumerated, thus motivating the goals of our work. A way to implement ASEO is to use Clingo's Python API that extends the functionality of the underlying solver. While the API enables quite straightforward implementation of ASEO algorithms detailed in the sequel, it excludes certain options available for native implementations.
We call our first approach to ASEO the naive algorithm. The algorithm simply enumerates all for i from 1 to p do 16: . . ,f p (A) for priority levels are recorded for an input program P . Once the ordinary ASE task is completed, the pairs recorded are sorted lexicographically based on their objective values. Then the output consists of all answer sets of the program in the sorted order. While our naive algorithm for ASEO is easy to implement, its obvious weakness is its consumption of space. However, it may perform surprisingly well in cases where the number of answer sets is low. Therefore, it makes a reasonable baseline algorithm for our purposes as well as a basis for comparisons against more sophisticated algorithms.
In contrast with the naive algorithm, the ASEO task can be alternatively implemented without enumerating all answer sets first. Such a procedure is obtained by first (i) performing ASO followed by (ii) the introduction of constraints that exclude solutions enumerated so far. Finally, (iii) the ASO task is performed on increasingly constrained problem instances until the desired number of answer sets have been found. The constraints have to be introduced with extra care so that the (polynomial) space consumption of the underlying ASEO task is not jeopardized. Algorithm 1 is such a constraining algorithm that augments the input program P between subsequent enumeration tasks. Once all currently optimal answer sets have been enumerated, it appends constraints that make subsequent answer sets strictly worse than those enumerated so far. We know by Theorem 1 that this does not necessarily make the remaining computational task any easier, i.e., the subsequent computation of next-best solutions can still be challenging. For a cost C = eval A ( ni j=1 (w i j × l i j )) associated with an answer set A and a priority level 1 ≤ i ≤ p, a rule-based constraint GT i (C), mimicking the ASP core standard (Calimeri et al. 2020), is: The objective value at the priority level 1 ≤ i ≤ p is fixed with a similar constraint EQ i (C): The weight enumeration algorithm starts in Line 2 by discovering an optimal answer set A using Clingo's optimization algorithm. The call ASP-Optimize(P ) returns an optimal answer set, if such an answer set A exists. Lines 5-7 cover programs having no answer sets. Lines 14-23 calculate objective values f 1 (A), . . . ,f p (A) for priority levels and introduce constraints (5) to enumerate all equally good answer sets. Enumeration is done by Clingo's answer set enumeration algorithm designed by Gebser et al. (2007a): it performs branch-and-bound search based on literals in the input program and conflicts that occur during the enumeration. In Line (18), the call ASP-Solve(P ) receives all answer sets of P , one at a time. Once these get enumerated, Lines 24-29 deploy constraints (5) for levels i = 1 . . . p − 1 and a constraint (4) for level p to find next-best answer sets. Lines 4-12 are used to virtually relax all constraints in the augmented program P so that next-best answer sets from the following priority level can be enumerated.

Example 1
Consider an optimization program P with three answer sets A 1 , A 2 , and A 3 with objective values f (A 1 ) = 1, 4, 1 , f (A 2 ) = 1, 4, 7 , and f (A 3 ) = 1, 7, 4 . Algorithm 1 starts by discovering the (only) optimal answer set A 1 and constrains the answer sets A of P by f 1 (A) = 1, f 2 (A) = 4, and f 3 (A) > 1. These make A 2 optimal and once found, the last is revised to f 3 (A) > 7. As a result, no answer sets exist, the constraints for f 2 and f 3 are superseded by f 2 (A) > 4. When A 3 is found, the constraints are revised to f 1 (A) = 1, f 2 (A) = 7, and f 3 (A) > 4. From this point onward, no answer sets are met, the level p is decreased down to 1, and Algorithm 1 terminates.
Few observations about Algorithm 1 are worthwhile. First, it is designed to find the best k answer sets as determined by the given objective functions f 1 , . . . ,f p . If the full ASEO is rather desired, the parameter k should be set to ∞, enforcing the algorithm to loop over all answer sets in an order compatible with f 1 , . . . ,f p . Second, the algorithm uses an (ordered) set S as an intermediate storage for answer sets. Instead of storing found answer sets in S, an option is to process them immediately, e.g., by printing them for the user. This saves the space taken by S.

Proposition 4
Algorithm 1 can be implemented so that it runs in space polynomial in |At(P )|.

Proof
The ASE and ASO tasks are feasible in polynomial space, see the CNDL-ENUM-ASP algorithm of Gebser et al. (2007a) for details. Using this algorithm via the Clingo API, Algorithm 1 is able to enumerate optimal as well as sub-optimal answer sets. Lines 4 and 12 therein relax constraints used to exclude optimal answer sets enumerated during previous solver calls by forgetting them and by adding tightened ones. Thus the size of P does not increase indefinitely and Algorithm 1 operates in space comparable to CNDL-ENUM-ASP regardless the value of k.
Due to our black-box implementation, Algorithm 1 may suffer from restarts after enumerating all answer sets pertaining to a particular objective value. The underlying solver could benefit from previously learned conflicts and there is even further potential expected from keeping track of seen non-optimal answer sets that could be reused in subsequent runs. However, such functionalities call for a native enumeration algorithm and/or modifications to the underlying solver.
Algorithm 2 realizes a window-based approach where answer sets are computed using an ASE process while keeping the best k answer sets A 1 , . . . ,A k in memory. In addition, the objective value f (A k ) of the k:th best answer set is used as a threshold t to discard found (partial) answer sets that are worse than A k and thus not contributing to the top k answer sets. Our implementation of Algorithm 2 relies much on Clingo API as it needs to keep track of assigned literals as well as to introduce new nogoods for excluding answer sets. As regards storing answer sets, the space requirement of Algorithm 2 is k × |At(P )|. If the underlying ASE algorithm were completely used a black box, the actual enumeration of answer sets is feasible in space polynomial in |At(P )|, but for sufficiently large values of k, this is dominated by k × |At(P )|. However, since Algorithm 2 adds nogoods explicitly and for a different purpose than the underlying ASE algorithm, their number may become exponential in |At(P )| in contrast with Algorithm 1 (cf. Proposition 4). This issue can be best approached in terms of either native algorithms or an enhanced API that is able to keep track and manipulate nogoods used for different purposes.
The main loop of smart enumeration in Lines 2-16 start with a call to ASP-Enumerate(P ) that is based on the standard ASE algorithm of Gebser et al. (2007a). The call provides a new partial assignment L, i.e., a set of literals for the input program P . For each iteration of the loop, L is extended by some chosen literal l and further literals may be derived using propagation. The underlying enumeration algorithm is also responsible for backtracking if L becomes inconsistent. Note that since L is partial, If L is complete (covers all of At(P )), Lines 8-12 append the respective answer set L + = {a ∈ L | a ∈ At(P )} and the corresponding cost(s) C to the list S of best k solutions so far. Moreover, the threshold value t for excluding future answer sets is updated. Line 8 implements insertion sort and places L + , C in the list S based on its cost(s) C. However, if L is not yet complete and its cost vector f (L) is greater than the current threshold value t, the program P can be extended by a nogood for excluding all supersets of L as done in Lines 13-15. The algorithm terminates once the whole search space limited by the nogoods is exhausted.

Example 2
Consider a single objective function f as defined by (l 1 , 5), (l 2 , 1), (l 3 , 2), (l 4 , 2), (l 5 , 6) and all possible answer sets A 1 = {l 1 , l 2 , l 3 }, A 2 = {l 1 , l 3 , l 5 }, A 3 = {l 2 , l 3 , l 5 }, A 4 = {l 1 , l 2 , l 4 }, A 5 = {l 1 , l 4 , l 5 } of a program P for which the best k = 2 answer sets are sought. Suppose that f (A 4 ) = 8 and f (A 3 ) = 9 are the currently known top k = 2 answer sets which gives threshold t = 9. If L = {l 1 } is the current partial answer set and the search heuristic picks l 5 as the next literal to branch on, we end up with L = {l 1 , l 5 } with cost f (L ) = 11. Since 11 > t, any extensions of L are eliminated by a nogood :-l 1 , l 5 . Thus the underlying ASP solver will skip A 2 and A 5 altogether as it cannot construct the corresponding partial assignments anymore.
Algorithms 1 and 2 work quite differently. For the program P n from the proof of Proposition 3, there exist exponentially many answer sets while finding and enumerating optimal values is easy. Using Algorithm 1 on this problem instance is fast due to easy enumeration while Algorithm 2 slows down as it needs to find objective values among an exponential number of possibilities.

Benchmarking: Optimization Problems
The performance of our ASEO algorithms is evaluated on several optimization problems adopted from ASP competitions, as reported by Calimeri et al. (2016). The criterion for choosing problems was solvability by Clingo in a reasonable time. For each benchmark problem, the difficulty of instances increases and the numbers of answer sets are problem-specific. In the experiments, we measure runtime subject to a timeout of 1 800 s. All experiments are conducted using an Intel(R) Xeon(R) CPU (E5-1650 v4 @ 3.60GHz), with 32GB RAM, and Ubuntu OS. Table 1 collects the results obtained for optimization problems. The rows of the table detail runs on different problems while columns denote different runs of our algorithms. The second column shows average time for the naive algorithm as well as number of timeouts in parentheses. The last column shows the collective number of timeouts (#TO) for an enumeration algorithm. A single cell reports the average runtimes for all instances of the benchmark problem in question, and for Algorithms 1 and 2. For each target of enumeration, the better algorithm is indicated in boldface. The average runtimes suggest that the weight enumeration algorithm tends to perform better than smart enumeration. Even when smart enumeration gives faster results, the runtime of weight enumeration is not too far off. Average runtimes obtained for BayesianNL and MaxSAT highlight how much faster weight enumeration can be while Hamilton is the only benchmark with a reverse effect. Yet it is important to note that for certain benchmarks, namely Connected and Hamilton, the naive algorithm that enumerates all answer sets first works faster than others. Fig. 1 reflects how enumeration algorithms scale differently. Fig. 1(a)    is relatively easy for weight enumeration (W). The switch from enumerating 10 answer sets to 10 4 answer sets seems to increase the runtime of weight enumeration somewhat linearly. Meanwhile, the performance of smart enumeration (S) algorithm degrades rabidly to the timeout.
In the same vein, we observe from Fig. 1(b) that the two enumeration algorithms scale quite similarly in the Supertree benchmark problem. Their runtimes start to increase simultaneously but the increase is more drastic for weight enumeration than it is for smart enumeration, and the latter algorithm runs always slightly faster than the former. It is evident that the algorithms behave differently across the instances and relative to each other. Algorithm 1 works considerably better than others when finding an optimal objective value and enumerating optimal values is easy as, for example, in the case of the logic program introduced in the proof of Proposition 3. However, enumeration based algorithms work better when the number of answer sets is reasonably low such that they can be enumerated in comparable time. Algorithm 2 seems to perform especially well when the objective values f (A 1 )≤ . . . ≤f (A k ) for the best k answer sets A 1 , . . . ,A k vary a lot and it is easier to locate the threshold value t = f (A k ) for ruling out worse answer sets.
To summarize the experiments reported in this section, Algorithm 1 (weight enumeration) works reasonably well for a majority of the problem instances. It seems to be able to discover next-best answer sets with ease similar to the previous ones and our best experimental results indicate linear increase in time with respect to the desired number of answer sets k. Meanwhile, Algorithm 2 (smart enumeration) does not perform as well on average and it fails on problem instances that are easy for weight enumeration. However, we are able to demonstrate that there exist problems for which the smart enumeration algorithm can be faster and thus beneficial.

Practical Application: Bayesian Sampling
For a practical application of ASEO, let us consider probabilistic inference in the context of Bayesian networks (BNs) with Boolean random variables and an approach similar to weighted model counting (Chavira and Darwiche 2008). The goal is to approximate queries by computing Maximum a Posteriori (MAP) assignments using an ASP encoding of the respective abduction  Beaver and Niemelä (1999). The optimal solutions of the problem satisfy i.e., c MAP is an assignment to random variables C in a network N that makes the observed evidence e in N most probable. Given a (Boolean) query variable q, the same ASP encoding and the ASEO algorithms proposed in this work can be used to enumerate assignments in a decreasing order of significance, i.e., starting from MAP assignments but continuing with next probable assignments, and so on. This leads to a goal directed sampling method as follows. Given a query q and some evidence e, we find out the k most probable assignments c 1 , . . . ,c k (resp. d 1 , . . . ,d k ) that satisfy q (resp. ¬q) and are compatible with e. Thus, we obtain an estimate Figure 2 illustrates the full procedure for approximating the query q given the evidence e. First, for the sake of fair comparison with other sampling methods, the network is simplified using d-separation following Geiger et al. (1989). Second, the input network is transformed into an ASP encoding for solving the respective MAP problem. Then, based on the given evidence e, the query q, and its negation ¬q, up to k most probable assignments are computed in order to approximate the probability p(q|e) in the sense of (7). Then, we evaluate our ASEO-based sampling method using few publicly available BNs whose size varies from small to large (cf. Table  2). The experiments are conducted by randomly picking a query variable and a set of evidence variables that represent 1 to 50% of the variables in the network in total. In what follows, we compare our sampling results to those obtained with a well-known native algorithm, viz. Gibbs sampler as provided in the pyAgrum library of Gonzales et al. (2017). In this implementation, the sampler stops by determining the convergence of the approximation with = 0.01. In Fig. 3, the sampling results for the input networks are presented. To obtain the results we use Algorithm 1 to conduct ASEO where the choice of the enumeration algorithm was based on better overall performance. In each image, true probabilities are mapped on the x-axis while the respective estimates are represented on the y-axis. Hence, the closer to the diagonal points are, the better approximations have been obtained. When looking at distributions, we observe that the ASEO-based approximations tend to be better on smaller networks while this is no longer obvious for the largest network Andes. As far as we can see, this is due to the fact that with larger networks there exists increasing number of assignments with an equal objective value (i.e., the same probability) and individual assignments having very low probability values. As a consequence, we do not get a good overview of the distribution of probabilities and the probabilities are far from the expected true ones as for the Andes network. Table 3 presents further qualitative results over approximations where distances to true probabilities are measured. The results of the table illustrate that ASEO-based sampling can be competitive even with known methods such as Gibbs sampler. However, when considering approximate reasoning task like Bayesian sampling, one should remember that there are highly optimized exact methods, such as that of Madsen and Jensen (1999), to determine true probabilities fast.

Discussion and Conclusion
In this work, we address solution enumeration in the context of combinatorial optimization problems, the goal of which is to generate all optimal solutions to a given problem instance. As a novelty, we take also sub-optimal solutions into consideration and propose algorithms that are able to recursively enumerate next-best solutions until (i) all solutions or, alternatively, (ii) the best k solutions have been enumerated. Such a procedure realizes our concept of solution enumeration by optimality (SEO). Besides screening the computational cost of SEO, we present dedicated SEO algorithms geared toward answer set programming (ASP) where solutions are captured with answer sets. The resulting ASEO algorithms are implemented in Python (Pajunen 2020) using the API of the Clingo solver and are put to the test in experiments. These algorithms generalize the ASO reasoning mode of Clingo. Moreover, we mention that the computation of k best solutions was considered in an experimental track of MaxSAT Evaluation run by Bacchus et al. (2020), but the solutions enumerated were supposed to assign different values to literals involved in the objective function. Thus, such an enumeration differs from SEO up to k solutions.
The experimental evaluation comprises of two parts. First, we evaluate our ASEO algorithms on a number of optimization problems that have been used in ASP competitions. The results indicate the feasibility of ASEO in practice, enabling the exploitation of next-best solutions when solving optimization problems. This is good news from the perspective of Theorem 1 which indicates that the remaining computational complexity does not necessarily decrease when excluding answer sets already enumerated. Second, we illustrate the potential of ASEO in approximating Bayesian inference. There is some resemblance to the approach of Chavira and Darwiche (2008) based on weighted model counting. The difference is that the objective function derived from conditional probability tables guides the search toward the most relevant assignments for query evaluation. Ermon et al. (2013) deploy optimization oracles for similar inference but in the presence of further constraints for uniformly distributing samples over the space of assignments. It is worth noting that Bayesian inference has been well studied and there are further good alternatives to ASEO. Our main objective is to demonstrate the new way of utilizing objective functions, effectively SEO, as means to reach the most significant solutions first.
As discussed in Section 4, our proof-of-concept implementations of ASEO are still suboptimal as they are essentially based on successive calls to ASO/ASE algorithms via an API. This prevents, e.g., the direct exploitation of nogoods learned during the subsequent invocations of the underlying enumeration algorithm. This observation suggests an obvious goal for future work: a native implementation of an ASEO reasoning mode in some state-of-the-art answer-set solver. To this end, we hope that our results make this goal attractive for developers.