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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
| height(3).
length(8).
test(File) :-
getObjects(Objects,File),
write('Objects = '),write(Objects), nl,
nbObjects(NO,Objects),
write('Nb objects = '),write(NO),nl,
container(NO,Objects).
% recuperer les objets
getObjects(Objects,File) :-
open(File,read,Str),
read_file(Str,Objects),
close(Str).
% transforme un objet en un composant de taille 1x1 avec une position
initComponents([],[]) :- !.
initComponents(O,[object(Id,size(H,L,_))|List]) :-
initComponents(S,List),
append([component(Id,pos(H,L))],S,O).
%-----------------------------
% renvoie le nombre d'objets maximal NO pour la liste d'objets Objects
nbObjects(NO, Objects) :-
length(Objects, All),
getValue(All,Objects,NO).
% recherche une solution valide pour N objets
getValue(N,Objects,Res) :-
height(H),
length(Ln),
Size is H*Ln,
length(L,Size),
sol(N,Objects,R,_),
initComponents(Components,R),
cont(L,Components),
Res is N,
!.
getValue(N,Objects,Res) :-
NewN is N-1,
getValue(NewN,Objects,Res).
split(L, [], L).
split([T|Q], [T|L1], L2) :-
split(Q, L1, L2).
% renvoie toutes les combinaisons possibles de prendre N objets
% dans X objets de la liste L (sans repetition)
% sol(2, [1,2,3,4],Res,_).
% Res = [1, 2] ;
% Res = [1, 3] ;
% Res = [1, 4] ;
% Res = [2, 3] ;
% Res = [2, 4] ;
% Res = [3, 4]
sol(0,L,[], L).
sol(N,L,[T|L3],R) :-
N>0,
split(L, L1, [T|L2]),
N1 is N-1,
sol(N1,L2,L3,L4),
append(L1,L4,R).
% renvoie toutes les solutions valides pour N objets
container(N,Objects) :-
sol(N,Objects,R,_),
initComponents(Components,R),
getSolution(Components), !.
% need cut because of counter (see getSolution)
% renvoie toutes les solutions valides pour une liste d'objets precise
getSolution(Components) :-
height(H),
length(Ln),
Size is H*Ln,
length(L,Size),
flag(counter,_,0),
cont(L,Components),
verifyEmpty(L),
print(L), nl,
flag(counter,C,C+1),
fail.
getSolution(_) :-
flag(counter,C,C), nl,
printCounter(C).
%-----------------------------
cont(_,[]).
cont(L,[X|O]) :-
length(Ln),
cont(L,O),
cont_components(X,L,Ln).
cont_components(X,[Y|L],Ln) :- cond(X,Y), updateLn(Ln,NewLn), cont_components(X,L,NewLn).
cont_components(component(Id,pos(NRow,NCol)),[component(Id,pos(NRow,NCol))|L],Ln) :-
Ln >= NCol,
insertComponents(component(Id,pos(NRow,NCol)), L).
% evite des cas d'objets scindes en deux
% Exemple avec 3 lignes et 2 colonnes :
% 1 | 2
% -----
% 2 | 2
% -----
% 2 | 3
%
% Vision du container sous forme de liste :
% [component(1, pos(1,1)), component(2, pos(2,2)), component(2,
% pos(2,1)), component(2, pos(1,2)), component(2, pos(1,1)),
% component(3, pos(1,1))]
updateLn(1,NewLn) :- length(NewLn).
updateLn(Ln,NewLn) :- Ln \= 1, NewLn is Ln-1.
% insere un composant
insertComponents(component(Id,pos(NRow,NCol)),L) :-
diagoComponent(component(Id,pos(NRow,NCol)),L),
nextComponents(component(Id,pos(NRow,NCol)),L),
subComponents(component(Id,pos(NRow,NCol)),L).
diagoCond(NRow,_) :- NRow \= 1, !.
diagoCond(_,NCol) :- NCol \= 1, !.
% place l'element dans la diagonale
diagoComponent(component(_,pos(1,1)),_).
diagoComponent(component(Id,pos(NRow,NCol)),L) :-
diagoCond(NRow,NCol),
diago(component(Id,pos(NRow,NCol)),Y,Size),
skip(L,Size,[Y|L1]),
insertComponents(Y,L1).
diago(component(Id,pos(1,XCol)),component(Id,pos(1,YCol)),0) :-
YCol is XCol-1.
diago(component(Id,pos(XRow,1)),component(Id,pos(YRow,1)),Size) :-
length(Length),
Size is Length-1,
YRow is XRow-1.
diago(component(Id,pos(XRow,XCol)),component(Id,pos(YRow,YCol)),Size) :-
length(Size),
YRow is XRow-1,
YCol is XCol-1.
% place les composants avec le meme numero de colonne
subComponents(component(Id,pos(NRow,NCol)),L) :-
length(Length),
Size is Length-1,
skip(L,Size,L1),
subComp(component(Id,pos(NRow,NCol)),L1).
subComp(component(_,pos(1,_)),_).
subComp(component(Id,pos(XRow,XCol)),[component(Id,pos(YRow,XCol))|L]) :-
XRow \= 1,
YRow is XRow-1,
subComponents(component(Id,pos(YRow,XCol)),L).
% place les composants avec le meme numero de ligne
nextComponents(component(_,pos(_,1)),_).
nextComponents(component(Id,pos(XRow,XCol)),[component(Id,pos(XRow,YCol))|L]) :-
YCol is XCol-1,
nextComponents(component(Id,pos(XRow,YCol)),L).
cond(_,Y) :- var(Y), !.
cond(X,Y) :- X \= Y.
% verifie si chaque composant ne flotte pas dans les airs
verifyEmpty([]) :- !.
verifyEmpty([X|L]) :- verifyEmptyCond(X), verifyEmpty(L), !.
verifyEmpty([component(_,pos(1,_))|L]) :-
length(Length),
Size is Length-1,
skip(L,Size,L1),
emp(L1),
verifyEmpty(L).
verifyEmptyCond(X) :- var(X).
verifyEmptyCond(component(_,pos(NRow,_))) :- NRow \= 1.
emp([]) :- !.
emp([component(Id,pos(NRow,NCol))|_]) :- nonvar(Id),nonvar(NRow), nonvar(NCol).
%-----------------------------
% permet de se balader dans la liste
%
skip([],_,[]) :- !.
skip(Xs,0,Xs) :- !.
skip([_|Xs],K,Zs) :- K > 0, K1 is K-1, skip(Xs,K1,Zs).
%-----------------------------
% Affiche du container
print([]).
print(Xs) :- nl,
printRow(Xs,Xs1),
write('--------------------------------------'), nl,
printRow(Xs1,Xs2),
write('--------------------------------------'), nl,
printRow(Xs2,_).
% write('--------------------------------------'), nl,
% printRow(Xs3,Xs4),
% write('--------------------------------------'), nl,
% printRow(Xs4,Xs5),
% write('--------------------------------------'), nl,
% printRow(Xs5,Xs6),
% write('--------------------------------------'), nl,
% printRow(Xs6,Xs7),
% write('--------------------------------------'), nl,
% printRow(Xs7,Xs8),
% write('--------------------------------------'), nl,
% printRow(Xs8,Xs9),
% write('--------------------------------------'), nl,
% printRow(Xs9,_).
printRow(Xs,Xs8) :-
printElement(Xs,Xs1), write(' | '),
printElement(Xs1,Xs2), write(' | '),
printElement(Xs2,Xs3), write(' | '),
printElement(Xs3,Xs4), write(' | '),
printElement(Xs4,Xs5), write(' | '),
printElement(Xs5,Xs6), write(' | '),
printElement(Xs6,Xs7), write(' | '),
printElement(Xs7,Xs8), nl.% write(' | '),
% printElement(Xs8,Xs9), write(' | '),
% printElement(Xs9,Xs10), nl.
printElement([X|Xs], Xs) :- var(X), !, write('.').
printElement([component(Id,_)|Xs], Xs) :- write(Id).
printCounter(0) :- !, write('No solution'), nl.
printCounter(1) :- !, write('1 solution'), nl.
printCounter(K) :- write(K), write(' solutions'), nl.
%-----------------------------
% Lecture du fichier
read_file(Stream,[]) :-
at_end_of_stream(Stream), !.
read_file(Stream, [X|List]) :-
\+ at_end_of_stream(Stream),
read(Stream, X),
read_file(Stream, List). |
Partager