You are on page 1of 16

18CSC305J

Artificial Intelligence
NIDHI SINGH
RA1911031010067
NIDHI SINGH
RA1911031010067
EX1 : Implementation of toy problems

Date : 02:02:2022

Aim: Find the shortest path in Travelling Salesman Problem using Python.

Problem Description: 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.
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:
from sys import maxsize from itertools
import permutations
V=4

# implementation of traveling Salesman Problem def


travellingSalesmanProblem(graph, s):
# store all vertex apart from source vertex
vertex = [] for i in
range(V): if i != s:
vertex.append(i)

# store minimum weight Hamiltonian Cycle


min_path = maxsize
next_permutation=permutations(vertex) for i in
next_permutation:

# store current Path weight(cost)


current_pathweight = 0

# compute current path weight


k=s
for j in i:
current_pathweight += graph[k][j]
k=j
current_pathweight += graph[k][s]

# update minimum
min_path = min(min_path, current_pathweight)

return min_path

# Driver Code if _name_ ==


"_main_":

# matrix representation of graph


matrix = [] for i in
range(4):
row = [] for j in
range(4):
element = int(input())
row.append(element) matrix.append(row)
s=0
print(travellingSalesmanProblem(matrix, s)) Output Screenshot:
Result: Successfully found and calculated the shortest path in Travelling
Salesman Problem using Python.
NIDHI SINGH
RA1911031010067
EX2 : Developing agent programs for real world problems
Date : 05:02:2022
Aim: A vacuum cleaner problem was designed using python and implemented on AWS.
Problem Description: Vacuum cleaner problem is a well-known search problem for an agent
which works on Artificial Intelligence. In this problem, our vacuum cleaner is our agent. It is
a goal based agent, and the goal of this agent, which is the vacuum cleaner, is to clean up
the whole area. So, in the classical vacuum cleaner problem, we have two rooms and one
vacuum cleaner. There is dirt in both the rooms and it is to be cleaned. The vacuum cleaner
is present in any one of these rooms. So, we have to reach a state in which both the rooms
are clean and are dust free.
Developed a simple reflex agent program in Python for the vacuum-cleaner world problem.
This program defines the States, Goal State, Goal Test, Actions, Transition Model, and Path
Cost. For each possible initial state, the program returns a sequence of actions that leads to
the goal state, along with the path cost. Generates two test cases Instructions:

1. Enter LOCATION A/B in capital letters where A and B are the two adjacent rooms
respectively.

2. Enter Status O/1 accordingly where 0 means CLEAN and 1 means DIRTY.
3. Vacuum Cleaner senses the status of the other room before performing any action, also
known as Environment sensing.
Program:
#INSTRUCTIONS
#Enter LOCATION A/B in captial letters
#Enter Status O/1 accordingly where 0 means CLEAN and 1 means DIRTY

def vacuum_world():
# initializing goal_state
# 0 indicates Clean and 1 indicates Dirty
goal_state = {'A': '0', 'B': '0'} cost = 0

location_input = input("Enter Location of Vacuum") #user_input of location vacuum is


placed
status_input = input("Enter status of " + location_input) #user_input if location is dirty or
clean
status_input_complement = input("Enter status of other room") print("Initial Location
Condition" + str(goal_state))
if location_input == 'A': #
Location A is Dirty.
print("Vacuum is placed in Location A") if
status_input == '1': print("Location A is
Dirty.") # suck the dirt and mark it as clean
goal_state['A'] = '0'
cost += 1 #cost for suck print("Cost for
CLEANING A " + str(cost)) print("Location A has been
Cleaned.")

if status_input_complement == '1':
# if B is Dirty
print("Location B is Dirty.") print("Moving
right to the Location B. ")
cost += 1 #cost for moving right print("COST for
moving RIGHT" + str(cost))
# suck the dirt and mark it as clean
goal_state['B'] = '0' cost += 1 #cost
for suck print("COST for SUCK " + str(cost))
print("Location B has been Cleaned. ") else:
print("No action" + str(cost))
# suck and mark clean
print("Location B is already clean.")

if status_input == '0': print("Location A is already


clean ") if status_input_complement == '1':# if B is Dirty
print("Location B is Dirty.") print("Moving RIGHT to the
Location B. ") cost += 1 #cost for moving
right print("COST for moving RIGHT " + str(cost))
# suck the dirt and mark it as clean goal_state['B'] = '0'
cost += 1 #cost for suck print("Cost for
SUCK" + str(cost)) print("Location B has been Cleaned.
") else:
print("No action " + str(cost))
print(cost)
# suck and mark clean
print("Location B is already clean.")
else:
print("Vacuum is placed in location B") #
Location B is Dirty. if status_input == '1':
print("Location B is Dirty.") # suck the dirt
and mark it as clean
goal_state['B'] = '0' cost += 1 # cost for suck
print("COST for CLEANING " + str(cost))
print("Location B has been Cleaned.")

if status_input_complement == '1':
# if A is Dirty print("Location A is Dirty.")
print("Moving LEFT to the Location A. ") cost += 1 #
cost for moving right print("COST for moving LEFT" +
str(cost)) # suck the dirt and mark it as clean
goal_state['A'] = '0' cost += 1 # cost
for suck print("COST for SUCK " + str(cost))
print("Location A has been Cleaned.")
else:
print(cost)
# suck and mark clean
print("Location B is already clean.")

if status_input_complement == '1': # if A is Dirty


print("Location A is Dirty.") print("Moving LEFT to the
Location A. ") cost += 1 # cost for moving right
print("COST for moving LEFT " + str(cost)) # suck the dirt
and mark it as clean
goal_state['A'] = '0' cost += 1 # cost
for suck print("Cost for SUCK " + str(cost))
print("Location A has been Cleaned. ") else:
print("No action " + str(cost))
# suck and mark clean
print("Location A is already clean.")

# done cleaning print("GOAL


STATE: ") print(goal_state)
print("Performance Measurement: " + str(cost))

vacuum_world() Output
Screenshot:
Result: Successfully done vacuum cleaner problem, designed using python and
implemented on AWS.
NIDHI SINGH
RA1911031010067
Ex3: Implementation of constraint satisfaction problems

Date : 11:02:2022
Aim: N queen problem was designed using python and implemented on AWS

Problem Description: N - Queens problem is to place n - queens in such a manner on an n x


n chessboard that no queens attack each other by being in the same row, column or
diagonal.

It can be seen that for n =1, the problem has a trivial solution, and no solution exists for n =2
and n =3. So first we will consider the 4 queens problem and then generate it to n - queens
problem.

Given a 4 x 4 chessboard and number the rows and column of the chessboard 1 through
4.Since, we have to place 4 queens such as q1 q2 q3 and q4 on the chessboard, such that no
two queens attack each other. In such a conditional each queen must be placed on a
different row, i.e., we put queen "i" on row "i."

Now, we place queen q1 in the very first acceptable position (1, 1). Next, we put queen q 2 so
that both these queens do not attack each other. We find that if we place q 2 in column 1 and
2, then the dead end is encountered. Thus the first acceptable position for q 2 in column 3,
i.e. (2, 3) but then no position is left for placing queen 'q 3' safely. So we backtrack one step
and place the queen 'q2' in (2, 4), the next best possible solution.

Then we obtain the position for placing 'q 3' which is (3, 2). But later this position also leads
to a dead end, and no place is found where 'q 4' can be placed safely. Then we have to
backtrack till 'q1' and place it to (1, 2) and then all other queens are placed safely by moving
q2 to (2, 4), q3 to (3, 1) and q4 to (4, 3). That is, we get the solution (2, 4, 1, 3). This is one
possible solution for the 4-queens problem.

For another possible solution, the whole method is repeated for all partial solutions. The
other solutions for 4 - queens problems is (3, 1, 4, 2) i.e.
Program:

n = int(input("Enter the value of n")) board = []

def getBoard(): for i in


range(n): nthList = []
for j in range(n):
nthList.append(0)
board.append(nthList)

def printBoard(): for i in range(n):


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

def isSafe(row, col): for i in


range(n): if board[row][i]
== 1:
return False for j
in range(n): if
board[j][col] == 1:
return False

i = row-1 j = col-1
while(i>=0 and j>=0): if
board[i][j] == 1:
return False i = i-1 j
= j-1

i = row-1 j = col+1 while(i>=0 and j<n): if board[i]


[j] == 1: return False i = i-1
j = j+1

i = row+1 j = col-1
while(i<n and j>=0): if
board[i][j] == 1:
return False i = i+1
j = j-1

i = row+1 j = col+1
while(i<n and j<n): if
board[i][j] == 1:
return False i = i+1
j = j+1
return True

def Put(n, count): if


count == n:
return True for i in
range(n): for j in
range(n): if
isSafe(i, j):
board[i][j] = 1 count =
count+1 if Put(n, count) ==
True:
return True
board[i][j] = 0 count
= count-1 return False

getBoard() Put(n, 0)
printBoard()
OUTPUT:
Result: Successfully completed n queen problem , designed using python and implemented
on AWS.

You might also like