Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog: Random Labeling

I have a program written in Sicstus Prolog using constraints. My goal is to use labeling/2 and some other method to obtain a random instantiation of my variables.

Example:

X #> 2, Y #= 2*X, Z #<10

If I use

List = [X,Y,Z],
labeling([], List)

The first result obtained will be X = Y = Z = 0. How do you think is the best way to return a random set of values for X, Y and Z?

like image 496
ecc Avatar asked Jan 01 '12 15:01

ecc


2 Answers

I do not know much about the labeling options in recent SICStus versions, but with library(clpfd) of SWI-Prolog, there are the options random_variable(Seed) and random_value(Seed), you can use them for example with labeling([random_variable(10),random_value(10)], List). Maybe you can get the authors of SICStus to integrate similar options?

like image 63
mat Avatar answered Sep 24 '22 21:09

mat


In sicstus, this is done with a custom selection of variables / values.

In your case, just do:

labeling([value(mySelValores)], List)

mySelValores(Var, _Rest, BB, BB1) :-
    fd_set(Var, Set),
    select_best_value(Set, Value),
    (   
        first_bound(BB, BB1), Var #= Value
        ;   
        later_bound(BB, BB1), Var #\= Value
    ).

select_best_value(Set, BestValue):-
    fdset_to_list(Set, Lista),
    length(Lista, Len),
    random(0, Len, RandomIndex),
    nth0(RandomIndex, Lista, BestValue).

See value(Enum) in https://sicstus.sics.se/sicstus/docs/4.0.4/html/sicstus/Enumeration-Predicates.html.

Hope it helps ;)

like image 44
Helder Antunes Avatar answered Sep 25 '22 21:09

Helder Antunes