'clingo return the max subset

I have the following code:

1{p(1..10)}10.
:- p(I*2).

and it shows 31 answers, But I just need the max subset p(1) p(3) p(5) p(7) p(9). How can I do it?

enter image description here



Solution 1:[1]

You are searching for an optimization. Easiest fix would be adding

#maximize{I:p(I)}.

resulting in

Solving...
Answer: 1
p(9)
Optimization: -9
Answer: 2
p(5) p(9)
Optimization: -14
Answer: 3
p(7) p(9)
Optimization: -16
Answer: 4
p(5) p(7) p(9)
Optimization: -21
Answer: 5
p(1) p(5) p(7) p(9)
Optimization: -22
Answer: 6
p(3) p(5) p(7) p(9)
Optimization: -24
Answer: 7
p(1) p(3) p(5) p(7) p(9)
Optimization: -25
OPTIMUM FOUND

Don't worry about the negative score, clingo converts #maximize into #minimize, therefore the negation. So what does #maximize{I:p(I)}. do? In general you want to maximize over I for all p(I), which in this case is the sum of I. Just counting would be

#maximize{1,I:p(I)}.

resulting in

Answer: 1
p(9)
Optimization: -1
Answer: 2
p(5) p(9)
Optimization: -2
Answer: 3
p(5) p(7) p(9)
Optimization: -3
Answer: 4
p(3) p(5) p(7) p(9)
Optimization: -4
Answer: 5
p(1) p(3) p(5) p(7) p(9)
Optimization: -5
OPTIMUM FOUND

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DuDa