Question

Consider the program:

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

I expected that result to cost(I, C). would be I = 1, C = 0; I = 2, C = 1 - all possible input arguments with corresponding minimum results.

But I get only one:

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

But if I explicitly point to all 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 enumerating all possible inputs?

Was it helpful?

Solution 2

Some time ago I found the answer right there in the B-Prolog manual: "Note that, if table modes are not respected, or if there is no bound for an optimized argument, a program may give unexpected answers".

Code in the question doesn't respect the "table cost(+, min)." modes and tries to use first cost/2 parameter as an output.

OTHER TIPS

A table declaration with an optimization mode (min or max) instructs the system to table one answer for each combination of the input arguments. You can give a cardinality limit to instruct the system to store multiple optimal answers. For example, you can give a declaration like:

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

Then, the system will table up to 3 best answers, ordered by optimality, for each input. The limit is assumed to be 1 if not explicitly given.

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 a sub-problem.

Neng-Fa Zhou

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top