AI - Lab Manual - Removed
AI - Lab Manual - Removed
importheapq
import copy
classPuzzleNode:
def_init_(self,state,parent=None,action=None,cost=0):
self.state = state
self.parent=parent
self.action=action
self.cost = cost
self.heuristic=self.calculate_heuristic()
return(self.cost+self.heuristic)<(other.cost+other.heuristic) def
calculate_heuristic(self):
#Asimpleheuristicfunction:Manhattandistance
heuristic=0
for i in range(3):
forjinrange(3):
ifself.state[i][j]!=0:
goal_i,goal_j=divmod(self.state[i][j]-1,3)
return heuristic
def get_blank_position(state):
for i in range(3):
for j in range(3):
if state[i][j]==0:
return i, j
def get_neighbors(node):
i,j=get_blank_position(node.state)
neighbors = []
new_i,new_j=i+action[0],j+action[1]
new_state= copy.deepcopy(node.state)
new_state[i][j],new_state[new_i][new_j]=new_state[new_i][new_j],new_state[i][j]
return neighbors
def a_star(initial_state):
start_node=PuzzleNode(initial_state)
frontier = [start_node]
explored=set()
while frontier:
node=heapq.heappop(frontier)
ifnode.state== [[1,2,3],[4,5,6],[7,8,0]]:
return get_solution_path(node)
explored.add(tuple(map(tuple, node.state)))
if tuple(map(tuple,neighbor.state))notinexplored:
heapq.heappush(frontier, neighbor)
return None
def get_solution_path(node):
path = []
while node.parent:
path.append((node.state, node.action))
node = node.parent
path.append((node.state, node.action))
path.reverse()
return path
def print_solution_path(path):
print(f"Step {i + 1}:")
print_state(state)
if action:
print(f"Action:Move{state[action[0]][action[1]]}{action_direction(action)}\n")
def action_direction(action):
ifaction==(0,1):
return "right"
elifaction==(0,-1):
return "left"
elifaction==(1,0):
return "down"
elifaction==(-1,0):
return "up"
def print_state(state):
print(row)
print()
if__name =="__main__":
initial_state=[[1,2,3],[4,0,5],[7,8,6]]
print("Initial State:")
print_state(initial_state)
solution_path=a_star(initial_state)
if solution_path:
print("Solution Path:")
print_solution_path(solution_path)
else:
OUTPUT:
Initial State:
[1, 2,3]
[4, 0,5]
[7,8,6]
Solution Path:
Step1:
[1, 2,3]
[4, 0,5]
[7, 8,6]
Step2:
[1, 2,3]
[4, 5,0]
[7, 8,6]
Step 3:
[1, 2,3]
[4, 5,6]
[7, 8,0]
>>>
PROGRAM:
ClassEightQueensProblem:
def_init_(self,size=8):
self.size=size
self.solution=[None]*size
defis_safe(self,row,col):
for prev_row in range(row):
prev_col=self.solution[prev_row]
if prev_col==color\
prev_col-prev_row==col-rowor\
prev_col + prev_row == col + row:
return False
return True
defsolve_queens(self,row):
if row == self.size:
returnTrue
for colinrange(self.size):
ifself.is_safe(row,col):
self.solution[row]=col
ifself.solve_queens(row+1):
return True
self.solution[row]=None
return False
def print_solution(self):
for row in range(self.size):
line = ""
for col in range(self.size):
if col==self.solution[row]:
line += "Q "
else:
line += ".
"print(line.strip())
if __name == "main":
queens_problem=Eight QueensProblem()
if queens_problem.solve_queens(0):
print("Solution Found:")
queens_problem.print_solution()
else:
print("No solution exists.")
OUTPUT:
SolutionFound:
Q.......
....Q...
....... Q
..... Q..
..Q.....
.......Q.
.Q......
...Q....
>
PROGRAM:
def is_valid(assignment,word,mapping):
return sum(mapping[letter]for letter in word[:-1])%10==mapping[word[-1]]
def solve_cryptarithmetic(puzzle):
letters = set("".join(puzzle))
if len(letters)>10:
print("Invalid puzzle.Too many unique letters.")
return None
def backtrack(mapping,index):
if index == len(letters):
returnTrue
letter=list(letters)[index]
mapping[letter]=None
returnFalse
def print_solution(mapping,puzzle):
for word in puzzle:
print("+".join(str(mapping[letter])for letter in word[:-1]),end="=")
print(mapping[word[-1]])
if__name =="__main__":
puzzle=["SEND","MORE","MONEY"]
print("Cryptarithmetic Puzzle:")
for word in puzzle:
print(word,end="")
print()
solution=solve_cryptarithmetic(puzzle)
if solution:
print("\nSolution:")
print_solution(solution, puzzle)
else:
print("\nNo solution found.")
OUTPUT:
Cryptarithmetic Puzzle:
SEND MORE MONEY
PROGRAM:
importheapq
def heuristic(node,goal):
#Euclidean distance heuristic
return((node[0]-goal[0])**2+ (node[1]-goal[1])**2)**0.5
def astar_search(start,goal,graph):
open_set = [(0, start)]
closed_set=set()
came_from = {}
g_score={start:0}
f_score={start:heuristic(start,goal)}
while open_set:
current_f,current=heapq.heappop(open_set)
if current==goal:
path=reconstruct_path(came_from,current)
return path
closed_set.add(current)
tentative_g=g_score[current]+1
return None
def reconstruct_path(came_from,current):
path = [current]
while current in came_from:
current=came_from[current]
path.append(current)
return path[::-1]
if __name =="__main__":
# Example usage
graph= {
(0,0):[(1,0),(0, 1)],
(1,0):[(0,0),(2, 0)],
(0,1):[(0,0),(1,1),(0,2)],
(1,1):[(0,1),(2, 1)],
(0,2):[(0,1),(1, 2)],
(2,0):[(1,0),(2, 1)],
(2,1):[(1,1),(2,0),(2,2)],
(1,2):[(0,2),(2, 2)],
(2,2):[(1,2),(2, 1)]
}
start_node=(0,0)
goal_node=(2, 2)
result=astar_search(start_node,goal_node,graph)
if result:
print(f"Shortest Path from{start_node}to{goal_node}:{result}")
else:
print("Nopathfound.")
OUTPUT:
Shortest Path from (0,0) to (2,2): [(0,0), (0,1), (0,2), (1,2), (2,2)]
>
PROGRAM:
import heapq
def heuristic(node,goal):
return abs(node[0]-goal[0])+abs(node[1]-goal[1])
def memory_bounded_astar(start,goal,graph,memory_limit):
g_score={start:0}
f_score={start:heuristic(start,goal)}
while open_set:
current_f,current=heapq.heappop(open_set)
if current==goal:
path=reconstruct_path(came_from,current)
return path
closed_set.add(current)
tentative_g=g_score[current]+1
return None
def reconstruct_path(came_from,current):
path = [current]
while current in came_from:
current=came_from[current]
path.append(current)
return path[::-1]
start_node=(0,0)
goal_node=(2,2)
memory_limit=5#Set the memory limit
if result:
print(f"Shortest Path from {start_node} to {goal_node}:{result}")
else:
print("No path found.")
OUTPUT:
Shortest Path from (0,0) to (2,2): [(0,0), (0,1), (0,2), (1,2), (2,2)]
>
PROGRAM:
def evaluate(board):
#Check rows, columns, and diagonals for a win
for row in board:
if all(cell=='X' for cell in row):
return 1# Player X wins
elif all(cell=='O' for cell in row):
return -1 # Player O wins
def is_terminal(board):
return evaluate(board)is not None
def minimax(board,depth,maximizingPlayer):
if depth == 0 or is_terminal(board):
return evaluate(board)
if maximizingPlayer:
maxEval=float('-inf')
for i in range(3):
for j in range(3):
if board[i][j]=='':
board[i][j]='X'
eval=minimax(board,depth-1,False)
board[i][j] = ''# Undo the move
maxEval = max(maxEval, eval)
return maxEval
else:
minEval=float('inf')
for i in range(3):
for j in range(3):
if board[i][j]=='':
board[i][j]='O'
eval=minimax(board,depth-1,True)
board[i][j] = ' ' # Undo the move
minEval = min(minEval, eval)
return minEval
def best_move(board):
bestVal=float('-inf')
bestMove = None
for i in range(3):
for j in range(3):
if board[i][j] ==' ':
board[i][j] ='X'
moveVal= minimax(board, 9,False) #Depth is set to 9 for Tic-Tac-Toe
board[i][j] = ' ' # Undo the move
return bestMove
def print_board(board):
for row in board:
print(" ".join(row))
if__name =="__main__":
# Example usage for Tic-Tac-Toe
initial_board=[['', '',''],['','',''],['','','']]
if is_terminal(initial_board):
break
print_board(initial_board)
ai_move = best_move(initial_board)
print(f"AI's move: {ai_move}")
initial_board[ai_move[0]][ai_move[1]] = 'X'
result = evaluate(initial_board)
print_board(initial_board)
if result == 1:
print("Player Xwins!")
elif result == -1:
print("Player Owins!")
else:
print("It's a draw!")
OUTPUT:
Enter your move (row and column): 1
PROGRAM:
def is_valid_assignment(assignment):
a, b, c = assignment
def print_solution(assignments):
print()
def solve_csp():
variables = [1, 2, 3, 4, 5, 6, 7, 8, 9]
all_assignments = list(product(*domains))
print_solution(valid_assignments)
if __name =="__main__":
solve_csp()
OUTPUT:
A:1,B:5,C:9
A:1,B:6,C:8
A:1,B:8,C:6
A:1,B:9,C:5
A:2,B:4,C:9
A:2,B:5,C:8
A:2,B:6,C:7
A:2,B:7,C:6
A:2,B:8,C:5
A:2,B:9,C:4
A:3,B:4,C:8
A:3,B:5,C:7
A:3,B:7,C:5
A:3,B:8,C:4
A:4,B:2,C:9
A:4,B:3,C:8
A:4,B:5,C:6
A:4,B:6,C:5
A:4,B:8,C:3
A:4,B:9,C:2
A:5,B:1,C:9
A:5,B:2,C:8
A:5,B:3,C:7
A:5,B:4,C:6
A:5,B:6,C:4
A:5,B:7,C:3
A:5,B:8,C:2
A:5,B:9,C:1
A:6,B:1,C:8
A:6,B:2,C:7
A:6,B:4,C:5
A:6,B:5,C:4
A:6,B:7,C:2
A:6,B:8,C:1
A:7,B:2,C:6
A:7,B:3,C:5
A:7,B:5,C:3
A:7,B:6,C:2
A:8,B:1,C:6
A:8,B:2,C:5
A:8,B:3,C:4
A:8,B:4,C:3
A:8,B:5,C:2
A:8,B:6,C:1
A:9,B:1,C:5
A:9,B:2,C:4
A:9,B:4,C:2
A:9,B:5,C:1
PROGRAM:
def evaluate_propositional_formula(formula,model):
if formula[0] == 'Var':
return evaluate_propositional_formula(formula[1],model)and
evaluate_propositional_formula(formula[2], model)
return evaluate_propositional_formula(formula[1],model) or
evaluate_propositional_formula(formula[2], model)
else:
raise ValueError("Invalidformula")
# Example usage
print(f"Does the formula hold in the model? {'Yes' if result else 'No'}")
OUTPUT:
>
PROGRAM:
def forward_chaining(knowledge_base,goal):
agenda = list(knowledge_base['facts'])
inferred = set()
while agenda:
current_fact= agenda.pop(0)
if current_fact = = goal:
inferred .add(current_fact)
if explored is None:
explored = set()
if goal in knowledge_base['facts']:
explored.add(goal)
if premises_satisfied:
return True
new = set()
while True:
for ci in clauses:
for cj in clauses:
if ci != cj:
resolvents = resolve(ci,cj)
if not resolvents:
new.update(resolvents)
if new.issubset(clauses):
clauses.update(new)
resolvents = set()
if frozenset([-literal]) in cj:
if not resolvent:
resolvents.add(frozenset(resolvent))
return resolvents
knowledge_base_forward = {
'facts':{'A','B'},
'rules':[
goal_forward = 'D'
knowledge_base_backward = {
'facts': {'D'},
'rules': [
{'premises':{'A'},'conclusion': 'C'},
{'premises':{'B','C'},'conclusion': 'D'},
goal_backward = 'A'
knowledge_base_resolution = {
query_resolution = -1
>
PROGRAM:
# Example data
corpus = [
vectorizer = CountVectorizer()
X_train_vectorized = vectorizer.fit_transform(X_train)
naive_bayes_classifier = MultinomialNB()
naive_bayes_classifier.fit(X_train_vectorized, y_train)
print(f"Accuracy: {accuracy:.2f}")
print("Classification Report:")
print(classification_report(y_test,predictions))
print(f"Text:{text},TrueLabel:{true_label},PredictedLabel:{predicted_label}")
OUTPUT:
Accuracy: 0.00
Classification Report:
precision recall f1-score support
negative 0.00 0.00 0.00 1.0
positive 0.00 0.00 0.00 0.0
accuracy 0.00 1.0
macro avg 0.00 0.00 0.00 1.0
weighted avg 0.00 0.00 0.00 1.0
Predictions for the test set:
Text: negative sentiment here, True Label: negative, Predicted Label: positive
PROGRAM:
model = BayesianModel([('A','C'),('B','C')])
cpd_a = ParameterEstimator(model).estimate_cpd('A')
cpd_b = ParameterEstimator(model).estimate_cpd('B')
model.add_cpds(cpd_a,cpd_b)
print("Model Structure:")
print(model.edges())
print(model.get_cpds('A'))
print(model.get_cpds('B'))
inference = VariableElimination(model)
query = inference.query(variables=['C'],evidence={'A':1,'B':0})
print("\nInference Result:")
print(query)
OUTPUT:
Collectingpgmpy
Downloadingpgmpy-0.1.24-py3-none-any.whl(2.0MB)
Requirement already satisfied: scipy in c:\users\a\anaconda3\lib\site-packages (from pgmpy) (1.7.3)
Requirement already satisfied: numpy in c:\users\a\anaconda3\lib\site-packages (from pgmpy) (1.21.5)
Requirement already satisfied: pandas in c:\users\a\anaconda3\lib\site-packages (from pgmpy) (1.4.2)
Requirementalreadysatisfied:statsmodelsinc:\users\a\anaconda3\lib\site-packages(frompgmpy)(0.13.2)
Requirement already satisfied: joblib in c:\users\a\anaconda3\lib\site-packages (from pgmpy) (1.1.0)
Requirement already satisfied: pyparsing in c:\users\a\anaconda3\lib\site-packages (from pgmpy) (3.0.4)
Requirement already satisfied: tqdm in c:\users\a\anaconda3\lib\site-packages (from pgmpy) (4.64.0)
Downloading pgmpy-0.1.23-py3-none-any.whl (1.9 MB)
Downloadingpgmpy-0.1.22-py3-none-any.whl(1.9MB)
Downloadingpgmpy-0.1.21-py3-none-any.whl(1.9MB)
Downloadingpgmpy-0.1.20-py3-none-any.whl(1.9MB)
Downloadingpgmpy-0.1.19-py3-none-any.whl(1.9MB)
Downloadingpgmpy-0.1.18-py3-none-any.whl(1.9MB)
Downloadingpgmpy-0.1.17-py3-none-any.whl(1.9MB)
Downloading pgmpy-0.1.16-py3-none-any.whl (1.9 MB)
Downloading pgmpy-0.1.15-py3-none-any.whl (1.9 MB)
Downloading pgmpy-0.1.14-py3-none-any.whl (331 kB)
Downloading pgmpy-0.1.13-py3-none-any.whl (324 kB)
Downloading pgmpy-0.1.12-py3-none-any.whl (322 kB)
Downloading pgmpy-0.1.11-py3-none-any.whl (314 kB)
Downloading pgmpy-0.1.10-py3-none-any.whl (339 kB)
Downloading pgmpy-0.1.9-py3-none-any.whl (331 kB)
Installing collected packages: pgmpy
Successfullyinstalledpgmpy-0.1.9