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

2 回答 2

5

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

:- lib(ic).

加载区间约束求解器,它可以在ECLiPSe Prolog 中正常工作。

于 2016-03-03T13:14:18.187 回答
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].

但我想这不是优化搜索问题,
缺少目标函数。

再见

于 2016-03-11T20:28:53.603 回答