All Pracs
More
Share
Explore

# AI DocsAI Docs

RMP File
dict_hn = {'Arad': 336, 'Bucharest': 0, 'Craiova': 160, 'Drobeta': 242, 'Eforie': 161,
'Fagaras': 176, 'Giurgiu': 77, 'Hirsova': 151, 'Iasi': 226, 'Lugoj': 244,
'Sibiu': 253, 'Timisoara': 329, 'Urziceni': 80, 'Vaslui': 199, 'Zerind': 374}
dict_gn = dict(
Bucharest=dict(Urziceni=85, Giurgiu=90, Pitesti=101, Fagaras=211),
Craiova=dict(Drobeta=120, Pitesti=138, Rimnicu=146),
Eforie=dict(Hirsova=86),
Fagaras=dict(Sibiu=99, Bucharest=211),
Giurgiu=dict(Bucharest=90),
Hirsova=dict(Eforie=86, Urziceni=98),
Iasi=dict(Neamt=87, Vaslui=92),
Neamt=dict(Iasi=87),
Pitesti=dict(Rimnicu=97, Bucharest=101, Craiova=138),
Rimnicu=dict(Sibiu=80, Pitesti=97, Craiova=146),
Urziceni=dict(Bucharest=85, Hirsova=98, Vaslui=142),
Vaslui=dict(Iasi=92, Urziceni=142),
)

### Practical 1

AIM: Implement Breadth first search algorithm for Romanian map problem.
Code
import queue as Q
from RMP import dict_gn
goal = 'Bucharest'
result = ''
def BFS(city, cityq, visitedq):
global result
if city == start:
result = result + ' '+city
for eachcity in dict_gn[city].keys():
if eachcity == goal:
result = result+' '+eachcity
return
if eachcity not in cityq.queue and eachcity not in visitedq.queue:
cityq.put(eachcity)
result = result+' '+eachcity
visitedq.put(city)
BFS(cityq.get(), cityq, visitedq)

def main():
cityq = Q.Queue()
visitedq = Q.Queue()
BFS(start, cityq, visitedq)
print("BFS Traversal from", start, "to", goal, "is: ")
print(result)

main()

### Practical 2

AIM: Implement IDDFS(Iterative Deepening Depth-First Search).
import queue as Q
from RMP import dict_gn
goal = 'Bucharest'
result = ''

def DLS(city, visitedstack, startlimit, endlimit):
global result
found = 0
result = result+city+''
visitedstack.append(city)
if city == goal:
return 1
if startlimit == endlimit:
return 0
for eachcity in dict_gn[city].keys():
if eachcity not in visitedstack:
found = DLS(eachcity, visitedstack, startlimit+1, endlimit)
if found:
return found

def IDDFS(city, visitedstack, endlimit):
global result
for i in range(0, endlimit):
print("Searching at Limit: ", i)
found = DLS(city, visitedstack, 0, 1)
if found:
print("Found")
break
else:
print(result)
print("--------")
result = ''
visitedstack = []

def main():
visitedstack = []
IDDFS(start, visitedstack, 9)
print("IDDFS Traversal from", start, " to ", goal, "is: ")
print(result)

main()

### Practical 3

AIM: Implement A* search.
import queue as Q
from RMP import dict_gn
from RMP import dict_hn
goal = 'Bucharest'
result = ''

def get_fn(citystr):
cities = citystr.split(" , ")
hn = gn = 0
for ctr in range(0, len(cities)-1):
gn = gn+dict_gn[cities[ctr]][cities[ctr+1]]
hn = dict_hn[cities[len(cities)-1]]
return(hn+gn)

def expand(cityq):
global result
tot, citystr, thiscity = cityq.get()
if thiscity == goal:
result = citystr+" : : "+str(tot)
return
for cty in dict_gn[thiscity]:
cityq.put((get_fn(citystr+" , "+cty), citystr+" , "+cty, cty))
expand(cityq)

def main():
cityq = Q.PriorityQueue()
thiscity = start
cityq.put((get_fn(start), start, thiscity))
expand(cityq)
print("The A* path wiht total is : ")
print(result)

main()

### Practical 4

import queue as Q
from RMP import dict_gn
from RMP import dict_hn
goal='Bucharest'
result=''
def get_fn(citystr):
cities=citystr.split(',')
hn=gn=0
for ctr in range(0,len(cities)-1):
gn=gn+dict_gn[cities[ctr]][cities[ctr+1]]
hn=dict_hn[cities[len(cities)-1]]
return(hn+gn)
def printout(cityq):
for i in range(0,cityq.qsize()):
print(cityq.queue[i])
def expand(cityq):
global result
tot,citystr,thiscity=cityq.get()
nexttot=999
if not cityq.empty():
nexttot,nextcitystr,nextthiscity=cityq.queue[0]
if thiscity==goal and tot<nexttot:
result=citystr+'::'+str(tot)
return
print("Expanded city---------------",thiscity)
print("Second best f(n)--------------",nexttot)
tempq=Q.PriorityQueue()
for cty in dict_gn[thiscity]:
tempq.put((get_fn(citystr+','+cty),citystr+','+cty,cty))
for ctr in range(1,3):
ctrtot,ctrcitystr,ctrthiscity=tempq.get()
if ctrtot<nexttot:
cityq.put((ctrtot,ctrcitystr,ctrthiscity))
else:
cityq.put((ctrtot,citystr,thiscity))
break
printout(cityq)
expand(cityq)
def main():
cityq=Q.PriorityQueue()
thiscity=start
cityq.put((999,"NA","NA"))
cityq.put((get_fn(start),start,thiscity))
expand(cityq)
print(result)
main()

### Practical 5

AIM: Implement Decision-Tree learning algorithm. #REQUIREMENTS:
balance-scale.names
2.2 kB
balance-scale.data
6.3 kB
1)---file1(balance-scale.data) & file2(balance-scale.names)
2)---install these modules:
python -m pip install pandas==0.18
python -m pip install scipy
python -m pip install scikit-learn
python -m pip install numpy

import numpy as np
import pandas as pd
import sklearn as sk
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report

# Importing dataset
def importdata():
# print the dataset shape
print("Dataset Length : ", len(balance_data))
# printing the dataset observations
return balance_data
# func to split the dataset

def splitdataset(balance_data):
# seperating the target variable
X = balance_data.values[:, 1:5]
Y = balance_data.values[:, 0]
# splitting the dataset into train and test
X_train, X_test, y_train, y_test = train_test_split(
X, Y, test_size=0.3, random_state=100)
return X, Y, X_train, X_test, y_train, y_test
# function to perform training with entropy

def train_using_entropy(X_train, X_test, y_train, y_test):
# decision tree with entropy
clf_entropy = DecisionTreeClassifier(
criterion="entropy", random_state=100, max_depth=3, min_samples_leaf=5)
# performing training
clf_entropy.fit(X_train, y_train)
return clf_entropy

def prediction(X_test, clf_object):
y_pred = clf_object.predict(X_test)
print("Predicted Values : ")
print(y_pred)
return y_pred

def cal_accuracy(y_test, y_pred):
print("Accuracy : ", accuracy_score(y_test, y_pred)*100)

def main():
data = importdata()
X, Y, X_train, X_test, y_train, y_test = splitdataset(data)
clf_entropy = train_using_entropy(X_train, X_test, y_train, y_test)
print("Results using entropy : ")
y_pred_entropy = prediction(X_test, clf_entropy)
cal_accuracy(y_test, y_pred_entropy)

main()

### Practical 6

AIM: Implement Naive-Bayes learning algo for RWP(Restaurant Waiting Problem).
RWp
rwp_examples = dict(
x1=dict(Alt='Y', Bar='N', Fri='N', Hun='Y', Pat='S', Price='\$\$\$',
Rain='N', Res='Y', Type='F', Est='0-10', ans='Y'),
x2=dict(Alt='Y', Bar='N', Fri='N', Hun='Y', Pat='F', Price='\$',
Rain='N', Res='N', Type='T', Est='30-60', ans='N'),
x3=dict(Alt='N', Bar='Y', Fri='N', Hun='N', Pat='S', Price='\$',
Rain='N', Res='N', Type='B', Est='0-10', ans='Y'),
x4=dict(Alt='Y', Bar='N', Fri='Y', Hun='Y', Pat='F', Price='\$',
Rain='Y', Res='N', Type='T', Est='10-30', ans='Y'),
x5=dict(Alt='Y', Bar='N', Fri='Y', Hun='N', Pat='F', Price='\$\$\$',
Rain='N', Res='Y', Type='F', Est='>60', ans='N'),
x6=dict(Alt='N', Bar='Y', Fri='N', Hun='Y', Pat='S', Price='\$\$',
Rain='Y', Res='Y', Type='I', Est='0-10', ans='Y'),
x7=dict(Alt='N', Bar='Y', Fri='N', Hun='N', Pat='N', Price='\$',
Rain='Y', Res='N', Type='B', Est='0-10', ans='N'),
x8=dict(Alt='N', Bar='N', Fri='N', Hun='Y', Pat='S', Price='\$\$',
Rain='Y', Res='Y', Type='T', Est='0-10', ans='Y'),
x9=dict(Alt='N', Bar='Y', Fri='Y', Hun='N', Pat='F', Price='\$',
Rain='Y', Res='N', Type='B', Est='>60', ans='N'),
x10=dict(Alt='Y', Bar='Y', Fri='Y', Hun='Y', Pat='F', Price='\$\$\$',
Rain='N', Res='Y', Type='I', Est='10-30', ans='N'),
x11=dict(Alt='N', Bar='N', Fri='N', Hun='N', Pat='N', Price='\$',
Rain='N', Res='N', Type='T', Est='0-10', ans='N'),
x12=dict(Alt='Y', Bar='Y', Fri='Y', Hun='Y', Pat='F', Price='\$',
Rain='N', Res='N', Type='B', Est='0-10', ans='Y')
)

from RWP import rwp_examples
total_exp = 12

def tot(attribute, value):
count = 0
for key, val in rwp_examples.items():
for key1, val1 in val.items():
if key1 == attribute:
if val1 == value:
count += 1
return count

def getProbab(attribute, attribval, value):
count = 0
for key, val in rwp_examples.items():
val1 = rwp_examples[key][attribute]
val2 = rwp_examples[key]['ans']
if val1 == attribval and val2 == value:
count += 1
probab = count / tot('ans', value)
return probab

def main():
PAltYes = tot('Alt', 'Y') / total_exp
PAltNo = tot('Alt', 'N') / total_exp
PBarYes = tot('Bar', 'Y') / total_exp
PBarNo = tot('Bar', 'N') / total_exp
PFriYes = tot('Fri', 'Y') / total_exp
PFriNo = tot('Fri', 'N') / total_exp
PHunYes = tot('Hun', 'Y') / total_exp
PHunNo = tot('Hun', 'N') / total_exp
PPatSome = tot('Pat', 'S') / total_exp
PPatFull = tot('Pat', 'F') / total_exp
PPatNone = tot('Pat', 'N') / total_exp
PPriceCheap = tot('Price', '\$') / total_exp
PPriceAvg = tot('Price', '\$\$') / total_exp
PPriceExp = tot('Price', '\$\$\$') / total_exp
PRainYes = tot('Rain', 'Y') / total_exp
PRainNo = tot('Rain', 'N') / total_exp
PResYes = tot('Res', 'Y') / total_exp
PResNo = tot('Res', 'N') / total_exp
PTypeFrench = tot('Type', 'F') / total_exp
PTypeItalian = tot('Type', 'I') / total_exp
PTypeBurger = tot('Type', 'B') / total_exp
PTypeThai = tot('Type', 'T') / total_exp
PEstFew = tot('Est', '0-10') / total_exp
PEstMore = tot('Est', '10-30') / total_exp
PEstStillMore = tot('Est', '30-60') / total_exp
PEstTooMuch = tot('Est', '>60') / total_exp
PAnsYes = tot('ans', 'Y') / total_exp
PAnsNo = tot('ans', 'N') / total_exp
print('Probability for will wait if there is an Alternate Restaurant Nearby: ')
print('Yes: Will Wait ', (getProbab(
'Alt', 'Y', 'Y') * PAnsYes/PAltYes) * 100, '%')
print('No: Will Wait ', (getProbab('Alt', 'Y', 'N') * PAnsNo/PAltYes) * 100, '%')
print('Probability for will wait if there No is an Alternate Restaurant Nearby: ')
print('Yes: Will Wait ', (getProbab('Alt', 'N', 'Y') * PAnsYes/PAltNo) * 100, '%')
print('No: Will Wait ', (getProbab('Alt', 'N', 'N') * PAnsNo/PAltNo) * 100, '%')