You are on page 1of 49

Student Name: Pravesh Azan UID: 19BCS2979

Branch:  BE CSE Section/Group : A

Semester: 5 Date of Performance: 17/01/2023

Subject Name: AIML LAB Subject Code: CSP303

EXPERIMENT NO 1

1. Aim/Overview of the practical:

Write a program to implement water jug problem


2. Task to be done/ Which logistics used:

to implement water jug problem using python

3. S/W Requirement :

Laptop/PC , Python Complier

4. Steps for experiment/practical/Code:

from collections import deque

def pour(jug1, jug2, max_vol):

if jug1 == 0:

return jug1, jug2

if jug2 == max_vol:

return jug1, jug2


if jug1 + jug2 >= max_vol:

return jug1-(max_vol-jug2), max_vol

return jug1, jug2+jug1

def bfs(max_vol1, max_vol2, goal):

visited = set()

q = deque()

q.append((0, 0))

while q:

curr = q.popleft()

if curr[0] == goal or curr[1] == goal:

return curr

if curr in visited:

continue

visited.add(curr)

q.append(pour(curr[0], curr[1], max_vol1)) # pour jug1 to jug2

q.append(pour(curr[1], curr[0], max_vol2)) # pour jug2 to jug1


q.append((max_vol1, curr[1])) # fill jug1

q.append((curr[0], max_vol2)) # fill jug2

q.append((0, curr[1])) # empty jug1

q.append((curr[0], 0)) # empty jug2

return None
max_vol1 = 4

max_vol2 = 3

goal = 2

print(bfs(max_vol1, max_vol2, goal))

5. Result/Output/Writing Summary:
Evaluation Grid (To be filled by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks

1. Understanding and 12
knowledge about the
procedure and the apparatus
2. Theoretical overview of the 8
experiment. (Viva/Voce)

3. Completion of worksheet 10
with learning outcomes
along with cleanliness and
discipline.
Signature of Faculty (with Total Marks 30
Date): Obtained:
Student Name: Pravesh Azan UID: 19BCS2979

Branch: BE CSE Section/Group : A

Semester: 5 Date of Performance: 17/01/2023

Subject Name: AIML LAB Subject Code: CSP303

EXPERIMENT NO 2

1. Aim/Overview of the practical:

Write a program to implement tic tac toe game using python


2. Task to be done/ Which logistics used:

To implement tic tac toe game using python.

3. S/W Requirement :

Laptop/PC , Python Complier

4. Steps for experiment/practical/Code:

board = [[" " for _ in range(3)] for _ in range(3)]

def print_board():
for row in board:
print("|".join(row))

def check_win(player):
# check rows
for row in board:
if row == [player, player, player]:
return True
# check columns
for i in range(3):
if board[0][i] == player and board[1][i] == player and board[2][i] ==
player:
return True
# check diagonals
if board[0][0] == player and board[1][1] == player and board[2][2] ==
player:
return True
if board[0][2] == player and board[1][1] == player and board[2][0] ==
player:
return True
return False

def play_game():
player = "X"
while True:
print_board()
print(f"{player}'s turn. Which row?")
row = int(input())
print(f"{player}'s turn. Which column?")
col = int(input())
if board[row][col] == " ":
board[row][col] = player
if check_win(player):
print_board()
print(f"{player} wins!")
break
player = "O" if player == "X" else "X"
else:
print("That space is already occupied. Try again.")

play_game()
5. Result/Output/Writing Summary:
Evaluation Grid (To be filled by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks

1. Understanding and 12
knowledge about the
procedure and the apparatus
2. Theoretical overview of the 8
experiment. (Viva/Voce)

3. Completion of worksheet 10
with learning outcomes
along with cleanliness and
discipline.
Signature of Faculty (with Total Marks 30
Date): Obtained:
Student Name: Pravesh Azan UID: 19BCS2979

Branch: BE CSE Section/Group : A

Semester: 5 Date of Performance: 17/01/2023

Subject Name: AIML LAB Subject Code: CSP303

EXPERIMENT NO 3

1. Aim/Overview of the practical:

Write a python program to implement BREADTH FIRST SEARCH


TRAVERSAL
2. Task to be done/ Which logistics used:

To implement breadth first search traversal using python

3. S/W Requirement :

Laptop/PC , Python Complier

4. Steps for experiment/practical/Code:

from collections import deque

# create a graph represented by an adjacency list


graph = {
"A": ["B", "C"],
"B": ["A", "D", "E"],
"C": ["A", "F"],
"D": ["B"],
"E": ["B", "F"],
"F": ["C", "E"]
}

def bfs(graph, start):


visited = set()
queue = deque([start])
while queue:
vertex = queue.popleft()
if vertex not in visited:
visited.add(vertex)
queue.extend(graph[vertex])
return visited

print(bfs(graph, "A"))

5. Result/Output/Writing Summary:
Evaluation Grid (To be filled by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks

1. Understanding and 12
knowledge about the
procedure and the apparatus
2. Theoretical overview of the 8
experiment. (Viva/Voce)

3. Completion of worksheet 10
with learning outcomes
along with cleanliness and
discipline.
Signature of Faculty (with Total Marks 30
Date): Obtained:
Student Name: Pravesh Azan UID: 19BCS2979

Branch: BE CSE Section/Group : A

Semester: 5 Date of Performance: 17/01/2023

Subject Name: AIML LAB Subject Code: CSP303

EXPERIMENT NO 4

1. Aim/Overview of the practical:

Write a python program to implement DEPTH FIRST SEARCH


TRANVERSAL
2. Task to be done/ Which logistics used:

To implement depth first search traversal program using python

3. S/W Requirement :

Laptop/PC , Python Complier

4. Steps for experiment/practical/Code:

# create a graph represented by an adjacency list


graph = {
"A": ["B", "C"],
"B": ["A", "D", "E"],
"C": ["A", "F"],
"D": ["B"],
"E": ["B", "F"],
"F": ["C", "E"]
}
def dfs(graph, vertex, visited=set()):
if vertex not in visited:
visited.add(vertex)
for neighbour in graph[vertex]:
dfs(graph, neighbour, visited)
return visited

print(dfs(graph, "A"))

5. Result/Output/Writing Summary:
Evaluation Grid (To be filled by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks

1. Understanding and 12
knowledge about the
procedure and the apparatus
2. Theoretical overview of the 8
experiment. (Viva/Voce)

3. Completion of worksheet 10
with learning outcomes
along with cleanliness and
discipline.
Signature of Faculty (with Total Marks 30
Date): Obtained:
Student Name: Pravesh Azan UID: 19BCS2979

Branch: BE CSE Section/Group : A

Semester: 5 Date of Performance: 17/01/2023

Subject Name: AIML LAB Subject Code: CSP303

EXPERIMENT NO 5

1. Aim/Overview of the practical:

Write a python program to implement 4 queens problem.


2. Task to be done/ Which logistics used:

To implement a 4 queens problem using python programming

3. S/W Requirement :

Laptop/PC , Python Complier

4. Steps for experiment/practical/Code:

def solve_n_queens(n):
def is_valid(board, row, col):
# Check if a queen can be placed on board[row][col]
# Check column
for i in range(row):
if board[i] == col or \
abs(board[i] - col) == abs(i - row):
return False
return True

def backtrack(board, row):


if row == n:
result.append(board[:])
else:
for col in range(n):
if is_valid(board, row, col):
board[row] = col
backtrack(board, row + 1)

result = []
backtrack([-1] * n, 0)
return result

print(solve_n_queens(4))

5. Result/Output/Writing Summary:
Evaluation Grid (To be filled by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks

1. Understanding and 12
knowledge about the
procedure and the apparatus
2. Theoretical overview of the 8
experiment. (Viva/Voce)

3. Completion of worksheet 10
with learning outcomes
along with cleanliness and
discipline.
Signature of Faculty (with Total Marks 30
Date): Obtained:
Student Name: Pravesh Azan UID: 19BCS2979

Branch: BE CSE Section/Group : A

Semester: 5 Date of Performance: 17/01/2023

Subject Name: AIML LAB Subject Code: CSP303

EXPERIMENT NO 6

1. Aim/Overview of the practical:

Write a python program to implement simple chatbot


2. Task to be done/ Which logistics used:

To implement a simple chatbot using python program

3. S/W Requirement :

Laptop/PC , Python Complier

4. Steps for experiment/practical/Code:

import random

responses = {
"hi": ["Hello!", "Hi there!", "Hey!"],
"how are you": ["I'm good, thank you!", "I'm doing well, thanks for
asking!", "I'm just a computer program, but I'm functioning well!"],
"bye": ["Goodbye!", "See you later!", "Bye!"]
}

def chatbot():
while True:
user_input = input("You: ").lower()
if user_input in responses:
print("Chatbot: " + random.choice(responses[user_input]))
elif user_input == "bye":
print("Chatbot: " + random.choice(responses["bye"]))
break
else:
print("Chatbot: I'm sorry, I didn't understand what you said.")

chatbot()

5. Result/Output/Writing Summary:
Evaluation Grid (To be filled by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks

1. Understanding and 12
knowledge about the
procedure and the apparatus
2. Theoretical overview of the 8
experiment. (Viva/Voce)

3. Completion of worksheet 10
with learning outcomes
along with cleanliness and
discipline.
Signature of Faculty (with Total Marks 30
Date): Obtained:
Student Name: Pravesh Azan UID: 19BCS2979

Branch: BE CSE Section/Group : A

Semester: 5 Date of Performance: 17/01/2023

Subject Name: AIML LAB Subject Code: CSP303

EXPERIMENT NO 7

1. Aim/Overview of the practical:

Write a python program to for text classification for the give sentence
2. Task to be done/ Which logistics used:

To implement a for text classification for the give sentence using python
program

3. S/W Requirement :

Laptop/PC , Python Complier

4. Steps for experiment/practical/Code:

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

# example sentence
sentence = "This is a positive sentence."
# create a dataframe with the sentence and its label
df = pd.DataFrame({'sentence': [sentence], 'label': ['positive']})

# create the feature matrix using CountVectorizer


count_vect = CountVectorizer()
X = count_vect.fit_transform(df['sentence'])

# create the target variable


y = df['label']

# train the model


clf = MultinomialNB().fit(X, y)

# test the model


test_sentence = "This is a negative sentence."
test_sentence_matrix = count_vect.transform([test_sentence])
predicted_label = clf.predict(test_sentence_matrix)[0]
print("Predicted label:", predicted_label)

5. Result/Output/Writing Summary:
Evaluation Grid (To be filled by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks

1. Understanding and 12
knowledge about the
procedure and the apparatus
2. Theoretical overview of the 8
experiment. (Viva/Voce)

3. Completion of worksheet 10
with learning outcomes
along with cleanliness and
discipline.
Signature of Faculty (with Total Marks 30
Date): Obtained:
Student Name: Pravesh Azan UID: 19BCS2979

Branch: BE CSE Section/Group : A

Semester: 5 Date of Performance: 17/01/2023

Subject Name: AIML LAB Subject Code: CSP303

EXPERIMENT NO 8

1. Aim/Overview of the practical:

Write a query in prolog to demonstrate a simple family tree


2. Task to be done/ Which logistics used:

To write a query in prolog to demonstrate a simple family tree

3. S/W Requirement :

Laptop/PC , Python Complier

4. Steps for experiment/practical/Code:

parent(john, jim).
parent(jim, jake).
parent(jake, jane).
parent(jane, jack).
parent(jane, jill).
parent(jim, jessica).
% rule to determine grandparents
grandparent(GP, GC) :- parent(GP, P), parent(P, GC).

% rule to determine siblings


sibling(S1, S2) :- parent(P, S1), parent(P, S2), S1 \= S2.

5. Result/Output/Writing Summary:
?- parent(john,X).
this will return “jim”

?- grandparent(X,jack).
this will return “john”

?- sibling(jake,X).
this will returrn “jessica”

Evaluation Grid (To be filled by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks

1. Understanding and 12
knowledge about the
procedure and the apparatus
2. Theoretical overview of the 8
experiment. (Viva/Voce)

3. Completion of worksheet 10
with learning outcomes
along with cleanliness and
discipline.
Signature of Faculty (with Total Marks 30
Date): Obtained:
Student Name: Pravesh Azan UID: 19BCS2979

Branch: BE CSE Section/Group : A

Semester: 5 Date of Performance: 17/01/2023

Subject Name: AIML LAB Subject Code: CSP303

EXPERIMENT NO 9

1. Aim/Overview of the practical:

Write a simple query in prolog for writing statements by using facts


2. Task to be done/ Which logistics used:

To write a simple query in prolog for writing statements by using facts

3. S/W Requirement :

Laptop/PC , Python Complier

4. Steps for experiment/practical/Code:

% facts
likes(jim, pizza).
likes(jim, ice_cream).
likes(jake, chocolate).
likes(jake, pizza).

% rule to determine if two people like the same thing


same_taste(P1, P2) :- likes(P1, F), likes(P2, F).

% query
?- same_taste(jim, jake).

5. Result/Output/Writing Summary:
Evaluation Grid (To be filled by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks

1. Understanding and 12
knowledge about the
procedure and the apparatus
2. Theoretical overview of the 8
experiment. (Viva/Voce)

3. Completion of worksheet 10
with learning outcomes
along with cleanliness and
discipline.
Signature of Faculty (with Total Marks 30
Date): Obtained:
Student Name: Pravesh Azan UID: 19BCS2979

Branch: BE CSE Section/Group : A

Semester: 5 Date of Performance: 17/01/2023

Subject Name: AIML LAB Subject Code: CSP303

EXPERIMENT NO 10

1. Aim/Overview of the practical:

Write a python code for building a simple neural network perceptron


2. Task to be done/ Which logistics used:

To write a python code for building a simple neural network perceptron

3. S/W Requirement :

Laptop/PC , Python Complier

4. Steps for experiment/practical/Code:

from sklearn.linear_model import Perceptron


from sklearn.metrics import accuracy_score

# training data
X = [[0,0], [0,1], [1,0], [1,1]]
y = [0, 1, 1, 1]

# create the perceptron model


clf = Perceptron(tol=1e-3, random_state=0)

# train the model


clf.fit(X, y)

# test the model


test_data = [[1,1], [1,0], [0,1], [0,0]]
test_labels = [1, 1, 1, 0]
predictions = clf.predict(test_data)
print("Predictions:", predictions)

# calculate accuracy
acc = accuracy_score(test_labels, predictions)
print("Accuracy:", acc)

5. Result/Output/Writing Summary:
Evaluation Grid (To be filled by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks

1. Understanding and 12
knowledge about the
procedure and the apparatus
2. Theoretical overview of the 8
experiment. (Viva/Voce)

3. Completion of worksheet 10
with learning outcomes
along with cleanliness and
discipline.
Signature of Faculty (with Total Marks 30
Date): Obtained:

You might also like