Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views5 pages

AOstar

Uploaded by

tanmaygwppt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

AOstar

Uploaded by

tanmaygwppt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

{

"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "d1f416c0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Graph - 1\n",
"HEURISTIC VALUES : {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5,
'H': 7, 'I': 7, 'J': 1}\n",
"SOLUTION GRAPH : {}\n",
"PROCESSING NODE : A\n",

"----------------------------------------------------------------------------------
-------\n",
"10 ['B', 'C']\n",
"HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G':
5, 'H': 7, 'I': 7, 'J': 1}\n",
"SOLUTION GRAPH : {}\n",
"PROCESSING NODE : B\n",

"----------------------------------------------------------------------------------
-------\n",
"6 ['G']\n",
"HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G':
5, 'H': 7, 'I': 7, 'J': 1}\n",
"SOLUTION GRAPH : {}\n",
"PROCESSING NODE : A\n",

"----------------------------------------------------------------------------------
-------\n",
"10 ['B', 'C']\n",
"HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G':
5, 'H': 7, 'I': 7, 'J': 1}\n",
"SOLUTION GRAPH : {}\n",
"PROCESSING NODE : G\n",

"----------------------------------------------------------------------------------
-------\n",
"8 ['I']\n",
"HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G':
8, 'H': 7, 'I': 7, 'J': 1}\n",
"SOLUTION GRAPH : {}\n",
"PROCESSING NODE : B\n",

"----------------------------------------------------------------------------------
-------\n",
"8 ['H']\n",
"HEURISTIC VALUES : {'A': 10, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G':
8, 'H': 7, 'I': 7, 'J': 1}\n",
"SOLUTION GRAPH : {}\n",
"PROCESSING NODE : A\n",

"----------------------------------------------------------------------------------
-------\n",
"12 ['B', 'C']\n",
"HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G':
8, 'H': 7, 'I': 7, 'J': 1}\n",
"SOLUTION GRAPH : {}\n",
"PROCESSING NODE : I\n",

"----------------------------------------------------------------------------------
-------\n",
"0 []\n",
"HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G':
8, 'H': 7, 'I': 0, 'J': 1}\n",
"SOLUTION GRAPH : {'I': []}\n",
"PROCESSING NODE : G\n",

"----------------------------------------------------------------------------------
-------\n",
"1 ['I']\n",
"HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G':
1, 'H': 7, 'I': 0, 'J': 1}\n",
"SOLUTION GRAPH : {'I': [], 'G': ['I']}\n",
"PROCESSING NODE : B\n",

"----------------------------------------------------------------------------------
-------\n",
"2 ['G']\n",
"HEURISTIC VALUES : {'A': 12, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G':
1, 'H': 7, 'I': 0, 'J': 1}\n",
"SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}\n",
"PROCESSING NODE : A\n",

"----------------------------------------------------------------------------------
-------\n",
"6 ['B', 'C']\n",
"HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1,
'H': 7, 'I': 0, 'J': 1}\n",
"SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}\n",
"PROCESSING NODE : C\n",

"----------------------------------------------------------------------------------
-------\n",
"2 ['J']\n",
"HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1,
'H': 7, 'I': 0, 'J': 1}\n",
"SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}\n",
"PROCESSING NODE : A\n",

"----------------------------------------------------------------------------------
-------\n",
"6 ['B', 'C']\n",
"HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1,
'H': 7, 'I': 0, 'J': 1}\n",
"SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}\n",
"PROCESSING NODE : J\n",

"----------------------------------------------------------------------------------
-------\n",
"0 []\n",
"HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1,
'H': 7, 'I': 0, 'J': 0}\n",
"SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G'], 'J': []}\n",
"PROCESSING NODE : C\n",

"----------------------------------------------------------------------------------
-------\n",
"1 ['J']\n",
"HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 1, 'D': 12, 'E': 2, 'F': 1, 'G': 1,
'H': 7, 'I': 0, 'J': 0}\n",
"SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G'], 'J': [], 'C': ['J']}\n",
"PROCESSING NODE : A\n",

"----------------------------------------------------------------------------------
-------\n",
"5 ['B', 'C']\n",
"FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE START NODE: A\n",
"------------------------------------------------------------\n",
"{'I': [], 'G': ['I'], 'B': ['G'], 'J': [], 'C': ['J'], 'A': ['B', 'C']}\n",
"------------------------------------------------------------\n"
]
}
],
"source": [
"class Graph:\n",
" def __init__(self, graph, heuristicNodeList, startNode): #instantiate
graph object with graph topology, heuristic values, start node\n",
" self.graph = graph\n",
" self.H=heuristicNodeList\n",
" self.start=startNode\n",
" self.parent={}\n",
" self.status={}\n",
" self.solutionGraph={}\n",
" \n",
" def applyAOStar(self): # starts a recursive AO* algorithm\n",
" self.aoStar(self.start, False)\n",
"\n",
" def getNeighbors(self, v): # gets the Neighbors of a given node\n",
" return self.graph.get(v,'')\n",
"\n",
" def getStatus(self,v): # return the status of a given node\n",
" return self.status.get(v,0)\n",
"\n",
" def setStatus(self,v, val): # set the status of a given node\n",
" self.status[v]=val\n",
"\n",
" def getHeuristicNodeValue(self, n):\n",
" return self.H.get(n,0) # always return the heuristic value of a given
node\n",
"\n",
" def setHeuristicNodeValue(self, n, value):\n",
" self.H[n]=value # set the revised heuristic value of a given node\n",
"\n",
" def printSolution(self):\n",
" print(\"FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE START
NODE:\",self.start)\n",
"
print(\"------------------------------------------------------------\")\n",
" print(self.solutionGraph)\n",
"
print(\"------------------------------------------------------------\")\n",
"\n",
" def computeMinimumCostChildNodes(self, v): # Computes the Minimum Cost of
child nodes of a given node v\n",
" minimumCost=0\n",
" costToChildNodeListDict={}\n",
" costToChildNodeListDict[minimumCost]=[]\n",
" flag=True\n",
" for nodeInfoTupleList in self.getNeighbors(v): # iterate over all the
set of child node/s\n",
" cost=0\n",
" nodeList=[]\n",
" for c, weight in nodeInfoTupleList:\n",
" cost=cost+self.getHeuristicNodeValue(c)+weight\n",
" nodeList.append(c)\n",
" if flag==True: # initialize Minimum Cost with the cost of first
set of child node/s\n",
" minimumCost=cost\n",
" costToChildNodeListDict[minimumCost]=nodeList # set the
Minimum Cost child node/s\n",
" flag=False\n",
" else: # checking the Minimum Cost nodes with the current Minimum
Cost\n",
" if minimumCost>cost:\n",
" minimumCost=cost\n",
" costToChildNodeListDict[minimumCost]=nodeList # set the
Minimum Cost child node/s\n",
" return minimumCost, costToChildNodeListDict[minimumCost] # return
Minimum Cost and Minimum Cost child node/s\n",
"\n",
" def aoStar(self, v, backTracking): # AO* algorithm for a start node and
backTracking status flag\n",
" print(\"HEURISTIC VALUES :\", self.H)\n",
" print(\"SOLUTION GRAPH :\", self.solutionGraph)\n",
" print(\"PROCESSING NODE :\", v)\n",
"
print(\"---------------------------------------------------------------------------
--------------\")\n",
" if self.getStatus(v) >= 0: # if status node v >= 0, compute Minimum
Cost nodes of v\n",
" minimumCost, childNodeList = self.computeMinimumCostChildNodes(v)\
n",
" print(minimumCost, childNodeList)\n",
" self.setHeuristicNodeValue(v, minimumCost)\n",
" self.setStatus(v,len(childNodeList))\n",
" solved=True # check the Minimum Cost nodes of v are solved\n",
" for childNode in childNodeList:\n",
" self.parent[childNode]=v\n",
" if self.getStatus(childNode)!=-1:\n",
" solved=solved & False\n",
" if solved==True: # if the Minimum Cost nodes of v are solved, set
the current node status as solved(-1)\n",
" self.setStatus(v,-1)\n",
" self.solutionGraph[v]=childNodeList # update the solution
graph with the solved nodes which may be a part of solution\n",
" if v!=self.start: # check the current node is the start node for
backtracking the current node value\n",
" self.aoStar(self.parent[v], True) # backtracking the current
node value with backtracking status set to true\n",
" if backTracking==False: # check the current call is not for
backtracking \n",
" for childNode in childNodeList: # for each Minimum Cost child
node\n",
" self.setStatus(childNode,0) # set the status of child node
to 0(needs exploration)\n",
" self.aoStar(childNode, False) # Minimum Cost child node\
n",
" \n",
" \n",
" #for simplicity we ll consider heuristic distances given\
n",
"print (\"Graph - 1\")\n",
"h1 = {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7,
'J': 1}\n",
"graph1 = {\n",
" 'A': [[('B', 1), ('C', 1)], [('D', 1)]],\n",
" 'B': [[('G', 1)], [('H', 1)]],\n",
" 'C': [[('J', 1)]],\n",
" 'D': [[('E', 1), ('F', 1)]],\n",
" 'G': [[('I', 1)]]\n",
"}\n",
"\n",
"G1= Graph(graph1, h1, 'A')\n",
"G1.applyAOStar()\n",
"G1.printSolution()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51f2dad7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

You might also like