0% found this document useful (0 votes)
31 views47 pages

Artificial Intelligence Laboratory

Uploaded by

vsksvskd5
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)
31 views47 pages

Artificial Intelligence Laboratory

Uploaded by

vsksvskd5
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/ 47

S.K.

P Engineering College
Thiruvannamalai

AD3311 - ARTIFICIAL
INTELLIGENCE LABORATORY

LAB MANUAL

B.TECH. ARTIFICIAL
INTELLIGENCE AND DATA
SCIENCE
SYLLABUS

AD3311-ARTIFICIAL INTELLIGENCE LABORATORY

LIST OF EXPERIMENTS:

1. Implement basic search strategies – 8-Puzzle, 8 - Queens problem, Cryptarithmetic.


2. Implement A* and memory bounded A* algorithms
3. Implement Minimax algorithm for game playing (Alpha-Beta pruning)
4. Solve constraint satisfaction problems
5. Implement propositional model checking algorithms
6. Implement forward chaining, backward chaining, and resolution strategies
7. Build naïve Bayes models
8. Implement Bayesian networks and perform inferences
9. Mini-Project
INDEX

Staff
Ex.No Date Name Of The Experiment Page Mark Sign

1A 8 Puzzle Problem 01

1B 8 Queen Problem 08

1C Crypt-Arithmetic Problem 10

2 A* Algorithm 16

3 Min-Max Algorithm 19

4 Constraint Satisfaction Problem 23

5 Propositional Model Checking 26


Algorithm

6 Forward Chaining 29

7 Naïve Bayes Model 32

Bayesian Networks And


8 38
Perform

9
Mini-Project Dog Vs Cat 41
Classification
01

Ex: 1.a 8 PUZZLE PROBLEM

Aim: To implement the 8 Puzzle Problem using python

Algorithm
START
1. In order to maintain the list of live nodes, algorithm LCSearch employs the
functions Least() and Add().
2. Least() identifies a live node with the least c(y), removes it from the list, and
returns it.
3. Add(y) adds y to the list of live nodes.
4. Add(y) implements the list of live nodes as a min-heap.
END

Program

# Importing copy for deepcopy function


import copy

# Importing the heap functions from python


# library for Priority Queue
from heapq import heappush, heappop

# This variable can be changed to change


# the program from 8 puzzle(n=3) to 15
# puzzle(n=4) to 24 puzzle(n=5)...
n=3

# bottom, left, top, right


row = [ 1, 0, -1, 0 ]
col = [ 0, -1, 0, 1 ]

# A class for Priority Queue


class priorityQueue:
02

# Constructor to initialize a
# Priority Queue
def init (self):
self.heap = []

# Inserts a new key 'k'


def push(self, k):
heappush(self.heap, k)

# Method to remove minimum element


# from Priority Queue
def pop(self):
return heappop(self.heap)

# Method to know if the Queue is empty


def empty(self):
if not self.heap:
return True
else:
return False

# Node structure
class node:

def init (self, parent, mat, empty_tile_pos,


cost, level):

# Stores the parent node of the


# current node helps in tracing
# path when the answer is found
self.parent = parent

# Stores the matrix


self.mat = mat
# Stores the position at which the
03

# empty space tile exists in the matrix


self.empty_tile_pos = empty_tile_pos

# Stores the number of misplaced tiles


self.cost = cost

# Stores the number of moves so far


self.level = level

# This method is defined so that the


# priority queue is formed based on
# the cost variable of the objects
def lt (self, nxt):
return self.cost < nxt.cost

# Function to calculate the number of


# misplaced tiles ie. number of non-blank
# tiles not in their goal position
def calculateCost(mat, final) -> int:

count = 0
for i in range(n):
for j in range(n):
if ((mat[i][j]) and
(mat[i][j] != final[i][j])):
count += 1

return count

def newNode(mat, empty_tile_pos, new_empty_tile_pos,


level, parent, final) -> node:

# Copy data from parent matrix to current matrix


new_mat = copy.deepcopy(mat)
04

# Move tile by 1 position


x1 = empty_tile_pos[0]
y1 = empty_tile_pos[1]
x2 = new_empty_tile_pos[0]
y2 = new_empty_tile_pos[1]
new_mat[x1][y1], new_mat[x2][y2] = new_mat[x2][y2], new_mat[x1][y1]

# Set number of misplaced tiles


cost = calculateCost(new_mat, final)

new_node = node(parent, new_mat, new_empty_tile_pos,


cost, level)
return new_node

# Function to print the N x N matrix


def printMatrix(mat):

for i in range(n):
for j in range(n):
print("%d " % (mat[i][j]), end = " ")

print()

# Function to check if (x, y) is a valid


# matrix coordinate
def isSafe(x, y):

return x >= 0 and x < n and y >= 0 and y < n

# Print path from root node to destination node


def printPath(root):

if root == None:
return
05

printPath(root.parent)
printMatrix(root.mat)
print()

# Function to solve N*N - 1 puzzle algorithm


# using Branch and Bound. empty_tile_pos is
# the blank tile position in the initial state.
def solve(initial, empty_tile_pos, final):

# Create a priority queue to store live


# nodes of search tree
pq = priorityQueue()

# Create the root node


cost = calculateCost(initial, final)
root = node(None, initial,
empty_tile_pos, cost, 0)

# Add root to list of live nodes


pq.push(root)

# Finds a live node with least cost,


# add its children to list of live
# nodes and finally deletes it from
# the list.
while not pq.empty():

# Find a live node with least estimated


# cost and delete it form the list of
# live nodes
minimum = pq.pop()

# If minimum is the answer node


if minimum.cost == 0:
06

# Print the path from root to


# destination;
printPath(minimum)
return

# Generate all possible children


for i in range(4):
new_tile_pos = [
minimum.empty_tile_pos[0] + row[i],
minimum.empty_tile_pos[1] + col[i], ]

if isSafe(new_tile_pos[0], new_tile_pos[1]):

# Create a child node


child = newNode(minimum.mat,
minimum.empty_tile_pos,
new_tile_pos,
minimum.level + 1,
minimum, final,)

# Add child to list of live nodes


pq.push(child)

# Driver Code

# Initial configuration
# Value 0 is used for empty space
initial = [ [ 1, 2, 3 ],
[ 5, 6, 0 ],
[ 7, 8, 4 ] ]

# Solvable Final configuration


# Value 0 is used for empty space
final = [ [ 1, 2, 3 ],
[ 5, 8, 6 ],
07

[ 0, 7, 4 ] ]

# Blank tile coordinates in


# initial configuration
empty_tile_pos = [ 1, 2 ]

# Function call to solve the puzzle


solve(initial, empty_tile_pos, final)

Output
1 2 3
5 6 0
7 8 4

1 2 3
5 0 6
7 8 4

1 2 3
5 8 6
7 0 4

1 2 3
5 8 6
0 4 7

Result : Thus the 8 - Puzzle Problem is solved by Python code.


08

Ex: 1.b 8 QUEEN PROBLEM

Aim: To implement the 8 Queen Problem using python

Algorithm
START
1. begin from the leftmost column
2. if all the queens are placed,
return true/ print configuration
3. check for all rows in the current column
a) if queen placed safely, mark row and column; and
recursively check if we approach in the current
configuration, do we obtain a solution or not
b) if placing yields a solution, return true
c) if placing does not yield a solution, unmark and
try other rows
4. if all rows tried and solution not obtained, return
false and backtrack
END

Program

# Taking number of queens as input from user


print ("Enter the number of queens")
N = int(input())

# here we create a chessboard


# NxN matrix with all elements set to 0
board = [[0]*N for _ in range(N)]

def attack(i, j):


#checking vertically and horizontally
for k in range(0,N):
if board[i][k]==1 or board[k][j]==1:
return True
#checking diagonally
for k in range(0,N):
[Type text]
09

for l in range(0,N):
if (k+l==i+j) or (k-l==i-j):
if board[k][l]==1:
return True
return False

def N_queens(n):
if n==0:
return True
for i in range(0,N):
for j in range(0,N):
if (not(attack(i,j))) and (board[i][j]!=1):
board[i][j] = 1
if N_queens(n-1)==True:
return True
board[i][j] = 0
return False

N_queens(N)
for i in board:
print (i)

Output
Enter the number of queens
8
[1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 1, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 0]
[0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0]

Result : Thus the 8 - Puzzle Problem is solved by Python code.


10

Ex: 1.C CRYPT-ARITHMETIC PROBLEM

Aim: To implement the Crypt-Arithmetic Problem using python

Procedure
The Crypt-Arithmetic problem in Artificial Intelligence is a type of
encryption problem in which the written message in an alphabetical form which is easily
readable and understandable is converted into a numeric form which is neither easily
readable nor understandable. In simpler words, the crypt-arithmetic problem deals with
the converting of the message from the readable plain text to the non-readable ciphertext.
The constraints which this problem follows during the conversion is as follows:

1. A number 0-9 is assigned to a particular alphabet.


2. Each different alphabet has a unique number.
3. All the same, alphabets have the same numbers.
4. The numbers should satisfy all the operations that any normal number does.

Let us take an example of the message: SEND MORE MONEY.


Here, to convert it into numeric form, we first split each word separately and represent it
as follows:

SEND
MORE

MONEY
These alphabets then are replaced by numbers such that all the constraints are satisfied.
So initially we have all blank spaces.
These alphabets then are replaced by numbers such that all the constraints are satisfied.
So initially we have all blank spaces.
We first look for the MSB in the last word which is 'M' in the word 'MONEY' here. It is
the letter which is generated by carrying. So, carry generated can be only one. SO, we
have M=1.
Now, we have S+M=O in the second column from the left side. Here M=1. Therefore,
we have, S+1=O. So, we need a number for S such that it generates a carry when added
with 1. And such a number is 9. Therefore, we have S=9 and O=0.
11

Now, in the next column from the same side we have E+O=N. Here we have O=0.
Which means E+0=N which is not possible. This means a carry was generated by the
lower place digits. So we have:
1+E=N ---------- (i)
Next alphabets that we have are N+R=E-------- (ii)
So, for satisfying both equations (i) and (ii), we get E=5 and N=6.
Now, R should be 9, but 9 is already assigned to S, So, R=8 and we have 1 as a carry
which is generated from the lower place digits.
Now, we have D+5=Y and this should generate a carry. Therefore, D should be greater
than 4. As 5, 6, 8 and 9 are already assigned, we have D=7 and therefore Y=2.
Therefore, the solution to the given Crypt-Arithmetic problem is:
S=9; E=5; N=6; D=7; M=1; O=0; R=8; Y=2
Which can be shown in layout form as:

9567
1085

10652

Program
# Function to check if the
# assignment of digits to
# characters is possible
def isSolvable(words, result):
# Stores the value
# assigned to alphabets
mp = [-1]*(26)

# Stores if a number
# is assigned to any
# character or not
used = [0]*(10)

# Stores the sum of position


# value of a character
12

# in every string
Hash = [0]*(26)

# Stores if a character
# is at index 0 of any
# string
CharAtfront = [0]*(26)

# Stores the string formed


# by concatenating every
# occurred character only
# once
uniq = ""

# Iterator over the array,


# words
for word in range(len(words)):
# Iterate over the string,
# word
for i in range(len(words[word])):
# Stores the character
# at ith position
ch = words[word][i]

# Update Hash[ch-'A]
Hash[ord(ch) - ord('A')] += pow(10, len(words[word]) - i - 1)

# If mp[ch-'A'] is -1
if mp[ord(ch) - ord('A')] == -1:
mp[ord(ch) - ord('A')] = 0
uniq += str(ch)

# If i is 0 and word
# length is greater
# than 1
if i == 0 and len(words[word]) > 1:
13

CharAtfront[ord(ch) - ord('A')] = 1

# Iterate over the string result


for i in range(len(result)):
ch = result[i]

Hash[ord(ch) - ord('A')] -= pow(10, len(result) - i - 1)

# If mp[ch-'A] is -1
if mp[ord(ch) - ord('A')] == -1:
mp[ord(ch) - ord('A')] = 0
uniq += str(ch)

# If i is 0 and length of
# result is greater than 1
if i == 0 and len(result) > 1:
CharAtfront[ord(ch) - ord('A')] = 1

mp = [-1]*(26)

# Recursive call of the function


return True

# Auxiliary Recursive function


# to perform backtracking
def solve(words, i, S, mp, used, Hash, CharAtfront):
# If i is word.length
if i == len(words):
# Return true if S is 0
return S == 0

# Stores the character at


# index i
ch = words[i]

# Stores the mapped value


14

# of ch
val = mp[ord(words[i]) - ord('A')]

# If val is -1
if val != -1:
# Recursion
return solve(words, i + 1, S + val * Hash[ord(ch) - ord('A')], mp, used, Hash,
CharAtfront)

# Stores if there is any


# possible solution
x = False

# Iterate over the range


for l in range(10):
# If CharAtfront[ch-'A']
# is true and l is 0
if CharAtfront[ord(ch) - ord('A')] == 1 and l == 0:
continue

# If used[l] is true
if used[l] == 1:
continue

# Assign l to ch
mp[ord(ch) - ord('A')] = l

# Marked l as used
used[l] = 1

# Recursive function call


x |= solve(words, i + 1, S + l * Hash[ord(ch) - ord('A')], mp, used, Hash,
CharAtfront)

# Backtrack
mp[ord(ch) - ord('A')] = -1
15

# Unset used[l]
used[l] = 0

# Return the value of x;


return x

arr = [ "SIX", "SEVEN", "SEVEN" ]


S = "TWENTY"

# Function Call
if isSolvable(arr, S):
print("Yes")
else:
print("No")

Output
Yes
D=1 E=5 M=0 N=3 O=8 R=2 S=7 Y=6

Result : Thus the Crypt-Arithmetic Problem is solved by Python code.


16

Ex: 2 A* ALGORITHM

Aim: To implement the A* algorithm using python.

Algorithm
1. Add the starting square (or node) to the open list.
2. Repeat the following:
A) Look for the lowest F cost square on the open list. We refer to this as the current
square.
B). Switch it to the closed list.
C) For each of the 8 squares adjacent to this current square …
• If it is not walk able or if it is on the closed list, ignore it. Otherwise do the
following.
• If it isn’t on the open list, add it to the open list. Make the current square the
parent of this square. Record the F, G, and H costs of the square.
• If it is on the open list already, check to see if this path to that square is
better, using G cost as the measure. A lower G cost means that this is a
better path. If so, change the parent of the square to the current square, and
recalculate the G and F scores of the square. If you are keeping your open
list sorted by F score, you may need to resort the list to account for the
change.
D) Stop when you:
• Add the target square to the closed list, in which case the path has been
found, or
• Fail to find the target square, and the open list is empty. In this case, there is
no path.
3. Save the path. Working backwards from the target square, go from each square to
its parent square until you reach the starting square. That is your path.

Program
def aStarAlgo(start_node, stop_node):
open_set = set(start_node)
closed_set = set()
g = {} #store distance from starting node
parents = {} # parents contains an adjacency map of all nodes
17

#distance of starting node from itself is zero


g[start_node] = 0
#start_node is root node i.e it has no parent nodes
#so start_node is set to its own parent node
parents[start_node] = start_node
while len(open_set) > 0:
n = None
#node with lowest f() is found
for v in open_set:
if n == None or g[v] + heuristic(v) < g[n] + heuristic(n):
n=v
if n == stop_node or Graph_nodes[n] == None:
pass
else:
for (m, weight) in get_neighbors(n):
#nodes 'm' not in first and last set are added to first
#n is set its parent
if m not in open_set and m not in closed_set:
open_set.add(m)
parents[m] = n
g[m] = g[n] + weight
#for each node m,compare its distance from start i.e g(m) to the
#from start through n node
else:
if g[m] > g[n] + weight:
#update g(m)
g[m] = g[n] + weight
#change parent of m to n
parents[m] = n
#if m in closed set,remove and add to open
if m in closed_set:
closed_set.remove(m)
open_set.add(m)
if n == None:
print('Path does not exist!')
return None
18

# if the current node is the stop_node


# then we begin reconstructin the path from it to the start_node
if n == stop_node:
path = []
while parents[n] != n:
path.append(n)
n = parents[n]
path.append(start_node)
path.reverse()
print('Path found: {}'.format(path))
return path
# remove n from the open_list, and add it to closed_list
# because all of his neighbors were inspected
open_set.remove(n)
closed_set.add(n)
print('Path does not exist!')
return None

#define fuction to return neighbor and its distance


#from the passed node
def get_neighbors(v):
if v in Graph_nodes:
return Graph_nodes[v]
else:
return None

Output
Path found: ['A', 'F', 'G', 'I', 'J']

Result : Thus the A* algorithm is implemented by Python code.


19

Ex: 3 MIN-MAX ALGORITHM

Aim: To implement Minimax algorithm for game playing (Alpha-Beta pruning)

Description
Suppose we have a binary tree representing a game state of a two player game. Every
internal node is filled with 0 and the leaves values represent the end score. Player 1
wants to maximize the end score while player 2 wants to minimize the end score.
Player 1 will always make moves on nodes at even levels and player 2 will always
make moves on odd levels. We have to fill in the binary tree with the resulting scores
assuming both of players play optimally. So, if the input is like

Algorithm
• Define a function helper() . This will take root, h, currentHeight
• if root is empty, then
o return
• helper(left of root, h, currentHeight + 1)
• helper(right of root, h, currentHeight + 1)
• if currentHeight < h, then
o if currentHeight is even, then
▪ if left of root and right of root are not null, then
▪ value of root := maximum of value of left of root, value of
right of root
▪ otherwise when left of root is not null, then
▪ value of root := value of left of root
▪ otherwise when right of root is not null, then
▪ value of root := value of right of root
o otherwise,
▪ if left of root and right of root are not null, then
20

▪ value of root := minimum of value of left of root, value of


right of root
▪ otherwise when left of root is not null, then
value of root := value of left of root

▪ otherwise when right of root is not null, then
▪ value of root := value of right of root
• Define a function height() . This will take root
• if root is null, then
o return 0
• return 1 + (maximum of height(left of root) , height(right of root))
• From the main method, do the following −
• h := height(root)
• helper(root, h, 0)
• return root

Program
class TreeNode:
def init (self, data, left = None, right = None):
self.val = data
self.left = left
self.right = right
class Solution:
def helper(self, root, h, currentHeight):
if not root:
return
self.helper(root.left, h, currentHeight + 1)
self.helper(root.right, h, currentHeight + 1)
if currentHeight < h:
if currentHeight % 2 == 0:
if root.left and root.right:
root.val = max(root.left.val, root.right.val)
elif root.left:
root.val = root.left.val
elif root.right:
root.val = root.right.val
else:
if root.left and root.right:
21

root.val = min(root.left.val, root.right.val)


elif root.left:
root.val = root.left.val
elif root.right:
root.val = root.right.val
def height(self, root):
if not root:
return 0
return 1 + max(self.height(root.left), self.height(root.right))
def solve(self, root):
h = self.height(root)
self.helper(root, h, 0)
return root
def print_tree(root):
if root is not None:
print_tree(root.left)
print(root.val, end = ', ')
print_tree(root.right)
ob = Solution()
root = TreeNode(0)
root.left = TreeNode(3)
root.right = TreeNode(0)
root.right.left = TreeNode(0)
root.right.right = TreeNode(0)
root.right.left.left = TreeNode(-3)
root.right.right.right = TreeNode(4)
print_tree(ob.solve(root))

Input
root = TreeNode(0)
root.left = TreeNode(3)
root.right = TreeNode(0)
root.right.left = TreeNode(0)
root.right.right = TreeNode(0)
root.right.left.left = TreeNode(-3)
root.right.right.right = TreeNode(4)
22

Output
3, 3, -3, -3, -3, 4, 4,

Result : Thus the MinMax algorithm is implemented using python


23

Ex: 4 CONSTRAINT SATISFACTION PROGRAM

Aim: To solve constraint satisfaction problem using python.

Description:
Constraint programming is an example of the declarative programming paradigm, as
opposed to the usual imperative paradigm that we use most of the time. Most
programming paradigms can be classified as a member of either the imperative or
declarative paradigm group.

Imperative programming, simply put, is based on the developer describing the


solution/algorithm for achieving a goal (some kind of result). This happens by
changing the program state through assignment statements, while executing
instructions step-by-step. Therefore it makes a huge difference in what order the
instructions are written.

Declarative programming does the opposite - we don't write the steps on how to
achieve a goal, we describe the goal, and the computer gives us a solution. A common
example you should be familiar with is SQL. Do you tell the computer how to give
you the results you need? No, you describe what you need - you need the values from
some column, from some table, where some conditions are met.

Procedure
Installing the python-constraint Module
In this procedure we'll be working with a module called python-constraint (Note:
there's a module called "constraint" for Python, that is not what we want), which aims
to bring the constraint programming idea to Python.

To install this module, open the terminal and run:

$ pip install python-constraint


Basics of Using python-constraint
This is the generalized skeleton of programs written using this module (Note: we use
import constraint and not import python-constraint)
• import constraint
• define a variable as our problem
24

• add variables and their respective intervals to our problem


• add built-in/custom constraints to our problem
• fetch the solutions
• go through the solutions to find the ones we need

As previously mentioned, constraint programming is a form of declarative


programming. The order of statements doesn't matter, as long as everything is there in
the end. It's usually used to solve problems like this:

Example A
Find all (x,y) where x ∈ {1,2,3} and 0 <= y < 10, and x + y >= 5
If we look at this sentence, we can see several conditions (let's call them constraints)
that x and y have to meet.

For example, x is "constrained" to the values 1,2,3, y has to be less than 10 and their
sum has to be greater than or equal to 5. This is done in a few lines of code and in a
few minutes using constraint programming.

Looking at the problem above you probably thought "So what? I can do this with 2 for
loops and half a cup of coffee in Python in less than 10 minutes".

You're absolutely right, though through this example we can get an idea of what
constraint programming looks like:

Program
import constraint

problem = constraint.Problem()

# We're using .addVariables() this time since we're adding


# multiple variables that have the same interval.
# Since Strings are arrays of characters we can write
# "TF" instead of ['T','F'].
problem.addVariables("TF", range(1, 10))
problem.addVariables("WOUR", range(10))
25

# Telling Python that we need TWO + TWO = FOUR


def sum_constraint(t, w, o, f, u, r):
if 2*(t*100 + w*10 + o) == f*1000 + o*100 + u*10 + r:
return True

# Adding our custom constraint. The


# order of variables is important!
problem.addConstraint(sum_constraint, "TWOFUR")

# All the characters must represent different digits,


# there's a built-in constraint for that
problem.addConstraint(constraint.AllDifferentConstraint())

solutions = problem.getSolutions()
print("Number of solutions found: {}\n".format(len(solutions)))

# .getSolutions() returns a dictionary


for s in solutions:
print("T = {}, W = {}, O = {}, F = {}, U = {}, R = {}"
.format(s['T'], s['W'], s['O'], s['F'], s['U'], s['R']))

Output
Number of solutions found: 7

T = 7, W = 6, O = 5, F = 1, U = 3, R = 0
T = 7, W = 3, O = 4, F = 1, U = 6, R = 8
T = 8, W = 6, O = 7, F = 1, U = 3, R = 4
T = 8, W = 4, O = 6, F = 1, U = 9, R = 2
T = 8, W = 3, O = 6, F = 1, U = 7, R = 2
T = 9, W = 2, O = 8, F = 1, U = 5, R = 6
T = 9, W = 3, O = 8, F = 1, U = 7, R = 6

Result : Thus the constraint satisfaction problem is resolved successfully.


26

Ex: 5 PROPOSITIONAL MODEL CHECKING ALGORITHM

Aim: To implement the Proportional Model Checking Algorithm

Representing knowledge is the key issue in Artificial Intelligence. We can


develop a knowledgeable AI agent that can analyze like humans. we will develop a
game engine that will detect a murder based on its knowledge base. If we want a
machine to be intelligent enough to think like a human, then first the machine needs
some information about the real-world situation/problem. The knowledge of the real
world needs to be represented to the machine in the right manner that is readable to a
computer system. Propositional logic is one of the simplest methods of knowledge
representation to a machine. We will implement propositional logic to make our game
engine knowledgeable, and then we will make able the engine to detect the murderer.

Problem Statement
Doctor Black has just been found dead in his mansion yesterday. Yesterday, there were
only Three People in Doctor Black’s mansion.
They are the prime suspects:
1. Col. Mustard
2. Prof. Plum
3. Ms. Scarlet
Police found Three Weapons in the mansion:
1. Knife
2. Revolver
3. Wrench
The murder has happened in one of the Three Rooms of the mansion:
1. Ballroom
2. Kitchen
3. Library

We have to find who the murderer of Dr. Black is. We can start creating the
knowledge base of our game engine by adding the above information. We will make
our game engine knowledgeable by implementing propositional logic. We will also
receive further clues during the investigation. That will be also added to the knowledge
base.
27

Program
import termcolor
# we have imported the logic file
from logic import *
#Now we are symboling all the characters,rooms,weapons
mustard=Symbol("col.mustard")
plum=Symbol("ProfPlum")
scarlet=Symbol("MsScarlet")

charecters=[mustard,plum,scarlet]

ballroom=Symbol("ballroom")
kitchen=Symbol("kitchen")
library=Symbol("library")

rooms=[ballroom,kitchen,library]
revolber=Symbol("revolber")
knife=Symbol("knife")
wrench=Symbol("wrench")
wreapons=[revolber,knife,wrench]

# Now we are concating characters,rooms,and weapons in symbols.


symbols=charecters+rooms+wreapons
# we are checking the model and get some truth value

def check_knowledge(knowledge_base):
for symbol in symbols:
if model_check(knowledge_base,symbol):
termcolor.cprint(f"{symbol}:YES","green")
elif not model_check(knowledge_base,Not(symbol)):
print(f"{symbol}:Maybe")
# Createing knowledge base
knowledge_base=And(
28

Or(mustard,plum,scarlet),
Or(ballroom,kitchen,library),
Or(knife,revolber,wrench)

# They are the clue


knowledge_base.add(And(
Not(mustard),Not(kitchen),Not(wrench)
))

knowledge_base.add(Or(
Not(scarlet),Not(library),Not(wrench)
))

knowledge_base.add(Not(plum))
knowledge_base.add(Not(ballroom))
knowledge_base.add(Not(revolber))
check_knowledge(knowledge_base)

Output
MsScalket : Yes
Library : Yes
Knife : Yes

Result : Thus the Proportional Model Checking Algorithm is implemented by Python


code.
29

Ex: 6 FORWARD CHAINING

Aim: To implement the Forward Chaining Concept

Procedure
Forward Chaining is one of the two methodologies using an inference engine, the
other one being backward Chaining. It starts with a base state and uses the inference
rules and available knowledge in the forward direction till it reaches the end state. The
process iterates till the final state is reached. Now Let’s understand Forward chaining
with FOL with an example:We will list down the facts initially and then convert facts
to First-order Logic (FOL) using inference laws until we reach the goal state.

Some Fact :
• It is crime for Americans to sell the weapon to the enemy of America
• Country Nono is an enemy of America
• Nono has some Missiles
• All missiles were sold to Nono by Colonel
• Colonel is American
• Missile is a weapon

Goal :
• Colonel is criminal

Steps to be followed :
Fact conversion to FOL:

Step1 : It is crime for Americans to sell the weapon to the enemy of America
FOL :American(x)Weapon(y)Enemy(z,America)Sell(x,y,z)Criminal(x)
Step2 : Country Nono is enemy of America
FOL : Enemy(Nono, America)
Step3 : Nono has some Missile
FOL : Owns(Nono ,x)
Missile(x)
Step4 : All missiles were sold to Nono by Colonel
FOL : Missile(x) ^ Owns(Nono,x) → Sell(Colonel,x,Nono)
Step5 : Colonel is American
30

FOL : American(Colonel)
Step6 : Missile is weapon
FOL : Missile(x) → Weapon (x)

Proof:
Iteration 1:
• Start with the knowledge base/known facts : The facts that are not deduced
from any other [ present in LHS of the statements ]

American(Colonel) Missile(x) Owns(Nono , x) Enemy(Nono , America)

Add inferences one by one to link the connections


• Rule (1) doesn’t satisfy the so it will not be added
• Rule (2) and Rule(3) FOL are already added
• Rule (4) matches the inference so we will add it.

• Rule(5) doesn’t have RHS so nothing added


• Rule (6) has weapon in RHS derived from Missile(x) , which is already a part
so we will add weapon in our next state

That completes our first iteration


31

Iteration 2:
• Rule(1) has all the LHS conditions satisfied as we can see in the first iteration.
So all the four FOL in LHS are available, now we can add Criminal(x) from
RHS in our next state

Output
We reached the goal state to deduce that: Colonel is a criminal

Result : Thus the Forward Chaining is deduced.


32

Ex: 7 NAIVE BAYES MODELS

Aim: To implement the Naive Bayes Model using python

Algorithm
Naive Bayes is among one of the very simple and powerful algorithms for
classification based on Bayes Theorem with an assumption of independence among
the predictors. The Naive Bayes classifier assumes that the presence of a feature in a
class is not related to any other feature. Naive Bayes is a classification algorithm for
binary and multi-class classification problems.

Bayes Theorem
Based on prior knowledge of conditions that may be related to an event, Bayes
theorem describes the probability of the event
• conditional probability can be found this way
• Assume we have a Hypothesis(H) and evidence(E),
According to Bayes theorem, the relationship between the probability of
Hypothesis before getting the evidence represented as P(H) and the probability
of the hypothesis after getting the evidence represented as P(H|E) is:

P(H|E) = P(E|H)*P(H)/P(E)
• Prior probability = P(H) is the probability before getting the evidence
Posterior probability = P(H|E) is the probability after getting evidence
• In general,
P(class|data) = (P(data|class) * P(class)) / P(data)

Assume we have to find the probability of the randomly picked card to be king given
that it is a face card. There are 4 Kings in a Deck of Cards which implies that P(King)
=4/52 as all the Kings are face Cards so P(Face|King) = 1
there are 3 Face Cards in a Suit of 13 cards and there are 4 Suits in total so P(Face) =
12/52 Therefore,

Program
# Importing library
import math
import random
import csv
# the categorical class names are changed to numberic data
33

# eg: yes and no encoded to 1 and 0


def encode_class(mydata):
classes = []
for i in range(len(mydata)):
if mydata[i][-1] not in classes:
classes.append(mydata[i][-1])
for i in range(len(classes)):
for j in range(len(mydata)):
if mydata[j][-1] == classes[i]:
mydata[j][-1] = i
return mydata

# Splitting the data


def splitting(mydata, ratio):
train_num = int(len(mydata) * ratio)
train = []
# initially testset will have all the dataset
test = list(mydata)
while len(train) < train_num:
# index generated randomly from range 0
# to length of testset
index = random.randrange(len(test))
# from testset, pop data rows and put it in train
train.append(test.pop(index))
return train, test

# Group the data rows under each class yes or


# no in dictionary eg: dict[yes] and dict[no]
def groupUnderClass(mydata):
dict = {}
for i in range(len(mydata)):
if (mydata[i][-1] not in dict):
dict[mydata[i][-1]] = []
dict[mydata[i][-1]].append(mydata[i])
return dict

# Calculating Mean
34

def mean(numbers):
return sum(numbers) / float(len(numbers))

# Calculating Standard Deviation


def std_dev(numbers):
avg = mean(numbers)
variance = sum([pow(x - avg, 2) for x in numbers]) / float(len(numbers) - 1)
return math.sqrt(variance)

def MeanAndStdDev(mydata):
info = [(mean(attribute), std_dev(attribute)) for attribute in zip(*mydata)]
# eg: list = [ [a, b, c], [m, n, o], [x, y, z]]
# here mean of 1st attribute =(a + m+x), mean of 2nd attribute = (b + n+y)/3
# delete summaries of last class
del info[-1]
return info

# find Mean and Standard Deviation under each class


def MeanAndStdDevForClass(mydata):
info = {}
dict = groupUnderClass(mydata)
for classValue, instances in dict.items():
info[classValue] = MeanAndStdDev(instances)
return info

# Calculate Gaussian Probability Density Function


def calculateGaussianProbability(x, mean, stdev):
expo = math.exp(-(math.pow(x - mean, 2) / (2 * math.pow(stdev, 2))))
return (1 / (math.sqrt(2 * math.pi) * stdev)) * expo

# Calculate Class Probabilities


def calculateClassProbabilities(info, test):
probabilities = {}
for classValue, classSummaries in info.items():
probabilities[classValue] = 1
35

for i in range(len(classSummaries)):
mean, std_dev = classSummaries[i]
x = test[i]
probabilities[classValue] *= calculateGaussianProbability(x, mean, std_dev)
return probabilities

# Make prediction - highest probability is the prediction


def predict(info, test):
probabilities = calculateClassProbabilities(info, test)
bestLabel, bestProb = None, -1
for classValue, probability in probabilities.items():
if bestLabel is None or probability > bestProb:
bestProb = probability
bestLabel = classValue
return bestLabel

# returns predictions for a set of examples


def getPredictions(info, test):
predictions = []
for i in range(len(test)):
result = predict(info, test[i])
predictions.append(result)
return predictions

# Accuracy score
def accuracy_rate(test, predictions):
correct = 0
for i in range(len(test)):
if test[i][-1] == predictions[i]:
correct += 1
return (correct / float(len(test))) * 100.0

# driver code
36

# add the data path in your system


filename = r'E:\user\MACHINE LEARNING\machine learning algos\Naive
bayes\filedata.csv'

# load the file and store it in mydata list


mydata = csv.reader(open(filename, "rt"))
mydata = list(mydata)
mydata = encode_class(mydata)
for i in range(len(mydata)):
mydata[i] = [float(x) for x in mydata[i]]

# split ratio = 0.7


# 70% of data is training data and 30% is test data used for testing
ratio = 0.7
train_data, test_data = splitting(mydata, ratio)
print('Total number of examples are: ', len(mydata))
print('Out of these, training examples are: ', len(train_data))
print("Test examples are: ", len(test_data))

# prepare model
info = MeanAndStdDevForClass(train_data)

# test model
predictions = getPredictions(info, test_data)
accuracy = accuracy_rate(test_data, predictions)
print("Accuracy of your model is: ", accuracy)

Input
Dataset Download
https://gist.github.com/ktisha/c21e73a1bd1700294ef790c56c8aec1f
37

Output

Total number of examples are: 200


Out of these, training examples are: 140
Test examples are: 60
Accuracy of your model is: 71.2376788

Result : Thus the Naive Bayes Model using python is implemented.


38

Ex: 8 BAYESIAN NETWORKS AND PERFORM INFERENCES

Aim: To implement the Bayesian networks and perform inferences using python

Procedure
A Bayesian network is a directed acyclic graph in which each edge corresponds to a
conditional dependency, and each node corresponds to a unique random variable.
Bayesian network consists of two major parts: a directed acyclic graph and a set of
conditional probability distributions
• The directed acyclic graph is a set of random variables represented by nodes.
• The conditional probability distribution of a node (random variable) is defined
for every possible outcome of the preceding causal node(s).

For illustration, consider the following example. Suppose we attempt to turn on our
computer, but the computer does not start (observation/evidence). We would like to
know which of the possible causes of computer failure is more likely. In this
simplified illustration, we assume only two possible causes of this misfortune:
electricity failure and computer malfunction.
The corresponding directed acyclic graph is depicted in below figure.

The goal is to calculate the posterior conditional probability distribution of each of the
possible unobserved causes given the observed evidence, i.e. P [Cause | Evidence].

Program
from pgmpy.models import BayesianNetwork
from pgmpy.factors.discrete import TabularCPD
import networkx as nx
39

import pylab as plt

# Defining Bayesian Structure


model = BayesianNetwork([('Guest', 'Host'), ('Price', 'Host')])

# Defining the CPDs:


cpd_guest = TabularCPD('Guest', 3, [[0.33], [0.33], [0.33]])
cpd_price = TabularCPD('Price', 3, [[0.33], [0.33], [0.33]])
cpd_host = TabularCPD('Host', 3, [[0, 0, 0, 0, 0.5, 1, 0, 1, 0.5],
[0.5, 0, 1, 0, 0, 0, 1, 0, 0.5],
[0.5, 1, 0, 1, 0.5, 0, 0, 0, 0]],
evidence=['Guest', 'Price'], evidence_card=[3, 3])

# Associating the CPDs with the network structure.


model.add_cpds(cpd_guest, cpd_price, cpd_host)

model.check_model()

# Infering the posterior probability


from pgmpy.inference import VariableElimination

infer = VariableElimination(model)
posterior_p = infer.query(['Host'], evidence={'Guest': 2, 'Price': 2})
print(posterior_p)

nx.draw(model, with_labels=True)
plt.savefig('model.png')
plt.close()

Input
Datset Download
https://drive.google.com/file/d/17vwRLAY8uR-6vWusM5prn08it-BEGp-f/view
40

Output

Result : Thus the Bayesian networks and perform inferences using python is
implemented.
41

Ex: 9 MINI-PROJECT DOG VS CAT CLASSIFICATION

Aim: To implement the Dog- Cat classification system using python

Procedure & Program


The Asirra (Dogs VS Cats) dataset:
The Asirra (animal species image recognition for restricting access) dataset was
introduced in 2013 for a machine learning competition. The dataset includes 25,000
images with equal numbers of labels for cats and dogs.

Download
https://www.kaggle.com/c/dogs-vs-cats/data

1. Import the libraries:


import numpy as np
import pandas as pd
from keras.preprocessing.image import ImageDataGenerator,load_img
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import random
import os

2. Define image properties:


Image_Width=128
Image_Height=128
Image_Size=(Image_Width,Image_Height)
42

Image_Channels=3

3. Prepare dataset for training model:


filenames=os.listdir("./dogs-vs-cats/train")
categories=[]
for f_name in filenames:
category=f_name.split('.')[0]
if category=='dog':
categories.append(1)
else:
categories.append(0)
df=pd.DataFrame({
'filename':filenames,
'category':categories
})

4. Create the neural net model:


from keras.models import Sequential
from keras.layers import Conv2D,MaxPooling2D,\
Dropout,Flatten,Dense,Activation,\
BatchNormalization
model=Sequential()
model.add(Conv2D(32,(3,3),activation='relu',input_shape=(Image_Width,Image_Hei
ght,Image_Channels)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(128,(3,3),activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
43

model.add(Dense(512,activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(2,activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',metrics=['accuracy'])

5. Analyzing model:
. model.summary()

6. Test data preparation:

test_filenames = os.listdir("./dogs-vs-cats/test1")
test_df = pd.DataFrame({
'filename': test_filenames
})
nb_samples = test_df.shape[0]

7. Make categorical prediction:

predict = model.predict_generator(test_generator,
steps=np.ceil(nb_samples/batch_size))
8. Convert labels to categories:

test_df['category'] = np.argmax(predict, axis=-1)


label_map = dict((v,k) for k,v in train_generator.class_indices.items())
44

test_df['category'] = test_df['category'].replace(label_map)
test_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })
9. Visualize the prediction results:

sample_test = test_df.head(18)
sample_test.head()
plt.figure(figsize=(12, 24))
for index, row in sample_test.iterrows():
filename = row['filename']
category = row['category']
img = load_img("./dogs-vs-cats/test1/"+filename, target_size=Image_Size)
plt.subplot(6, 3, index+1)
plt.imshow(img)
plt.xlabel(filename + '(' + "{}".format(category) + ')' )
plt.tight_layout()
plt.show()

Output

Result : Thus Cat – Dog prediction is implemented using Python.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy