2016-03-02 134 views
3
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

這不是SWI-Prolog。大概是[tag:eclipse-prolog] – false

+0

爲什麼你說它有什麼問題? – lurker

回答

6

它沒有問題。距離http://eclipseclp.org/examples/xkcd287.ecl.txt的例子,如果你沒有忽略它加載interval constraint solver

:- lib(ic). 

,它會工作在ECLiPSe Prolog的就好了。

+2

(參見http://xkcd.com/287/)--- ...和'lib(ic)'是什麼,整數約束庫? –

+2

'lib(ic)'代表區間限制。它實現了對整數和實數的約束(表示爲浮點間隔)。我會修改我的答案。 – jschimpf

+0

感謝您的澄清! –

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

但我想它不是一個優化搜索問題,
目標函數丟失。

再見