B-Prolog table and backtracking modes

Consider the program:

:- table cost(+, min).
cost(1, 0).
cost(1, 1).
cost(2, 1).

      

I expected that the result for cost(I, C).

would be i = 1, C = 0; i = 2, C = 1 - all possible input arguments with corresponding minimum results.

But I only get one:

cost(I, C).
I = 1
C = 0 ?;
no

      

But if I explicitly point out all the possibilities for the input argument, I get what I want:

( I = 1 ; I = 2 ), cost(I, C).
I = 1
C = 0 ?;
I = 2
C = 1 ?;
no

      

Is it possible to get all combinations of input arguments with corresponding minimum results without explicitly listing all possible inputs?

+3


source to share


2 answers


Some time ago I found the answer directly in the B-Prolog manual: "Note that if the table modes are not respected , or if there are no restrictions on the optimized argument, the program may give unexpected answers ."



The code in the question does not take into account "table cost (+, min)". and tries to use the first cost / 2 parameter as an output.

+1


source


Declaring a table with optimization mode (min. Or max.) Tells the system one answer for each combination of input arguments. You can give a power limitation to instruct the system to store multiple optimal responses. For example, you can give a declaration like:

:- table cost(+, min):3.

      

The system will then compose up to 3 best answers, ordered by optimality, for each input. The limit is assumed to be 1 unless explicitly stated.



The system does not store all answers. This is important because for dynamic programming problems you only need to remember the current best answer to the subproblem.

Neng-Fa Zhou

+2


source







All Articles