【发布时间】:2016-04-23 12:04:30
【问题描述】:
我有以下几点:
:-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 命令“破解”了。做这个的最好方式是什么?如果我使用标签,那么这些值不会是随机的?如果经常违反约束,那么搜索将非常低效。最好的方法是什么?
【问题讨论】:
-
要使此类关系有意义,请添加种子参数。
-
有意义是什么意思?
-
如果不添加种子,则定义不是关系。