TP1 version fonctionnelle

This commit is contained in:
Nabzzz 2021-03-21 11:48:49 +01:00
parent 7e31c799b2
commit db6d674e93

102
TP1.pl
View file

@ -45,12 +45,12 @@ Les autres prédicats sont spécifiques au Taquin.
%******************** %********************
% format : initial_state(+State) ou State est une matrice (liste de listes) % format : initial_state(+State) ou State est une matrice (liste de listes)
/*
initial_state([ [b, h, c], % C'EST L'EXEMPLE PRIS EN COURS initial_state([ [b, h, c], % C'EST L'EXEMPLE PRIS EN COURS
[a, f, d], % [a, f, d], %
[g,vide,e] ]). % h1=4, h2=5, f*=5 [g,vide,e] ]). % h1=4, h2=5, f*=5
*/
% AUTRES EXEMPLES POUR LES TESTS DE A* % AUTRES EXEMPLES POUR LES TESTS DE A*
@ -68,11 +68,11 @@ initial_state([ [b, c, d],
initial_state([ [f, g, a], initial_state([ [f, g, a],
[h,vide,b], [h,vide,b],
[d, c, e] ]). % h2=16, f*=20 [d, c, e] ]). % h2=16, f*=20
*/
initial_state([ [e, f, g], initial_state([ [e, f, g],
[d,vide,h], [d,vide,h],
[c, b, a] ]). % h2=24, f*=30 [c, b, a] ]). % h2=24, f*=30
/*
initial_state([ [a, b, c], initial_state([ [a, b, c],
[g,vide,d], [g,vide,d],
[h, f, e]]). % etat non connexe avec l'etat final (PAS DE SOLUTION) [h, f, e]]). % etat non connexe avec l'etat final (PAS DE SOLUTION)
@ -191,7 +191,7 @@ delete(N,X,[Y|L], [Y|R]) :-
coordonnees([L,C], Mat, Elt) :- coordonnees([L,C], Mat, Elt) :-
nth1(L,Mat ,Ligne2), nth1(C,Ligne2,Elt). nth1(L,Mat ,Ligne), nth1(C,Ligne,Elt).
%************* %*************
@ -222,8 +222,16 @@ heuristique(U,H) :-
heuristique1(U, H) :- findall(P,bad_placed(U,P),Liste), length(Liste,H) . heuristique1(U, H) :- findall(P,bad_placed(U,P),Liste), length(Liste,H) .
manhattan(U,P, M) :- /*
* manhattan(U,P, M) :-
final_state(Fin), nth1(L,Fin,Ligne), nth1(C,Ligne,_P2), nth1(L,U ,Ligne2), nth1(C,Ligne2,P), coordonnees([L1,C1],U,P), coordonnees([L2,C2],Fin,P), M1 is abs(L1-L2), M2 is abs(C1-C2), M is (M1+M2), P \= vide. final_state(Fin), nth1(L,Fin,Ligne), nth1(C,Ligne,_P2), nth1(L,U ,Ligne2), nth1(C,Ligne2,P), coordonnees([L1,C1],U,P), coordonnees([L2,C2],Fin,P), M1 is abs(L1-L2), M2 is abs(C1-C2), M is (M1+M2), P \= vide.
*/
manhattan(U,Element,Cout):-
final_state(Fin),
coordonnees([X,Y],Fin,Element),
coordonnees([A,B],U,Element),
Element \= vide ,
Cout is (abs(X - A) + abs(B - Y)).
%**************** %****************
%HEURISTIQUE no 2 %HEURISTIQUE no 2
@ -233,9 +241,7 @@ heuristique(U,H) :-
% entre sa position courante et sa positon dans l'etat final % entre sa position courante et sa positon dans l'etat final
heuristique2(U, H) :- findall(M,manhattan(U,_,M),Liste), sumlist(Liste,H) . %******** heuristique2(U, H) :- findall(M,manhattan(U,_,M),Liste), sumlist(Liste,H) .
% A FAIRE
%********
@ -693,8 +699,8 @@ main :-
empty(Pf0), empty(Pf0),
empty(Pu0), empty(Pu0),
empty(Q), empty(Q),
insert([[H,H,0],S0],Pf0,Pf), % CORRECT insert([[H,H,0],S0],Pf0,Pf),
insert([S0,[H,H,0],nil,nil],Pu0,Pu), % CORRECT insert([S0,[H,H,0],nil,nil],Pu0,Pu),
% lancement de Aetoile % lancement de Aetoile
aetoile(Pf,Pu,Q), !. aetoile(Pf,Pu,Q), !.
@ -707,34 +713,20 @@ aetoile(Pf, _, _) :-
aetoile(Pf, Pu, Qs) :- aetoile(Pf, Pu, Qs) :-
suppress_min([[_,_,_],Fin],Pf,_Pf_new), suppress_min([[_,_,_],Fin],Pf,_Pf_new),
initial_state(Deb),
final_state(Fin), final_state(Fin),
writeln("Solution trouvée !"), writeln("Solution trouvée !"),
suppress([Fin,[F,H,G],Pere,Action],Pu,_Pu_new), suppress([Fin,[F,H,G],Pere,Action],Pu,_Pu_new),
affiche_solution([Fin,[F,H,G],Pere,Action], Qs). affiche_solution([Fin,[F,H,G],Pere,Action], Qs).
aetoile(Pf, Pu, Qs) :- aetoile(Pf, Pu, Qs) :-
%(Pf) CORRECT
suppress_min([[_,_,_],U],Pf,Pf_new), % le nœud de Pf correspondant à létat U à développer suppress_min([[_,_,_],U],Pf,Pf_new), % le nœud de Pf correspondant à létat U à développer
suppress([U,[F,H,G],Pere,Action],Pu,Pu_new), %le nœud frère associé dans Pu suppress([U,[F,H,G],Pere,Action],Pu,Pu_new), %le nœud frère associé dans Pu
%(Pf) CORRECT
expand(U,G,Liste), expand(U,G,Liste),
loop_successors(Liste, Pf_new,Pu_new,Qs,Pf_last,Pu_last), loop_successors(Liste, Pf_new,Pu_new,Qs,Pf_last,Pu_last),
% INCORRECT
insert([U,[F,H,G],Pere,Action],Qs,Qs_new), insert([U,[F,H,G],Pere,Action],Qs,Qs_new),
%(Qs) CORRECT
%print(U),
aetoile(Pf_last,Pu_last,Qs_new). aetoile(Pf_last,Pu_last,Qs_new).
/*
aetoile(Pf, Pu, Q) :-
suppress_min([_, U],Pf, PfNext),
suppress([U, [F, H, G], Pere, A], Pu, PuNext),
expand(U, G, L),
loop_successors(L, PfNext, PuNext, Q, PfFinal, PuFinal),
insert([U, [F, H, G], Pere, A], Q, QFinal),
aetoile(PfFinal, PuFinal, QFinal).
*/
expand(U,G,L):- findall(U3,(rule(Action,1,U,U2),heuristique(U2,H), F is H+G+1, G2 is G+1, U3 = [U2,[F,H,G2],U,Action]),L). expand(U,G,L):- findall(U3,(rule(Action,1,U,U2),heuristique(U2,H), F is H+G+1, G2 is G+1, U3 = [U2,[F,H,G2],U,Action]),L).
@ -747,16 +739,7 @@ affiche_solution([Debut,_,nil,nil],_) :-
writeln("Etat initial : "), writeln("Etat initial : "),
writeln(Debut). writeln(Debut).
/*
affiche_solution(State,Qs) :-
State = [U,[_,_,G],Pere,Action],
final_state(U),
suppress([Pere,Cout,Pere1,Action1],Qs, Qs_new),
affiche_solution([Pere,Cout,Pere1,Action1],Qs_new),
writeln(""),
write("Final state : "),
print(U).
*/
affiche_solution(State,Qs):- affiche_solution(State,Qs):-
State = [U,[_,_,G],Pere,Action], State = [U,[_,_,G],Pere,Action],
@ -799,50 +782,3 @@ loop_successors([D|F], Pf,Pu,Qs,Pf_last,Pu_last) :-
insert([U,[Fu,Gu,Hu],Pere,Action],Pu,Pu_N), insert([U,[Fu,Gu,Hu],Pere,Action],Pu,Pu_N),
insert([[Fu,Gu,Hu],U],Pf,Pf_N), insert([[Fu,Gu,Hu],U],Pf,Pf_N),
loop_successors(F,Pf_N,Pu_N,Qs,Pf_last,Pu_last). loop_successors(F,Pf_N,Pu_N,Qs,Pf_last,Pu_last).
/*loop_successors([D|F], Pf,Pu,Qs) :-
D = [U,[Fu,Gu,Hu],Pere,Action],
belongs([U,_,_,_],Pu) -> %- S est connu dans Pu
( suppress([U,[FF,GG,HH],Father,A],Pu,Pu_new),
suppress([_,U],Pf,Pf_new),
% garder le terme associé à la meilleure évaluation
( FF < Fu ) ->
( insert([U,[FF,GG,HH],Father,A],Pu_new,Pu_N),
insert([[FF,GG,HH],U],Pf_new,Pf_N),
loop_successors(F,Pf_N,Pu_N,Qs) )
;
( insert([U,[Fu,Gu,Hu],Pere,Action],Pu_new,Pu_N),
insert([[Fu,Gu,Hu],U],Pf_new,Pf_N),
loop_successors(F,Pf_N,Pu_N,Qs) )
)
;
(
D = [U,Cout,Pere,_Action],
insert([U,Cout,Pere,Action],Pu,Pu_N),
insert([Cout,U],Pf,Pf_N),
loop_successors(F,Pf_N,Pu_N,Qs)
).
loop_successors([D|F], Pf,Pu,Qs) :-
D = [U,[Fu,Gu,Hu],Pere,Action],
belongs([U,_,_,_],Pu), %- S est connu dans Pu
suppress([U,[FF,GG,HH],Father,A],Pu,Pu_new),
suppress([_,U],Pf,Pf_new),
% garder le terme associé à la meilleure évaluation
( FF < Fu ) ->
( insert([U,[FF,GG,HH],Father,A],Pu_new,Pu_N),
insert([[FF,GG,HH],U],Pf_new,Pf_N),
loop_successors(F,Pf_N,Pu_N,Qs) )
;
( insert([U,[Fu,Gu,Hu],Pere,Action],Pu_new,Pu_N),
insert([[Fu,Gu,Hu],U],Pf_new,Pf_N),
loop_successors(F,Pf,Pf_N,Qs) )
.
loop_successors([D|F], Pf,Pu,Qs) :-
D = [U,[Fu,Gu,Hu],Pere,Action],
insert([U,[Fu,Gu,Hu],Pere,Action],Pu,Pu_N),
insert([[Fu,Gu,Hu],U],Pf,Pf_N),
loop_successors(F,Pf,Pf_N,Qs).*/