Whats wrong with this prolog optimization solution?

随声附和 提交于 2019-12-10 18:25:56

问题


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

回答1:


There is nothing wrong with it. It is the example from http://eclipseclp.org/examples/xkcd287.ecl.txt, and if you hadn't omitted the line

:- lib(ic).

which loads the interval constraint solver, it would work just fine in ECLiPSe Prolog.




回答2:


Does also work in 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].

But I guess its not an optimization search problem,
the objective function is missing.

Bye



来源:https://stackoverflow.com/questions/35744705/whats-wrong-with-this-prolog-optimization-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!