2

If I have X #> 3. Prolog will give the residual goal X in 4..sup.. How can I assign X one of the possible values from 4 to sup? Any random value will suffice.

false
  • 10,264
  • 13
  • 101
  • 209
Asad-ullah Khan
  • 1,573
  • 18
  • 22

2 Answers2

2

In SWI-Prolog,you can get random value assign with using secret option random_value(Seed). see this:How does `random_variable `random_value` work in SWI-Prolog's labeling/2?



    :- use_module(library(clpfd)).

    random_labeling_test(A):-
        A in 1..5,
        labeling([random_value(332)],[A]).



    ?- random_labeling_test(A).
    A = 5 ;
    A = 1 ;
    A = 2 ;
    A = 4 ;
    A = 3.

332 is meaningless.Set random seed into here.current milliseconds is often used .

I'm not sure this option is safe.

But in your case,this cannot work because upper bounds is infinite.It is reasonable.

In ECLiPSe, labeling variable selection and assigning value selection is completly controllable with using indomain and delete. see this:http://eclipseclp.org/doc/tutorial/tutorial088.html

Taku Koyahata
  • 558
  • 2
  • 13
2

One naive way to solve this is to use a predicate like this:

enumeration(Z) :-
        length(_, N),
        enumeration_(N, Z).

enumeration_(N, N).
enumeration_(N0, N) :- N #= -N0.

You can now solve your task as follows:

?- X #> 3, enumeration(X).
X = 4 ;
X = 5 ;
X = 6 ;
X = 7 ;
X = 8 .

Pros:

  • a quite simple solution
  • portable to different Prolog systems.

Con:

  • potentially quite slow.

Example:

?- X #> 2^100, enumeration(X).
[waiting...]

To make this more efficient, you need to take into account the actual domains of variables.

There are at least two ways to do this:

(a) Use your solver's reflection predicates

Reflection predicates let you reason about the actual domains of variables by making them available as Prolog terms.

For example, where available, you can use fd_inf/2 to obtain the infimum of a constrained variable's domain:

?- X #> 3, fd_inf(X, Inf).
Inf = 4,
X in 4..sup.

You can use this as a starting point for the enumeration. I leave this as a challenge.

See your Prolog system's manual for more information about its reflection predicates.

(b) Inspect the residual goals

Alternatively, you can inspect the residual goals as Prolog terms. This is also a reflection mechanism, though one that only needs a single predicate for all kinds of different constraint solvers.

For example, where available, you can use copy_term/3 to obtain the residual goals as a list of Prolog goals:

?- X #> 3, copy_term(X, X, Gs).
Gs = [clpfd:(X in 4..sup)],
X in 4..sup.

From these goals, it is again straight-forward to deduce the infimum of X's domain.

Note that the top-level can use this same mechanism to actually produce the residual goals.

mat
  • 40,498
  • 3
  • 51
  • 78