Prelab work AI(KCS751A) Student
Prelab work AI(KCS751A) Student
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:
What is Prolog?
• Prolog stands for programming in logic.
• Prolog is a declarative language, which means that a program consists of data
based on the facts and rules (Logical relationship) rather than computing how
to find a solution.
• A logical relationship describes the relationships which hold for the given
application.
• To obtain the solution, the user asks a question rather than running a program.
When a user asks a question, then to determine the answer, the run time
system searches through the database of facts and rules.
• Prolog is a declarative language that means we can specify what problem we
want to solve rather than how to solve it.
• Prolog is used in some areas like database, natural language processing,
artificial intelligence, but it is pretty useless in some areas like a numerical
algorithm or instance graphics.
• In artificial intelligence applications, prolog is used. The artificial intelligence
applications can be automated reasoning systems, natural language interfaces,
and expert systems. The expert system consists of an interface engine and a
database of facts. The prolog's run time system provides the service of an
interface engine.
Applications of Prolog
• Specification Language
• Robot Planning
• Natural language understanding
• Machine Learning
• Problem Solving
• Intelligent Database retrieval
• Expert System
• Automated Reasoning
Starting Prolog
• Prolog system is straightforward.
• Prolog will produce a number of lines of headings in the starting, which is
followed by a line. It contains just
• ?-
• The above symbol shows the system prompt. The prompt is used to show that
the Prolog system is ready to specify one or more goals of sequence to the
user.
• Using a full stop, we can terminate the sequence of goals.
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 1
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:
• For example:
• ?- write('Welcome to AI lab Class'),nl,write('Example of Prolog'),nl.
• nl indicates 'start a new line'. When we press 'return' key, the above line will
show the effect like this:
Welcome to AI Lab Class
Example of Prolog
yes
• ?- prompt shows the sequence of goal which is entered by the user. The user
will not type the prompt.
• Prolog system will automatically generate this prompt. It means that it is ready
to receive a sequence of goals.
• To show that the goals have succeeded, we will output yes.
• 'Query' is a sequence of one or more goals.
Prolog Programs
• To write a Prolog program, firstly, the user has to write a program which is
written in the Prolog language, load that program, and then specify a sequence
of one or more goals at the prompt.
• To create a program in Prolog, the simple way is to type it into the text editor
and then save it as a text file like prolog1.pl.
• The following example shows a simple program of Prolog. The program
contains three components, which are known as clauses. Each clause is
terminated using a full stop.
dog(rottweiler).
cat(munchkin).
animal(A) :- cat(A).
• Using the built-in predicate 'consult', the above program can be loaded in the
Prolog system.
• ?-consult('prolog1.pl').
• This shows that prolog1.pl file exists, and the prolog program is systemically
correct, which means it has valid clauses, the goal will succeed, and to confirm
that the program has been correctly read, it produces one or more lines of
output. e.g.,
• ?-
# 0.00 seconds to consult prolog1.pl
?-
• The alternative of 'consult' is 'Load', which will exist on the menu option if the
Prolog system has a graphical user interface.
• When the program is loaded, the clause will be placed in a storage area, and
that storage area is known as the Prolog database. In response to the system
prompt, specify a sequence of goals, and it will cause Prolog to search for and
use the clauses necessary to evaluate the goals.
Terminology
In the following program, three lines show the clauses.
dog(rottweiler).
cat(munchkin).
animal(A) :- cat(A).
Using the full stop, each clause will be terminated. Prolog programs have a
sequence of clauses. Facts or rules are described by these clauses.
Experiment-1 (B)
OBJECTIVE: Write simple fact for the statements using PROLOG.
Facts
Queries
In Prolog, the query is the action of asking the program about the information which
is available within its database. When a Prolog program is loaded, we will get the
query prompt,
?-
After this, we can ask about the information to the run time system. Using the above
simple database, we can ask a question to the program like
?- 'It is sunny'.
and it will give the answer
yes
?-
The system responds to the query with yes if the database information is consistent to
answer the query. Using the available database information, we can also check that
the program is capable of proving the query true. No indicates that the fact is not
deducible based on the available information.
The system answers no to the query if the database does not have sufficient
information.
?- 'It is cold'.
no
?-
Output:
Example 2.
Statements:
Statements in Prolog:
Program Clauses:
Screenshot Output-
Experiment-2 (A)
OBJECTIVE: Write predicates One converts centigrade temperatures to
Fahrenheit, the other checks if a temperature is below freezing.
Rule
Centigrade to Fahrenheit (c_to_f)F is C * 9 / 5 + 32
Program-
Output:
Screenshot Output-
Experiment-2 (B)
OBJECTIVE: Write a program to solve the Monkey Banana problem.
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 6
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:
Program:
OUTPUT:
Screenshot Output:
Experiment-3
OBJECTIVE: Write a program to design medical diagnosis expert system in SWI
Prolog. The system must work for diagnosis of following diseases:-
a) measles
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 7
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:
b) germanmeasles
c) Flu
d) commoncold
e) mumps
f). chickenpox
Program Code:
OUTPUT:
Screenshot Output:
Experiment-4 (A)
OBJECTIVE: WAP to implement factorial, Fibonacci of a given number.
Program-
Output-
Scrennshot output:
The Fibonacci sequence is a sequence where the next term is the sum of the previous
two terms. The first two terms of the Fibonacci sequence are 0 followed by 1.
Program-
Output:
Scrennshot output:
Experiment-4 (B)
OBJECTIVE: Write a program to solve 4-Queen problem.
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 9
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:
N queens problem is one of the most common examples of backtracking. Our goal is
to arrange N queens on an NxN chessboard such that no queen can strike down any
other queen. A queen can attack horizontally, vertically, or diagonally.
So, we start by placing the first queen anywhere arbitrarily and then place the next
queen in any of the safe places. We continue this process until the number of unplaced
queens becomes zero (a solution is found) or no safe place is left. If no safe place is
left, then we change the position of the previously placed queen.
The above picture shows a 4x4 chessboard and we have to place 4 queens on it.
So, we will start by placing the first queen in the first row.
Now, the second step is to place the second queen in a safe position. Also, we
can't place the queen in the first row, so we will try putting the queen in the
second row this time.
Let's place the third queen in a safe position, somewhere in the third row.
Now, we can see that there is no safe place where we can put the last queen.
So, we will just change the position of the previous queen i.e., backtrack and
change the previous decision.
Also, there is no other position where we can place the third queen, so we will
go back one more step and change the position of the second queen.
And now we will place the third queen again in a safe position other than
the previously placed position in the third row.
We will continue this process and finally, we will get the solution as shown
below.
Program:
OUTPUT:
Screenshot Output:
Experiment-5
OBJECTIVE: Write a program to solve traveling salesman problem.
Given a set of cities and distances between every pair of cities, the problem is to find
the shortest possible route that visits every city exactly once and returns to the starting
point.
Note the difference between Hamiltonian Cycle and TSP. The Hamiltonian cycle
problem is to find if there exists a tour that visits every city exactly once. Here we
know that Hamiltonian Tour exists (because the graph is complete) and in fact, many
such tours exist, the problem is to find a minimum weight Hamiltonian Cycle.
For example, consider the graph shown in the figure on the right side. 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.
Program:
Output
Screenshot Output:
Experiment-6
OBJECTIVE: Write a program to solve water jug problem.
Problem: You are given two jugs, a 4-gallon one and a 3-gallon one. Neither has any
measuring mark on it. There is a pump that can be used to fill the jugs with water.
How can you get exactly 2 gallons of water into the 4-gallon jug.
Solution:
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 15
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:
• The state space for this problem can be described as the set of ordered pairs of
integers (x,y)
• Where,
• X represents the quantity of water in the 4-gallon jug X= 0,1,2,3,4
• Y represents the quantity of water in 3-gallon jug Y=0,1,2,3
• Start State: (0,0)
• Goal State: (2,0)
Production Rules:
OUTPUT:
Screenshot Output:
Experiment-7
OBJECTIVE: Write a Python program to Support vector Machine algorithm.
SVM is one of the most popular Supervised Learning algorithms, used for
Classification as well as Regression problems. However, primarily, it is used for
Classification problems in Machine Learning.
The goal of the SVM algorithm is to create the best line or decision boundary that can
segregate n-dimensional space into classes so that we can easily put the new data
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 16
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:
point in the correct category in the future. This best decision boundary is called a
hyperplane.
SVM chooses the extreme points/vectors that help in creating the hyperplane. These
extreme cases are called as support vectors, and hence algorithm is termed as
Support Vector Machine. Consider the below diagram in which there are two different
categories that are classified using a decision boundary or hyperplane:
Types of SVM
SVM can be of two types:
o Linear SVM: Linear SVM is used for linearly separable data, which means if
a dataset can be classified into two classes by using a single straight line, then
such data is termed as linearly separable data, and classifier is used called as
Linear SVM classifier.
o Non-linear SVM: Non-Linear SVM is used for non-linearly separated data,
which means if a dataset cannot be classified by using a straight line, then
such data is termed as non-linear data and classifier used is called as Non-
linear SVM classifier.
Hyperplane and Support Vectors in the SVM algorithm:
o Hyperplane: There can be multiple lines/decision boundaries to segregate the
classes in n-dimensional space, but we need to find out the best decision
boundary that helps to classify the data points. This best boundary is known as
the hyperplane of SVM.
The dimensions of the hyperplane depend on the features present in the
dataset, which means if there are 2 features (as shown in image), then
hyperplane will be a straight line. And if there are 3 features, then hyperplane
will be a 2-dimension plane.
We always create a hyperplane that has a maximum margin, which means the
maximum distance between the data points.
o Support Vectors:
The data points or vectors that are the closest to the hyperplane and which
affect the position of the hyperplane are termed as Support Vector. Since these
vectors support the hyperplane, hence called a Support vector.
Linear SVM:
The working of the SVM algorithm can be understood by using an example. Suppose
we have a dataset that has two tags (green and blue), and the dataset has two features
x1 and x2. We want a classifier that can classify the pair(x1, x2) of coordinates in
either green or blue. Consider the below image:
So as it is 2-d space so by just using a straight line, we can easily separate these two
classes. But there can be multiple lines that can separate these classes. Consider the
below image:
Hence, the SVM algorithm helps to find the best line or decision boundary; this best
boundary or region is called as a hyperplane. SVM algorithm finds the closest point of
the lines from both the classes. These points are called support vectors. The distance
between the vectors and the hyperplane is called as margin. And the goal of SVM is
to maximize this margin. The hyperplane with maximum margin is called the optimal
hyperplane.
Non-Linear SVM:
If data is linearly arranged, then we can separate it by using a straight line, but for
non-linear data, we cannot draw a single straight line. Consider the below image:
So to separate these data points, we need to add one more dimension. For linear data,
we have used two dimensions x and y, so for non-linear data, we will add a third
dimension z. It can be calculated as:
z=x2 +y2
By adding the third dimension, the sample space will become as below image:
So now, SVM will divide the datasets into classes in the following way. Consider the
below image:
Program:
In the model the building part, you can use the cancer dataset, which is a very famous
multi-class classification problem. This dataset is computed from a digitized image of
a fine needle aspirate (FNA) of a breast mass. They describe characteristics of the cell
nuclei present in the image.
This data has two types of cancer classes: malignant (harmful) and benign (not
harmful).
Here, you can build a model to classify the type of cancer. The dataset is available in
the scikit-learn library or you can also download it from the UCI Machine Learning
Library.
Loading Data
Let's first load the required dataset you will use.
Exploring Data
After you have loaded the dataset, you might want to know a little bit more about it.
You can check feature and target names.
You can also check the shape of the dataset using shape.
Splitting Data
To understand model performance, dividing the dataset into a training set and a test
set is a good strategy.
Split the dataset by using the function train_test_split(). you need to pass 3
parameters features, target, and test_set size. Additionally, you can
use random_state to select records randomly.
Subject Code: KCS-751A Subject Name: Artificial Intelligence
Lab
Name of Student: Roll No.: Page 20
ABES Engineering College,
Ghaziabad
Department of Computer Science & Engineering
th
Year/Semester: 4 /VII Section:
Generating Model
Let's build support vector machine model. First, import the SVM module and create
support vector classifier object by passing argument kernel as the linear kernel in
SVC() function.
Then, fit your model on train set using fit() and perform prediction on the test set
using predict().
Experiment-8
OBJECTIVE: Write a python program for Decision tree algorithm.
Introduction: Decision Tree is a Supervised learning technique that can be used for
both classification and Regression problems, but mostly it is preferred for solving
Classification problems. It is a tree-structured classifier, where internal nodes
represent the features of a dataset, branches represent the decision
rules and each leaf node represents the outcome.
Below are the two reasons for using the Decision tree:
1. Decision Trees usually mimic human thinking ability while making a decision,
so it is easy to understand.
2. The logic behind the decision tree can be easily understood because it shows a
tree-like structure.
For the next node, the algorithm again compares the attribute value with the other
sub-nodes and move further. It continues the process until it reaches the leaf node of
the tree. The complete process can be better understood using the below algorithm:
o Step-1: Begin the tree with the root node, says S, which contains the complete
dataset.
o Step-2: Find the best attribute in the dataset using Attribute Selection
Measure (ASM).
o Step-3: Divide the S into subsets that contains possible values for the best
attributes.
o Step-4: Generate the decision tree node, which contains the best attribute.
o Step-5: Recursively make new decision trees using the subsets of the dataset
created in step -3. Continue this process until a stage is reached where you
cannot further classify the nodes and called the final node as a leaf node.
1. Information Gain:
o Information gain is the measurement of changes in entropy after the
segmentation of a dataset based on an attribute.
o It calculates how much information a feature provides us about a class.
o According to the value of information gain, we split the node and build the
decision tree.
o A decision tree algorithm always tries to maximize the value of information
gain, and a node/attribute having the highest information gain is split first. It
can be calculated using the below formula:
1. Information Gain= Entropy(S)- [(Weighted Avg) *Entropy(each feature)
Entropy: Entropy is a metric to measure the impurity in a given attribute. It specifies
randomness in data. Entropy can be calculated as:
Entropy(s)= -P(yes)log2 P(yes)- P(no) log2 P(no)
Where,
o S= Total number of samples
o P(yes)= probability of yes
o P(no)= probability of no
2. Gini Index:
o Gini index is a measure of impurity or purity used while creating a decision
tree in the CART(Classification and Regression Tree) algorithm.
o An attribute with the low Gini index should be preferred as compared to the
high Gini index.
o It only creates binary splits, and the CART algorithm uses the Gini index to
create binary splits.
o Gini index can be calculated using the below formula:
Gini Index= 1- ∑jPj2
PROGRAM:
Output
Experiment-9
OBJECTIVE: Write a python program for Gaussian Naïve Bayes Classifier.
It is a variant of Naive Bayes that follows Gaussian normal distribution and supports
continuous data.
Naive Bayes Classifiers are based on the Bayes Theorem. These classifiers assume
that the value of a particular feature is independent of the value of any other feature.
In a supervised learning situation, Naive Bayes Classifiers are trained very efficiently.
Naive Bayed classifiers need a small training data to estimate the parameters needed
for classification
or both (i.e., σ)
Gaussian Naive Bayes supports continuous valued features and models each as
conforming to a Gaussian (normal) distribution.
of the points within each label, which is all what is needed to define such a
distribution.
The above illustration indicates how a Gaussian Naive Bayes (GNB) classifier works.
At every data point, the z-score distance between that point and each class-mean is
calculated, namely the distance from the class mean divided by the standard deviation
of that class.
Program:
OUTPUT: