Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
87 views
8 pages
Lab Lec5
Uploaded by
احمد سالم
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save lab-lec5 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
87 views
8 pages
Lab Lec5
Uploaded by
احمد سالم
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save lab-lec5 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 8
Search
Fullscreen
Artificial Intelligence Dr.Ghaidaa Almulla Morning Study fr Ass.tec.Nibras Ridha Practical Lecture -5 ‘Ass.tec. Huda Sameer Best-First Search Algorithm in Python Greedy Best Search Greedy best-first search algorithm always selects the path which appears best at that moment. It is the combination of depth-first search and breadth-first search algorithms. It uses the heuristic function and search. Best-first search allows us to take the advantages of both algorithms. With the help of best-first search, at each step, we can choose the most promising node. In the best first search algorithm, we expand the node which is closest to the goal node and the closest cost is estimated by heuristic function, i.e. f(n)= g(n). Were, h(n)= estimated cost from node n to the goal. The greedy best first algorithm is implemented by the priority queue. Best first search algorithm steps: 2 Step 1: Place the starting node into the OPEN list. © Step 2: If the OPEN list is empty, Stop and return failure. © Step 3: Remove the node n, from the OPEN list which has the lowest value of h(n), and places it in the CLOSED list. © Step 4: Expand the node n, and generate the successors of node n. 2 Step 5: Check each successor of node n, and find whether any node is a goal node or not. If any successor node is goal node, then return success and terminate the search, else proceed to Step 6. Step 6: For each successor node, algorithm checks for evaluation function f(n), and then check if the node has been in either OPEN or CLOSED list. If the node has not been in both list, then add it to the OPEN list. © Step 7: Return to Step 2. Advantages: © Best first search can switch between BFS and DFS by gaining the advantages of both the algorithms. ©. This algorithm is more efficient than BFS and DFS algorithms. Disadvantages: © It can behave as an unguided depth-first search in the worst case scenario. It can get stuck in a loop as DFS. © This algorithm is not optimal.Artificial Intelligence Dr.Ghaidaa Almulla Morning Study fr Ass.tec.Nibras Ridha Practical Lecture -5 ‘Ass.tec. Huda Sameer Example: We have created a graph from a map in this problem, actual distances is used in the graph. The goal is to find the shortest path from one city to another city. We are using a Graph class and a Node class in the best-first search algorithm. We are using straight-line distances (air-travel distances) between cities as our heuristic, these distances will never overestimate the actual distances between cities. # This class represent a graph class Graph: # Initialize the class def _init_(self, graph_dict=None, directed=True): self.graph_dict = graph_dict or {} self.directed = directed Af not directed: self.make_undirected() # Ciwate aa WRASeucked gregh By: uading: symmetete ayes dot make_undirected (self) : for a in list (self.graph_ dict .keys()): for (b, dist) in self.graph dict [a].items(): self.graph_dict.setdefault (b, {}) [a] - dist # Add a link from A and B of given distance, and also the inverse link if the graph is undirected def connect (self, A, B, distanc: self.graph_dict.setdefault(a, {}) [B] = distance if not self.directed self.graph_dict.setdefault(B, {))[A] - distance 4 Get neighbors or a neighborArtificial Intelligence Dr.Ghaidaa Almulla Morning Study fr Ass.tec.Nibras Ridha Practical Lecture -5 Ass.tec. Huda Sameer def get (self, a, b=None): links = self.graph_dict setdefault(a, (}) if b is None: return links else: return links.get (b) # Return a list of nodes in the graph def nodes (sel£): al set ([k for k in self.graph_dict-keys()]) 82 set([k2 for v in self.graph_dict.values() for k2, v2 in v.items() |) nodes = s1-union(s2) return list (nodes) # This class represent a node class Node: # Initialize the class def _init__(self, name:str, parent:str): self.name = name self.parent = parent self.g - 0 # Distance to start node self.h = 0 # Distance to goal node self.£ 0 # Total cost # Compare nodes def _eq_ (self, other): return self.name -= other.nane # Sort nodesArtificial Intelligence Dr.Ghaidaa Almulla Morning Study fr Ass.tec.Nibras Ridha Practical Lecture -5 Ass.tec. Huda Sameer def _lt_(self, other): return self.f < other.£ # Print node def __repr__(self): return ('({0},{1})'.format (self-position, self.f)) | # Best-first search def best_first_search (graph, heuristics, start, end): # Create lists for open nodes and closed nodes open = [] closed = [] # Create a start node and an goal node etart_node ~ Node (start, None) goal_node = Node (end, None) # Add the start node open. append (start_node) # Loop until the open list is empty while len (open) > 0: # Sort the open list to get the node with the lowest cost first open. sort () # Get the node with the lowest cost current_node = open. pop (0) # Add the current node to the closed list closed. append (current_node) # Check if we have reached the goal, return the path Af current_node goal_nede: path = [JArtificial Intelligence Dr.Ghaidaa Almulla Morning Study fr Ass.tec.Nibras Ridha Practical Lecture -5 Ass.tec. Huda Sameer while current_node != start_node: path. append (current_node.name + ': ' + atr(current_node.g)) current_node = current_node.parent path.append(start_node.name + ': ' + str(start_node.g)) # Return reversed path return path[::-1] # Get neighbours neighbors = graph.get (current_node-name) ¥ Loop neighbors for key, value in neighbors. items (): # Create a neighbor node neighbor ~ Node (key, current_node) # Check if the neighbor is in the closed list if (neighbor in closed) : continue # Calculate cost to goal neighbor.g = current _node.g + graph.get (curven neighbor. name) node .name, neighbor-h = heuristics.get (neighbor .name) neighbor.£ - neighbor. # Check if neighbor is in open list and if it has a lower f value if(add_to_open(open, neighbor) True): # Everything is green, add neighbor to open list open. append (neighbor) # Return None, no path is found return NoneArtificial Intelligence Dr.Ghaidaa Almulla Morning Study Ass.tec.Nibras Ridha Practical Lecture -5 Ass.tec.Huda Sameer # Check if a neighbor should be added to open list def add_to_open (open, neighbor) : for node in open: Af (neighbor -- node and neighbor.£ >- node.£): return False I return True # The main entry point for this module def main(): # Czeate a graph graph = Graph () # Create graph connections (Actual distance) qraph.connact ("Frankfurt', 'Worsburg', 111) graph.connect ("Frankfurt', ‘Mannheim’, 85) graph.connect (*Wurzburg', ‘Nurnberg', 104) graph connect (‘Wurzburg', "Stuttgart’, 140) gqraph.connect (‘Wurzburg', ‘Ulm’, 183) graph.connect ("Mannheim', ‘Nurnberg', 230) graph.connect (‘Mannheim', 'xarlsruhe', 67) graph.connect ("Karisruhe', ‘Basel’, 191) graph.connect ("Karlsruhe', 'Stuttgart', 64) graph.connect (‘Nurnberg', 'Ulm', 171) graph.connect (*Nurnberg', ‘Munchen, 170) qraph.connect ("Nurnberg', ‘Passau’, 220) graph.connect ("Stuttgart', ‘Ulm’, 107) graph.connect ("Basel', 'Bern', 91) graph.connect ("Basel', ‘Zurich’, 85) I i | | | i | | | : | | | i | | | : | | | i | | | : | | | i | | i | | | : | | | i | | | | | | | | 1 | | | | | | | | | baArtificial Intelligence Dr.Ghaidaa Almulla Morning Study Ass.tec.Nibras Ridha Practical Lecture -5 Ass.tec.Huda Sameer graph.connect ("Bern', ‘Zurich', 120) qraph.connect (*Zurich", 'Menmingen', 184) graph connect (*Memmingen', ‘Uim', 55) graph.connect ("Menmingen', ‘Munchen', 115) graph.connect ("Munchen', "Ulm", 123) graph.connect ("Munchen', 'Passau', 189) graph.connect (*Munchen', 'Rosenheim', 99) graph. connect ("Rosenheim', ‘Salzburg’, 81) graph.connect ("Passau', 'Linz", 102) graph .connect ('Sa1zbu: ‘Linz’, 126) # Make graph undirected, create eymmetric connections graph make undirected |) # Create heuristics (straight-line distance, air-travel distance) heuristics = () heuristics["Basel'] = 204 heuristics["Bern'] = 247 heuristics['Frankfurt'] = 215 137 heuristics ['Karlsruhe"] heuristics["Linz'] - 318 heuristics ['Mannheim'] = 164 heuristics {'Munchen')] = 120 heuristics['memmingen'] = 47 heuristics['Nurnberg'] = 132 heuristics["Passau'] = 257 heuristics [‘Rosenheim'] = 168Artificial Intelligence Dr.Ghaidaa Almulla Morning Study fr Ass.tec.Nibras Ridha Practical Lecture -5 Ass.tec. Huda Sameer heuristice|‘stuttgart') = 75 heuristics['Salzburg'] = 236 i ucisticet wuebors!l = 183 i heuristics['Zurich'] - 157 | heuristics["Ulm'] = 0 # Run search algorithm path = best_first_search(graph, heuristics, ‘Frankfurt’, ‘Ulm') print (path) print () # Tell python to run main method 4 mame "main": main) Output ['Frankfurt: 0', ‘Wurzburg: 111', "Ulm: 294")
You might also like
AI ML Lab Manual - Prepared by Mrs. R. Viniba-1
PDF
No ratings yet
AI ML Lab Manual - Prepared by Mrs. R. Viniba-1
44 pages
Aiml Lab Manual Upto DT
PDF
No ratings yet
Aiml Lab Manual Upto DT
40 pages
CS3491 Ai & ML Lab Manual
PDF
No ratings yet
CS3491 Ai & ML Lab Manual
57 pages
ML Manual
PDF
No ratings yet
ML Manual
42 pages
AIML Lab - SRMTRPEC - Observation - 20 Feb 24
PDF
No ratings yet
AIML Lab - SRMTRPEC - Observation - 20 Feb 24
73 pages
Aiml Journal 18CSL76
PDF
No ratings yet
Aiml Journal 18CSL76
36 pages
Ai Practical File
PDF
No ratings yet
Ai Practical File
14 pages
AIML Manual
PDF
No ratings yet
AIML Manual
123 pages
Lab Programs CS3491
PDF
No ratings yet
Lab Programs CS3491
56 pages
Ai Unit 1
PDF
No ratings yet
Ai Unit 1
75 pages
AIML Manual V1 2-86
PDF
No ratings yet
AIML Manual V1 2-86
85 pages
Ai&ml Lab Manual
PDF
No ratings yet
Ai&ml Lab Manual
106 pages
AI Lab8
PDF
No ratings yet
AI Lab8
6 pages
AI - 5thsem - Manual Updated
PDF
No ratings yet
AI - 5thsem - Manual Updated
25 pages
Ai-Lab Task-07
PDF
No ratings yet
Ai-Lab Task-07
8 pages
Heuristic Search Algorithms
PDF
No ratings yet
Heuristic Search Algorithms
8 pages
Ai Lab FINAL-28 Merged
PDF
No ratings yet
Ai Lab FINAL-28 Merged
81 pages
Online Exam Project Report
PDF
No ratings yet
Online Exam Project Report
60 pages
AIML Practical
PDF
No ratings yet
AIML Practical
33 pages
Cs3491 - Ai&Ml Labrecord
PDF
No ratings yet
Cs3491 - Ai&Ml Labrecord
29 pages
56d0f5 - Informed Search Algorithms
PDF
No ratings yet
56d0f5 - Informed Search Algorithms
16 pages
AIML Lab Manual
PDF
No ratings yet
AIML Lab Manual
23 pages
Document Aiml Lab??
PDF
No ratings yet
Document Aiml Lab??
23 pages
AI Lab
PDF
No ratings yet
AI Lab
45 pages
Lab Manual For Aiml
PDF
No ratings yet
Lab Manual For Aiml
28 pages
Artificial Intelligence Complete Lab Manual... - 1
PDF
No ratings yet
Artificial Intelligence Complete Lab Manual... - 1
18 pages
A Star
PDF
No ratings yet
A Star
5 pages
Ai&ml Rec Final
PDF
No ratings yet
Ai&ml Rec Final
66 pages
Xzno22222222222 PDF
PDF
No ratings yet
Xzno22222222222 PDF
278 pages
Final AI & ML Lab Manual
PDF
No ratings yet
Final AI & ML Lab Manual
59 pages
Ai-Lab R22cse
PDF
No ratings yet
Ai-Lab R22cse
4 pages
AI Print
PDF
No ratings yet
AI Print
14 pages
CS3491 - AI&ML Lab Record
PDF
No ratings yet
CS3491 - AI&ML Lab Record
47 pages
Ai&ml Lab Manual Final
PDF
No ratings yet
Ai&ml Lab Manual Final
50 pages
Lab Manual
PDF
No ratings yet
Lab Manual
43 pages
AI Lab Record
PDF
No ratings yet
AI Lab Record
42 pages
Aiml-Lab-Manual 24-25
PDF
No ratings yet
Aiml-Lab-Manual 24-25
39 pages
Assignment 5
PDF
No ratings yet
Assignment 5
7 pages
AI Lab Manual For V 5SEM PDF
PDF
No ratings yet
AI Lab Manual For V 5SEM PDF
83 pages
AIML Record Programs (PDF - Io) - 1
PDF
No ratings yet
AIML Record Programs (PDF - Io) - 1
35 pages
AIML Lab Manual
PDF
No ratings yet
AIML Lab Manual
41 pages
AI Algorithms and Programs
PDF
No ratings yet
AI Algorithms and Programs
15 pages
AIML LAB MANUAL Printe
PDF
No ratings yet
AIML LAB MANUAL Printe
47 pages
AIML Sem4 Record
PDF
No ratings yet
AIML Sem4 Record
34 pages
Artificial Intelligence Lab Record
PDF
No ratings yet
Artificial Intelligence Lab Record
20 pages
AIML Lab Manual
PDF
No ratings yet
AIML Lab Manual
76 pages
Cs 3491 Ai ML Lab Manual
PDF
No ratings yet
Cs 3491 Ai ML Lab Manual
43 pages
AIML Record Programs (PDF - Io)
PDF
No ratings yet
AIML Record Programs (PDF - Io)
36 pages
AIML With Outputs
PDF
No ratings yet
AIML With Outputs
34 pages
Ai 4 Bestfs
PDF
No ratings yet
Ai 4 Bestfs
6 pages
AIML Lab Manual Final
PDF
No ratings yet
AIML Lab Manual Final
43 pages
AIML Lab Manual
PDF
No ratings yet
AIML Lab Manual
44 pages
CS3491-AI &ML Lab Manual
PDF
No ratings yet
CS3491-AI &ML Lab Manual
38 pages
Cs3491 Aiml Lab PDF
PDF
No ratings yet
Cs3491 Aiml Lab PDF
31 pages
Program
PDF
No ratings yet
Program
25 pages
Aiml Lab Manual
PDF
No ratings yet
Aiml Lab Manual
38 pages
Aiml Lab Manual Upto DT
PDF
No ratings yet
Aiml Lab Manual Upto DT
40 pages
Cs3491-Aiml Lab Manual
PDF
No ratings yet
Cs3491-Aiml Lab Manual
59 pages
Aiml Lab Manual Upto DT
PDF
No ratings yet
Aiml Lab Manual Upto DT
40 pages
AIML Lab Manual
PDF
No ratings yet
AIML Lab Manual
38 pages
AI Python Lab Report CSIT 4th Semester Part II
PDF
No ratings yet
AI Python Lab Report CSIT 4th Semester Part II
28 pages
All NL All The Day
PDF
No ratings yet
All NL All The Day
52 pages
Republic of Iraq Ministry of Higher Education and Scientific Research University of Babylon College of Information Technology Department of Software
PDF
No ratings yet
Republic of Iraq Ministry of Higher Education and Scientific Research University of Babylon College of Information Technology Department of Software
12 pages
Java PDF
PDF
No ratings yet
Java PDF
86 pages
All NL All The Day
PDF
No ratings yet
All NL All The Day
74 pages
التقرير النهائي
PDF
No ratings yet
التقرير النهائي
9 pages
2 Background To The Study: 2.1 The Evaluative Framework: Cunningsworth's Four Guidelines
PDF
No ratings yet
2 Background To The Study: 2.1 The Evaluative Framework: Cunningsworth's Four Guidelines
8 pages
احمدسالم صالح PDF
PDF
No ratings yet
احمدسالم صالح PDF
16 pages
Hash Functions: Presented By: Tahir Mehmood (1019) Presented To: Mr. Inam Ul Haq Assistant Professor
PDF
No ratings yet
Hash Functions: Presented By: Tahir Mehmood (1019) Presented To: Mr. Inam Ul Haq Assistant Professor
7 pages
Hash Functions: Presented By: Tahir Mehmood (1019) Presented To: Mr. Inam Ul Haq Assistant Professor
PDF
No ratings yet
Hash Functions: Presented By: Tahir Mehmood (1019) Presented To: Mr. Inam Ul Haq Assistant Professor
7 pages
Breadth-First Search in Python: The Algorithm
PDF
No ratings yet
Breadth-First Search in Python: The Algorithm
3 pages
Protocol:TCP: How I Merger Port Bridge Ip Remotely? Answer
PDF
No ratings yet
Protocol:TCP: How I Merger Port Bridge Ip Remotely? Answer
2 pages