5.AI Lab-Manual
5.AI Lab-Manual
CE & IT Department
Artificial Intelligence (2180703)
Laboratory Manual
Year: 2020-2021
CE Department Mission:
1) To provide healthy Learning Environment based on current and future Industrial demands.
2) To promote curricular, co-curricular and extra-curricular activities for overall personality
development of the students.
3) To groom technically powerful and ethically dominant engineers having real life problem
solving capabilities.
4) To provide platform for Effective Teaching Learning.
IT Department Vision:
To provide quality education and assistance to the students through innovative teaching learning
methodology for shaping young mind technically sound and ethically strong.
IT Department Mission:
1) To serve society by producing technically and ethically sound engineers.
2) To generate groomed and efficient problem solvers as per Industrial needs by adopting
innovative teaching learning methods.
3) To emphasis on overall development of the students through various curricular, co-curricular
and extra-curricular activities.
INDEX
Page No.
Sr.No. Experiment Date Marks Signature
From To
Write a program to implement Tic-Tac-
1. Toe game problem (use any 3 4
programming language).
Write a program to implement BFS
2. 5 7
(use any programming language).
Write a program to implement DFS
3. 8 10
(use any programming language).
Write a program to implement
4. Tower Of Hanoi Problem (use any 11 11
programming language).
Write a program to solve N-Queens
5. problem (use any programming 12 13
language).
Write a program to solve travelling
6. salesman problem (use any 14 14
programming language).
Write a program to Implement A*
7. Algorithm (use any programming 15 16
language).
Write a PRO LOG program for
8. 17 18
Family Relationship
Convert the Prolog predicates into
9. 19 20
Semantic Net for the given data
Write the Conceptual Dependency for
10. given statements. 21 22
Page 2 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
EXPERIMENT NO: 1 DATE: / /
TITLE: Write a program to implement Tic-Tac-Toe game problem (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:
Page 3 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
EXCERCISE:
EVALUATION:
Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)
Page 4 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
EXPERIMENT NO: 2 DATE: / /
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
Page 5 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
2
Page 6 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
6
EXCERCISE:
Implement BFS algorithm for 8 puzzle problem or Water Jug problem or any AI search
problem (use any programming language).
EVALUATION:
Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)
Page 7 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
EXPERIMENT NO: 3 DATE: / /
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.)
Page 8 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
2
Page 9 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
6
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.
EXCERCISES:
EVALUATION:
Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)
Page 10 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
EXPERIMENT NO: 4 DATE: / /
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
EXCERCISE:
Implement Tower Of Hanoi Problem(use any programming language).
EVALUATION:
Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)
Page 11 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
Signature with date: ________________
THEORY:
The chess queens can attack in any direction as horizontal, vertical, horizontal and
diagonal way.
A binary matrix is used to display the positions of N Queens, where no queens can
attack other queens.
For ex, 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.
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
Page 12 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
In this output, the value 1 indicates the correct place for the queens.
The 0 denotes the blank spaces on the chess board.
EXCERCISE:
EVALUATION:
Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)
Page 13 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
EXPERIMENT NO: 6 DATE: / /
TITLE: Write a program to solve travelling salesman problem (use any programming
language).
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 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.
The problem is a famous NP hard problem. There is no polynomial time know
solution for this problem.
EXCERCISE:
Implement travelling salesman problem (use any programming language).
EVALUATION:
Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)
Page 14 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
Signature with date: ________________
EXPERIMENT NO: 7 DATE: / /
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, it has
“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 below sections.
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
Page 15 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
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 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
EXCERCISE:
EVALUATION:
Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)
Page 16 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
EXPERIMENT NO: 8 DATE: / /
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.
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)
Page 17 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
For example:
Rules
father_of(X,Y):- male(X),
parent_of(X,Y).
mother_of(X,Y):- female(X),
parent_of(X,Y).
Queries
?-mother_of(jess,helen).
?-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).
EXCERCISE: Use a range of queries, similar to the one above to interroagte the
knowledge base and generate the family tree using the example given above.
EVALUATION:
Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)
Page 18 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
Signature with date: ________________
EXPERIMENT NO: 9 DATE: / /
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.
Page 19 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
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 labelled 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.
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))
EXCERCISE:
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 Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)
Page 20 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
Signature with date: ________________
EXPERIMENT NO: 10 DATE: / /
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).
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,
conceptual dependency can be written for any Statement using following data
Page 21 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
1) Conceptual Primitives
4) Conceptual Roles
Example
EXCERCISE:
EVALUATION:
Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)
Page 22 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703