Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
89 views10 pages

AI Lab Report: BFS & DFS Solutions

Uploaded by

pehep10730
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views10 pages

AI Lab Report: BFS & DFS Solutions

Uploaded by

pehep10730
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1

CS302 Artificial Intelligence Lab Report


Group : N EXUS
Anaykumar Pandey-202211063 , Rahul Gupta -202211069, Nitin Kumar-202211059, Ankit Gautam-202211003

Abstract—The Artificial Intelligence Lab report provides an The challenge is to move the rabbits past each other without any
extensive examination of addressing challenges in AI using stepping into the water, given specific movement constraints.
established software libraries and techniques covered in our
coursework. It explores practical applications of these concepts,
demonstrating their effectiveness in solving real-world prob-
A. Problem Statement:
lems. The report encompasses various topics such as graph
search, heuristic functions, the Traveling Salesman Problem, non- a. The missionaries and cannibals problem is usually stated
deterministic search, simulated annealing, MINIMAX, Alpha- as follows. Three missionaries and three cannibals are
Beta Pruning, Bayesian Networks, Hidden Markov Models, on one side of a river, along with a boat that can hold
Decision Trees, Hopfield networks, and the n-armed bandit
problem. Additionally, it discusses algorithm development and one or two people. Find a way to get everyone to the
solution design using popular Python libraries. In essence, the other side without ever leaving a group of missionaries
report serves as a valuable resource for understanding how AI in one place outnumbered by the cannibals in that place.
concepts and tools can be applied practically, highlighting the This problem is famous in AI because it was the subject
relevance of coursework in tackling real-world challenges. of the first paper that approached problem-formulation
from an analytical viewpoint.
b. In the rabbit leap problem, three east-bound rabbits stand
I. I NTRODUCTION in a line blocked by three west-bound rabbits. They are
RTificial Intelligence research entails the development crossing a stream with stones placed in the east west
A of systems capable of emulating human intelligence to
execute tasks like comprehending natural language, identifying
direction in a line. There is one empty stone between
them. The rabbits can only move forward one or two
patterns, making decisions, and learning from data. Within steps. They can jump over one rabbit if the need arises,
an AI lab, the focus lies in experimenting with various AI but not more than that. Are they smart enough to cross
techniques and algorithms to tackle the challenges inherent in each other without having to step into the water?
crafting intelligent systems.
This report offers a synopsis of experiments conducted B. Solution for Missionary-Cannibal problem:
within an AI lab aimed at addressing key challenges encoun- A. Problem Modelling
tered by AI systems and devising solutions to overcome them.
a) State-Space Search Problem: We can model this
These experiments span a spectrum of AI topics, encompass-
problem as a state space search problem where:
ing machine learning, natural language processing, computer
vision, robotics, and more. i) State: (M, C, B), where M is the number of
The experiments within the AI lab encompass activities missionaries on the initial side, C is the number
such as data importing and preprocessing, machine learning of cannibals on the initial side, and B is the boat
model training and evaluation, algorithm design for decision- position (1 for initial side, 0 for opposite side).
making and problem-solving, and the integration of AI systems ii) Initial state: (3, 3, 1).
into real-world applications. The objective is to cultivate iii) Goal state: (0, 0, 0).
AI systems capable of comprehensively understanding and iv) Actions: Move 1 or 2 people across the river,
responding to user requirements while navigating challenges ensuring valid states.
such as accuracy, efficiency, scalability, and adaptability to b) Search Space Size: The search space size is deter-
novel data and environments. mined by the possible combinations of missionar-
ies and cannibals on each side, multiplied by the
II. LABORATORY 1: M ISSIONARY-C ANNIBAL R ABBIT two possible boat positions.
L EAP C HALLENGE i) Maximum possible states: 4 * 4 * 2 = 32
Abstract—The missionaries and cannibals problem is a classic ii) However, not all of these states are valid due
river-crossing puzzle in artificial intelligence. It involves three to the constraint that cannibals must not out-
missionaries and three cannibals attempting to cross a river using number missionaries. After eliminating invalid
a boat that can carry at most two people. The challenge lies in states, we are left with 16 valid states.
ensuring that cannibals never outnumber missionaries on either
side of the river at any point during the crossing. The rabbit 1) Breadth-First Search Algorithm
leap problem is a puzzle involving three east-bound rabbits and a) Algorithm:
three west-bound rabbits on a line of stones crossing a stream.
i) Initialize a queue with the initial state.
ii) While the queue is not empty:
2

A) Dequeue a state.
B) If the state is the goal state, return the
solution.
C) Generate all valid successor states.
D) Enqueue the successor states if not visited.
iii) If the queue is empty and goal not reached,
return failure.
b) Pseudo Code:
function BFS(initial_state,
goal_state):
queue = Queue()
visited = Set()
queue.enqueue(initial_state)
visited.add(initial_state)
while not queue.is_empty():
current_state = queue.dequeue()
Figure 1. State Space Graph for missionaries cannibals problem
if current_state == goal_state:
return reconstruct_path(cur
rent_state) if successor not in
for successor in generate_succ visited:
essors(current_state): stack.push(successor)
if successor not in visited: return failure
queue.enqueue(successor)
visited.add(succes
sor) You can click on link to open the code.
return failure c) Solution and Optimality: The DFS solution may
not be optimal, as DFS does not guarantee finding
You can click on link to open the code. the shortest path. The number of steps in the
c) Solution and Optimality: The BFS solution found DFS solution may vary depending on the order of
is optimal as BFS always finds the shortest path in successor generation.
an unweighted graph. The solution has 11 steps, d) Comparison of BFS and DFS solutions:
which is the minimum number of moves required i) Solution Quality:
to solve the problem. A) BFS: Always finds the optimal (shortest)
2) Depth-First Search Algorithm solution.
a) Algorithm: B) DFS: May find a suboptimal solution, de-
i) Initialize a stack with the initial state. pending on the search order.
ii) While the stack is not empty: ii) Time Complexity:
A) Pop a state from the stack. A) BFS: O(bd̂), where b is the branching factor
B) If the state is the goal state, return the and d is the depth of the shallowest solution.
solution. B) DFS: O(bm̂), where m is the maximum
C) Generate all valid successor states. depth of the search tree.
D) Push the successor states onto the stack if iii) Space Complexity:
not visited. A) BFS: O(bd̂), as it stores all nodes at the
iii) If the stack is empty and goal not reached, current level.
return failure. B) DFS: O(bm), as it only needs to store the
b) Pseudo Code: nodes on the current path.
e) Conclusion: Both BFS and DFS can solve the mis-
function DFS(initial_state, sionaries and cannibals problem. BFS guarantees
goal_state):
stack = Stack() an optimal solution but may use more memory,
visited = Set() while DFS uses less memory but may find a subop-
stack.push(initial_state) timal solution. The choice between them depends
while not stack.is_empty(): on the specific requirements of the problem-solving
current_state = stack.pop() context.
if current_state == goal_state:
return reconstruct_path(cur
rent_state) C. Solution for Rabbit Leap problem:
if current_state not in visited: A. Problem Modelling
visited.add(current_state)
for successor in i) State-Space Search Problem: We can model
generate_succ this problem as a state space search problem
essors(current_state): where:
3

A) State: A string of length 7, representing the A) Initialize a stack with the initial state.
positions of rabbits and the empty space. ’E’ B) While the stack is not empty:
for east-bound rabbits, ’W’ for west-bound C) Pop a state from the stack.
rabbits, and ’ ’ for the empty space. D) If the state is the goal state, return the
B) Initial state: ”EEEW WWW”. solution.
C) Goal state: ”WWWE EEE”. E) Generate all valid successor states.
D) Actions: Move a rabbit one or two steps F) Push the successor states onto the stack if
forward into the empty space. not visited.
ii) Search Space Size: The search space size is G) If the stack is empty and goal not reached,
determined by the possible arrangements of return failure.
rabbits and the empty space. ii) Pseudo Code:
A) Maximum possible states: 7! / (3! * 3!) =
70. function DFS(initial_state,
goal_state):
B) However, not all of these states are reach- stack = Stack()
able due to the movement constraints. The visited = Set()
actual number of reachable states is smaller. stack.push(initial_state)
a) Breadth-First Search Algorithm while not stack.is_empty():
current_state = stack.pop()
i) Algorithm: if current_state ==
A) Initialize a queue with the initial state. goal_state:
B) While the queue is not empty: return
reconstruct_path(curr
C) Dequeue a state. ent_state)
D) If the state is the goal state, return the if current_state not in
solution. visited:
E) Generate all valid successor states. visited.add(current_state)
F) Enqueue the successor states if not visited. for successor in
generate_successors(c
G) If the queue is empty and goal not reached, urrent_state):
return failure. if successor not in
ii) Pseudo Code: visited:
stack.push(succes
function BFS(initial_state, sor)
goal_state): return failure
queue = Queue()
visited = Set() You can click on link to open the code.
queue.enqueue(initial_state)
visited.add(initial_state) iii) Solution and Optimality: The DFS solution
while not queue.is_empty(): may not be optimal, as DFS does not guaran-
current_state = tee finding the shortest path. The number of
queue.dequeue() steps in the DFS solution may vary depending
if current_state == on the order of successor generation.
goal_state:
return c) Comparison of BFS and DFS solutions:
reconstruct_path(cur i) Solution Quality:
rent_state)
for successor in A) BFS: Always finds the optimal (short-
generate_succ est) solution of 15 steps.
essors(current_state): B) DFS: May find a suboptimal solution,
if successor not in
potentially with more than 15 steps,
visited:
queue.enqueue(successor) depending on the search order.
visited.add(succes ii) Time Complexity:
sor)
return failure A) BFS: O(bd̂), where b is the branching
factor (maximum 4 in this case) and d
You can click on link to open the code. is the depth of the shallowest solution
iii) Solution and Optimality: The BFS solution (15 in this case).
found is optimal as BFS always finds the short- B) DFS: O(bm̂), where m is the maximum
est path in an unweighted graph. The solution depth of the search tree (which could be
has 15 steps, which is the minimum number of larger than the optimal solution depth).
moves required to solve the problem. iii) Space Complexity:
b) Depth-First Search Algorithm A) BFS: O(bd̂), as it stores all nodes at the
i) Algorithm: current level.
4

search algorithm, a best-first search, can be applied to


efficiently find the optimal alignment between two texts.
2) Prerequisites: A Search Overview:
A* search is a graph traversal algorithm that finds
the shortest path from a start node to a goal node.
It combines the strengths of Dijkstra’s algorithm and
Figure 2. State Diagram for Rabbit Leap problem
greedy best-first search by using a heuristic to estimate
the cost of reaching the goal. The A* algorithm uses the
B) DFS: O(bm), as it only needs to store following cost function:
the nodes on the current path.
d) Conclusion: Both BFS and DFS solve the f (n) = g(n) + h(n)
rabbit leap problem effectively. BFS guarantees an Where:
optimal solution in 15 steps but uses more memory, • g(n) is the cost of the path from the start node to
while DFS is more memory-efficient but may find node n.
a suboptimal solution. The choice depends on • h(n) is the estimated cost from node n to the goal
whether memory usage or optimality is prioritized. (heuristic function).

III. LABORATORY 2: G RAPH S EARCH AGENT AND


C. Solutions for Exp 1
P LAGIARISM D ETECTION S YSTEM U SING A* S EARCH
A LGORITHM A.1 The graph search agent involves exploring nodes
(states) in a graph, checking if they are the goal, and expanding
Abstract—This report presents two lab assignments focusing nodes to explore further.
on graph search agents and a plagiarism detection system
using the A* search algorithm. The first assignment involves function GraphSearch(problem):
designing a graph search agent, exploring its implementation, initialize frontier as a queue with
and analyzing the Puzzle-8 problem. The second assignment aims the initial state
to develop a plagiarism detection system through text alignment, initialize explored as an empty set
employing A* search to detect similar or identical sequences in while frontier is not empty:
two documents. state = frontier.pop()
if state is goal:
return solution
A. Experiment 1 add state to explored
for each action in
1) Write a pseudocode for a graph search agent. Represent problem.actions(state):
the agent in the form of a flowchart. Clearly mention all child = problem.result(state, action)
the implementation details with reasons. if child is not in explored and not
2) Write a collection of functions imitating the environment in frontier:
for Puzzle-8. if child is goal:
return solution
3) Describe what is Iterative Deepening Search. frontier.add(child)
4) Considering the cost associated with every move to return failure
be the same (uniform cost), write a function that can
backtrack and produce the path taken to reach the goal A.2 This function should be able to simulate the environ-
state from the source/initial state. ment by performing basic operations such as moving tiles,
5) Generate Puzzle-8 instances with the goal state at depth checking for the goal state, and generating successor states.
“d”. A collection of functions which represent and manipulate the
6) Prepare a table indicating the memory and time require- state of the 8-puzzle problem are:
ments to solve Puzzle-8 instances (depth “d”) using your A.3 Iterative Deepening Search (IDS) is a search strategy
graph search agent. that combines the depth-first search’s space-efficiency with
the breadth-first search’s optimality. IDS performs a series of
B. Experiment 2 depth-limited searches with increasing depth limits until the
The goal of this lab is to implement a plagiarism detection goal is found.
system using the A* search algorithm applied to text align- Working:
ment. The system will identify similar or identical sequences • Start with a depth limit of 0 and perform a depth-limited

of text between two documents by aligning their sentences or search.


paragraphs and detecting potential plagiarism. • If the goal is not found, increase the depth limit by 1 and

1) Background: Plagiarism detection involves comparing perform another depth-limited search.


• Repeat until the goal is found.
two documents to identify shared sequences of words or
phrases that might indicate copying. Text alignment is Advantages:
a common approach to this, where one text is aligned • Memory-efficient compared to Breadth-First Search since
with another based on similarity measures. The A* it doesn’t need to store all nodes at the current level.
5

Figure 4. Performance Table

in two documents. The system checks sentences from both


documents to find potential plagiarism.
Background Plagiarism detection means comparing two
documents to see if they have the same phrases or sentences.
One way to do this is by aligning the text based on how similar
the sentences are. The A* search algorithm is a method that
helps find the best way to align the text efficiently.
Problem Definition
The goal is to match the sentences of two documents in a way
that minimizes the number of changes needed (edit distance)
to see if there’s any plagiarism.
Approach
1) State Representation
• Each state shows a part of the alignment between
the two documents.
• A state includes the current position in each docu-
Figure 3. Flowchart for Graph Search Agent
ment and the total cost (edit distance).
2) Initial and Goal States
• Finds the shortest path in terms of the number of moves • Initial State: Starts with the first sentence from both
(optimal for unweighted graphs). documents.
Disadvantage: • Goal State: Reached when all sentences are aligned.

• The repeated exploration of nodes increases the time 3) Transition Function


complexity compared to BFS. • Possible actions include aligning the current sen-

A.4 This backtracking function reconstructs the path from tences, skipping one sentence, or skipping both.
the start state to the goal state using a dictionary that maps 4) Cost Function (g(n))
each state to its predecessor. The print_solution func- • The cost is based on the edit distance between the
tion is used to display the sequence of moves. sentences being compared.
A.5 This function starts with the goal state and applies 5) Heuristic Function (h(n))
random valid moves for the specified number of steps (depth). • This estimates how much more alignment is needed
The resulting state is guaranteed to be solvable and will be at for the remaining sentences.
most ’depth’ moves away from the goal. 6) A* Search Algorithm
A.6 This function measures the average time and memory
• The A* search algorithm explores different ways to
usage for solving Puzzle-8 instances at different depths,
align the sentences to find the best option with the
providing insights into the performance of the graph search
lowest cost.
agent.
Implementation Steps
The complete code for the problem can be found here. 1) Text Preprocessing: Break the documents into sen-
tences. Clean the text by making it lowercase and
removing punctuation.
D. Solutions for Exp 2 2) Define the A* Search Function: Set up the A* search
B.1 Plagiarism detection is important for ensuring that work using the defined states, costs, and actions.
is original and not copied. This report describes a system that 3) Compute Edit Distance: Use the Levenshtein distance
uses the A* search algorithm to find similar or identical text formula to find out how different two sentences are.
6

4) Perform Alignment: Use the A* search algorithm to B. Understanding the Problem Statements:
align sentences from both documents. 1) Marble Solitaire The goal of Marble Solitaire is to
5) Detect Plagiarism: Look for sentences that are similar manipulate the marbles on a board until only one
(have low edit distances) as signs of plagiarism. remains in the center. To solve this problem, we will:
6) Evaluate the System: Test the system with different
cases to see how well it works.
a) Implement Priority Queue-Based Search: This al-
Test Cases gorithm will prioritize moves based on path cost,
1) Identical Documents: Both documents are the same; exploring the least costly options first to reach the
sentences align perfectly. goal.
2) Slightly Modified Document: One document has small b) Propose Heuristic Functions: Remaining Marbles
changes; most sentences still align closely. Count: This function estimates progress by count-
3) Completely Different Documents: The documents have ing how many marbles are left; fewer marbles
high differences, indicating no plagiarism. indicate closer proximity to the goal. Distance
4) Partial Overlap: Some sentences are similar; low edit from Center: This function focuses on marbles
distance suggests possible plagiarism. closest to the center, as these moves are more
likely to lead to the desired configuration.
You can click on link to open the code. c) Implement Best First Search and A* Algorithms:
These algorithms will use heuristic evaluations to
Conclusion systematically explore options and reach the goal
This report presents two lab assignments that showcase the efficiently.
application of graph search algorithms. The first assignment d) Compare Results of Various Search Algorithms:
involved designing a graph search agent for the Puzzle-8 prob- We will analyze the performance of the
lem, providing insights into search strategies’ efficiency. The implemented algorithms in terms of efficiency
second assignment developed a plagiarism detection system and success in reaching the goal state.
using the A* search algorithm for text alignment, highlighting
its effectiveness in identifying similarities in text documents. Random k-SAT Problem Generation and Solution
This section involves generating and solving random k-
IV. L ABORATORY 3: H EURISTIC S EARCH A LGORITHMS SAT problems: Random k-SAT Problem Generator: We
FOR M ARBLE S OLITAIRE AND U NIFORM R ANDOM K -SAT will create a program that generates uniform random
P ROBLEMS k-SAT problems based on user-defined values for k
Abstract—This lab assignment looks into how heuristic func- (number of variables per clause), m (number of clauses),
tions can help reduce the search space for solving Marble Solitaire and n (total variables). Solving Uniform Random 3-
and creating random k-SAT problems. I’ve implemented and SAT Problems: We will implement algorithms to solve
compared different search algorithms like priority queue-based various 3-SAT problems, comparing their performance
search, Best First Search, and A*. I’ve also written programs
to generate and solve random 3-SAT problems using different
across different combinations of m and n.
heuristic search methods. Heuristic Search Algorithms: We will explore several
techniques:
1. Hill-Climbing: An iterative search that moves to the
A. Introduction: neighbor with the best score.
This lab focuses on applying heuristic search algorithms 2. Beam Search: This method will use beam widths of
to tackle complex problems, specifically Marble Solitaire and 3 and 4, maintaining a limited set of best candidates.
random k-SAT problems. We start by solving Marble Soli- 3. Variable Neighborhood Descent: This algorithm will
taire, a single-player game where the objective is strategically explore multiple neighborhoods using three different
moving marbles on a board to leave only one marble in the functions.
center. Next, we generate uniform random k-SAT problems, 4. Comparative Analysis: Using two different heuristic
where each clause contains distinct variables or their nega- functions, we will compare the algorithms based on their
tions. This process involves creating random 3-SAT prob- effectiveness in finding solutions and overall efficiency.
lems with varying combinations of clauses and variables. We Through these tasks, we aim to enhance our understand-
then apply several heuristic search algorithms, including Hill- ing of heuristic search algorithms and their applications
Climbing, Beam Search (with beam widths of 3 and 4), and in solving complex problems
Variable-Neighborhood-Descent (utilizing three neighborhood 1) Problem Definition:
functions), to solve these problems. The performance of these a) Problem I: Read about the game of marble soli-
algorithms is compared using two different heuristics, allowing taire. The figure shows the initial board config-
us to evaluate their effectiveness in finding solutions effi- uration. The goal is to reach the board configu-
ciently. This lab provides insights into how heuristic methods ration where only one marble is left at the cen-
can optimize the search process in complex problem-solving ter. To solve marble solitaire, Implement priority
scenarios. queue-based search considering path cost, suggest
7

two different heuristic functions with justification, 8: Select a random subset of size k from variables
Implement best-first search algorithm, Implement list (without repetition) ← randomv ariables
A*, and Compare the results of various search 9 : f oreachvariableinrandomv ariablesdo
algorithms. 10 : N egation¸Randomlychoosetrueorf alse
b) Problem II: Write a program to randomly generate 11 : if N egationistruethen
k-SAT problems. The program must accept values 12 : Append − variabletoclause
for k, m the number of clauses in the formula, and 13 : else
n the number of variables. Each clause of length 14 : Appendvariabletoclause
k must contain distinct variables or their negation. 15 : endif
Instances generated by this algorithm belong to 16 : endf or
fixed clause length models of SAT and are known 17 : Addclausetoclauses
as uniform random k-SAT problems. 18 : endf or
19 : returnclauses
2) Implementation Overview And Algorithm:
20 : endprocedure
a) Problem I Implementation Overview:
1. Priority Queue-Based Search: We will imple-
ment a search algorithm using a priority queue c) 1. Input Parameters:
to efficiently explore possible moves in Marble k: Number of literals in each clause.
Solitaire, focusing on minimizing the path cost m: Number of clauses to generate.
until reaching the goal state of having one marble n: Total number of variables.
at the center. 2. Initialization: A list of variables from 1
2. Heuristic Functions: Manhattan Distance: This to n is created. An empty list of clauses is
heuristic estimates the total distance of all marbles initialized to store the generated clauses.
from the center, guiding the search toward con- 3. Clause Generation: For each clause (up
figurations that reduce this distance.Empty Spaces to m), a random subset of k variables is
Heuristic: This function prioritizes moves that chosen from the list. For each variable in
create more empty spaces, enhancing future move- the subset, a random decision is made to
ment options and facilitating the path to the goal. either negate the variable or keep it as is.
3. Best-First Search Algorithm: This algorithm 4. Output:The program returns a list of
will expand the node with the lowest heuristic m clauses where each clause contains k
value at each step, allowing for an informed traver- distinct variables or their negation
sal toward the goal state.
4. A* Algorithm: We will implement the A*
algorithm, combining uniform cost search with Problem III
heuristic evaluation to find optimal solutions by a) Algorithm
balancing the actual cost and estimated cost to the 1: procedure SOLVERANDOM3SAT(k, m, n)
goal. 2: Input:
5. Performance Comparison: Finally, we will com- k ← Length of each clause (set to 3 for 3-SAT)
pare the performance of these algorithms based m ← Number of clauses
on solution optimality, computational efficiency, n ← Number of variables
and the number of nodes explored, providing 3:Output:
insights into their effectiveness for solving Marble Best solution found using each algorithm and
Solitaire. comparison based on penetrance
b) Problem II 4:Generate clauses using the GENERATERANDOMK-
Algorithm 1 SAT procedure
1: procedure GENERATERANDOMKSAT(k, m, 5:Initialize algorithms: Hill-Climbing, Beam Search,
n) VND
2: Input: 6:for each algorithm do
k ← Length of each clause (number of literals) 7:Initialize random variable assignment ←
m ← Number of clauses initials olution
n ← Number of variables 8 : U seheuristic1(satisf iedclauses)andheuristic2(Hammingdi
3: Output: 9 : f oreachclausecombination(m, n)do
clauses ← A list of m clauses, each of length k 10 : ApplyHill − Climbingalgorithm
with distinct variables or their negations. 11 : ApplyBeamSearchwithbeamwidths3and4
4: Initialize a list of variables ← [1, 2, ..., n] 12 : ApplyV ariable − N eighborhood − Descent
5: Initialize clauses ← Empty list 13 : endf or
6: for i ← 1 to m do 14 : Comparetheperf ormanceof eachalgorithmusingpenetranc
7: clause ← Empty list 15 : returnP erf ormancecomparison
8

16 : endprocedure the nodes are locations of cities, and edges are labeled
Implementation Overview with the cost of traveling between cities, find a cycle
a) •Step 1: Generate Random 3-SAT Problem: containing each city exactly once, such that the total
This step generates clauses with 3 literals per cost of the tour is as low as possible.
clause using a random combination of variables
and their negations. C. Solution of Traveling Salesman Problem:
Step 2: Solve Using Algorithms:Hill-
Climbing,Beam Search (with beam widths 3 A. Problem Formulation:
and 4) Variable-Neighborhood-Descent (VND) Let G= (V,E) be a complete graph, where V is a set
with 3 different neighborhood functions. of cities (or points) and E is the connections between
Step 3: Heuristic Functions: them, with weights showing how much it costs to travel
Heuristic 1: Number of satisfied clauses. between cities. The goal is to find a route (cycle) C that:
Heuristic 2: Hamming distance from a known • Visits every city v in V exactly once.
solution (or some other logical metric). • Has the lowest total travel cost.
Step 4: Performance Evaluation: The penetrance The cost to travel between two cities i and j is called
(percentage of successfully solved problems) d(i,j). The total cost of the trip is the sum of these travel
and the number of satisfied clauses are used to costs for all the cities in the route. Mathematically, we
compare the algorithms. can express the Traveling Salesman Problem (TSP) as:
N
The links for the codes can be found here. X
min d(ci, ci + 1)
i=1
Conclusion
a) In conclusion, this lab assignment successfully where ci are the cities visited in order, and d(cn, c1) is
demonstrated the effectiveness of heuristic search the cost to return to the starting city.
algorithms in solving complex problems such B. Algorithm:
as Marble Solitaire and uniform random k-SAT A simple approach to TSP involves selecting the nearest
problems. We observed that heuristics signifi- unvisited city from the current city at each step.
cantly optimize the search process by implement- • Start from any city.
ing and comparing various algorithms, includ- • Select the nearest unvisited city and travel there.
ing Hill-Climbing, Beam Search, and Variable- • Repeat the process until all cities are visited.
Neighborhood-Descent. The performance analysis • Return to the starting city.
revealed insights into the strengths and weak- C. Time Complexity:
nesses of each algorithm, paving the way for The algorithm runs in O(n2) time.
future exploration of advanced heuristic methods The code can be found here.
in combinatorial optimization problems.
D. Conclusion:
V. L ABORATORY 4: T RAVELING S ALESMAN P ROBLEM The is a simple and fast way to solve the Traveling
Abstract—The Traveling Salesman Problem (TSP) is a common Salesman Problem (TSP). It quickly finds a good route
problem where you need to find the best route to visit several by always going to the closest unvisited city. However,
cities and return to where you started, while spending the least
amount of money on travel. This paper talks about to solve this
it doesn’t always find the best route because it uses a
problem. straightforward method instead of looking at all possi-
bilities.

A. Introduction:
VI. L ABORATORY 5: U SING BAYESIAN AND NAIVE BAYES
The Traveling Salesman Problem (TSP) is a difficult puzzle M ODELS TO P REDICT S TUDENT G RADES AND I NTERNSHIP
where you need to find the shortest way to visit a list of cities Q UALIFICATION .
once and then return home. This problem is important for
Abstract—This paper explains how we can predict students’
things like delivery, making products, and managing computer grades and whether they qualify for an internship using two
networks. models: Bayesian networks and Naive Bayes classifiers. First, we
Since it’s hard to solve when there are many cities, people look at how grades in different courses are related to each other
use different methods to help. Some methods find the best to predict a student’s grade in a specific course. Then, we use
answer, while others are faster but might not be the best. In Naive Bayes to predict if a student will qualify for an internship
based on their grades, assuming the grades in different courses
this paper, we look at how to solve this problem. don’t affect each other. We train the model with 70% of the
data and test it on the remaining 30%. We repeat this process
B. Problem Statement: 20 times to check how accurate the predictions are. Finally, we
do the same experiment again, but this time, we don’t assume
The Traveling Salesman Problem (TSP) is a hard prob- the grades are independent, and we compare the results.
lem, and is simple to state. Given a graph in which
9

A. Introduction: in different courses are independent, which makes the


Understanding how students’ grades in different courses classification process simpler. We divide the data into
are connected can help predict their future performance and 70% for training and 30% for testing. The classifier is
if they qualify for internships. In this study, we use two trained to predict if a student qualifies for an internship
methods: a Bayesian network, which looks at how grades based on their grades. This process is repeated 20 times
in different courses are related, and a Naive Bayes classifier, with different random splits of the training and testing
which assumes grades are not connected. We use these models sets, and we check the accuracy of the classifier for each
to predict a student’s grade in one course based on their other split. We report the average accuracy over the 20 runs
grades, and predict if the student will qualify for an internship and also provide a confusion matrix for one example
based on their overall grades. split.
D. Classifier with Dependent Courses:
The Bayesian network, unlike the Naive Bayes approach,
B. Problem Statement: looks at how the courses are connected, which might
A table containing grades earned by students in respec- help us better predict if a student qualifies for an
tive courses is made available to you in (codes folder) internship. We use the same 70-30 split of the data,
2020 bn nb data.txt. training the classifier on 70% of it for each split.
• Consider grades earned in each of the courses as random We check how well this classifier performs over 20
variables and learn the dependencies between courses. random splits and compare the results with those from
• Using the data, learn the CPTs for each course node. the Naive Bayes classifier.
• What grade will a student get in PH100 if he earns DD E. Result:
in EC100, CC in IT101 and CD in MA101. • Prediction of PH100 Grade: Using the Conditional
• The last column in the data file indicates whether a Probability Tables (CPTs) from the Bayesian net-
student qualifies for an internship program or not. From work, we predict that a student who receives DD
the given data, take 70 percent data for training and in EC100, CC in IT101, and CD in MA101 is most
build a naive Bayes classifier (considering that the grades likely to get a grade of X in PH100. We calculate
earned in different courses are independent of each other) the conditional probabilities for each possible grade
which takes in the student’s performance and returns the in PH100 and choose the grade with the highest
qualification status with a probability. Test your classifier probability.
on the remaining 30 percent data. Repeat this experiment • Naive Bayes Classifier Accuracy: The Naive
for 20 random selection of training and testing data. Bayes classifier, which assumes that the grades
Report results about the accuracy of your classifier. in different courses are independent, achieves
• Repeat 4, considering that the grades earned in different an average accuracy of X% over the 20 random
courses may be dependent. training/testing splits. The confusion matrix for
one of the experiments is displayed in Table.
C. Solution of above problem: True Yes No
A. Data and Reprocessing: Yes A C
The data comes from a file, which includes students’ No C D
grades in four courses (EC100, IT101, MA101, and The accuracy is calculated as:
PH100) and whether they qualified for an internship.
The grades are given as letters like AA, AB, BB, BC, (a + d)/(a + b + c + d)
CC, CD, DD, F, and the internship status is either
”Yes” or ”No.” Before using the data in the model, we • Classifier with Dependent Courses: When we use
convert these letter grades into numbers so the model the Bayesian network to consider how courses are
can understand and work with them. connected, the classifier gets an average accuracy
B. Learning Conditional Probability Tables for Courses: of Y%. This is a slight improvement (or about the
To model the relationships between courses, we treat the same) compared to the Naive Bayes classifier.
grades for each course as random variables and create a The code can be found here.
Bayesian network. This network shows how the grades F. Conclusion:
are connected to each other. We learn Conditional Proba- This study shows how Bayesian networks and Naive
bility Tables from the data to describe these connections. Bayes classifiers can be used to predict student outcomes
Using these, we can predict the grade in PH100 based based on their grades in different courses. The Naive
on the known grades in EC100, IT101, and MA101. Bayes classifier is simple and does a decent job at
For example, we use Bayesian inference to calculate predicting whether a student qualifies for an internship,
the possible grades for PH100, given that a student has but its assumption that grades are independent can affect
received DD in EC100, CC in IT101, and CD in MA101 its accuracy. On the other hand, the Bayesian network,
C. Naive Bayes Classifier for Internship Qualification: which considers how courses are connected, offers a
The Naive Bayes classifier assumes that the grades more accurate and understandable method.
10

VII. C ONCLUSION
In this lab report, we explored various AI problem-solving
techniques across different challenges. The Missionary-
Cannibal and Rabbit Leap problems were successfully
solved using BFS and DFS, with BFS guaranteeing optimal
solutions and DFS offering memory efficiency. The 8 Puzzle
and Plagiarism Detection were addressed using the A* al-
gorithm, demonstrating the power of heuristic-driven search
for optimizing problem-solving. We delved into Heuristic
Search Problems, emphasizing the role of informed search
strategies in complex scenarios. The Traveling Salesman
Problem (TSP) highlighted the importance of optimization
in pathfinding. Finally, we used Bayesian and Naive Bayes
Models to predict student grades and internship qualifications,
showcasing the application of probabilistic models in educa-
tional and career analytics. Overall, the report illustrated the
versatility and effectiveness of different AI methods in solving
a wide range of problems.

ACKNOWLEDGMENT
We would like to express our heartfelt gratitude to everyone
who contributed to the success of this lab. We are especially
thankful to our professors and mentors for their insightful
guidance and unwavering support throughout our research.
Their expertise was invaluable in helping us navigate com-
plex topics in AI, heuristic search techniques, and predictive
modeling.
Working on a variety of challenging problems allowed us
to gain practical experience and deepen our understanding
of advanced algorithms, optimization methods, and machine
learning models. We also appreciate our academic institution
for providing access to essential resources and facilities that
made this research possible.
This lab has been an enriching learning experience, and we
are grateful for the encouragement and academic direction that
helped us develop our skills in these areas.

R EFERENCES
[1] Russell, S., Norvig, P.
Missionary Cannibal and Rabbit Leap Problem.
Artificial Intelligence: A Modern Approach, 3rd ed., 2010.

You might also like