我有以下内容:
:-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
用于生成 1 和 0 的随机向量,其中 conindexes 位置中的元素为 1。
?-length_conindexes_conrandomvector(4,[0,1],R).
R = [1, 1, 0, 1].
length_conindexes_notconrandomvector/3
用于生成一个随机向量,其中并非所有的索引都是 1。
?- length_conindexes_notconrandomvector(3,[0,1,2],R).
R = [1, 0, 1] ;
R = [0, 1, 1] ;
R = [1, 1, 0]
我觉得我已经用这个repeat
命令“破解”了。做这个的最好方式是什么?如果我使用标签,那么这些值不会是随机的?如果经常违反约束,那么搜索将非常低效。做这个的最好方式是什么?