Ai Record - 65
Ai Record - 65
Ai Record - 65
EX.NO: 1
DATE: 02.08.2023
QUESTION:
AIM:
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)
RESULT:
EX.NO: 2a
DATE:
16.08.2023
IMPLEMENTATION OF BASIC SEARCH STRATEGIES –
8-PUZZLE PROBLEM
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
Roll Number: 2127220502065 Page No.:
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 = [
No. of moves = 4
RESULT:
EX.NO: 2b
DATE:
16.08.2023
IMPLEMENTATION OF BASIC SEARCH STRATEGIES –
8-QUEENS PROBLEM
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):
[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
QUESTION:
AIM:
ALGORITHM:
PROGRAM:
aStarAlgo('A', 'J')
RESULT:
EX.NO: 4
DATE: 20.09.2023
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
else:
best = MAX
for i in range(0, targetdepth-1):
val = minimax(depth + 1, nodeIndex * 2 + i,
True, values, alpha, beta,targetdepth)
best = min(best, val)
beta = min(beta, 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)
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:
{'a': ‘red’, 'b': ‘blue’, 'c': ‘green’, 'd': ‘blue’, 'e': ‘green’}
RESULT: