












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
les algorithmes de recherche en intelligence artifiicelle programmés en Python
Typology: Slides
1 / 20
This page cannot be seen from the preview
Don't miss anything!













Réalisée par : Mme CHIHA Ibtissem : Maitre Assistante en Informatique Industrielle et Automatique 1
Recherche en largeur d’une traversée (breadth-first traversal ) : BFT
PLAN DU COURS 2
DEPTH-FIRST TRAVERSAL : DFT 4 Visited Stack : FIFO A E B F C G A B, C, E B,C, (^) F B,C B, G A E F C G B D B D D, visited = [] stack = [] def dft(visited, graph, node) : visited.append(node) stack.append(node) while stack: m = queue.pop() print (m, end = " ") for node in graph[m] : if node not in visited : visited.append(node) stack.append(node) print("Following is the Depdth-First Traversal") dft(visited, graph, 'A') queue.pop() queue.pop() F
DEPTH-FIRST TRAVERSAL : DFT 5 Visited Stack : FIFO A B G D F C A E, C, B E,C, (^) F,D E,C,F E,C A B D F C G E E,G E E visited = [] stack = [] def dft(visited, graph, node) : visited.append(node) stack.append(node) while stack: m = queue.pop() print (m, end = " ") for node in graph[m] : if node not in visited : visited.append(node) stack.append(node) print("Following is the Depdth-First Traversal") dft(visited, graph, 'A') queue.pop() queue.pop() Ou
BREADTH-FIRST SEARCH : BFS 7 Visited Queue : FIFO [A]
[A,B,F] visited = [] queue = [[start]] def bfs(graph,start,goal) : visited.append(node) queue.append(node) while queue : path=queue.pop(0) node=path[-1] else: adjacentnodes=graph.get(node,[ ]) for node2 in adjacentnodes : newpath=path.copy( ) newpath.append(node2) queue.append(newpath) solution= bfs(graph, 'A', ‘F') print( ' solution is ', solution) queue.pop( 0 ) queue.pop(0)
if node in visited : continue if node ==goal : return path visited.append(node)
B B D F
goal
BREADTH-FIRST SEARCH : DFS 8 Visited Stack : LIFO [A]
[A,E,F] visited = [] stack = [[start]] def dfs(graph,start,goal) : visited.append(node) queue.append(node) while stack : path=stack.pop() node=path[-1] else: adjacentnodes=graph.get(node,[ ]) for node2 in adjacentnodes : newpath=path.copy( ) newpath.append(node2) stack.append(newpath) solution= dfs(graph, 'A', ‘F') print( ' solution is ', solution) queue.pop() queue.pop()
if node in visited : continue if node ==goal : return path visited.append(node)
E E F
goal
DIJKSTRA 10
ALGORITHME DE DIJKSTRA 11 Visited Priority Queue : FIFO A , E , F et cost = 5 [A] 0 queue.pop(0) [A] 0 [A,E]^1 , [A,C]^3 , [A,B]^5 [A,E] 1 [A,C] 3 , [A,B] 5 , [A,E,F] 5 [A,C] 3 [A,B] 5 , [A,E,F] 5 , [A,C,G] 5 [A,B] 5
[A,E,F] 5 Goal queue.sort(key=pathcos t)
13 ALGORITHME DE RECHERCHE A*
v La recherche ordonnée revient à choisir à développer le meilleur nœud au sens d’un certain critère centrée sur le nœud ayant les meilleurs chances de mener au but L’utilisation d’une heuristique est basée sur une fonction d’évaluation pour ordonner la recherche appelée fonction heuristique nommée h(u) v h est la fonction heuristique qui estime le coût du passage de l'état u à l'état final. Soit f une fonction d’évaluation, f(n) exprime la valeur de cette fonction pour le noeud n f(n) représente le coût idéal du chemin passant par un nœud n pour arriver au but ALGORITHME DE RECHERCHE : A RECHERCHE AVEC HEURISTIQUE ET COÛT VARIABLE 14
ALGORITHME DE A*
def path_f_cost(path): g_cost= for(node,cost)in path: g_cost+=cost last_node=path[-1][0] h_cost=H_table[last_node] f_cost=g_cost+h_cost return f_cost, last_node def A_star_search(graph,start,goal) : visited=[ ] queue =[[(start, 0 )]] while queue : queue.sort(key=path_f_cost) path=queue.pop(0) node=path[-1][0] if node in visited : continue visited.append(node) if node==goal : return path else : adjacentnodes=graph.get(node,[ ]) for (node 2 ,cost) in adjacentnodes : newpath=path.copy( ) newpath.append((node 2 ,cost)) queue.append(newpath) solution= A_star_search (graph,'A','F') print('solution is', solution) print('cost of solution is', path_f_cost(solution)) Visited Priority Queue : FIFO [A] (^2) queue.pop(0) [A] 2
[A,E] 5 [A,C] 5 [A,C,G] 5 , [A,E,F] 9 , [A,B] 11
[A,E,F] 9 Goal (^) queue.sort(key=pathcos t)
17 ALGORITHME DE RECHERCHE GREEDY BEST FIRST
ALGORITHME DE GREEDY FIRST BEST 19 Visited Priority Queue : FIFO A , E , F et cost = [A] 2 queue.pop(0) [A] 2 [A,C]^2 , [A,E]^4 , [A,B]^6 [A,C] 2 [A,C,G]0, [A,E,]4 , , [A,B] 6 [A,C,G] 0 , [A,E] 4 , [A,B] 6
[A,E,F] 4 Goal queue.sort(key=pathcos t)
ALGORITHME DE GREEDY
def path_h_cost(path): g_cost= 0 for(node,cost)in path: g_cost+=cost last_node=path[- 1 ][ 0 ] h_cost=H_table[last_node] return h_cost, last_node def Greedy_search(graph,start,goal) : visited=[ ] queue =[[(start, 0 )]] while queue : queue.sort(key=path_h_cost) path=queue.pop(0) node=path[-1][0] if node in visited : continue visited.append(node) if node==goal : return path else : adjacentnodes=graph.get(node,[ ]) for (node2,cost) in adjacentnodes : newpath=path.copy( ) newpath.append((node2,cost)) queue.append(newpath) solution= Greedy_search (graph,'A','F') print('solution is', solution) print('cost of solution is', path_h_cost(solution)) Visited Priority Queue : FIFO queue.pop(0) [A] 2 [A] 2 [A,C] 2 , [A,E] 4 , [A,B] 6 [A,C] 2 [A,C,G] 4
[A,E,F] 4 Goal queue.sort(key=path_h_cos t)