【发布时间】:2017-11-13 12:18:59
【问题描述】:
这些是我的规则,我的问题出在哪里:
get_row([H|_],1,H):-!.
get_row([_|T],I,X) :-
I1 is I-1,
get_row(T,I1,X).
get_column([],_,[]).
get_column([H|T], I, [R|X]):-
get_row(H, I, R),
get_column(T,I,X).
good_by_coulmns(Solution) :-
length(Solution, Length),
forall((between(1, Length, X),
get_column(Solution, X, Y)),
all_distinct(Y)).
createRow(Solution, Domain, Row) :-
maplist(member, Row, Domain),
all_distinct(Row),
good_by_coulmns(Solution).
%, write(Solution), nl.
tryToSolve(Domains, Solution) :-
maplist(createRow(Solution),
Domains, Solution),
length(Solution, L),
length(Domains, L),
good_by_coulmns(Solution).
问题是,最后一条规则生成了大约 20 个好的答案,但之后它进入了无限循环。第一条规则中有调试写入。
它写出这样的行(数字总是在变化),同时无限循环:
[[1, 2, 3, 4], [3, 1, 4, 2], [4, 3, 2, 1], [2, 4, 1, 3], _8544, _8550, _8556, _8562]
[[1, 2, 3, 4], [3, 4, 1, 2], _8532, _8538, _8544, _8550, _8556, _8562]
我们等待的解决方案是一个 4x4 矩阵。在第一行中,如果我们去掉前 4 个元素,这是一个很好的解决方案。
以_开头的变量个数一直在增加,而矩阵的第一行([1,2,3,4])从不改变。
你有什么想法,这里出了什么问题?
实际查询:
tryToSolve([[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]], L).
【问题讨论】:
-
@nullpointer:这种琐碎的编辑没有任何帮助,甚至没有纠正拼写错误
-
我添加了一个基本查询,并添加了规则。请看上面。它正在实现一个谜题求解器,此时类似于基本数独。
-
clpfd 是你所需要的...
-
我不知道那个库,你能告诉我,我怎么能在这里使用它?
-
您已经在使用
library(clpfd)!这就是all_distinct/1的来源。
标签: list matrix prolog infinite-loop failure-slice