Professional Documents
Culture Documents
Surya Cadfsd
Surya Cadfsd
EX.NO: 1
DATE: 02.08.2023
IMPLEMENTATION OF INTELLIGENT AGENTS –
VACUUM WORLD PROBLEM
QUESTION:
Implement intelligent agents – Vacuum world problem.
AIM:
To implement intelligent agents – Vacuum world problem.
ALGORITHM:
1. Start
2. Declare a list of elements with 0 representing absence of dirt and 1
representing presence of dirt.
3. Use random module to access the elements randomly
4. Clean the dirt if the value is 1
5. Calculate the cost as 1 for moving from one index to another and for
sucking the dirt
6. Keep track of the visited indices to optimise the solution
7. Display the total path cost
8. Stop
PROGRAM:
import random as rd
a=[1,0,1,1,0,0,1]
n=len(a)
visited=[]
count=0
cost=-1
while(count<n+1):
while(True):
i=rd.randint(0,n-1)
if(i not in visited):
count+=1
break
print("At index: ",i,end="\t")
cost+=1
print("Total path cost: ",cost)
visited.append(i)
if (a[i]==1):
a[i]=0
cost+=1
print("---Sucking dirt---")
print("Total path cost: ",cost)
RESULT:
QUESTION:
AIM:
ALGORITHM:
1. Start
2. Define a function find_next() .
3. Define moves = a map defining moves as a list corresponding to each value
4. Define a function get_paths()
5. Find the paths using the given functions along with a count variable
6. Keep track of new paths and new lists
7. Display the number of moves to solve the problem
8. Stop
PROGRAM:
class Solution:
def solve(self, board):dict
= {}
flatten = []
for i in range(len(board)):
flatten += board[i]
flatten = tuple(flatten)
dict[flatten] = 0
if flatten == (0, 1, 2, 3, 4, 5,
6, 7, 8):
return 0
return self.get_paths(dict)
results = []
pos_0 = node.index(0)
for move in moves[pos_0]:
new_node = list(node)
new_node[move], new_node[pos_0] = new_node[pos_0], new_node[move]
results.append(tuple(new_node))
return results
ob = Solution()
matrix = [
[3, 1, 2],
[4, 7, 5],
[6, 8, 0]]
print("No. of moves = ",ob.solve(matrix))
RESULT:
QUESTION:
AIM:
ALGORITHM:
1. Start
2. Create an 8x8 board.
3. Create user-defined functions for checking whether any of the queens can
attack each other and for placing the queens on the board by implementing a
recursive function
4. Backtrack if the position is not right
5. Display the result as an 8x8 board with the queens placed.
6. Stop
PROGRAM:
N=8
board = [[0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0]]
def attack(i, j):
for k in range(0,N):
if board[i][k]==1 or board[k][j]==1:
return True
for k in range(0,N): 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
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:
EX.NO: 3
DATE: 13.09.2023
IMPLEMENT A* ALGORITHM – ROUTE-FINDING PROBLEM
QUESTION:
AIM:
ALGORITHM:
PROGRAM:
aStarAlgo('A', 'J')
RESULT:
EX.NO: 4
DATE: 20.09.2023
MINIMAX ALGORITHM – ALPHA BETA PRUNING
QUESTION:
AIM:
ALGORITHM:
1. Start
2. Declare max and min values for alpha-beta pruning
3. Create a user-defined function “minimax” with depth, nodeindex,
maximising or minimising player as a Boolean value. Alpha, beta and
target depth values
4. If maximising player, compare with alpha else compare with beta
5. Assign the values to the nodes corresponding to the results of the
comparison
6. Display the optimal value
7. Stop
PROGRAM:
import math
MAX = 1000
MIN = -1000
def minimax(depth, nodeIndex, maximizingPlayer, values, alpha,beta,targetdepth):
if depth==targetdepth:
return values[nodeIndex]
if maximizingPlayer:
best = MIN
for i in range(0, targetdepth-1):
val = minimax(depth + 1, nodeIndex * 2 + i,
False, values, alpha, beta,targetdepth)best
= max(best, val)
alpha = max(alpha, best)if
beta <= alpha:
break
return best
RESULT:
EX.NO: 5a
DATE: 04.10.2023
CONSTRAINT SATISFACTION PROBLEM – CRYPTARITHMETIC
QUESTION:
AIM:
ALGORITHM:
1. Start
2. Import itertools
3. Create user-defined functions to get values and to solve the equation
containing the letters using the concept of permutations
4. Convert the solution into a dictionary and zip it as one iterable variable
5. Print all the possible combinations of the letters
6. Stop
PROGRAM:
import itertools
def get_value(word, substitution):s = 0
factor = 1
for letter in reversed(word):
s += factor * substitution[letter]
factor *= 10
return s
def solve2(equation):
left, right = equation.lower().replace(' ', '').split('=')left
= left.split('+')
letters = set(right)for
word in left:
for letter in word:
letters.add(letter)
letters = list(letters)
digits = range(10)
for perm in itertools.permutations(digits, len(letters)):sol =
dict(zip(letters, perm))
if sum(get_value(word, sol) for word in left) == get_value(right, sol):
print(' + '.join(str(get_value(word, sol)) for word in left) + " =
{}".format(get_value(right, sol), sol))
RESULT:
EX.NO: 5b
DATE: 04.10.2023
CONSTRAINT SATISFACTION PROBLEM – MAP COLOURING
QUESTION:
AIM:
ALGORITHM:
1. Start
2. Create a function to colour the vertices with a list of vertices and coloured
vertices and keep track of unused colours with respect to colour and
connected vertices
3. Check the neighbouring vertices before colouring the vertices every time
4. Colour the vertex if the colour is unused
5. Repeat this process till all the vertices are coloured
6. Display the coloured graph
7. Stop
PROGRAM:
def colour_vertices(graph):
vertices = sorted((list(graph.keys())))
colour_graph = {}
for vertex in vertices:
unused_colours = len(vertices) * [True]for
neighbor in graph[vertex]:
if neighbor in colour_graph:
colour = colour_graph[neighbor]
unused_colours[colour] = False
for colour, unused in enumerate(unused_colours):
if unused:
colour_graph[vertex] = colour
break
return colour_graph
graph = { "a" : ["c", "d", "b", "e"],
"b" : ["c", "e", "a"],
"c" : ["a", "b"],
"d" : ["a","e"],
"e" : ["d", "b", "a"], }
result = (colour_vertices(graph))
Roll Number: 2127220502102 Page No.:
colour_code={0:’red’,1:’blue’,2:’green’}for i
in result:
result[i]=colour_code[result[i]]
print(result)
{'a': ‘red’, 'b': ‘blue’, 'c': ‘green’, 'd': ‘blue’, 'e': ‘green’}
RESULT: