Version fonctionnelle

This commit is contained in:
Nabzzz 2021-02-22 19:54:29 +01:00
parent 0937e66efb
commit 68f191c7da

89
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)
/*
initial_state([ [b, h, c], % C'EST L'EXEMPLE PRIS EN COURS
[a, f, d], %
[g,vide,e] ]). % h1=4, h2=5, f*=5
*/
% AUTRES EXEMPLES POUR LES TESTS DE A*
@ -59,18 +59,20 @@ initial_state([ [ a, b, c],
[ g, h, d],
[vide,f, e] ]). % h2=2, f*=2
initial_state([ [b, c, d],
[a,vide,g],
[f, h, e] ]). % h2=10 f*=10
initial_state([ [f, g, a],
[h,vide,b],
[d, c, e] ]). % h2=16, f*=20
*/
initial_state([ [e, f, g],
[d,vide,h],
[c, b, a] ]). % h2=24, f*=30
/*
initial_state([ [a, b, c],
[g,vide,d],
[h, f, e]]). % etat non connexe avec l'etat final (PAS DE SOLUTION)
@ -197,8 +199,8 @@ delete(N,X,[Y|L], [Y|R]) :-
%*************
heuristique(U,H) :-
heuristique1(U, H). % au debut on utilise l'heuristique 1
% heuristique2(U, H). % ensuite utilisez plutot l'heuristique 2
% heuristique1(U, H). % au debut on utilise l'heuristique 1
heuristique2(U, H). % ensuite utilisez plutot l'heuristique 2
%****************
@ -634,6 +636,7 @@ avl_test(10, Final) :-
*
*
*/
%*******************************************************************************
% AETOILE
%*******************************************************************************
@ -696,17 +699,18 @@ main :-
aetoile(Pf,Pu,Q), !.
%*******************************************************************************
aetoile(Pf, Pu, _) :-
empty(Pf),
empty(Pu),
write("PAS de SOLUTION : LETAT FINAL NEST PAS ATTEIGNABLE !").
aetoile(Pf, _, Qs) :-
aetoile(Pf, _, _) :-
empty(Pf),
writeln("PAS de SOLUTION : LETAT FINAL NEST PAS ATTEIGNABLE !").
aetoile(Pf, Pu, Qs) :-
suppress_min([[_,_,_],Fin],Pf,_Pf_new),
initial_state(Deb),
final_state(Fin),
suppress([Fin,[F,H,G],Pere,Action],_Pu,_Pu_new),
write("Solution trouvée !"),
writeln(""),
writeln("Solution trouvée !"),
suppress([Fin,[F,H,G],Pere,Action],Pu,_Pu_new),
affiche_solution([Fin,[F,H,G],Pere,Action], Qs).
aetoile(Pf, Pu, Qs) :-
@ -719,27 +723,31 @@ aetoile(Pf, Pu, Qs) :-
% INCORRECT
insert([U,[F,H,G],Pere,Action],Qs,Qs_new),
%(Qs) CORRECT
%print(U),
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).
/*affiche_soltuion(State,nil) :-
State = [U,[_,_,G],_Pere,Action],
writeln(""),
print(U),
writeln(""),
write("Cout = "), print(G),
writeln(""),
write("Action = "), print(Action),
writeln("").
*/
affiche_solution([Debut,_,nil,nil],_) :-
initial_state(Debut),
write("Etat initial : ").
writeln("Etat initial : "),
writeln(Debut).
/*
affiche_solution(State,Qs) :-
State = [U,[_,_,G],Pere,Action],
final_state(U),
@ -748,16 +756,16 @@ affiche_solution(State,Qs) :-
writeln(""),
write("Final state : "),
print(U).
*/
affiche_solution(State,Qs):-
State = [U,[_,_,G],Pere,Action],
suppress([Pere,Cout,Pere1,Action1],Qs, Qs_new),
affiche_solution([Pere,Cout,Pere1,Action1],Qs_new),
print(U),
writeln(""),
write("Cout = "), print(G),
writeln(""),
write("Action = "), print(Action).
write("Cout = "), writeln(G),
write("Action = "), writeln(Action),
writeln(U).
@ -768,32 +776,33 @@ loop_successors([D|F], Pf,Pu,Qs,Pf_last,Pu_last) :-
belongs([U,_,_,_],Qs), %S est connu dans Q alors oublier cet état
loop_successors(F,Pf,Pu,Qs,Pf_last,Pu_last).
loop_successors([D|F], Pf,Pu,Qs,Pf_last,Pu_last) :-
D = [U,[Fu,Gu,Hu],Pere,Action],
belongs([U,_,_,_],Pu),
suppress([U,[FF,GG,HH],Father,A],Pu,Pu_new),
suppress([_,U],Pf,Pf_new),
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,Pf_last,Pu_last).
D = [U,[Fu,_Gu,_Hu],_Pere,_Action],
belongs([U,[FF,_GG,_HH],_Father,_A],Pu),
FF =< Fu,
loop_successors(F,Pf,Pu,Qs,Pf_last,Pu_last).
loop_successors([D|F], Pf,Pu,Qs,Pf_last,Pu_last) :-
D = [U,[Fu,Gu,Hu],Pere,Action],
belongs([U,_,_,_],Pu),
belongs([U,[FF,GG,HH],Father,A],Pu),
FF > Fu,
suppress([U,[FF,GG,HH],Father,A],Pu,Pu_new),
suppress([_,U],Pf,Pf_new),
FF >= Fu,
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,Pf_last,Pu_last).
loop_successors([D|F], Pf,Pu,Qs,Pf_last,Pu_last) :-
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_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