Ai Lab
Ai Lab
AI LAB MANUAL
Artificial Intelligence Lab Using Python
S. No. Particulars
2 Course Outcomes
CO2 2 2 3 2 1 2 2
CO3 2 3 2 2 1 2 2
CO4 2 2 2 3 2 1 1 2
CO5 2 2 2 3 2 1 2 1
7. Questions for lab tests and exam need not necessarily be limited to the questions
in the manual, but could involve some variations and / or combinations of the
questions.
List of Experiments:
1. Write a Program to Implement Breadth First Search using
Python.
2. Write a Program to Implement Depth First Search using
Python.
3. Write a Program to Implement Tic-Tac-Toe game using
Python.
4. Write a Program to Implement 8-Puzzle problem using
Python.
5. Write a Program to Implement Water-Jug problem using
Python.
6. Write a Program to Implement Travelling Salesman
Problem using Python.
7. Write a Program to Implement Tower of Hanoi using
Python.
8. Write a Program to Implement Monkey Banana Problem
using Python.
9. Write a Program to Implement Alpha-Beta Pruning using
Python.
10. Write a Program to Implement 8-Queens Problem
using Python.
EXPERIMENT 1
while queue:
s = queue.pop(0)
print (s, end = " ")
Output:-
A B C D E F
EXPERIMENT 2
A
B
D
E
F
C
EXPERIMENT 3
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] == 0:
l.append((i, j))
return(l)
for y in range(len(board)):
if board[x, y] != player:
win = False
continue
if win == True:
return(win)
return(win)
for y in range(len(board)):
if board[y][x] != player:
win = False
continue
if win == True:
return(win)
return(win)
while winner == 0:
for player in [1, 2]:
board = random_place(board, player)
print("Board after " + str(counter) + "
move") print(board) sleep(2) counter += 1
winner = evaluate(board) if winner != 0:
break
return(winner)
[[0 0 0]
[0 0 0]
[0 0 0]]
Board after 1 move
[[0 0 0]
[0 0 0]
[1 0 0]]
Board after 2 move
[[0 0 0]
[0 2 0]
[1 0 0]]
Board after 3 move
[[0 1 0]
[0 2 0]
[1 0 0]]
Board after 4 move
[[0 1 0]
[2 2 0]
[1 0 0]]
Board after 5 move
[[1 1 0]
[2 2 0]
[1 0 0]]
Board after 6 move
[[1 1 0]
[2 2 0]
[1 2 0]]
Board after 7 move
[[1 1 0]
[2 2 0]
[1 2 1]]
Board after 8 move
[[1 1 0]
[2 2 2]
[1 2 1]]
Winner is: 2
EXPERIMENT 4
class Solution:
def solve(self, board):
dict = {}
flatten = []
for i in range(len(board)):
flatten += board[i]
flatten = tuple(flatten)
dict[flatten] = 0
return self.get_paths(dict)
moves = {
0: [1, 3],
1: [0, 2, 4],
2: [1, 5],
3: [0, 4, 6],
4: [1, 3, 5, 7],
5: [2, 4, 8],
6: [3, 7],
7: [4, 6, 8],
8: [5, 7],
}
results = []
pos_0 = node.index(0)
for move in moves[pos_0]:
new_node = list(node)
new_node[move], new_node[pos_0] = new_node[pos_0], new_node[move]
results.append(tuple(new_node))
return results
ob = Solution()
matrix = [
[3, 1, 2],
[4, 7, 5],
[6, 8, 0]
]
print(ob.solve(matrix))
Output:-
EXPERIMENT 5
waterJugSolver(amt1, amt2):
else:
return False
print("Steps: ")
waterJugSolver(0, 0)
Output:-
Steps:
0 0
4 0
4 3
0 3
3 0
3 3
4 2
0 2
EXPERIMENT 6
# Driver code
EXPERIMENT 7
# Driver code n
=4
TowerOfHanoi(n,'A','B','C')
# A, C, B are the name of rods
Output:-
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 3 from rod A to rod B
Move disk 1 from rod C to rod A
Move disk 2 from rod C to rod B
Move disk 1 from rod A to rod B
Move disk 4 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 2 from rod B to rod A
Move disk 1 from rod C to rod A
Move disk 3 from rod B to rod C
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
EXPERIMENT 8
def Monkey_move_box(x,y):
global i i = i + 1
print('step:', i, 'monkey take the box from', x, 'deliver to' + y)
import sys
else:
best = MAX
# Recur for left and
# right children for
i in range(0, 2):
return best
EXPERIMENT 10
global N
N=4
def printSolution(board):
for i in range(N):
for j in range(N):
print board[i][j],
print
return True
def solveNQUtil(board, col):
# base case: If all queens are placed
# then return true if
col >= N:
return True
for i in range(N):
if isSafe(board, i, col):
# Place this queen in board[i][col]
board[i][col] = 1
# recur to place rest of the queens if
solveNQUtil(board, col + 1) == True:
return True
board[i][col] = 0
return False
def solveNQ():
board = [ [0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
Output:-
0 0 1 0
1 0 0 0
0 0 0 1
0 1 0 0