How can you count the number of backtracks in the Prolog SWI or CHR Prolog SWI

I create several puzzle solvers in the Prolog SWI with CHR (Restriction Handling Rules)

Everything works fine, but I like to test which one is better. Therefore, I like to find out which solver uses the least number of digressions.

Is there a smart way to find out (or print) the number of return paths that the solver required to solve a particular puzzle?

Logically, counting will help, but it's not → backtracking! <-. In addition, printing a new line on the screen is inefficient due to the SWI GUI. You cannot print more than +/- 50 lines and you cannot select correctly

+4
source share
1 answer

It really is not so difficult to do, because the Rights restriction rules support a "repository of restrictions", and the implementation of the rules can add , rewrite or delete from this store at runtime. This changes the state of the program and makes it difficult to track global states at runtime.

However, since CHR is integrated into the SWI, you can use the illogical operation nb_setarg / 3 to maintain the number of backtracks .

Notes from the document:

  • Compatible with GNU-Prolog setarg (A, T, V, false)

  • This implementation is thread safe, reentrant, and capable of handling exceptions.

EDIT

, , , , , CHR, , "" (= CHR). , , , , , .

:

invalid_state ==> increment_backtracks, fail.
        guess <=> branch
+2

Source: https://habr.com/ru/post/1542666/


All Articles