Ai Lab File For Ptu
Ai Lab File For Ptu
AI LAB FILE
SIXTH SEMESTER
PAGE N0. 1
SUSEC 1916810
INDEX
S.NO EXPERIMENTS DATE OF SUB.
1.
2.
3.
4.
5.
6.
7.
PAGE N0. 2
SUSEC 1916810
EXPERIMENT :- 1
Reasoning
Learning
Problem Solving
Perception
Linguistic Intelligence
PAGE N0. 3
SUSEC 1916810
Applications of AI
PAGE N0. 4
SUSEC 1916810
Major Goals
Knowledge reasoning
Planning
Machine Learning
Computer Vision
Robotics
EXPERIMENT :- 2
PAGE N0. 5
SUSEC 1916810
Facts:-
A fact s a predicate expression that makes a declarative statement about the problem domain. Whenever
a variable occurs in a Prolog expression, it is assumed to be universally quantified. Note that all Prolog
sentences must end with a period.
likes(john, susie). /* John likes Susie */
likes(X, susie). /* Everyone likes Susie */
likes(john, Y). /* John likes everybody */
likes(john, Y), likes(Y, john). /* John likes everybody and everybody likes John */
likes(john, susie); likes(john,mary). /* John likes Susie or John likes Mary */
not(likes(john,pizza)). /* John does not like pizza */
likes(john,susie) :- likes(john,mary)./* John likes Susie if John likes Mary.
Rules:-
A rule is a predicate expression that uses logical implication (:-) to describe a relationship among facts.
Thus a Prolog rule takes the form
left_hand_side :- right_hand_side .
This notation is known as a Horn clause. In Horn clause logic, the left hand side of the clause is the
conclusion, and must be a single positive literal. The right hand side contains the premises. The Horn
clause calculus is equivalent to the first-order predicate calculus.
ashish is a boy.
PAGE N0. 6
SUSEC 1916810
PAGE N0. 7
SUSEC 1916810
EXPERIMENT :- 3
self.graph = defaultdict(list)
def addEdge(self,u,v):
self.graph[u].append(v)
while queue:
PAGE N0. 8
SUSEC 1916810
for i in self.graph[s]:
if visited[i] == False:
queue.append(i)
visited[i] = True
PAGE N0. 9
SUSEC 1916810
# Constructor
def __init__(self):
PAGE N0. 10
SUSEC 1916810
visited = set()
self.DFSUtil(v, visited)
PAGE N0. 11
SUSEC 1916810
return H[n]
def a_star_algorithm(self, start_node, stop_node):
open_list = set([start_node])
closed_list = set([])
g = {}
g[start_node] = 0
parents = {}
parents[start_node] = start_node
PAGE N0. 12
SUSEC 1916810
for v in open_list:
if n == None or g[v] + self.h(v) < g[n] + self.h(n):
n = v;
if n == None:
print('Path does not exist!')
return None
if n == stop_node:
reconst_path = []
while parents[n] != n:
reconst_path.append(n)
n = parents[n]
reconst_path.append(start_node)
reconst_path.reverse()
PAGE N0. 13
SUSEC 1916810
parents[m] = n
if m in closed_list:
closed_list.remove(m)
open_list.add(m)
open_list.remove(n)
closed_list.add(n)
PAGE N0. 14
SUSEC 1916810
for v, c in graph[u]:
if visited[v] == False:
visited[v] = True
pq.put((c, v))
print()
addedge(0, 1, 3)
addedge(0, 2, 6)
addedge(0, 3, 5)
PAGE N0. 15
SUSEC 1916810
addedge(1, 4, 9)
addedge(1, 5, 8)
addedge(2, 6, 12)
addedge(2, 7, 14)
addedge(3, 8, 7)
addedge(8, 9, 5)
addedge(8, 10, 6)
addedge(9, 11, 1)
addedge(9, 12, 10)
addedge(9, 13, 2)
source = 0
target = 9
best_first_search(source, target, v)
PAGE N0. 16
SUSEC 1916810
EXPERIMENT :- 4
Write a programme to conduct game search
#include <iostream>
#include <iomanip>
#include <array> using namespace std;
void printarray(int num[3][3])
{
for(int i = 0;i < 3; i++)
{
for(int j = 0; j< 3; j++)
{
cout << num[i][j] << " ";
}
cout << "\n";
}
}
PAGE N0. 17
SUSEC 1916810
}
else if(i==1&&j==2)
{
swapValues(array, i, j, i,
j-1); swapValues(array,
i, j, i, j-2);
}
else if(i==0&&j==0)
{
swapValues(array, i, j+2,
i, j); swapValues(array,
i, j+1, i, j);
}
else if(i==0&&j==2)
{
swapValues(array, i, j, i, j);
}
else
swapValues(array, i, j, i-
1, j); swapValues(array,
i, j, i+1, j);
}
PAGE N0. 18
SUSEC 1916810
swapValues(array, i+1, j,
i, j); swapValues(array,
i-1, j, i, j);
}
else if(i==0&&j==2)
{
swapValues(array, i, j, i+1, j);
}
else
swapValues(array, i, j, i-
1, j); swapValues(array,
i, j, i+1, j);
}
else
swapValues(array, i, j, i-
1, j); swapValues(array,
i, j, i+1, j);
}
PAGE N0. 20
SUSEC 1916810
swapValues(array, i, j, i-
1, j); swapValues(array,
i, j, i+2, j);
//int coord1d = j * 3 + i;
}
class Puzzle
{
public:
Puzzle();
void swapValues(int num[3][3], int i1, int
j1, int i2, int j2); //void moveleft(int[3][3],
int start1, int start2); void moveup(int
(&array)[3][3], int i, int j);
void moveright(int (&array)[3][3],
int i, int j); void moveleft(int
(&array)[3][3], int i, int j); void
movedown(int (&array)[3][3], int i,
int j); void printarray(int[3][3]);
};
int main()
{
int state1[3][3] = {{2, 8, 3}, {1, 6, 4}, {7, 0, 5}};
int state2[3][3] = {{2, 8, 1}, {4, 6, 3}, {0, 7, 5}};
int gstate[3][3] = {{1, 2, 3}, {8, 0, 4}, {7, 6, 5}};
cout << "this is state 1"
<< endl;
printarray(state1);
cout << "\n\n";
cout << "now this is state 2"
<< endl; printarray(state2);
cout << "\n\n";
cout << "now this is the goal state"
<< endl;; printarray(gstate);
int start1, start2;
PAGE N0. 21
SUSEC 1916810
cout << endl << endl << "please enter your starting point in the states
listed above going up or down" << endl;
cin >> start1;
cout << "now enter the point going left
to right" << endl; cin >> start2;
//moveleft(state1, start1, start2);
cout << "this is moving coordinate (1, 1)
upwards" << endl; moveup(state1, start1,
start2);
printarray(state
1); cout <<
endl;
cout << "this is moving coordinate (1, 1) to
the left" << endl; moveleft(state1, start1,
start2);
printarray(state
1); cout <<
endl;
cout << "this is moving coordinate (1, 1)
downwards" << endl; movedown(state1,
start1, start2);
printarray(state
1); cout <<
endl;
cout << "this is moving coordinate (1, 1) to the
right" << endl; moveright(state1, start1, start2);
printarray(state
1); cout <<
endl; cin.get();
cin.get();
return 0;
}
PAGE N0. 22
SUSEC 1916810
PAGE N0. 23
SUSEC 1916810
EXPERIMENT :- 5
heartDisease = pd.read_csv('heart.csv')
heartDisease = heartDisease.replace('?',np.nan)
model= BayesianModel([('age','heartdisease'),('sex','heartdisease'),
('exang','heartdisease'),('cp','heartdisease'),('heartdisease','restecg'),
('heartdisease','chol')])
print('\nLearning CPD using Maximum likelihood estimators')
model.fit(heartDisease,estimator=MaximumLikelihoodEstimator)
PAGE N0. 24
SUSEC 1916810
OUTPUT:-
PAGE N0. 25
SUSEC 1916810
EXPERIMENT :- 6
PAGE N0. 26
SUSEC 1916810
PAGE N0. 27
SUSEC 1916810
EXPERIMENT :- 7
PAGE N0. 28
SUSEC 1916810
action = ""
OUTPUT:-
PAGE N0. 29