这个prolog优化解决方案有什么问题?

时间:2016-03-02 10:35:44

标签: prolog clpfd eclipse-clp

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).

2 个答案:

答案 0 :(得分:6)

它没有任何问题。这是来自http://eclipseclp.org/examples/xkcd287.ecl.txt的示例,如果您没有省略该行

:- lib(ic).

加载interval constraint solver,它会在ECLiPSe Prolog中正常工作。

答案 1 :(得分:2)

在SWI-Prolog中也有效:

?- use_module(library(clpfd)).
?- [user].
solve(Amounts) :-
    Total = 1505,
    Prices = [215,275,335,355,420,580],
    length(Prices, N),
    length(Amounts, N),
    min_list(Prices, MinPrice),
    MaxAmount is Total//MinPrice,
    Amounts ins 0..MaxAmount,
    scalar_product(Prices, Amounts, #=, Total),
    label(Amounts).
^D
?- solve(X).
X = [1, 0, 0, 2, 0, 1] ;
X = [7, 0, 0, 0, 0, 0].

但我猜它不是优化搜索问题,
目标函数缺失。

再见