Professional Documents
Culture Documents
Artificial Intelligence
(CSE401)
Lab File
Bachelor of Technology (Computer Science & Engineering)
Name:- Vikas Kumar
Enrollment No.:- A41105219017
Semester :- 6th
Batch:- 2019 -2023
2. Write a program to 1
implement Single Player
Game.
3. Write a program to 1
implement Tic-Tac-Toe
game problem
6. Write a program to 1
implement BFS for water
jug problem using Python
7. Write a program to 1
implement DFS using
Python
9. 1
Design an XOR truth table
using Python
10. Study of SCIKIT fuzzy 1
10
Experiment 1
Program
class Node():
self.parent = parent
self.position = position
self.g = 0
self.h = 0
self.f = 0
"""Returns a list of tuples as a path from the given start to the given end in
the given maze"""
open_list = []
closed_list = []
open_list.append(start_node)
current_node = open_list[0]
current_index = 0
current_node = item
current_index = index
open_list.pop(current_index)
closed_list.append(current_node)
if current_node == end_node:
path = []
current = current_node
path.append(current.position)
current = current.parent
# Generate children
children = []
for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1),
(1, -1), (1, 1)]: # Adjacent squares
continue
if maze[node_position[0]][node_position[1]] != 0:
continue
# Append
children.append(new_node)
if child == closed_child:
continue
# Create the f, g, and h values
child.g = current_node.g + 1
continue
open_list.append(child)
def main():
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
start = (0, 0)
end = (7, 6)
print(path)
main()
Output
Experiment 2
Write a program to implement Single Player Game (eight Puzzle using BFS)
Program
# a is the initial state of the board and b is the final state of the
board
# Empty spot is represented by 0
a = [[1, 2, 3],
[5, 6, 0],
[7, 8, 4]]
b = [[1,2,3],
[5,8,6],
[0,7,4]]
class Node:
def __init__(self, board, parent):
self.board = [[0,0,0],[0,0,0],[0,0,0]]
for i in range(len(board)):
for j in range(len(board[i])):
self.board[i][j] = board[i][j]
self.parent = parent
def matchBoard(o1,o2):
for i in range(len(o1)):
for j in range(len(o1[i])):
if(o1[i][j] != o2[i][j]):
return False
return True
def serielizeBoard(board):
rv = ""
for arr in board:
for el in arr:
rv = rv + str(el) + " "
rv = rv + "\n"
return rv
def findEmptySlot(board):
xPos = -1
yPos = -1
for i in range(0, 3):
for j in range(0, 3):
if board[i][j] == 0:
xPos = i
yPos = j
return [xPos,yPos]
q = queue.Queue(maxsize=-1)
visited = {}
q.put(Node(a,None))
n = False
while(not q.empty()):
n = q.get()
if visited.__contains__(serielizeBoard(n.board)):
continue
visited[serielizeBoard(n.board)] = True
if matchBoard(n.board,b):
break
pos = findEmptySlot(n.board)
if pos[0] > 0:
swap(n.board,pos[0],pos[1],pos[0]-1,pos[1])
q.put(Node(n.board,n))
swap(n.board,pos[0],pos[1],pos[0]-1,pos[1])
if pos[1] > 0:
swap(n.board,pos[0],pos[1],pos[0],pos[1]-1)
q.put(Node(n.board,n))
swap(n.board,pos[0],pos[1],pos[0],pos[1]-1)
if pos[0] < 2:
swap(n.board,pos[0],pos[1],pos[0]+1,pos[1])
q.put(Node(n.board,n))
swap(n.board,pos[0],pos[1],pos[0]+1,pos[1])
if pos[1] < 2:
swap(n.board,pos[0],pos[1],pos[0],pos[1]+1)
q.put(Node(n.board,n))
swap(n.board,pos[0],pos[1],pos[0],pos[1]+1)
if not matchBoard(n.board,b):
print("Not Possible!")
else:
stack = queue.LifoQueue(maxsize=-1)
while n != None:
stack.put(n)
n = n.parent
while stack.qsize() != 0:
n = stack.get()
print(serielizeBoard(n.board))
print("\n")
Output
Experiment 3
Program
# Tic-Tac-Toe Program using
# random number in Python
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)
for x in range(len(board)):
if board[x, x] != player:
win = False
return(win)
winner = player
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)
# Driver Code
print("Winner is: " + str(play_game()))
Output
Experiment 4
Implement Brute force solution to the Knapsack problem in Python
Program
class Bounty:
def init (self, value, weight, volume):
self.value, self.weight, self.volume = value, weight, volume
best_amounts = (0, 0, 0)
Output
Experiment 5
Program
def color_nodes(graph):
# Order nodes in descending degree
nodes = sorted(list(graph.keys()), key=lambda x: len(graph[x]),
reverse=True)
color_map = {}
return color_map
Output
Experiment 6
Write a program to implement BFS for water jug problem using Python
Program
def pour(jug1, jug2):
max1, max2, fill = 5, 7, 4 #Change maximum capacity and final capacity
print("%d\t%d" % (jug1, jug2))
if jug2 is fill:
return
elif jug2 is
max2: pour(0,
jug1)
elif jug1 != 0 and jug2 is 0:
pour(0, jug1)
elif jug1 is
fill:
pour(jug1, 0)
elif jug1 < max1:
pour(max1, jug2)
elif jug1 < (max2-jug2):
pour(0, (jug1+jug2))
else:
pour(jug1-(max2-jug2), (max2-jug2)+jug2)
print("JUG1\tJUG2")
pour(0, 0)
Output
Experiment 7
Program
# Python3 program to print DFS traversal
# from a given given graph
from collections import defaultdict
# Constructor
def init (self):
# Driver code
Output
Experiment 8
Program
import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize
text = "God is Great! I won a lottery."
print(word_tokenize(text))
Output
Experiment 9
Program
# Driver code
if name ==' main ':
print(XOR(5, 5))
print("+---------------+---------------+")
print(" | XOR Truth Table | Result |")
print(" A = False, B = False | A AND B =",XOR(False,False)," |
")
print(" A = False, B = True | A AND B =",XOR(False,True)," | ")
print(" A = True, B = False | A AND B =",XOR(True,False)," | ")
print(" A = True, B = True | A AND B =",XOR(True,True)," | ")
Output
Experiment 10
Study of SCIKIT fuzzy
Theory