因此,这是它计算的最少数量的硬币与这些硬币一定值进行的程序。 该项目工程,但它的方式太慢了......当您更换或小于7,它的工作原理值的长度...但是8或以上,这是真的,真的很慢。 有什么办法,以加快这一方案?
% LIBRARIES NEEDED FOR FUNCTION TO WORK
:- lib(ic).
:- lib(ic_global).
:- lib(branch_and_bound).
questionSix(Values, Coins) :-
init_vars(Values, Coins),
coin_cons(Values, Coins, Pockets),
clever_cons(Values, Coins),
Min #= sum(Coins),
minimize((labeling(Values), labeling(Coins), check(Pockets)), Min).
init_vars(Values, Coins) :-
length(Values, 8),
occurrences(5, Values, 1),
Values :: 1..99,
increasing(Values),
length(Coins, 8),
Coins :: 0..99.
increasing(List) :-
( fromto(List, [This, Next | Rest], [Next | Rest], [_])
do
This #< Next
).
clever_cons(Values, Coins) :-
( fromto(Values, [V1 | NV], NV, []),
fromto(Coins, [N1 | NN], NN, [])
do
( NV = [V2 | _]
-> N1*V1 #< V2;
N1*V1 #< 100
)
).
coin_cons(Values, Coins, Pockets) :-
( for(Price, 1, 99),
foreach(CoinsforPrice, Pockets),
param(Coins, Values)
do
price_cons(Price, Coins, Values, CoinsforPrice)
).
price_cons(Price, Coins, Values, CoinsforPrice) :-
( foreach(V, Values), foreach(C, CoinsforPrice), foreach(Coin, Coins),
foreach(Prod, ProdList)
do
Prod = V*C,
0 #=< C,
C #=< Coin
),
Price #= sum(ProdList).
check(Pockets) :-
( foreach(CoinsforPrice, Pockets)
do
once(labeling(CoinsforPrice))
).
任何帮助表示赞赏! 谢谢!