无法从 CLPFD 中最小化工作
我和一个朋友正在编写一个程序,旨在解决 CLP 问题。我们想使用最小化来优化解决方案,但它不起作用,因为它一直说我们从 sum(P,#=,S) 得到的数字在两个数字之间(例如 5..7)。我们无法找到从中提取任何数字或以任何方式操纵它的好方法,因此正在寻求您的帮助。
问题似乎出自我们的 gen_var 方法,该方法规定列表的每个元素必须介于 0 和 1 之间,因此某些数字显示为“0..1”,而不是正确设置。
即使我们得到像“5..7”这样的数字,有什么方法可以使用最小化,或者有什么方法可以操纵该数字,以便我们只得到 5? S(列表中元素的总和)是我们试图最小化的。
gen_var(0, []).
gen_var(N, [X|Xs]) :-
N > 0,
M is N-1,
gen_var(M, Xs),
domain([X],0,1).
find([],_).
find([H|T],P):- match(H,P),find(T,P).
match(pri(_,L),P):-member(X,L), nth1(X,P,1).
main(N,L,P,S) :- gen_var(N,P), minimize(findsum(L,P,S),S).
findsum(L,P,S):- find(L,P), sum(P,#=,S).
Me and a friend are writing a program which is supposed to solve a CLP problem. We want to use minimize to optimize the solution but it won't work, because it keeps saying that the number we get from sum(P,#=,S) is between two numbers (for example 5..7). We haven't been able to find a good way to extract any number from this or manipulate it in any way and are therefore looking for your help.
The problem seems to arise from our gen_var method which says that each element of a list must be between 0 and 1, so some numbers come out as "0..1" instead of being set properly.
Is there any way to use minimize even though we get a number like "5..7" or any way to manipulate that number so that we only get 5? S (the sum of the elements in a list) is what we're trying to minimize.
gen_var(0, []).
gen_var(N, [X|Xs]) :-
N > 0,
M is N-1,
gen_var(M, Xs),
domain([X],0,1).
find([],_).
find([H|T],P):- match(H,P),find(T,P).
match(pri(_,L),P):-member(X,L), nth1(X,P,1).
main(N,L,P,S) :- gen_var(N,P), minimize(findsum(L,P,S),S).
findsum(L,P,S):- find(L,P), sum(P,#=,S).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我稍微修改了你的代码,以适应 SWI-Prolog CLP(FD),它似乎可以工作(有点)。但我认为最小值始终是 0!
该输出样本是预期结果的正确子集吗?
I've slightly modified your code, to adapt to SWI-Prolog CLP(FD), and it seems to work (kind of). But I think the minimum it's always 0!
Is this output sample a correct subset of the expected outcome?