Home
RoadMap
JAVA FULL STACK
Programming
_DATA STRUCTURE
_C PROGRAMMING
_C++ PROGRAMMING
_JAVA PROGRAMMING
_PYTHON PROGRAMMING
INTERNSHIP
PLACEMENT
_TCS
_WIPRO
_INFOSYS
_COGNIZANT
_CAPGEMINI
_INFY-TQ
_ACCENTURE
INTERVIEW
_APTITUDE
_GROUP DISCUSSION
_TECHNICAL
_HR INTERVIEW
Search
Home
18CSL76 ML LAB
18CSL76 Implement AO* Search algorithm.
18CSL76 Implement AO* Search algorithm.
class Graph: def __init__(self, graph, heuristicNodeList, startNode): self.graph = graph self.H=heuristicNodeList self.start=startNode self.parent={} self.status={} self.solutionGraph={} def applyAOStar(self): self.aoStar(self.start, False) def getNeighbors(self, v): return self.graph.get(v,'') def getStatus(self,v): return self.status.get(v,0) def setStatus(self,v, val): self.status[v]=val def getHeuristicNodeValue(self, n): return self.H.get(n,0) def setHeuristicNodeValue(self, n, value): self.H[n]=value def printSolution(self): print("FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE START NODE:",self.start) print("------------------------------------------------------------") print(self.solutionGraph) print("------------------------------------------------------------") def computeMinimumCostChildNodes(self, v): minimumCost=0 costToChildNodeListDict={} costToChildNodeListDict[minimumCost]=[] flag=True for nodeInfoTupleList in self.getNeighbors(v): cost=0 nodeList=[] for c, weight in nodeInfoTupleList: cost=cost+self.getHeuristicNodeValue(c)+weight nodeList.append(c) if flag==True: minimumCost=cost costToChildNodeListDict[minimumCost]=nodeList flag=False else: if minimumCost>cost: minimumCost=cost costToChildNodeListDict[minimumCost]=nodeList return minimumCost, costToChildNodeListDict[minimumCost] def aoStar(self, v, backTracking): print("HEURISTIC VALUES :", self.H) print("SOLUTION GRAPH :", self.solutionGraph) print("PROCESSING NODE :", v) print("-----------------------------------------------------------------------------------------") if self.getStatus(v) >= 0: minimumCost, childNodeList = self.computeMinimumCostChildNodes(v) print(minimumCost, childNodeList) self.setHeuristicNodeValue(v, minimumCost) self.setStatus(v,len(childNodeList)) solved=True for childNode in childNodeList: self.parent[childNode]=v if self.getStatus(childNode)!=-1: solved=solved & False if solved==True: self.setStatus(v,-1) self.solutionGraph[v]=childNodeList if v!=self.start: self.aoStar(self.parent[v], True) if backTracking==False: for childNode in childNodeList: self.setStatus(childNode,0) self.aoStar(childNode, False) print ("Graph - 1") h1 = {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J': 1} graph1 = { 'A': [[('B', 1), ('C', 1)], [('D', 1)]], 'B': [[('G', 1)], [('H', 1)]], 'C': [[('J', 1)]], 'D': [[('E', 1), ('F', 1)]], 'G': [[('I', 1)]] } G1= Graph(graph1, h1, 'A') G1.applyAOStar() G1.printSolution()
Click to Copy
Post a Comment
0 Comments
Search Topic
Popular posts
Wipro PRP, PJP Exam Sample Question Paper | Wipro Training
May 30, 2022
Wipro Syllabus 2023 and Test Pattern 2023
May 27, 2022
Wipro JAVA PJP Solutions and Mini projects | Wipro 2022 Batch
February 04, 2022
Wipro PJP PRP Sample Coding Questions and Solutions
June 02, 2022
TCS Syllabus 2023, Test Pattern and TCS Selection Process 2023
May 25, 2022
TCS NQT Cut-Off and Qualifying Marks for 2022
September 18, 2021
0 Comments