有人可以提供一个简单的渠道约束示例吗?
通道约束用于组合约束问题的视点.约束编程手册很好地解释了它是如何工作的以及为什么它有用:
搜索变量可以是其中一个视点的变量,比如X1(这将在下面进一步讨论).随着搜索的进行,传播约束C1从X1中的变量的域中移除值.然后,信道约束可以允许从X2中的变量的域中移除值.使用第二模型C2的约束来传播这些值删除可以从这些变量中移除更多值,并且这些移除可以通过信道约束再次转换回第一视点.最终结果可能是在视点V1内移除的值多于仅由约束C1移除的值,导致搜索减少.
我不明白这是如何实现的.究竟是什么限制,它们在真正的问题中看起来如何?一个简单的例子非常有用.
我必须以包含9个向量(每个长度为9)的向量的格式解决数独谜题.看到矢量是Prolog中的链接列表,我认为如果我首先以2D数组格式转换谜题,搜索会更快.
示例拼图:
puzzle(P) :- P =
[[_,_,8,7,_,_,_,_,6],
[4,_,_,_,_,9,_,_,_],
[_,_,_,5,4,6,9,_,_],
[_,_,_,_,_,3,_,5,_],
[_,_,3,_,_,7,6,_,_],
[_,_,_,_,_,_,_,8,9],
[_,7,_,4,_,2,_,_,5],
[8,_,_,9,_,5,_,2,3],
[2,_,9,3,_,8,7,6,_]].
Run Code Online (Sandbox Code Playgroud)
我正在使用ECLiPSe CLP来实现解算器.到目前为止,我提出的最好的方法是写一个这样的域:
domain(P):-
dim(P,[9,9]),
P[1..9,1..9] :: 1..9.
Run Code Online (Sandbox Code Playgroud)
和拼图的转换器(参数P是给定的拼图,Sudoku是具有2D阵列的新定义的网格).但是我无法将给定初始拼图中的值链接到我的2D数组.
convertVectorsToArray(Sudoku,P):-
( for(I,1,9),
param(Sudoku,P)
do
( for(J,1,9),
param(Sudoku,P,I)
do
Sudoku[I,J] is P[I,J]
)
).
Run Code Online (Sandbox Code Playgroud)
在此之前,我尝试使用array_list(http://eclipseclp.org/doc/bips/kernel/termmanip/array_list-2.html),但我一直遇到类型错误.我以前怎么做过:
convertVectorsToArray(Sudoku,P):-
( for(I,1,9),
param(Sudoku,P)
do
( for(J,1,9),
param(Sudoku,P,I)
do
A is Sudoku[I],
array_list(A,P[I])
)
).
Run Code Online (Sandbox Code Playgroud)
当我的Sudoku最终输出以下格式的示例拼图P时:
Sudoku = []([](_Var1, _Var2, 8, 7, ..., 6), [](4, ...), ...)
Run Code Online (Sandbox Code Playgroud)
那我会很开心
更新
我再次尝试使用array_list; 它几乎与以下代码一起使用:
convertVectorsToArray(Sudoku,P):-
( for(I,1,9),
param(Sudoku,P)
do
X is Sudoku[I],
Y …Run Code Online (Sandbox Code Playgroud) 如何在ECLiPSe CLP中测量方法的执行时间?目前,我有这个:
measure_traditional(Difficulty,Selection,Choice):-
statistics(runtime, _),
time(solve_traditional(Difficulty,Selection,Choice,_)),
time(solve_traditional(Difficulty,Selection,Choice,_)),
time(solve_traditional(Difficulty,Selection,Choice,_)),
time(solve_traditional(Difficulty,Selection,Choice,_)),
time(solve_traditional(Difficulty,Selection,Choice,_)),
time(solve_traditional(Difficulty,Selection,Choice,_)),
time(solve_traditional(Difficulty,Selection,Choice,_)),
time(solve_traditional(Difficulty,Selection,Choice,_)),
statistics(runtime,[_|T]), % T
write(T).
Run Code Online (Sandbox Code Playgroud)
我需要编写执行方法solve_traditional(...)并将其写入文本文件所花费的时间.但是,它不够精确.有时,给定方法的时间将打印0.015或0.016秒,但通常打印0.0秒.
确定方法完成得太快,我决定使用统计信息(运行时,...)来测量两次运行时调用之间的时间.然后,我可以测量完成20次方法调用所需的时间,并将测量的时间T除以20.
唯一的问题是,20次调用T等于0,16,32或48毫秒.显然,它分别测量每个方法调用的时间,并找到执行时间的总和(通常只有0.0秒).这超过了测量N个方法调用的运行时间并将时间T除以N的全部目的.
简而言之:我用于执行时间测量的当前方法是不合适的.有没有办法使它更精确(例如9位小数)?
如何确保序言列表的所有元素都是0或1?
我需要生成一个给定长度的列表,并确保它只有这两个数字:
例如[0,0,0,0,0,0],[1,0,0,1,0,1,1等等
使用ECLiPSe Prolog lib(ic)我偶然发现了David H. Bailey的"解决科学计算中的数值异常"的问题.这是我在Unum书中提到的.实际上,它只是其中的一部分.首先,让我根据方程式来制定方程式(is)/2.另请注意,所有这些十进制数都在基数2浮点数(包括IEEE)中具有精确表示:
ECLiPSe Constraint Logic Programming System [kernel]
...
Version 6.2development #21 (x86_64_linux), Wed May 27 20:58 2015
[eclipse 1]: lib(ic).
...
Yes (0.36s cpu)
[eclipse 2]: X= -1, Y = 2, Null is 0.80143857*X+1.65707065*Y-2.51270273.
X = -1
Y = 2
Null = 0.0
Yes (0.00s cpu)
Run Code Online (Sandbox Code Playgroud)
所以这真的是0.0(根本没有四舍五入).但现在同样$=代替is:
[eclipse 3]: X= -1, Y = 2, Null $= 0.80143857*X+1.65707065*Y-2.51270273. …Run Code Online (Sandbox Code Playgroud) prolog floating-accuracy interval-arithmetic eclipse-clp clpr
solve(Amounts) :-
Total = 1505,
Prices = [215, 275, 335, 355, 420, 580],
length(Prices, N),
length(Amounts, N),
Amounts :: 0..Total//min(Prices),
Amounts * Prices #= Total,
labeling(Amounts).
Run Code Online (Sandbox Code Playgroud) 上一个问题给出了Prolog实例化模式的完整列表:Prolog谓词参数中实例化模式指示符的含义.
但是,我无法在ECLiPSe Prolog中找到有关双加(++)实例化模式的任何参考或解释.使用'++'模式的ECLiPSe文档中的示例谓词是setval(++,?).
有谁知道这种模式的含义是什么?
我在ECLiPSe下遇到了我的CSP问题.我想在我的密码中添加一个约束,要求TWO表示的数字可以被2整除.
[eclipse 11]: test(Xs).
instantiation fault in (_268{[1..4]}*100 + _200{[0..9]}*10 + _302{[0..9]}*1) mod 2#=0
Abort
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
我的代码:
/*
T W O
+ T H R E E
+ T H R E E
---------
E I G H T
*/
:- lib(fd).
myCsp(Xs):-
Xs=[W,I,G,H,T,R,O,E],
Xs::0..9,
[C1,C2,C3,C4]::0..2,
T #> 0,E #> 0,
O + E + E #= C1*10 + T,
W + E + E + C1 #= C2*10 + H,
T + R + R + C2 #= C3*10 …Run Code Online (Sandbox Code Playgroud) prolog constraint-programming eclipse-clp cryptarithmetic-puzzle instantiation-error
eclipse-clp ×8
prolog ×8
clpfd ×2
arrays ×1
clpr ×1
constraints ×1
elements ×1
list ×1
performance ×1
signature ×1