Printing solutions on the way to a goal

Hi!

I am playing around with inductive logic programming, getting Prolog to search for theories. Naturally, such searches take a lot of time. Are there any good ways to print out every Nth solution during the search (instead of every solution)? In more concrete terms, and simplifying, suppose I have

list([a,b,c,d,e,f,g,h]).
p:- 
  list(List),
  member(X1, List),
  q(X2),
  format('~w~w ~w~n', ['X1 and X2 are ', X1, X2]).
q(X2):- 
  list(List),
  member(X2, List).

test:- p, fail.

Instead of printing every solution, as the format/2 above does, I would like to print, say, every 10th solution. Or maybe a solution every 10 seconds. In my actual program I don’t want to have a recursive loop to conduct the search (in that case I know that I could have a counter that increments on each recursion and that I could use to print at certain intervals).

Could I use, for example, statistics(runtime, [CurrentTime | _]) somehow? I did some dubious experiments with maybe/1 but that did really not feel right :see_no_evil: Any suggestions welcome :slight_smile:

Kind regards, JCR

One way would be to use mod/2 with the real_time value, something like this.

p :- 
  % ...
  statistics(real_time,[T1,T2]),
  % writeln([t1=T1,t2=T2]),
  (T1 mod 10 =:= 0 -> format('~w~w ~w~n', ['X1 and X2 are ', X1, X2]) ; true),

If the program is fast it will print a lot of solutions so you have to play with the specific value of the modulo value. For testing this you can use sleep/1 for make the program slower.

1 Like

Thanks for the suggestion!

If you want it count based rather than time based, use call_nth/2 to get solutions and their count at the same time.

1 Like

Thanks! I’ll also try this.