1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| :- use_module(library(clpfd)).
:- use_module(library(lambda)).
/* exemple sans solution
articles([[1,1,0,0,2], [0,0,1,0,2], [0,1,0,1,0], [2,0,2,0,0],[0,0,0,0,2]]).
*/
/* exemple avec solution */
articles([[1,1,0,0], [0,0,1,0], [0,1,0,1], [2,0,2,0],[2,2,0,0]]).
solve :-
articles(Lst_Art),
transpose(Lst_Art, Lst_Prod),
% on affecte à chaque produit un coefficient
length(Lst_Prod, Len_Prod),
length(Coef_Prod, Len_Prod),
Coef_Prod ins 0..1,
sum(Coef_Prod, #=, Som_Prod),
% calcul des contraintes
post_constraint(Lst_Art, 1, Coef_Prod, [], Mat),
% on impose un minimum de 5 au colonnes
numlist(1, Len_Prod, NL),
maplist(set_colonnes(Mat), NL),
labeling([min(Som_Prod)], Coef_Prod),
writeln(Coef_Prod).
post_constraint([], _, _, Mat, Mat).
post_constraint([Art | T], N, Coef_Prod, Cur_Mat, Mat) :-
foldl(\A^B^Y^Z^(C #= A*B, append(Y, [C], Z)), Art, Coef_Prod, [], New_Line),
sum(New_Line, #>, 0),
append(Cur_Mat, [New_Line], New_Mat),
N1 is N+1,
post_constraint(T, N1, Coef_Prod, New_Mat, Mat).
% le total des colonnes doit être inférieur à 5
set_colonnes(Mat, Num):-
foldl(Num +\X^Y^Z^(element(Num, X, Value), Z #=Value + Y), Mat, 0, R),
R #=< 5. |
Partager