Ai 5 11
Ai 5 11
Practical No:5
Problem Statement:
Write a program to implement DFS (for 8 puzzle problem or Water Jug
problem or any AI search problem)
Code:
print("The capacity of jug 1: 4")
print("The capacity of jug 2: 3")
path.append((a, b))
visited.add((a, b))
if a == target or b == target:
return True
# Empty Jug 1
if a > 0:
if DFS(0, b, target, visited, path):
return True
# Empty Jug 2
if b > 0:
if DFS(a, 0, target, visited, path):
return True
SCET/CO/2023-24/Odd/BE Div-I/Sem-VII 1
Subject Code:3170716 Subject Name:Artificial Intelligence Date:
Enrollment No:200420107052 Name:Devangi K. Patel
pour = min(b, 4 - a)
if DFS(a + pour, b - pour, target, visited, path):
return True
# No solution found
path.pop()
return False
if __name__ == '__main__':
Jug1, Jug2, target = 4, 3, 2
visited = set()
path = []
SCET/CO/2023-24/Odd/BE Div-I/Sem-VII 2
Subject Code:3170716 Subject Name:Artificial Intelligence Date:
Enrollment No:200420107052 Name:Devangi K. Patel
Practical No:6
Problem Statement:
Write a program to Implement A* Algorithm.
Code:
import heapq
# A* search function
def astar_search(initial_state, goal_state, get_successors, heuristic):
open_set = [] # Priority queue (min-heap) for nodes to be explored
closed_set = set() # Set to store visited states
while open_set:
current_node = heapq.heappop(open_set)
SCET/CO/2023-24/Odd/BE Div-I/Sem-VII 3
Subject Code:3170716 Subject Name:Artificial Intelligence Date:
Enrollment No:200420107052 Name:Devangi K. Patel
continue
# Calculate the cost and heuristic values for the successor node
successor_cost = current_node.cost + step_cost
successor_heuristic = heuristic(successor_state, goal_state)
# If the open set becomes empty and the goal is not reached, there is no solution
return None
if __name__ == '__main__':
initial_state = (0, 0)
goal_state = (5, 5)
path = astar_search(initial_state, goal_state, get_successors, simple_heuristic)
if path:
print("Path from", initial_state, "to", goal_state, ":", path)
else:
print("No path found.")
Output:
SCET/CO/2023-24/Odd/BE Div-I/Sem-VII 4
Subject Code:3170716 Subject Name:Artificial Intelligence Date:
Enrollment No:200420107052 Name:Devangi K. Patel
Practical No:7
Problem Statement:
Write a program to implement mini-max algorithm for any game
development.
Code:
# Define the tic-tac-toe board as a 3x3 grid
board = [[' ', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' ']]
return False
# Minimax algorithm
def minimax(board, depth, is_maximizing):
if is_game_over(board):
if is_maximizing:
return -1 # Opponent wins
else:
return 1 # AI wins
elif is_maximizing:
max_eval = -float('inf')
for i in range(3):
SCET/CO/2023-24/Odd/BE Div-I/Sem-VII 5
Subject Code:3170716 Subject Name:Artificial Intelligence Date:
Enrollment No:200420107052 Name:Devangi K. Patel
for j in range(3):
if board[i][j] == ' ':
board[i][j] = 'O'
eval = minimax(board, depth + 1, False)
board[i][j] = ' '
max_eval = max(max_eval, eval)
return max_eval
else:
min_eval = 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, True)
board[i][j] = ' '
min_eval = min(min_eval, eval)
return min_eval
for i in range(3):
for j in range(3):
if board[i][j] == ' ':
board[i][j] = 'O'
eval = minimax(board, 0, False)
board[i][j] = ' '
if eval > best_eval:
best_eval = eval
best_move = (i, j)
return best_move
SCET/CO/2023-24/Odd/BE Div-I/Sem-VII 6
Subject Code:3170716 Subject Name:Artificial Intelligence Date:
Enrollment No:200420107052 Name:Devangi K. Patel
if is_game_over(board):
break
ai_move = find_best_move(board)
board[ai_move[0]][ai_move[1]] = 'O'
print_board(board)
SCET/CO/2023-24/Odd/BE Div-I/Sem-VII 7
Subject Code:3170716 Subject Name:Artificial Intelligence Date:
Enrollment No:200420107052 Name:Devangi K. Patel
Practical No:8
Problem Statement:
Write a program to solve Tower of Hanoi problem using Prolog.
Code:
hanoi(N) :- move(N, left, right, center).
move(1, A, B, _) :-
write('Move top disk from '), write(A), write(' to '), write(B), nl.
move(N, A, B, C) :-
N1 is N - 1,
move(N1, A, C, B),
move(1, A, B, _),
move(N1, C, B, A).
Output:
SCET/CO/2023-24/Odd/BE Div-I/Sem-VII 8
Subject Code:3170716 Subject Name:Artificial Intelligence Date:
Enrollment No:200420107052 Name:Devangi K. Patel
Practical No:9
Problem Statement:
Write a program to solve N-Queens problem using Prolog.
Code:
% N-Queens problem solver
n_queens(N, Solution) :-
range(1, N, Rows),
permutation(Rows, Solution),
is_safe(Solution).
% Permutation of a list
permutation([], []).
permutation(List, [H|PermRest]) :-
select(H, List, Rest),
permutation(Rest, PermRest).
% Example usage:
% n_queens(8, Solution) will solve the 8-Queens problem and return the solution.
Output:
SCET/CO/2023-24/Odd/BE Div-I/Sem-VII 9
Subject Code:3170716 Subject Name:Artificial Intelligence Date:
Enrollment No:200420107052 Name:Devangi K. Patel
Practical No:10
Problem Statement:
Write a program to solve 8 puzzle problem using Prolog.
Code:
% Define the predicate move, which takes a list of integers representing the current
state of the puzzle
% and returns a list of integers representing the new state of the puzzle after a valid
move.
move([X1,0,X3,X4,X5,X6,X7,X8,X9], [0,X1,X3,X4,X5,X6,X7,X8,X9]).
move([X1,X2,X3,0,X5,X6,X7,X8,X9], [X1,X2,X3,X5,0,X6,X7,X8,X9]).
move([X1,X2,X3,X4,0,X6,X7,X8,X9], [X1,X2,X3,X4,X6,0,X7,X8,X9]).
move([X1,X2,X3,X4,X5,0,X7,X8,X9], [X1,X2,X3,X4,X5,X7,0,X8,X9]).
move([X1,X2,X3,X4,X5,X6,0,X8,X9], [X1,X2,X3,X4,X5,X6,X8,0,X9]).
move([X1,X2,X3,X4,X5,X6,X7,0,X9], [X1,X2,X3,X4,X5,X6,X7,X9,0]).
move([X1,X2,X3,X4,X5,X6,X7,X8,0], [X1,X2,X3,X4,X5,X6,X7,0,X8]).
move([X1,X2,X3,X4,X5,X6,X7,X8,X9], [X1,X2,X3,X4,X5,X6,X7,X9,X8]).
% Define the predicate solve, which takes a list of integers representing the current
state of the puzzle
% and a list of integers representing the goal state of the puzzle.
solve(State, Goal) :-
bfs([[State]], [], Path),
reverse(Path, Goal).
% Define the predicate bfs, which takes a list of lists representing the frontier,
% a list of integers representing the explored set, and a list of lists representing the
path to the goal.
bfs([[State|Path]|_], _, [State|Path]) :-
State = [0,1,2,3,4,5,6,7,8].
bfs([Path|Paths], Explored, Solution) :-
last(Path, State),
findall([NewState, State|Path], (move(State, NewState), \+ member(NewState,
Explored)),
NewPaths),
append(Paths, NewPaths, Frontier),
append(Explored, [State], NewExplored),
bfs(Frontier, NewExplored, Solution).
% Define the predicate find_solution, which takes a list of integers representing the
current state of the puzzle
% and prints the solution.
find_solution(State) :-
Goal = [0,1,2,3,4,5,6,7,8],
solve(State, Solution),
write('Solution: '), nl,
print_solution(Solution).
SCET/CO/2023-24/Odd/BE Div-I/Sem-VII 10
Subject Code:3170716 Subject Name:Artificial Intelligence Date:
Enrollment No:200420107052 Name:Devangi K. Patel
% Define the predicate print_solution, which takes a list of lists representing the path
to the goal
% and prints the solution.
print_solution([]).
print_solution([State|Path]) :-
print_state(State), nl,
print_solution(Path).
% Define the predicate print_state, which takes a list of integers representing the
current state of the puzzle
% and prints the state.
print_state([X1,X2,X3,X4,X5,X6,X7,X8,X9]) :-
write([X1,X2,X3]), nl,
write([X4,X5,X6]), nl,
write([X7,X8,X9]), nl, nl.
% Example usage:
% find_solution([1, 2, 3, 0, 4, 5, 6, 7, 8]) will solve the 8-puzzle problem with the
given initial state.
Output:
SCET/CO/2023-24/Odd/BE Div-I/Sem-VII 11
Subject Code:3170716 Subject Name:Artificial Intelligence Date:
Enrollment No:200420107052 Name:Devangi K. Patel
Practical No:11
Problem Statement:
Write a program to solve travelling salesman problem using Prolog.
Code:
% Define the distance between cities
distance(city(a, 0, 0), city(b, 1, 1), 2).
distance(city(b, 1, 1), city(c, 2, 2), 2).
distance(city(c, 2, 2), city(d, 3, 3), 2).
distance(city(d, 3, 3), city(e, 4, 4), 2).
distance(city(e, 4, 4), city(a, 0, 0), 4).
% Define the cities
city(a, 0, 0).
city(b, 1, 1).
city(c, 2, 2).
city(d, 3, 3).
city(e, 4, 4).
SCET/CO/2023-24/Odd/BE Div-I/Sem-VII 12
Subject Code:3170716 Subject Name:Artificial Intelligence Date:
Enrollment No:200420107052 Name:Devangi K. Patel
% Find the optimal tour and print the result
find_and_print_optimal_tour :-
find_optimal_tour(OptimalTour, OptimalDistance),
write('Optimal Tour: '), write(OptimalTour), nl,
write('Optimal Distance: '), write(OptimalDistance), nl.
Output:
SCET/CO/2023-24/Odd/BE Div-I/Sem-VII 13