【发布时间】:2016-04-03 19:59:10
【问题描述】:
我是约束逻辑编程的新手,想知道如何使用 clpfd 在 prolog 中设置 12 x 12 矩阵。我正在使用 swi prolog ide。
【问题讨论】:
-
这是 Prolog 还是 ECLiPSe?
-
@WillemVanOnsem 这是序言我使用的是 swi prolog 而不是 ECLiPSe
我是约束逻辑编程的新手,想知道如何使用 clpfd 在 prolog 中设置 12 x 12 矩阵。我正在使用 swi prolog ide。
【问题讨论】:
要分配一个变量矩阵,我们可以在普通的 Prolog 中做:
matrix(N,Rows) :- bagof(R,Y^(between(1,N,Y),length(R,N)),Rows).
然后,使用 library(yall) 约束每个“单元格”从域(比如说 1..3)中获取值:
:- use_module(library(clpfd)).
?- matrix(12, Mat), maplist([R]>>(R ins 1..3), Mat).
或者,使用内置函数:
?- matrix(12, Mat), bagof(t, R^(member(R, Mat), R ins 1..3), _).
【讨论】: