So this is a program where it calculates the fewest number of coins to carry with certain values on these coins. The program works, but it's way too slow... When you replace the length of the values 7 or less, it works... But 8 or above, it's really, really slow. Is there any way to speed up this program?
% 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))
).
Any help is appreciated! Thanks!