0% found this document useful (0 votes)
17 views22 pages

5.AI Lab-Manual

Lab manual for the artificial intelligence

Uploaded by

jdmochi1278
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)
17 views22 pages

5.AI Lab-Manual

Lab manual for the artificial intelligence

Uploaded by

jdmochi1278
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/ 22

Ahmedabad Institute of Technology

CE & IT Department
Artificial Intelligence (2180703)

Laboratory Manual
Year: 2020-2021

Prepared By: - Prof. Parinda Prajapati


CE Department Vision:
To produce technically sound and ethically responsible Computer Engineers to the society by
providing Quality Education.

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).

OBJECTIVES: The aim of this project is to develop a


 Tic-Tac-Toe game for mobile device. The game is supposed to consist of two
parts, one a single player game (a player against a system), and the other a
multi-player game (two players on their mobile devices, playing against each
other).
 It is aimed to prove the min max algorithm in this game

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:

Implement Tic-Tac-Toe game problem (use any programming language).

EVALUATION:

Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)

Signature with date: ________________

Page 4 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
EXPERIMENT NO: 2 DATE: / /

TITLE: Write a program to implement BFS

OBJECTIVES: On completion of this experiment student will able to…


 Know the Concept of BFS and its working

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

Step Traversal Description

Initialize the queue.

Page 5 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
2

We start from visiting S (starting node),


and mark it as visited.

We then see an unvisited adjacent node


from S. In this example, we have three
nodes but alphabetically we choose A,
mark it as visited and enqueue it.

Next, the unvisited adjacent node


from S is B. We mark it as visited and
enqueue it.

Next, the unvisited adjacent node


from S is C. We mark it as visited and
enqueue it.

Page 6 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
6

Now, S is left with no unvisited adjacent


nodes. So, we dequeue and find A.

From A we have D as unvisited adjacent


node. We mark it as visited and enqueue it

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)

Signature with date: _______________

Page 7 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
EXPERIMENT NO: 3 DATE: / /

TITLE: Write a program to implement DFS

OBJECTIVES: On completion of this experiment student will able to…


 Understand the concept of DFS algorithm and its working.
THEORY:
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles
Depth First Search (DFS) algorithm traverses a graph in a depth ward motion and
uses a stack 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, DFS algorithm traverses from S to A to D to G to E


to B first, then to F and lastly to C. It employs the following rules.

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.

Step Traversal Description

Initialize the stack.

Page 8 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
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.

Mark A as visited and put it onto the stack.


Explore any unvisited adjacent node from A.
Both S and D are adjacent to A but we are
concerned for unvisited nodes only.

Visit D and mark it as visited and put onto the


stack. Here, we have B and C nodes, which
are adjacent to D and both are unvisited.
However, we shall again choose in an
alphabetical order.

We choose B, mark it as visited and put onto


the stack. Here B does not have any unvisited
adjacent node. So, we pop B from the stack.

Page 9 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
6

We check the stack top for return to the


previous node and check if it has any
unvisited nodes. Here, we find D to be on the
top of the stack.

Only unvisited adjacent node is


from D is C now. So we visit C, mark it as
visited and put it onto the stack.

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:

1. Implement DFS algorithm for 8 puzzle problem


2. Implement Water Jug problem (use any programming language).

EVALUATION:
Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)

Signature with date: ________________

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

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.

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: ________________

EXPERIMENT NO: 5 DATE: / /


TITLE: Write a program to solve N-Queens problem (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 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.

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

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:

Implement N-Queens problem (use any programming language).

EVALUATION:

Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)

Signature with date: ________________

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).

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 challenge of 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 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: / /

TITLE: Write a program to Implement A* Algorithm (use any programming language).

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

 g(n) the cost (so far) to reach the node


 h(n) estimated cost to get from the node to the goal
 f(n) estimated total cost of path through n to goal. It is implemented using
priority queue by increasing f(n).

EXCERCISE:

Implement A* Algorithm (use any programming language)..

EVALUATION:

Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)

Signature with date: ________________

Page 16 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703
EXPERIMENT NO: 8 DATE: / /

TITLE: Write a PRO LOG program for Family Relationship

OBJECTIVES: On completion of this experiment student will able to…


 Implement Relationship Tree structure using knowledge base(facts and
rules) using queries
THEORY:

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.

Knowledge Base (Facts & Rules)


Check the following knowledge base used to store the information that appears on a
family tree:
For example:
Facts

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

True or False Queries:


The following queries would return either True or False.
For example:

?-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.

OBJECTIVES: On completion of this experiment student will able to…


 learn concept of Semantic Net and use of Predicates.

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: / /

TITLE: Write the Conceptual Dependency for given statements.

OBJECTIVES: On completion of this experiment student will able to…


 what is conceptual dependency?

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.

The main motivation for the development of CD as a knowledge representation


techniques are given below:

1. To construct computer programs which can understand natural 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

5. To provide a means of representation which are language independent.

Knowledge is represented in CD by elements what are called as conceptual


structures. What forms the basis of CD representation is that for two sentences
which have identical meaning there must be only one representation and implicitly
packed information must be explicitly stated. In order that knowledge is represented
in CD form, certain primitive actions have been developed.

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

2) Conceptual Primitives for Actions

3) Other Primitive Actions

4) Conceptual Roles

5) Conceptual Syntax Rules

6) Conceptual Tenses and States

Example

EXCERCISE:

Write the Conceptual Dependency for following statements.

(a) John gives Mary a book

(b) John gave Mary the book yesterday

EVALUATION:

Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)

Signature with date: ________________

Page 22 of 22
Prepared By: Prof. Parinda Prajapati AI 2180703

You might also like