5.AI Lab-Manual
5.AI Lab-Manual
: 210020116018
Laboratory Manual
Year: 2024
1
AI(3161608)
En. No.: 210020116018
INDEX
Page No.
Sr.No. Experiment Date Marks Signature
From To
2
AI(3161608)
En. No.: 210020116018
TITLE: Write a program to implement Tic-Tac-Toe game problems (use any programming
language).
Theory:
The game Tic Tac Toe is also known as Noughts and Crosses or Xs and Os ,the player
needs to take turns marking the spaces in a 3x3 grid with their own marks,if 3 consecutive
marks (Horizontal, Vertical,Diagonal) are formed then the player who owns these moves
get won.
Assume ,
Player 1 - X
Player 2 - O
So,a player who gets 3 consecutive marks first,they will win the game .
Board's Data Structure:
3
AI(3161608)
En. No.: 210020116018
EXERCISE:
EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)
4
AI(3161608)
En. No.: 210020116018
5
AI(3161608)
En. No.: 210020116018
THEORY:
Breadth First Search (BFS) algorithm traverses a graph in a breadth ward motion and uses
a queue to remember to get the next vertex to start a search, when a dead end occurs in any
iteration As in the example given above, BFS algorithm traverses from A to B to E to F first then
to C and G lastly to D. It employs the following rules.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue.
Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty
6
AI(3161608)
En. No.: 210020116018
7
AI(3161608)
En. No.: 210020116018
EXERCISE:
Implement BFS algorithm for 8 puzzle problems or Water Jug problems or any AI search
problem (use any programming language).
EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)
8
AI(3161608)
En. No.: 210020116018
9
AI(3161608)
En. No.: 210020116018
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a stack.
Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all
the vertices from the stack, which do not have adjacent vertices.) Rule 3 − Repeat Rule 1
and Rule 2 until the stack is empty.
2
Mark S as visited and put it onto the stack. Explore
any unvisited adjacent node from S. We have three
nodes and we can pick any of them. For this
example, we shall take the node in an alphabetical
order.
10
AI(3161608)
En. No.: 210020116018
11
AI(3161608)
En. No.: 210020116018
As C does not have any unvisited adjacent node so we keep popping the stack until we find a node
that has an unvisited adjacent node. In this case, there's none and we keep popping until the stack is
empty.
EXERCISES:
EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)
12
AI(3161608)
En. No.: 210020116018
13
AI(3161608)
En. No.: 210020116018
TITLE: Write a program to implement Tower Of Hanoi Problem (use any programming
language).
OBJECTIVES: The objective of the puzzle is to move the entire stack to another rod,
obeying the simple rules
THEORY:Tower of Hanoi is a mathematical puzzle where we have three rods and n disks.
1) Only one disk can be moved at a time.
2) Each move consists of taking the upper disk from one of the stacks and placing it on
top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
3) No disk may be placed on top of a smaller disk. With 3 disks, the puzzle can be solved
in 7 moves. The minimal number of moves required to solve a Tower of Hanoi puzzle is 2n
− 1, where n is the number of disks.
EXERCISE:
Implement Tower Of Hanoi Problem(use any programming language).
EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)
14
AI(3161608)
En. No.: 210020116018
15
AI(3161608)
En. No.: 210020116018
TITLE: Write a program to solve N-Queens problems (use any programming language).
OBJECTIVES: This problem is to find an arrangement of N queens on a chess board, such that no
queen can attack any other queens on the board.
THEORY:
The chess queens can attack in any direction as horizontal, vertical, horizontal and diagonal.
A binary matrix is used to display the positions of N Queens, where no queens can attack other
queens.
For example, the Eight queens problem is a constraint satisfaction problem. The task is to place
eight queens in the 64 available squares in such a way that no queen attacks each other.
So the problem can be formulated with variables x1,x2,x3,x4,x5,x6,x7,x8 and
y1,y2,y3,y4,y5,y6, y7,y8; the xs represent the rows and ys the column. Now a solution for this
problem is to assign values for x and for y such that the constraint is satisfied
Input:
The size of a chess board. Generally, it is 8. as (8 x 8 is the size of a normal chess board.
Output:
The matrix that represents in which row and column the N Queens can be placed.
If the solution does not exist, it will return false.
10000000
00000010
00001000
00000001
01000000
00010000
00000100
00100000
In this output, the value 1 indicates the correct place for the queens.
The 0 denotes the blank spaces on the chess board.
EXERCISE:
Implement N-Queens problem (use any programming language).
EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)
16
AI(3161608)
En. No.: 210020116018
17
AI(3161608)
En. No.: 210020116018
TITLE: Write a program to solve traveling salesman problems (use any programming
language).
OBJECTIVES: The traveling salesman problem consists of a salesman and a set of cities.
The salesman has to visit each one of the cities starting from a certain one (e.g. the
hometown) and returning to the same city. The problem is that the traveling salesman wants
to minimize the total length of the trip.
THEORY:
The traveling salesman problem can be described as
follows: TSP = {(G, f, t): G = (V, E) a complete graph, f
is a function V×V → Z, t ∈ Z,
G is a graph that contains a traveling salesman tour with a cost that does not exceed t}.
For example, consider the graph shown in figure . A TSP tour in the graph is 1-2-4-3-1. The
cost of the tour is 10+25+30+15 which is 80.
EXERCISE:
Implement traveling salesman problems (use any programming language).
EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)
18
AI(3161608)
En. No.: 210020116018
19
AI(3161608)
En. No.: 210020116018
OBJECTIVES: A* Search algorithm is one of the best and popular techniques used in path-
finding and graph traversals
THEORY:
A* is the most popular choice for pathfinding, because it’s fairly flexible and can be used in a
wide range of contexts.
Informally speaking, A* Search algorithms, unlike other traversal techniques, have
“brains”. What it means is that it is really a smart algorithm which separates it from the
other conventional algorithms. This fact is cleared in detail in the sections below.
And it is also worth mentioning that many games and web-based maps use this algorithm to
find the shortest path very efficiently (approximation).
A* is like Dijkstra’s Algorithm in that it can be used to find a shortest path. A* is like Greedy
Best-First-Search in that it can use a heuristic to guide itself. In the simple case, it is as fast as
Greedy Best-First-Search:
In the example with a concave obstacle, A* finds a path as good as what Dijkstra’s Algorithm
found:
The secret to its success is that it combines the pieces of information that Dijkstra’s
Algorithm uses (favoring vertices that are close to the starting point) and information that
Greedy Best-First-Search uses (favoring vertices that are close to the goal). In the standard
terminology used when talking about A*, g(n) represents the exact cost of the path from
the starting point to any vertex n, and h(n) represents the heuristic estimated cost from
vertex n to the goal. In the above diagrams, the yellow (h) represents vertices far from the
goal and teal (g) represents vertices far from the starting point. A* balances the two as it
moves from the starting point to the goal. Each time through the main loop, it examines the
vertex n that has the lowest f(n) = g(n) + h(n).
t is the best-known form of Best First search. It avoids expanding paths that are already expensive,
but expands most promising paths first.
f(n) = g(n) + h(n), where
20
AI(3161608)
En. No.: 210020116018
EXERCISE:
EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)
21
AI(3161608)
En. No.: 210020116018
22
AI(3161608)
En. No.: 210020116018
Prolog is a language built around the Logical Paradigm: a declarative approach to problem-
solving. There are only three basic constructs in Prolog: facts, rules, and queries.A
collection of facts and rules is called a knowledge base (or a database) and Prolog
programming is all about writing knowledge bases. That is, Prolog programs simply are
knowledge bases, collections of facts and rules which describe some collection of
relationships that we find interesting.So how do we use a Prolog program? By posing
queries. That is, by asking questions about the information stored in the knowledge base.
The computer will automatically find the answer (either True or False) to our queries.
male(harry).
female(helen).
female(sophie).
female(jess).
female(lily).
parent_of(jess, simon).
parent_of(ali, simon).
parent_of(lily, harry).
parent_of(james, harry)
For example:
Rules
father_of(X,Y):- male(X),
parent_of(X,Y).
mother_of(X,Y):- female(X),
parent_of(X,Y).
Queries
True or False Queries:
The following queries would return either True or False.
For example:
?-mother_of(jess,helen).
23
AI(3161608)
En. No.: 210020116018
?-brother_of(james,simon).
?-ancestor_of(jack,simon).
Other Queries: The following queries would return a solution which could be either:
A unique value
A list of values
False if no solution can be found
For example:
?-
mother_of(X,jess).
?-parent_of(X,simon).
?-sister_of(X,lily).
?-ancestor_of(X,lily).
EXERCISE: Use a range of queries, similar to the one above to interrogate the knowledge base
and generate the family tree using the example given above.
EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)
24
AI(3161608)
En. No.: 210020116018
25
AI(3161608)
En. No.: 210020116018
TITLE: Convert the Prolog predicates into Semantic Net for the given data.
THEORY:
Semantic Nets:
Semantic networks are an alternative to predicate logic as a form of knowledge
representation. The idea is that we can store our knowledge in the form of a graph, with
nodes representing objects in the world, and arcs representing relationships between those
objects.
For example, the following:
is intended to represent the
data:
Tom is a cat.
Tom caught a bird.
Tom is owned by John.
Tom is ginger in colour.
Cats like cream.
The cat sat on the mat.
A cat is a mammal.
A bird is an animal.
All mammals are animals.
Mammals have fur.
It is argued that this form of representation is closer to the way humans structure knowledge
by building mental links between things than the predicate logic we considered earlier.
some confusion here which stems from the imprecise nature of semantic nets. A particular
problem is that we haven’t distinguished between nodes representing classes of things, and
nodes representing individual objects. So, for example, the node labeled Cat represents both
the single (nameless) cat who sat on the mat, and the whole class of cats to which Tom
belongs, which are mammals and which like cream. The is_a link has two different
meanings – it can mean that one object is an individual item from a class, for example Tom
is a member of the class of cats, or that one class is a subset of another, for example, the
class of cats is a subset of the class of mammals.
26
AI(3161608)
En. No.: 210020116018
This confusion does not occur in logic, where the use of quantifiers, names and predicates makes
it clear what we mean so:
Tom is a cat is represented by Cat(Tom)
The cat sat on the mat is represented by ∃x∃y(Cat(x)∧Mat(y)∧SatOn(x,y)) A
cat is a mammal is represented by ∀x(Cat(X)→Mammal(x))
EXERCISE:
Convert following Prolog predicates into Semantic
Net cat(tom). cat(cat1). mat(mat1).
sat_on(cat1,mat1).
bird(bird1).
caught(tom,bird1).
like(X,cream) :– cat(X).
mammal(X) :– cat(X).
has(X,fur) :– mammal(X).
animal(X) :– mammal(X).
animal(X) :– bird(X).
owns(john,tom).
is_coloured(tom,ginger).
EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)
27
AI(3161608)
En. No.: 210020116018
28
AI(3161608)
En. No.: 210020116018
THEORY:
Frames and scripts, as knowledge representation schemes take into account context and
relationships. These techniques also provide a useful formulation for representing more
complex structures such as objects, scenes and multiple-sentence stories. One of the key
ideas of the script approach is to reduce a sentence or story to a set of semantic primitives
using a formalism called conceptual dependency (CD).
Conceptual dependency (CD) is a theory of natural language processing which mainly deals
with representation of semantics of a language.
2. To make inferences from the statements and also to identify conditions in which two sentences
can have similar meaning,
3. To provide facilities for the system to take part in dialogues and answer questions,
4. To provide a necessary plank that sentences in one language can be easily translated into other
languages, and
conceptual dependency can be written for any Statement using following data
1) Conceptual Primitives
2) Conceptual Primitives for Actions
3) Other Primitive Actions
4) Conceptual Roles
5) Conceptual Syntax Rules
6) Conceptual Tenses and States
29
AI(3161608)
En. No.: 210020116018
Example
EXERCISE:
EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)
30
AI(3161608)
En. No.: 210020116018
31
AI(3161608)