0% found this document useful (0 votes)
2 views

Lab_AI[1]

The document is a laboratory report submitted by Yash Modi for the Artificial Intelligence Lab at the National Institute of Technology Srinagar. It includes experiments on Prolog programming, the N-Queen problem, and the Water Jug problem using both depth-first search (DFS) and breadth-first search (BFS) algorithms. Each section provides problem statements, code implementations, and explanations of concepts related to artificial intelligence.

Uploaded by

YASH MODI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab_AI[1]

The document is a laboratory report submitted by Yash Modi for the Artificial Intelligence Lab at the National Institute of Technology Srinagar. It includes experiments on Prolog programming, the N-Queen problem, and the Water Jug problem using both depth-first search (DFS) and breadth-first search (BFS) algorithms. Each section provides problem statements, code implementations, and explanations of concepts related to artificial intelligence.

Uploaded by

YASH MODI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

<2022BCSE051/>

Department of Computer Science and Engineering


National Institute of Technology Srinagar
Hazratbal, Srinagar, Jammu and Kashmir - 190006, India.

LABORATORY REPORT
Artificial Intelligence Lab (CSL353)

Submitted by

YASH MODI
2022BCSE051
B.Tech. CSE (6th Semester – Section ‘A’)

Submitted to

Dr. Tawseef Ayoub Shaikh


Assistant Professor
Department of Computer Science and Engineering
National Institute of Technology Srinagar
Hazratbal, Srinagar, Jammu and Kashmir - 190006, India.

SPRING 2025
<2022BCSE051/>
<2022BCSE051/>

CONTENTS

Page Faculty
Week # Date Experiment
No. Sign

Self-Declaration

I have done all the above programs on my own to the best of my knowledge.

Student’s Signature
<2022BCSE051/>
<2022BCSE051/>

LAB-01
Introduction to Prolog

1. Introduction
Prolog (Programming in Logic) is a declarative programming language primarily
used in Artificial Intelligence (AI), natural language processing, and symbolic
reasoning. Unlike imperative languages, Prolog is based on logic and rules rather
than sequential execution of commands.

1.1 Why Prolog for AI?


Prolog is widely used in AI because it allows for:
• Expressing knowledge in a logical and structured manner.
• Reasoning and inference through logical deduction.
• Efficiently handling rule-based systems, expert systems, and natural
language processing.

2. Basics of Prolog
2.1 Facts, Rules, and Queries
Prolog programs consist of facts, rules, and queries.
2.1.1 Facts
Facts represent basic information known to the system. Example:
parent(john, mary).
parent(mary, alice).
This states that John is the parent of Mary and Mary is the parent of
Alice.
2.1.2 Rules
Rules define relationships between facts using logical conditions.
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
This rule states that X is a grandparent of Y if X is a parent of Z and Z
is a parent of Y.
2.1.3 Queries
Queries allow users to retrieve information from the knowledge
base.
?- grandparent(john, alice).
This query asks whether John is a grandparent of Alice.
2.2 Variables in Prolog
Variables in Prolog are written in uppercase.
<2022BCSE051/>

?- parent(X, mary).
This asks, "Who is the parent of Mary?" Prolog will return X = john.
3. Prolog Execution Mechanism
3.1 Unification
Unification is the process of matching terms to satisfy a query.
3.2 Backtracking
If Prolog encounters a failure, it backtracks to previous rules to find
alternative solutions.
3.3 Recursion
Prolog heavily relies on recursion. Example:
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
This defines an ancestor as a parent or a parent of an ancestor.

4. Lists in Prolog
Lists are fundamental in Prolog for handling collections of data.
member(X, [X|_]).
member(X, [_|T]) :- member(X, T).
This checks if X is a member of a list.

5. Prolog for AI Applications


5.1 Expert Systems
Prolog is used to develop expert systems that can make decisions based on
predefined rules.
5.2 Natural Language Processing (NLP)
Prolog helps in parsing and understanding human language structures.
5.3 Pathfinding & Search Algorithms
Prolog is used in AI algorithms like Depth-First Search (DFS) and Breadth-
First Search (BFS).
<2022BCSE051/>

LAB-02
N-QUEEN PROBLEM

Problem Statement:
Place N queens on an N × N chessboard such that no two queens:
1. Share the same row.
2. Share the same column.
3. Lie on the same diagonal.
Objective:
Find one or all valid arrangements of N queens satisfying the above constraints.

Code:

N=8
def print_solution(board):
for row in board:
print(" ".join(str(cell) for cell in row))

def is_safe(board, row, col):


for i in range(col):
if board[row][i] == 1:
return False

for i, j in zip(range(row, -1, -1), range(col, -1, -1)):


if board[i][j] == 1:
return False

for i, j in zip(range(row, N, 1), range(col, -1, -1)):


if board[i][j] == 1:
return False

return True

def solve(board, col):


if col >= N:
return True
<2022BCSE051/>

for i in range(N):
if is_safe(board, i, col):
board[i][col] = 1
if solve(board, col + 1):
return True
board[i][col] = 0

return False

def solve_n_queens():
board = [[0] * N for _ in range(N)]
if not solve(board, 0):
print("No solution exists")
return False

print_solution(board)
return True

solve_n_queens()

Output:
0010
1000
0001
0100
<2022BCSE051/>

LAB-03
Water Jug Problem

Problem Statement:
Place N queens on an N × N chessboard such that no two queens:
1. Share the same row.
2. Share the same column.
3. Lie on the same diagonal.
Objective:
Find one or all valid arrangements of N queens satisfying the above
constraints.

def gcd(a, b):


while b:
a, b = b, a % b
return a

def is_solvable(x, y, z):


return z <= max(x, y) and z % gcd(x, y) == 0

def dfs(x, y, z, current, visited, path):


a, b = current

if (a, b) in visited:
return False

visited.add((a, b))
path.append((a, b))

if a == z or b == z:
print("Solution found!")
for step in path:
print(step)
return True

possible_moves = [
<2022BCSE051/>

(x, b),
(a, y),
(0, b),
(a, 0),
(a - min(a, y - b), b + min(a, y - b)),
(a + min(b, x - a), b - min(b, x - a))
]

for move in possible_moves:


if dfs(x, y, z, move, visited, path):
return True

path.pop()
return False

def solve_water_jug_dfs(x, y, z):


if not is_solvable(x, y, z):
print("No solution possible.")
return

visited = set()
path = []

if not dfs(x, y, z, (0, 0), visited, path):


print("No solution found.")

x = int(input("Enter Jug 1 capacity: "))


y = int(input("Enter Jug 2 capacity: "))
z = int(input("Enter required amount: "))

solve_water_jug_dfs(x, y, z)

Enter Jug 1 capacity: 5


Enter Jug 2 capacity: 3
Enter required amount: 2
Solution found!
(0, 0)(5, 0)(5, 3)(0, 3)(3, 0)(3, 3)(5, 1)(0, 1)(1, 0)(1, 3)(4, 0)(4, 3)(5,
2)
<2022BCSE051/>

LAB-04
Water Jug Problem(BFS)
Problem Statement:

def gcd(a, b):


while b:
a, b = b, a % b
return a

def is_solvable(x, y, z):


return z <= max(x, y) and z % gcd(x, y) == 0

def solve_water_jug_bfs(x, y, z):


if not is_solvable(x, y, z):
print("No solution possible.")
return

queue = deque([(0, 0)])


visited = set()
parent = {}

while queue:
a, b = queue.popleft()

if (a, b) in visited:
continue

visited.add((a, b))

if a == z or b == z:
path = []
while (a, b) in parent:
path.append((a, b))
a, b = parent[(a, b)]
path.append((0, 0))
path.reverse()

print("Solution found!")
<2022BCSE051/>

for step in path:


print(step)
return

possible_moves = [
(x, b),
(a, y),
(0, b),
(a, 0),
(a - min(a, y - b), b + min(a, y - b)),
(a + min(b, x - a), b - min(b, x - a))
]

for move in possible_moves:


if move not in visited:
queue.append(move)
parent[move] = (a, b)

print("No solution found.")

x = int(input("Enter Jug 1 capacity: "))


y = int(input("Enter Jug 2 capacity: "))
z = int(input("Enter required amount: "))

solve_water_jug_bfs(x, y, z)

Enter Jug 1 capacity: 5


Enter Jug 2 capacity: 3Enter required amount: 2Solution found!(0,0)(5,
0)(5, 3)(0, 3)(3, 0)(3, 3)(5, 1)(0, 1)(1, 0)(1, 3)(4, 0)(4, 3)(5, 2)

You might also like