我有以下生成隨機值:其中約束不和不保持
:-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
被用來產生一和零,其中在conindexes位置的元素中1的一個隨機向量。
?-length_conindexes_conrandomvector(4,[0,1],R).
R = [1, 1, 0, 1].
length_conindexes_notconrandomvector/3
被用來產生一個隨機向量,其中不是所有的conindexes的是那些。
?- length_conindexes_notconrandomvector(3,[0,1,2],R).
R = [1, 0, 1] ;
R = [0, 1, 1] ;
R = [1, 1, 0]
這個我覺得我已經用'0123'配置了這個repeat
命令。做這個的最好方式是什麼?如果我使用標籤,那麼這些值不會是隨機的?如果約束經常被違反,那麼搜索將是非常低效的。做這個的最好方式是什麼?
要使這種關係有意義,請添加種子參數。 – false
你是什麼意思? – user27815
如果您不添加種子,您的定義不是關係。 – false