You are on page 1of 14

ai-practical-file

January 3, 2024

1 Best First Search


[18]: SuccList ={
'A':[['B',3],['C',2]],
'B':[['A',5],['C',2],['D',2],['E',3]],
'C':[['A',5],['B',3],['F',2],['G',4]],
'D':[['H',1],['I',99]],
'F': [['J',99]],
'G':[['K',99],['L',3]]}
Start='A'
Goal='E'
Closed = list()
SUCCESS=True
FAILURE=False
State=FAILURE

def MOVEGEN(N):
New_list=list()
if N in SuccList.keys():
New_list=SuccList[N]

return New_list

def GOALTEST(N):
if N == Goal:
return True
else:
return False

def APPEND(L1,L2):
New_list=list(L1)+list(L2)
return New_list

def SORT(L):
L.sort(key = lambda x: x[1])
return L

1
def BestFirstSearch():
OPEN=[[Start,5]]
CLOSED=list()
global State
global Closed
while (len(OPEN) != 0) and (State != SUCCESS):
print("------------")
N= OPEN[0]
print("N=",N)
del OPEN[0] #delete the node we picked

if GOALTEST(N[0])==True:
State = SUCCESS
CLOSED = APPEND(CLOSED,[N])
print("CLOSED=",CLOSED)
else:
CLOSED = APPEND(CLOSED,[N])
print("CLOSED=",CLOSED)
CHILD = MOVEGEN(N[0])
print("CHILD=",CHILD)
for val in CLOSED:
if val in CHILD:
CHILD.remove(val)
for val in OPEN:
if val in CHILD:
CHILD.remove(val)
OPEN = APPEND(CHILD,OPEN) #append movegen elements to␣
↪OPEN
print("Unsorted OPEN=",OPEN)
SORT(OPEN)
print("Sorted OPEN=",OPEN)

Closed=CLOSED
return State

#Driver Code
result=BestFirstSearch() #call search algorithm
print(Closed,result)

------------
N= ['A', 5]
CLOSED= [['A', 5]]
CHILD= [['B', 3], ['C', 2]]
Unsorted OPEN= [['B', 3], ['C', 2]]
Sorted OPEN= [['C', 2], ['B', 3]]
------------
N= ['C', 2]

2
CLOSED= [['A', 5], ['C', 2]]
CHILD= [['A', 5], ['B', 3], ['F', 2], ['G', 4]]
Unsorted OPEN= [['F', 2], ['G', 4], ['B', 3]]
Sorted OPEN= [['F', 2], ['B', 3], ['G', 4]]
------------
N= ['F', 2]
CLOSED= [['A', 5], ['C', 2], ['F', 2]]
CHILD= [['J', 99]]
Unsorted OPEN= [['J', 99], ['B', 3], ['G', 4]]
Sorted OPEN= [['B', 3], ['G', 4], ['J', 99]]
------------
N= ['B', 3]
CLOSED= [['A', 5], ['C', 2], ['F', 2], ['B', 3]]
CHILD= [['A', 5], ['C', 2], ['D', 2], ['E', 3]]
Unsorted OPEN= [['D', 2], ['E', 3], ['G', 4], ['J', 99]]
Sorted OPEN= [['D', 2], ['E', 3], ['G', 4], ['J', 99]]
------------
N= ['D', 2]
CLOSED= [['A', 5], ['C', 2], ['F', 2], ['B', 3], ['D', 2]]
CHILD= [['H', 1], ['I', 99]]
Unsorted OPEN= [['H', 1], ['I', 99], ['E', 3], ['G', 4], ['J', 99]]
Sorted OPEN= [['H', 1], ['E', 3], ['G', 4], ['I', 99], ['J', 99]]
------------
N= ['H', 1]
CLOSED= [['A', 5], ['C', 2], ['F', 2], ['B', 3], ['D', 2], ['H', 1]]
CHILD= []
Unsorted OPEN= [['E', 3], ['G', 4], ['I', 99], ['J', 99]]
Sorted OPEN= [['E', 3], ['G', 4], ['I', 99], ['J', 99]]
------------
N= ['E', 3]
CLOSED= [['A', 5], ['C', 2], ['F', 2], ['B', 3], ['D', 2], ['H', 1], ['E', 3]]
[['A', 5], ['C', 2], ['F', 2], ['B', 3], ['D', 2], ['H', 1], ['E', 3]] True

2 Breadth First Search


[19]: graph={
'5': ['3','7'],
'3': ['2','4'],
'7': ['8'],
'2': [ ],
'4': ['8'],
'8': [ ]
}
visited = [] #list for visited nodes
queue = [] #initialize a queue

def bfs(graph,start):

3
visited = []
queue = [start]
visited.append(start)

while queue: #creating loop to vist each node


m= queue.pop(0) #will pop 5 from queue
print(m, end= " ") #print 5 in output

for neighbour in graph[m]: #similarly for neighbo. nodes it will visit and␣
end the loop until the empty node will come

if neighbour not in visited:


visited.append(neighbour)
queue.append(neighbour)

print("breadth first search is: ")


bfs(graph, '5') #function calling

5 3 7 2 4 8 breadth first search is:

3 Depth First Search


[3]: graph={
'5': ['3','7'],
'3': ['2','4'],
'7': ['8'],
'2': [ ],
'4': ['8'],
'8': [ ]
}
visited = set()

def dfs(visited, graph, node): #function for dfs


if node not in visited:
print (node)
visited.add(node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour)

print("depth first search is: ")


dfs(visited, graph, '5')

depth first search is:


5
3
2
4
8

4
7

4 A* Search Algorithm
[5]: import heapq

class Node:
def __init__(self, state, parent=None, g=0, h=0):
self.state = state
self.parent = parent
self.g = g
self.h = h

def __lt__(self, other):


return (self.g + self.h) < (other.g + other.h)
#search funct.
def astar_search(start, goal, neighbors_fn, heuristic_fn):
open_set = [Node(start, None, 0, heuristic_fn(start))]
closed_set = set()

while open_set:
current_node = heapq.heappop(open_set)

if current_node.state == goal:
path = []
while current_node:
path.append(current_node.state)
current_node = current_node.parent
return path[::-1]

closed_set.add(current_node.state)

for neighbor in neighbors_fn(current_node.state):


if neighbor in closed_set:
continue

g = current_node.g + 1
h = heuristic_fn(neighbor)
new_node = Node(neighbor, current_node, g, h)

if new_node not in open_set:


heapq.heappush(open_set, new_node)

return None

# Example
def neighbors(state):

5
x, y = state
possible_moves = [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
return [(nx, ny) for nx, ny in possible_moves if 0 <= nx < 5 and 0 <= ny <␣
↪5]

def heuristic(state):
goal = (4, 4)
return abs(state[0] - goal[0]) + abs(state[1] - goal[1])

start_state = (0, 0)
goal_state = (4, 4)

path = astar_search(start_state, goal_state, neighbors, heuristic)


print("A* Path:", path)

A* Path: [(0, 0), (0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (4,
4)]

5 AO* Search
[7]: class Graph:
def __init__(self, graph, heuristicNodeList, startNode): #instantiate graph␣
↪object with graph topology, heuristic values, start node

self.graph = graph
self.H=heuristicNodeList
self.start=startNode
self.parent={}
self.status={}
self.solutionGraph={}

def applyAOStar(self): # starts a recursive AO* algorithm


self.aoStar(self.start, False)

def getNeighbors(self, v): # gets the Neighbors of a given node


return self.graph.get(v,'')

def getStatus(self,v): # return the status of a given node


return self.status.get(v,0)

def setStatus(self,v, val): # set the status of a given node


self.status[v]=val

def getHeuristicNodeValue(self, n):


return self.H.get(n,0) # always return the heuristic value of a given␣
↪node

6
def setHeuristicNodeValue(self, n, value):
self.H[n]=value # set the revised heuristic value of a given node

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): # Computes the Minimum Cost of␣


↪child nodes of a given node v
minimumCost=0
costToChildNodeListDict={}
costToChildNodeListDict[minimumCost]=[]
flag=True
for nodeInfoTupleList in self.getNeighbors(v): # iterate over all the␣
↪set of child node/s

cost=0
nodeList=[]
for c, weight in nodeInfoTupleList:
cost=cost+self.getHeuristicNodeValue(c)+weight
nodeList.append(c)
if flag==True: # initialize Minimum Cost with the cost of first set␣
↪of child node/s

minimumCost=cost
costToChildNodeListDict[minimumCost]=nodeList # set the Minimum␣
↪Cost child node/s

flag=False
else: # checking the Minimum Cost nodes with the current Minimum␣
↪Cost

if minimumCost>cost:
minimumCost=cost
costToChildNodeListDict[minimumCost]=nodeList # set the␣
↪Minimum Cost child node/s

return minimumCost, costToChildNodeListDict[minimumCost] # return␣


↪Minimum Cost and Minimum Cost child node/s

def aoStar(self, v, backTracking): # AO* algorithm for a start node and␣


↪backTracking status flag

print("HEURISTIC VALUES :", self.H)


print("SOLUTION GRAPH :", self.solutionGraph)
print("PROCESSING NODE :", v)

↪print("-------------------------------------------------------------------------------------

7
if self.getStatus(v) >= 0: # if status node v >= 0, compute Minimum␣
↪Cost nodes of v
minimumCost, childNodeList = self.computeMinimumCostChildNodes(v)
print(minimumCost, childNodeList)
self.setHeuristicNodeValue(v, minimumCost)
self.setStatus(v,len(childNodeList))
solved=True # check the Minimum Cost nodes of v are solved
for childNode in childNodeList:
self.parent[childNode]=v
if self.getStatus(childNode)!=-1:
solved=solved & False
if solved==True: # if the Minimum Cost nodes of v are solved, set␣
↪the current node status as solved(-1)

self.setStatus(v,-1)
self.solutionGraph[v]=childNodeList # update the solution graph␣
↪with the solved nodes which may be a part of solution

if v!=self.start: # check the current node is the start node for␣


↪backtracking the current node value

self.aoStar(self.parent[v], True) # backtracking the current␣


↪node value with backtracking status set to true

if backTracking==False: # check the current call is not for␣


↪backtracking

for childNode in childNodeList: # for each Minimum Cost child␣


↪node

self.setStatus(childNode,0) # set the status of child node␣


↪to 0(needs exploration)

self.aoStar(childNode, False) # Minimum Cost child node is␣


↪further explored with backtracking status as false

#for simplicity we'll consider heuristic distances given ex.


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()

Graph - 1
HEURISTIC VALUES : {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5,

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

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

10
1 ['J']
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 1, 'D': 12, 'E': 2, 'F': 1, 'G': 1,
'H': 7, 'I': 0, 'J': 0}
SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G'], 'J': [], 'C': ['J']}
PROCESSING NODE : A
--------------------------------------------------------------------------------
---------
5 ['B', 'C']
FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE START NODE: A
------------------------------------------------------------
{'I': [], 'G': ['I'], 'B': ['G'], 'J': [], 'C': ['J'], 'A': ['B', 'C']}
------------------------------------------------------------

6 Different functions in pandas & numpy


[9]: import pandas as pd
import numpy as np

# sample dataframe
data = {'Category': ['A', 'A', 'B', 'B', 'A', 'B', 'A', 'B'],
'Values': [10, 15, 20, 25, 30, 35, 40, 45]}

df = pd.DataFrame(data)

# Create a crosstab
cross_tab = pd.crosstab(index=df['Category'], columns='count')

# Compute mean, median, mode, and other statistics


mean_value = df['Values'].mean()
median_value = df['Values'].median()
mode_value = df['Values'].mode().tolist()
descriptive_stats = df['Values'].describe()
null_values = df.isnull().sum()
data_range = np.ptp(df['Values'])
arange = np.arange(start=1, stop=10, step=3)
# Print results
print("\nMean:", mean_value)
print("Median:", median_value)
print("Mode:", mode_value)
print("\nDescriptive Statistics:",descriptive_stats)
print("\nNull Values:")
print(null_values)
print("\nRange:", data_range)
print("\nArange:", arange)

Mean: 27.5

11
Median: 27.5
Mode: [10, 15, 20, 25, 30, 35, 40, 45]

Descriptive Statistics: count 8.000000


mean 27.500000
std 12.247449
min 10.000000
25% 18.750000
50% 27.500000
75% 36.250000
max 45.000000
Name: Values, dtype: float64

Null Values:
Category 0
Values 0
dtype: int64

Range: 35

Arange: [1 4 7]

7 Conditional Probability
[1]: import pandas as pd
x=pd.read_csv('lung_cancer.csv')
x
conditional_prob = x.groupby('gender')['smoker'].value_counts(normalize=True).
↪unstack()

print("coditional probability :\n",conditional_prob)

coditional probability :
smoker Current Former
gender
Female 0.501232 0.498768
Male 0.468477 0.531523

8 marginal probability
[5]: x=pd.read_csv('lung_cancer.csv')
marginal_prob = x['smoker'].value_counts(normalize=True)
marginal_prob

[5]: Former 0.51809


Current 0.48191
Name: smoker, dtype: float64

12
9 joint probabilitie
[7]: x=pd.read_csv('lung_cancer.csv')
joint_probabilities = x.groupby(['age', 'smoker']).size().div(len(x))
joint_probabilities

[7]: age smoker


43 Current 0.000019
49 Former 0.000019
50 Current 0.000019
52 Current 0.000019
54 Current 0.000019
55 Current 0.057125
Former 0.043461
56 Current 0.053381
Former 0.043368
57 Current 0.044734
Former 0.037472
58 Current 0.037640
Former 0.035301
59 Current 0.038071
Former 0.037191
60 Current 0.037228
Former 0.037322
61 Current 0.031089
Former 0.034833
62 Current 0.027383
Former 0.031800
63 Current 0.024912
Former 0.030340
64 Current 0.022779
Former 0.028487
65 Current 0.020327
Former 0.026167
66 Current 0.015722
Former 0.022236
67 Current 0.013888
Former 0.020926
68 Current 0.013214
Former 0.018848
69 Current 0.010594
Former 0.016153
70 Current 0.009714
Former 0.014974
71 Current 0.007992
Former 0.012391
72 Current 0.006738

13
Former 0.011230
73 Current 0.005615
Former 0.009639
74 Current 0.003669
Former 0.005877
75 Former 0.000056
79 Current 0.000019
dtype: float64

[ ]:

14

You might also like