在这个答案中,我们使用了两种不同的clpfd“风味”:sicstus-prolog 和gnu-prolog。
:-
use_module(
library(clpfd))。
limited_repetitions__SICStus(Zs) :-
length(Zs, 10),
domain(Zs, 1, 4),
域([C1,C2,C3,C4], 0, 5),
global_cardinality(Zs, [1-C1,2-C2,3-C3,4-C4]),
labeling([], Zs)。
limited_repetitions__gprolog(Zs) :-
length(Zs, 10),
fd_domain(Zs, 1, 4),
maplist(
fd_atmost(5,Zs), [1,2,3,4]),
fd_labeling(Zs)。
使用SICStus Prolog 4.3.2 版和GNU Prolog 1.4.4 运行的简单示例查询:
?- limited_repetitions__SICStus(Zs)。 % ?- limited_repetitions__gprolog(Zs)。
Zs = [1,1,1,1,1,2,2,2,2,2] % Zs = [1,1,1,1,1,2,2,2,2,2]
; Zs = [1,1,1,1,1,2,2,2,2,3] % ; Zs = [1,1,1,1,1,2,2,2,2,3]
; Zs = [1,1,1,1,1,2,2,2,2,4] % ; Zs = [1,1,1,1,1,2,2,2,2,4]
; Zs = [1,1,1,1,1,2,2,2,3,2] % ; Zs = [1,1,1,1,1,2,2,2,3,2]
; Zs = [1,1,1,1,1,2,2,2,3,3] % ; Zs = [1,1,1,1,1,2,2,2,3,3]
; Zs = [1,1,1,1,1,2,2,2,3,4] % ; Zs = [1,1,1,1,1,2,2,2,3,4]
; Zs = [1,1,1,1,1,2,2,2,4,2] % ; Zs = [1,1,1,1,1,2,2,2,4,2]
... % ...
让我们测量计算解决方案数量所需的时间!
call_succeeds_n_times(G_0, N) :-
findall(t, call(G_0), Ts),
length(Ts, N)。
?-
call_time(call_succeeds_n_times(limited_repetitions__SICStus(_), N), T_ms)。
N = 965832,T_ms = 6550。% w/SICStus Prolog 4.3.2
?- call_time(call_succeeds_n_times(limited_repetitions__gprolog(_), N), T_ms)。
N = 965832,T_ms = 276。% w/GNU Prolog 1.4.4