Vous êtes sur la page 1sur 20

A

LAB MANUAL
ON
Artificial Intelligence
In fulfillment towards the award of the degree of
Master of Computer Application(5th SEM)
Under
GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD.
2017-2018

INTERNAL GUIDE : SUBMITTED BY:


Prof.Upasana Mehta Name:Lad Pinkalkumar K.
Enrollment No:175043693018
CERTIFICATE

This is to certify that the “ AI LAB Manual “ was prepared by

Name: Enrollment No:


Lad Pinkalkumar K. 175043693018

under my guidance for fulfillment of the degree of Master of Computer Applications


of Gujarat Technological University, Ahmedabad during the academic year 2016-17.
I certify that this manual is his/her original work and not copied from other sources.

______________________
Internal Guide Signature
1)Write a program to find the factorial of given number.

def recur_factorial(n):
"""Function to return the factorial
of a number using recursion"""
if n == 1:
return n
else:
return n*recur_factorial(n-1)

num = int(input("Enter a number: "))


if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",recur_factorial(num))

*****************************Output********************************
2)Write a code in python to solve Tower of Hanoi Problem. Write a function to input no of
disk.

def TowerOfHenoi(n,source,dest,aux):

if(n==1):

print ("Move Disk 1 From",source,"To",dest)

return

else:

TowerOfHenoi(n-1,source,aux,dest)

print("Move Disk",n,"From",source,"To",dest)

TowerOfHenoi(n-1,aux,dest,source)

n=int(input("Enter The Disk ="))

TowerOfHenoi(n,'A','C','B')
************************************Output***********************************
3)Write a program to implement Depth First Search(DFS).

def DFS(graph,start):

open, close =[start],[]

##close=[]

while open:

node=open.pop()

if node in close:

continue

close.append(node)

for n in graph[node]:

open.append(n)

return close

graph = {1: [2, 3], 2: [4, 5],

3: [5], 4: [6], 5: [6],

6: [7], 7: []}
print(DFS(graph, 1))

# [1, 3, 5, 6, 7, 2, 4]

*************************************Output************************************
4)Write a program to implement Best First Search(BFS).

def BFS(graph,start):

open, close =[start],[]

##close=[]

while open:

node=open.pop()

if node in close:

continue

close.append(node)

for n in graph[node]:

open.insert(0,n)

return close

graph = {1: [2, 3], 2: [4, 5],

3: [5], 4: [6], 5: [6],

6: [7], 7: []}
print(BFS(graph, 1))

# [1, 2, 3, 4, 5, 6, 7]

**********************************Output****************************************
5)Write a program to implement water jug problem.

capacity=(4,3)

x=capacity[0]

y=capacity[1]

ans=[]

memory={}

def waterjug(stat):

a=stat[0]

b=stat[1]

if(a==2 and b==0):

print("Goal is achived")

ans.append(stat)

return True

if((a,b) in memory):

return False

ans.append(stat)

memory[(a,b)]=1
if(b>0):

if(a+b<=x):

if(waterjug((a+b,0))):

return True

else:

if(waterjug((x,b-(x-a)))):

return True

elif(b==0):

if(waterjug((a,y))):

return True

if(a>0):

if((a+b)<=y):

if(waterjug((0,a+b))):

return True

elif(a+b>=x):

if(waterjug((a-(y-b),y))):

return True
elif(a==x):

if(waterjug((0,b))):

return True

elif(a==0):

if(waterjug((x,b))):

return True

return False

init=(0,0)

waterjug(init)

for i in ans:

print (i)

*********************************Output**************************************
6)WAP to implement 8-puzzle problem using any of algorithm
import os

os.system("clear")

def printstat(stat):

print(stat[0],stat[1],stat[2])

print(stat[3],stat[4],stat[5])

print(stat[6],stat[7],stat[8])

print()

def move_up(stat):

new_node=stat[:]

print(new_node)

index=new_node.index(0)

print(index)

if index not in [0,1,2]:

temp=new_node[index-3]

new_node[index-3]=new_node[index]

new_node[index]=temp

return new_node
else:

return None

def move_down(stat):

new_node=stat[:]

index=new_node.index(0)

if index not in [6,7,8]:

temp=new_node[index+3]

new_node[index+3]=new_node[index]

new_node[index]=temp

return new_node

else:

return None

def move_left(stat):

new_node=stat[:]

index=new_node.index(0)

if index not in [0,3,6]:

temp=new_node[index-1]
new_node[index-1]=new_node[index]

new_node[index]=temp

return new_node

else:

return None

def move_right(stat):

new_node=stat[:]

index=new_node.index(0)

if index not in [2,5,8]:

temp=new_node[index+1]

new_node[index+1]=new_node[index]

new_node[index]=temp

return new_node

else:

return None

class Node :

def __init__(self,stat,parent,operator,depth):
self.stat=stat

self.parent=parent

self.operator=operator

self.depth=depth

def create_node(stat,parent,operator,depth):

return Node(stat,parent,operator,depth)

def expand_node(node):

expanded_node=[]

expanded_node.append(create_node(move_up(node.stat),node,"U",node.depth+1))

expanded_node.append(create_node(move_down(node.stat),node,"D",node.depth+1))

expanded_node.append(create_node(move_left(node.stat),node,"L",node.depth+1))

expanded_node.append(create_node(move_right(node.stat),node,"R",node.depth+1))

expanded_node=[node for node in expanded_node if node.stat!=None]

return expanded_node

def bfs(stat,goal):

nodes=[]

visited=[]
nodes.append(create_node(stat,None,None,0))

visited.append(create_node(stat,None,None,0))

while True:

temp=[]

if(len(nodes)==0):

return None

node=nodes.pop(0)

if(node.stat==goal):

move=[]

tmp=node

while True:

move.append(tmp.operator)

if(tmp.depth<=1):

break

tmp=tmp.parent

return move

temp.extend(expand_node(node))

for tempnode in temp:


if(tempnode not in visited):

nodes.insert(0,tempnode)

visited.append(tempnode)

def main():

init_stat=[1,2,3,8,4,0,7,6,5]

goal_stat=[1,2,3,8,0,4,7,6,5]

print("init stat")

printstat(init_stat)

move=bfs(init_stat,goal_stat)

print (move[::-1])

print("goal stat")

printstat(goal_stat)

main()

***********************************Output************************************

Vous aimerez peut-être aussi