AIML (1st 2 PRGS)
AIML (1st 2 PRGS)
graph_nodes = {
def heuristic(n):
H_dist = {
'S': 14,
'B': 12,
'C': 11,
'D': 6,
'E': 4,
'F': 11,
'G': 0
return H_dist[n]
pq = PriorityQueue()
pq.put((0, start))
visited = set()
g_score[start] = 0
f_score[start] = heuristic(start)
current = pq.get()[1]
if current == goal:
return g_score[current]
visited.add(current)
pq.put((f_score[neighbor], neighbor))
return float('inf')
start_node = 'S'
goal_node = 'G'
self.graph = graph
self.H = heuristicNodeList
self.start = startNode
self.parent = {}
self.status = {}
self.solutionGraph = {}
def applyAOStar(self):
self.aoStar(self.start, False)
return self.status.get(v, 0)
self.status[v] = val
return self.H.get(n, 0)
self.H[n] = value
def printSolution(self):
print("Solution Graph:")
print(self.solutionGraph)
minimumCost = float('inf')
costToChildNodeListDict = {}
minimumCost = cost
costToChildNodeListDict[minimumCost] = [c for c, _ in nodeInfoTupleList]
if minimumCost == float('inf'):
return 0, []
if self.getStatus(v) >= 0:
self.setHeuristicNodeValue(v, minimumCost)
self.setStatus(v, len(childNodeList))
if solved:
self.setStatus(v, -1)
self.solutionGraph[v] = childNodeList
self.aoStar(self.parent[v], True)
if not backTracking:
self.setStatus(childNode, 0)
self.parent[childNode] = v
self.aoStar(childNode, False)
# Example 1
h1 = {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J': 1, 'T': 3}
graph1 = {
}
G1 = Graph(graph1, h1, 'A')
G1.applyAOStar()
G1.printSolution()
# Example 2
h2 = {'A': 1, 'B': 6, 'C': 12, 'D': 10, 'E': 4, 'F': 4, 'G': 5, 'H': 7}
graph2 = {
G2.applyAOStar()
G2.printSolution()