Search code examples
prologclpfd

Get a value from residual goals


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.


Solution

  • 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