• No results found

Listing 3.7: Subsets without arithmetic progressions of length 3 (Exercise 3.10) 1 % P a r t i t i o n {1 ,... , n } i n t o r s u b s e t s t h a t do not c o n t a i n 2 % a r i t h m e t i c p r o g r e s s i o n s of l e n g t h 3. 3 4 % i n p u t : p o s i t i v e i n t e g e r s n , r . 5 6 % in ( I , J ) m e a n s t h a t I b e l o n g s to the J - th s u b s e t . 7 8 { in ( I , 1 . . r )} = 1 : - I = 1.. n . 9 % a c h i e v e d : set {1 ,... , n } is p a r t i t i o n e d i n t o r s u b s e t s . 10 11 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 12 % a c h i e v e d : t h e s e s u b s e t s do not c o n t a i n a r i t h m e t i c 13 % p r o g r e s s i o n s of l e n g t h 3.

sum-free. If we want to describe partitions of {1, . . . , n} into r almost sum-free sets, how will you change the program in Listing 3.6?

If we run the program in Listing 3.6 forr = 3 and various values of n then we will see that the largest n for which stable models exist is 13. In other words, S(3) = 13. In a similar way, clingo can tell us that S(4) = 44. In the next section we say more about using clingo for calculating Schur numbers.

Exercise 3.9. Useclingo to verify thatS(5)≥130.

Exercise 3.10. About a set of numbers we say that it contains an arithmetic progression of length 3 if it has three different elementsa,b, c such that b−a=c−b. We would like to write a clingo program that partitions {1, . . . , n} into r subsets that do not contain arithmetic progressions of length 3, if this is possible. What rule would you place in Line 11 of Listing 3.7 to get this result?

3.4

Digression on Grounding and Solving

If we save the program from Listing 3.6 in the file schur.lpand execute the command

clingo schur.lp -c r=4 -c n=44

then the output of clingomay look like this:

Answer: 1

in(1,1) in(2,3) ... . . .

The difference between the total time, 0.189sec, and the solving time, 0.16sec, is explained by the fact that the operation ofclingobegins withgrounding—generating and simplifying the instances of rules that are essential for finding the stable models. In this case, the grounding time was around 0.03 sec; grounding was followed by the solving phase, which took 1.16sec.

There are cases when the solving time of aclingoprogram is so large that the program is unusable. This happens, for instance, with the program in Listing 3.6 for large values ofr andn. This is not surprising, because answer set solvers (and satisfiability solvers) are often used to solve intrinsically difficult, NP-hard problems. The runtimes of all (known) algorithms solving NP-hard problems grow exponentially with the size of the problem. But if large solving time is an issue then it may be useful to remember thatclingohas several solving strategies, and we can instruct it to try several strategies in parallel. For example, the command

clingo -c r=4 -c n=45 schur.lp -t4

will instructclingoto try solving the problem with 4 “threads,” and the output may look like this:

UNSATISFIABLE . . .

Threads : 4 (Winner: 2)

Information on the “winner” tells us which thread reached the goal first.

There are also cases when the grounding time of a program is unacceptably large. In the case of the program from Exercise 2.18, the solving time is negligible, but the grounding time grows quickly asn becomes larger. To understand why, look at the rule

three(N) :- N = 1..n, I = 0..n, J = 0..n, K = 0..n, N = I**2+J**2+K**2.

suggested in Appendix A for Line 6 of the program (page 155). The instances of this rule that are identified byclingo as essential for generating the stable model are obtained by substituting arbitrary values between 0 and n for each of the variables I, J, K, and the number of such instances is (n+ 1)3, it grows quickly with n. Significant grounding times are typical for ASP programs containing a rule with many variables, and we try to avoid including such rules in an encoding when possible.

The grounding time of the program from Exercise 2.18 can be improved using the fact that the summands in the expressioni2+j2+k2 can be always reordered so thati≤j≤k. Rewriting the definition ofthree/1in the form

three(N) :- N = 1..n, I = 0..n, J = I..n, K = J..n, N = I**2+J**2+K**2.

decreases the number of instances identified by clingo as essential by approximately a factor of 6, and the grounding time goes down accordingly. Using the symmetry of a problem to improve the performance of an algorithm is known as “symmetry breaking.” In Section 6.3 we will see how symmetry breaking can be used to speed up search in the Schur numbers example.