0% found this document useful (0 votes)
26 views32 pages

NNAI File-3

Uploaded by

kkalpanaa02
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)
26 views32 pages

NNAI File-3

Uploaded by

kkalpanaa02
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/ 32

INDIRA GANDHI DELHI TECHNICAL

UNIVERSITY FOR WOMEN

LAB FILE
Neural Network and Artificial Intelligence(BAI-205)
B.Tech , Electronic and Communication Engineering-
Artificial Intelligence (2021-2025)

Submitted to: Submitted by:


Ms. Vaishali Kikan Akanksha Rani
10601182021
ECE-AI(2), GP-1

1
INDEX
S TITLE DATE SIGN
NO.
1 Introduction to artificial intelligence and its 08-09-2022
applications.

2 Write a program to implement the hill climbing 15-09-2022


techniques.

3 Write a program to implement the BFS search


22-09-2022
method.

4 Write a program to solve the monkey banana 22-09-2022


problem.

5 Write a program to implement inform A* search


29-09-2022
method.

6 Write a program to implement a Tic-Tac-Toe game. 06-10-2022

7 Write a program to show the back propagation 06-10-2022


network for the X-OR function.

8 Write a program to find how the Perceptron learning 03-11-2022


rule works for linearly separable problems.

9 Write a program to implement an artificial neural 03-11-2022


network with backpropagation.

10 Implement a decision tree for restaurant waiting


10-11-2022
problem.

2
EXPERIMENT-1
AIM: Introduction to artificial intelligence and its applications.

THEORY:
What is artificial intelligence?
Artificial intelligence is the simulation of human intelligence processes by machines,
especially computer systems.
Why artificial intelligence:
With the help of AI, you can create your own virtual assistant, such as
cortana, google assistant, siri etc.
With the help of AI, you can build such robots which can work in an
environment where the survival of humans can be at risk.
AI opens a path for other new technologies, new devices, and new
opportunities.
Applications of artificial intelligence:

❖ AI in astronomy: AI can be very useful to solve complex universe


problems. AI technology can be helpful for understanding the universe
such as how it works, origin, etc.
❖ AI in healthcare: healthcare industries are playing AI to make a better and
faster diagnosis than humans. AI can help doctors with diagnoses and can
inform when patients are worsening so that medical help can reach to the
patient before hospitalization.
❖ AI in gaming: AI can be used for gaming purpose. The AI machines can play
strategics games like chess, where the machine needs to think of a large
number of possible places.
❖ AI in finance: AI and finance industries are the best matches for each
other. The finance industry is implementing automation, chatbot, adaptive
intelligence, algorithm trading, and machine learning into financial
processes.
❖ AI in data security: the security of data is crucial for every company and
cyber- attacks are growing very rapidly in the digital worlds. AI can be used
to make your data more safe and secure.
❖ AI in entertainment: we are currently using AI based applications in our daily
life with some entertainment services show the recommendations for programs
or shows.

3
Advantages:
It defines a more powerful and more useful computers.
It introduces a new and improved interface for human interaction.
It introduces a new technique to solve new problems.
It handles the information better than humans.
It is very helpful for the conversion of information into knowledge.
It improves work efficiency so reduce the duration of time to accomplish a task in
comparison to humans.
Disadvantages:
The implementation cost of AI is very high.
The difficulties with software development for AI implementation are
that the development of software is slow and expensive.
A robot is one of the implementations of AI with replacing jobs and lead to
serve unemployment.
Machines can easily lead to destruction if the implementation of machine put
in the wrong hands the results are hazardous for human beings.
Scope in AI:
The adoption of artificial intelligence in India is promising. However, currently, it is
at a nascent stage. While there are a few industries such as IT, manufacturing,
automobile, etc, that are leveraging the prowess of AI, there are still many areas in
which its potential is unexplored.
The immense potential that AI holds can be understood by the various other
technologies that are covered under the umbrella of AI. Some off the examples of
such technologies include self-improving algorithms, machine learning, pattern
recognition, etc. in the next few years, it is predicted that there will hardly be any
industry left untouched by this powerful tool. This is the reason why AI has so
much potential to grow in India.
B)- Python program

4
OUTPUT:

5
EXPERIMENT-2
AIM: Write a program to implement the hill climbing techniques.
THEORY:
Hill climbing algorithm is a local search algorithm which continuously moves
in the direction of increasing elevation/value to find the peak of the mountain
or best solution to the problem. It terminates when it reaches a peak value
where no neighbor has a higher value.
Hill climbing algorithm is a technique which is used for optimizing the
mathematical problems. One of the widely discussed examples of Hill
climbing algorithm is Traveling-salesman Problem in which we need to
minimize the distance traveled by the salesman.
It is also called greedy local search as it only looks to its good immediate
neighbor state and not beyond that.
A node of hill climbing algorithm has two components which are state and value.
Hill Climbing is mostly used when a good heuristic is available.
In this algorithm, we don't need to maintain and handle the search tree or
graph as it only keeps a single current state.

FEATURES OF HILL CLIMBING:


Following are some main features of Hill Climbing Algorithm:
Generate and Test variant: Hill Climbing is the variant of Generate and
Test method. The Generate and Test method produce feedback which
helps to decide which direction to move in the search space.
Greedy approach: Hill-climbing algorithm search moves in the direction
which optimizes the cost.
No backtracking: It does not backtrack the search space, as it does not
remember the previous states.

STATE SPACE DIAGRAM FOR HILL CLIMBING:


The State-space diagram is a graphical representation of the set of states(input) our
search algorithm can reach vs the value of our objective function. Here;
1. The X-axis denotes the state space i.e., states or configuration our algorithm may reach.
2. The Y-axis denotes the values of objective function corresponding to a particular
state. The best solution will be that state space where objective function has
maximum value or global maxima.

6
ALGORITHM FOR HIL CLIMBING TECHNIQUES:
Step 1: define the current state (initial state)
Step 2: apply the loop until goal is achieved or no more operations can be applied
to the current state.
• Apply a heuristic function or operation to the current state and get a new state.
• Compare the new state with goal state.
• Quit if the goal is achieved
• If the new state is closer to the goal, state that update the current state to the
new state
• If the new state is closer to the goal, state, then update the current state to the
new state.
h(x)=+1 If the element is at desired place.
h(x)=-1 If the element is not at desired place.

7
OUTPUT:

8
EXPERIMENT-3
AIM: Write a program to implement the BFS search method.

THEORY:
Breadth-First Search (BFS) is an algorithm used for traversing graphs or trees. Traversing
means visiting each node of the graph. Breadth-First Search is a recursive algorithm to search
all the vertices of a graph or a tree.
A standard BFS implementation puts each vertex of the graph into one of two categories:
Visited Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.

BFS ALGORITHM:
1. Start by putting any one of the graph's vertices at the back of a queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the
visited list to the back of the queue.
4. Keep repeating steps 2 and 3 until the queue is empty.
The graph might have two different disconnected parts so to make sure that we cover
every vertex, we can also run the BFS algorithm on every node.

CODE:

9
OUTPUT:

10
EXPERIMENT-4
AIM: Write a program to solve the monkey banana problem.
THEORY:
Problem Statement
Suppose the problem is as given below –
• A hungry monkey is in a room, and he is near the door.
• The monkey is on the floor.
• Bananas have been hung from the centre of the ceiling of the room.
• There is a block (or chair) present in the room near the window.
• The monkey wants the banana, but cannot reach it.

ALGORITHM:
So if the monkey is clever enough, he can come to the block, drag the block to the center, climb
on it, and get the banana. Below are few observations in this case −
Monkey can reach the block, if both of them are at the same level. From the above image, we
can see that both the monkey and the block are on the floor.
If the block position is not at the center, then monkey can drag it to the center.
If monkey and the block both are on the floor, and block is at the center, then the monkey can
climb up on the block. So the vertical position of the monkey will be changed.
When the monkey is on the block, and block is at the center, then the monkey can get the
bananas.
Now, let us see how we can solve this using Prolog. We will create some predicates as follows
We have some predicates that will move from one state to another state, by performing action.
When the block is at the middle, and monkey is on top of the block, and monkey does not have
the banana (i.e. has not state), then using the grasp action, it will change from has not state
to have state.
From the floor, it can move to the top of the block (i.e. on top state), by performing the
action climb.
The push or drag operation moves the block from one place to another.
Monkey can move from one place to another using walk or move clauses.
Another predicate will be canget(). Here we pass a state, so this will perform move predicate
from one state to another using different actions, then perform canget() on state 2. When we
have reached to the state ‘has>’, this indicates ‘has banana’. We will stop the execution.

11
CODE:

12
OUTPUT:

13
EXPERIMENT-5
AIM: Write a program to implement inform A* search method.

THEORY:
It is a searching algorithm that is used to find the shortest path between an initial and a final
point. It is a handy algorithm that is often used for map traversal to find the shortest path to be
taken. A* was initially designed as a graph traversal problem, to help build a robot that can find
its own course. It still remains a widely popular algorithm for graph traversal. It searches for
shorter paths first, thus making it an optimal and complete algorithm. An optimal algorithm will
find the least cost outcome for a problem, while a complete algorithm finds all the possible
outcomes of a problem. Another aspect that makes A* so powerful is the use of weighted
graphs in its implementation. A weighted graph uses numbers to represent the cost of taking
each path or course of action. This means that the algorithms can take the path with the least
cost, and find the best route in terms of distance and time. A major drawback of the algorithm is
its space and time complexity. It takes a large amount of space to store all possible paths and a
lot of time to find them.
ALGORITHM:

• Initialize the open list.


• Initialize the closed list put the starting node on the open list (you can leave its f at zero).
• While the open list is not empty

A. find the node with the least f on the open list, call it "q".
B. pop q off the open list.
C. generate q's 8 successors and set their parents to q.
D. for each successor:

1. if successor is the goal, stop search


2. else, compute both g and h for successor successor.g = q.g + distance between
3. successor and q successor.h = distance from goal to successor.
4. successor.f = successor.g + successor.h
5. if a node with the same position as successor is in the OPEN list which has a
6. lower f than successor, skip this successor.

• If a node with the same position as successor is in the CLOSED list which has a lower f
than successor, skip this successor otherwise, add the node to the open list end (for loop).
• Push q on the closed list end (while loop).

14
CODE:

15
OUTPUT:

16
EXPERIMENT-6
AIM: Write a program to implement a Tic-Tac-Toe game.
THEORY:
Tic-tac-toe is a very popular game, so let’s implement an automatic Tic-tac-toe game using
Python.
The game is automatically played by the program and hence, no user input is needed. Still,
developing a automatic game will be lots of fun. Let’s see how to do this.
numpy and random Python libraries are used to build this game. Instead of asking the user to
put a mark on the board, code randomly chooses a place on the board and put the mark. It will
display the board after each turn unless a player wins. If the game gets draw, then it returns -1.

ALGORITHM:
play_game() is the main function, which performs following tasks :
• Calls create_board() to create a 9×9 board and initializes with 0.
• For each player (1 or 2), calls the random_place() function to randomly choose a location
on board and mark that location with the player number, alternatively.
• Print the board after each move.
• Evaluate the board after each move to check whether a row or column or a diagonal has
the same player number. If so, displays the winner’s name. If after 9 moves, there are no
winner then displays -1.

CODE:

17
18
19
20
OUTPUT:

21
EXPERIMENT-7
AIM: Write a program to show the back propagation network for the X-OR function.
THEORY:
Implementing logic gates using neural networks help understand the mathematical computation
by which a neural network processes its inputs to arrive at a certain output. This neural network
will deal with the XOR logic problem. An XOR (exclusive OR gate) is a digital logic gate that
gives a true output only when both its inputs differ from each other. The truth table for an XOR
gate is shown below:

Truth Table for XOR

ALGORITHM:
• Initialize the weights and biases randomly.
• Iterate over the data:
a) Compute the predicted output using the sigmoid function
b) Compute the loss using the square error loss function
c) W(new) = W(old) — α ∆W
d) B(new) = B(old) — α ∆B
• Repeat until the error is minimal.

CODE:

22
23
OUTPUT:

24
EXPERIMENT-8
AIM: Write a program to find how the Perceptron learning rule works for linearly separable
problems.

THEORY:
The perceptron model is a more general computational model than McCulloch-Pitts neuron. It
takes an input, aggregates it (weighted sum) and returns 1 only if the aggregated sum is more
than some threshold else returns 0.
A single perceptron can only be used to implement linearly separable functions. It takes both
real and boolean inputs and associates a set of weights to them, along with a bias (the threshold
thing I mentioned above). We learn the weights; we get the function.
ALGORITHM:
We initialize w with some random vector. We then iterate over all the examples in the data,
(P U N) both positive and negative examples. Now if an input x belongs to P, ideally what
should the dot product w.x be? I’d say greater than or equal to 0 because that’s the only thing
what our perceptron wants at the end of the day so let's give it that. And if x belongs to N, the
dot product MUST be less than 0. So if you look at the if conditions in the while loop:
Case 1: When x belongs to P and its dot product w.x < 0
Case 2: When x belongs to N and its dot product w.x ≥ 0
Only for these cases, we are updating our randomly initialized w. Otherwise, we don’t
touch w at all because Case 1 and Case 2 are violating the very rule of a perceptron. So, we are
adding x to w (ahem vector addition ahem) in Case 1 and subtracting x from w in Case 2.

CODE:

25
OUTPUT:

26
27
EXPERIMENT-9
AIM: Write a program to implement an artificial neural network with backpropagation.
THEORY:
The Backpropagation algorithm is a supervised learning method for multilayer feed-forward
networks from the field of Artificial Neural Networks.
Feed-forward neural networks are inspired by the information processing of one or more neural
cells, called a neuron. A neuron accepts input signals via its dendrites, which pass the electrical
signal down to the cell body. The axon carries the signal out to synapses, which are the
connections of a cell’s axon to other cell’s dendrites.
The principle of the backpropagation approach is to model a given function by modifying
internal weightings of input signals to produce an expected output signal. The system is trained
using a supervised learning method, where the error between the system’s output and a known
expected output is presented to the system and used to modify its internal state.

ALGORITHM:
• Initialize Network.
• Forward Propagate.
• Back Propagate Error.
• Train Network.
• Predict.
• Seeds Dataset Case Study.
These steps will provide the foundation that you need to implement the backpropagation
algorithm from scratch and apply it to your own predictive modelling problems.

28
CODE:

OUTPUT:

29
EXPERIMENT-10
AIM: Implement a decision tree for restaurant waiting problem.

THEORY:
A decision tree represents a function that takes as input a vector of attribute values and returns a
“decision”—a single output value. The input and output values can be discrete or continuous.
For now we will concentrate on problems where the inputs have discrete values and the output
has exactly two possible values; this is Boolean classification, where each example input will
be classified as true (a positive example) or false (a negative example). A decision tree reaches
its decision by performing a sequence of tests. Each internal node in the tree corresponds to a
test of the value of one of the input attributes, Ai, and the branches from the node are labelled
with the possible values of the attribute, Ai =vik. Each leaf node in the tree specifies a value to
be returned by the function. The decision tree representation is natural for humans; indeed,
many “How To” manuals (e.g., for car repair) are written entirely as a single decision tree
stretching over hundreds of pages.

ALGORITHM:
Assumptions we make while using Decision tree:
• At the beginning, we consider the whole training set as the root.
• Attributes are assumed to be categorical for information gain and for gini index,
attributes are assumed to be continuous.
• On the basis of attribute values records are distributed recursively.
• We use statistical methods for ordering attributes as root or internal node.
1. Find the best attribute and place it on the root node of the tree.
2. Now, split the training set of the dataset into subsets. While making the subset make sure
that each subset of training dataset should have the same value for an attribute.
3. Find leaf nodes in all branches by repeating 1 and 2 on each subset.

CODE:

30
OUTPUT:

31
32

You might also like