0

I want to run a clingo program containing an optimization. I already have a (suboptimal) model. Is there a way to "help" clingo with the existing model? I'm using the python api.

Minimal example (n queens, but minimize the sum of the product of the coordinates):

from clingo import Control

ctl = Control(["0"])
ctl.add("base", [], """
    #const n = 8.
    { q(I,1..n) } == 1 :- I = 1..n.
    { q(1..n,J) } == 1 :- J = 1..n.
    :- { q(D-J,J) } >= 2, D =   2..2*n.
    :- { q(D+J,J) } >= 2, D = 1-n..n-1.
    #minimize{X*Y:q(X,Y)}.
""")
ctl.ground([("base", [])])
lastmdl = None
with ctl.solve(yield_=True) as models:
    for model in models:
        lastmdl = model.symbols(atoms=True)
        print(lastmdl, model.cost)

will give me the output

[q(7,1), q(1,2), q(3,3), q(8,4), q(6,5), q(4,6), q(2,7), q(5,8)] [158]
[q(4,1), q(8,2), q(1,3), q(3,4), q(6,5), q(2,6), q(7,7), q(5,8)] [154]
[q(2,1), q(6,2), q(8,3), q(3,4), q(1,5), q(4,6), q(7,7), q(5,8)] [132]
[q(6,1), q(8,2), q(2,3), q(4,4), q(1,5), q(7,6), q(5,7), q(3,8)] [128]
[q(1,1), q(6,2), q(8,3), q(3,4), q(7,5), q(4,6), q(2,7), q(5,8)] [126]

In a previous step I already found a model with a value of 132. How can I use the knowledge of this existing model to my advantage in the sense that I could skip the first 3 models in the current solve call? To be clear: The goal is to speed up the search for the optimal model.

DuDa
  • 3,718
  • 4
  • 16
  • 36

1 Answers1

0

A possible naive solution could be adding the following rules:

z1(Z):-  q(X,Y), Z = X*Y.
sz(Z):- Z = #sum{X:z1(X)}.
:- sz(Z), Z > 132.

However, the computation of the aggregate may be expensive, so there could be a more efficient way.

damianodamiano
  • 2,528
  • 2
  • 13
  • 21
  • Yeah, I thought of that too but there are hardly any advantages of this method. The ressources required to handle a value I'm actually not interested in is just not worth it. And I'd have to fiddle with externals to reuse the program. – DuDa Apr 19 '23 at 18:17
  • Oh and I guess you did not run your code, since it contains ... let's call it an easter egg. Hint: `q(1,6) q(2,3)` – DuDa Apr 19 '23 at 18:26