I have the following:
:-use_module(library(clpfd)).
list_index_value(List,Index,Value):-
nth0(Index,List,Value).
length_conindexes_conrandomvector(Length,Conindexs,Randomvector):-
length(Randomvector,Length),
same_length(Conindexs,Ones),
maplist(=(1),Ones),
maplist(list_index_value(Randomvector),Conindexs,Ones),
term_variables(Randomvector,Vars),
maplist(random_between(0,1),Vars).
length_conindexes_notconrandomvector(Length,Conindexes,Randomvector):-
length(Randomvector,Length),
length(Conindexes,NumberOfCons),
same_length(Conindexes,Values),
sum(Values,#\=,NumberOfCons),
maplist(list_index_value(Randomvector),Conindexes,Values),
term_variables(Randomvector,Vars),
repeat,
maplist(random_between(0,1),Vars).
length_conindexes_conrandomvector/3
is used to generate a random vector of ones and zeros where the elements in the conindexes positions are 1s.
?-length_conindexes_conrandomvector(4,[0,1],R).
R = [1, 1, 0, 1].
length_conindexes_notconrandomvector/3
is used to generate a random vector where NOT ALL of the conindexes are ones.
?- length_conindexes_notconrandomvector(3,[0,1,2],R).
R = [1, 0, 1] ;
R = [0, 1, 1] ;
R = [1, 1, 0]
This I feel I have 'hacked' with the repeat
command. What is the best way to do this? If I use labelling then the values will not be random? If the constraint is often violated then the search would be very inefficient. What is the best way to do this?