diff --git a/TP4.pl b/TP4.pl new file mode 100644 index 0000000..04d892b --- /dev/null +++ b/TP4.pl @@ -0,0 +1,22 @@ +% Ex 5.5 +isBST(null). + +isBST(T):- + isBST(T,_,_) + +isBST(tree(X,null,null),X,X):- + number(X). + +isBST(tree(X,L,R),MinL,MaxR):- + isBST(L,MinL,MaxL), + isBST(R,MinR,MaxR), + MaxL==X,!. + +isBST(tree(X,L,null),Min,X):- + isBST(L,Min,Max), + Max>X,!. + +isBST(tree(X,null,R),X,Max):- + isBST(R,Min,Max), + Min0. + +filter(_,[],[]). + +filter(F,[H1|L1], [H1|L2]):- + call(F,H1),!, + filter(F,L1,L2). + +filter(F,[_|L1],L2):- + filter(F,L1,L2). + +% ex 7 + + +vertex(a). +vertex(b). +vertex(c). +vertex(d). +vertex(e). +vertex(f). + +edge(a,b). +edge(a,c). +edge(a,d). +edge(a,e). +edge(a,f). +edge(b,c). +edge(b,e). +edge(b,f). +edge(c,d). +edge(d,e). +edge(e,f). + +color(rouge). +color(vert). +color(jaune). +color(bleu). + +all_vertices(Vertices):- + findall(V, vertex(V), Vertices). + +create_coloring([],[]). +create_coloring([Vertex|Vertices], [c(Vertex, Color)| Colors]):- + color(Color), + create_coloring(Vertices, Colors). + +valid_coloring([c(V,C)|Colors]) + . + +goal(Colors):- + all_vertices(Vertices), + create_coloring(Vertices,Colors), + valid_coloring(Colors). + +colored_vertex(X,C):- + vertex(X), + color(C). + +ok_vertices(X,Y):- + X \= Y, + edge(X,Y), + colored_vertex(X,C1), + colored_vertex(Y,C2), + C2 \= C1. + +goal2(Colors):- + vertex(Y),findall(vertex(X),ok_vertices(X,Y),Colors). diff --git a/TP6.pl b/TP6.pl new file mode 100644 index 0000000..a35d230 --- /dev/null +++ b/TP6.pl @@ -0,0 +1,56 @@ +% pt 7 +%EX 1 +:-dynamic(fib/2). +fib(0,0):-!. +fib(1,1):-!. +fib(N,F):- + N1 is N-1, + N2 is N-2, + fib(N1,F1), + fib(N2,F2), + F is F1+F2, + asserta(fib(N,F):-!). + +% EX2 +:- dynamic person/1, friend/2, group/2. +person(jef). +person(vero). +person(tom). +person(christian). +person(pierre). +friend(vero,tom). +friend(vero,jef). +friend(vero,christian). +friend(tom,jef). +friend(tom,pierre). +group(g1,[tom,vero,jef]). +group(g2,[vero,christian]). +group(g3,[pierre]). + +remove_person(P):- + retract(person(P)), + %forall(friend(P,X),retract(friend(P,X))), + %forall(friend(X,P),retract(friend(X,P))), + retractall(friend(P,_)), + retractall(friend(_,P)), + forall((group(X,L),member(P,L)),(select(P,L,NL),retract(group(X,L)),asserta(group(X,NL)))). + +add_person(P,Friends,Groups):- + asserta(person(P)), + forall((member(F,Friends)),(asserta(friend(P,F)))), + forall((member(G,Groups),group(G,L), not(member(P,L))),(select(P,NL,L),retract(group(G,L)),asserta(group(G,NL)))). + +friends(P,X):- + friend(P,X);friend(X,P). + +mediator(P):- + person(P), + friends(P,F1), + friends(P,F2), + F2 \= F1, + group(G1,L1), + member(F1,L1), + group(G2,L2), + member(F2,L2), + G2 \= G1. + diff --git a/TP7.pl b/TP7.pl new file mode 100644 index 0000000..61c1eb8 --- /dev/null +++ b/TP7.pl @@ -0,0 +1,49 @@ +:- use_module(library(clpfd)). + +magique3(C):- + length(C,9), + C ins 1..9, + all_distinct(C), + contraintes(C), + label(C). + +contraintes(C):- + C = [C11,C12,C13,C21,C22,C23,C31,C32,C33], + SL1 #= C11+C12+C13, + SL1 #= C21+C22+C23, + SL1 #= C31+C32+C33, + + SL1 #= C11+C21+C31, + SL1 #= C12+C22+C32, + SL1 #= C13+C23+C33, + + SL1 #= C11+C22+C33, + SL1 #= C13+C22+C31. + +machine(T,P,L,R) :- + L = [E2,E1,C50,C20,C10], + R = [R2,R1,R50,R20,R10], + RV #= T - P, + R2 * 200 + R1 * 100 + R50 * 50 + R20 * 20 + R10*10 #= RV, + R2 in 0..E2, + R1 in 0..E1, + R50 in 0..C50, + R20 in 0..C20, + R10 in 0..C10, + label(R). + +puzzle([C,R,O,S,S] + [R,O,A,D,S] = [D,A,N,G,E,R] +) :- + C in 1..9, + R in 1..9, + D in 1..9, + O in 0..9, + S in 0..9, + A in 0..9, + N in 0..9, + E in 0..9, + G in 0..9, + all_distinct([C,R,D,O,S,A,N,E,G]), + C*10000 + R*1000 + O*100 + S*10 +S+ R*10000+ O*1000+ A*100+ D*10+ S #= D*100000+ A*10000+ N*1000+ G*100+ E*10 +R, + label([C,R,D,O,S,A,N,E,G]). + diff --git a/TP8.pl b/TP8.pl new file mode 100644 index 0000000..71621d9 --- /dev/null +++ b/TP8.pl @@ -0,0 +1,17 @@ +arc(a,b,1). +arc(b,f,2). +arc(a,f,4). +arc(b,e,4). +arc(f,e,1). +arc(f,a,3). + +chemin(X,Y,C) :- + arc(X,Y,C). + +chemin(X,Y,C):- + X \=Y, + chemin(X,Z,A), + arc(Z,Y,B), + C is A+B. + +