Ai All Practical 56
Ai All Practical 56
Ai All Practical 56
Practical : 1
Aim : Write a program to implement Tic-Tac-Toe game problem.
import numpy as np
import random
from time import sleep
# Creates an empty board
def create_board():
return(np.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]))
# Check for empty places on board
def possibilities(board):
l = []
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] == 0:
l.append((i, j))
return(l)
# Select a random place for the player
def random_place(board, player):
selection = possibilities(board)
current_loc = random.choice(selection)
board[current_loc] = player
return(board)
# Checks whether the player has three
# of their marks in a horizontal row
def row_win(board, player):
for x in range(len(board)):
win = True
for y in range(len(board)):
if board[x, y] != player:
win = False
continue
if win == True:
return(win)
return(win)
# Checks whether the player has three
# of their marks in a vertical row
def col_win(board, player):
for x in range(len(board)):
win = True
for y in range(len(board)):
if board[y][x] != player:
win = False
continue
if win == True:
return(win)
return(win)
1|Page
ARTIFICIAL INTELIGENCE(3170716) 200490131056
while winner == 0:
for player in [1, 2]:
board = random_place(board, player)
print("Board after " + str(counter) + " move")
print(board)
sleep(2)
counter += 1
winner = evaluate(board)
if winner != 0:
break
return(winner)
# Driver Code
print("Winner is: " + str(play_game()))
2|Page
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Output :
3|Page
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Practical : 2
Aim : Write a program to implement BFS (for 8 puzzle problem or Water
Jug problem or Any AI search problem).
4|Page
ARTIFICIAL INTELIGENCE(3170716) 200490131056
break
# If we have not reached final state
# then, start developing intermediate
# states to reach solution state
q.append([u[0], b]) # Fill Jug2
q.append([a, u[1]]) # Fill Jug1
for ap in range(max(a, b) + 1):
# Pour amount ap from Jug2 to Jug1
c = u[0] + ap
d = u[1] - ap
# Check if this state is possible or not
if (c == a or (d == 0 and d >= 0)):
q.append([c, d])
# Pour amount ap from Jug 1 to Jug2
c = u[0] - ap
d = u[1] + ap
# Check if this state is possible or not
if ((c == 0 and c >= 0) or d == b):
q.append([c, d])
# Empty Jug2
q.append([a, 0])
# Empty Jug1
q.append([0, b])
# No, solution exists if ans=0
if (not isSolvable):
print("No solution")
# Driver code
if __name__ == '__main__':
Jug1, Jug2, target = 4, 3, 2
print("Path from initial state "
"to solution state ::")
BFS(Jug1, Jug2, target)
Output :
5|Page
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Practical : 3
Aim : Write a program to implement DFS (for 8 puzzle problem or Water
Jug problem or Any AI search problem).
#include <cstdio>
#include <stack>
#include <map>
#include <algorithm>
using namespace std;
struct state {
int x, y;
bool operator < (const state& that) const {
if (x != that.x) return x < that.x;
return y < that.y;
}
};
int capacity_x, capacity_y, target;
void dfs(state start, stack <pair <state, int> >& path) {
stack <state> s;
state goal = (state) {-1, -1};
map <state, pair <state, int> > parentOf;
s.push(start);
parentOf[start] = make_pair(start, 0);
while (!s.empty()) {
state top = s.top();
s.pop();
if (top.x == target || top.y == target) {
goal = top;
break;
}
// Rule 1: (x, y) -> (capacity_x, y) if x < capacity_x
// Fill the first jug
if (top.x < capacity_x) {
state child = (state) {capacity_x, top.y};
// Consider this state for visiting only if it has not been visited before
if (parentOf.find(child) == parentOf.end()) {
s.push(child);
parentOf[child] = make_pair(top, 1);
}
}
// Rule 2: (x, y) -> (x, capacity_y) if y < capacity_y
// Fill the second jug
if (top.y < capacity_y) {
state child = (state) {top.x, capacity_y};
if (parentOf.find(child) == parentOf.end()) {
s.push(child);
parentOf[child] = make_pair(top, 2);
}
}
// Rule 3: (x, y) -> (0, y) if x > 0
6|Page
ARTIFICIAL INTELIGENCE(3170716) 200490131056
7|Page
ARTIFICIAL INTELIGENCE(3170716) 200490131056
8|Page
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Output :
9|Page
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Practical : 4
Aim : Write a program to implement Single Player Game (Using any
Heuristic Function)
Import random
print("#------------------#")
print("| GUESS THE NUMBER |")
print("#------------------#") print('\
n')
while True:
guess = int(input('Guess the number: '))
if guess > number:
print('\nHmmm, try a lower number...\n')
else:
print("Right on! Well done!")
break
10 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Output :
11 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Practical : 5
Aim : Write a program to Implement A* Algorithm.
Class node()
"""A node class for A* Pathfinding"""
def __init__(self, parent=None, position=None):
self.parent = parent
self.position = position
self.g = 0
self.h = 0
self.f = 0
def __eq__(self, other):
return self.position == other.position
def astar(maze, start, end):
"""Returns a list of tuples as a path from the given start to the given end in the given
maze"""
12 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
path.append(current.position)
current = current.parent
return path[::-1] # Return reversed path
# Generate children
children = []
# Adjacent squares
for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]:
# Append
children.append(new_node)
13 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
def main():
start = (0, 0)
end = (7, 6)
if __name__ == '__main__':
main()
Output :
14 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Practical : 6
Aim : Write a program to implement mini-max algorithm for any game
development.
# A simple Python3 program to find
# maximum score that
# maximizing player can get
import math
if (maxTurn):
return max(minimax(curDepth + 1, nodeIndex * 2,
False, scores, targetDepth),
minimax(curDepth + 1, nodeIndex * 2 + 1,
False, scores, targetDepth))
else:
return min(minimax(curDepth + 1, nodeIndex * 2,
True, scores, targetDepth),
minimax(curDepth + 1, nodeIndex * 2 + 1,
True, scores, targetDepth))
# Driver code
scores = [3, 5, 2, 9, 12, 5, 23, 23]
treeDepth = math.log(len(scores), 2)
Output :
15 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Practical : 7
Aim : Assume given a set of facts of the form father(name1,name2) (name1
is the father of name2).
female(pam).
female(liz).
female(pat).
female(ann).
male(jim).
male(bob).
male(tom).
male(peter).
parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).
parent(bob,peter).
parent(peter,jim).
mother(X,Y):- parent(X,Y),female(X).
father(X,Y):-parent(X,Y),male(X).
sister(X,Y):-parent(Z,X),parent(Z,Y),female(X),X\==Y.
brother(X,Y):-parent(Z,X),parent(Z,Y),male(X),X\==Y.
grandparent(X,Y):-parent(X,Z),parent(Z,Y).
grandmother(X,Z):-mother(X,Y),parent(Y,Z).
grandfather(X,Z):-father(X,Y),parent(Y,Z).
wife(X,Y):-parent(X,Z),parent(Y,Z),female(X),male(Y).
uncle(X,Z):-brother(X,Y),parent(Y,Z).
16 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Output :
17 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Practical : 8
Aim : Define a predicate brother(X,Y) which holds iff X and Y are
brothers.
Define a predicate cousin(X,Y) which holds iff X and Y are cousins.
Define a predicate grandson(X,Y) which holds iff X is a grandson of Y.
Define a predicate descendent(X,Y) which holds iff X is a descendent of Y.
Consider the following genealogical tree: father(a,b).
father(a,c). father(b,d). father(b,e). father(c,f).
Say which answers, and in which order, are generated by your
definitions for the following queries in Prolog:
?- brother(X,Y).
?- cousin(X,Y).
?- grandson(X,Y).
?- descendent(X,Y).
father(anil,harshvardhan).
father(anil,arjun).
father(bonny,sonam).
father(bonny,jhanvi).
father(arjun,ridham).
grandson(X,Y):-father(X,K),father(K,Y)
cousin(A,B):-father(K,X),father(K,Y),father(X,A),father(Y,B) brother(X,Y):-
father(K,X),father(K,Y) descendent(X,Y):-father(K,X),father(K,Y)
descendent(X,Y):-father(K,X),father(K,Y),father(X,A),father(Y,B)
descendent(X,Y):-father(X,K),father(K,Y)
Output :
18 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Practical : 9
Aim : Write a program to solve Tower of Hanoi problem using Prolog.
move(1,X,Y,_) :-
write('Move top disk from '), write(X), write(' to '),write(Y), nl.
move(N,X,Y,Z) :-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).
Output :
19 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Practical : 10
Aim : Write a program to solve N-Queens problem using Prolog.
% render solutions nicely. use_rendering(chess).
queens(N, Queens) :-
length(Queens, N),
board(Queens, Board, 0, N, _, _),
queens(Board, 0, Queens).
constraints(0, _, _, _) :- !.
constraints(N, Row, [R|Rs], [C|Cs]) :-
arg(N, Row, R-C),
M is N-1,
constraints(M, Row, Rs, Cs).
queens([], _, []).
queens([C|Cs], Row0, [Col|Solution]) :-
Row is Row0+1,
select(Col-Vars, [C|Cs], Board),
arg(Row, Vars, Row-Row),
queens(Board, Row, Solution).
Output:-
20 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Practical : 11
Aim : Write a program to solve 8 puzzle problem using Prolog.
21 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
22 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
Output :
Practical : 12
Aim : Write a program to solve travelling salesman problem using Prolog.
edge(a, b, 3).
23 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
edge(a, c, 4).
edge(a, d, 2).
edge(a, e, 7).
edge(b, c, 4).
edge(b, d, 6).
edge(b, e, 3).
edge(c, d, 5).
edge(c, e, 8).
edge(d, e, 6).
edge(b, a, 3).
edge(c, a, 4).
edge(d, a, 2).
edge(e, a, 7).
edge(c, b, 4).
edge(d, b, 6).
edge(e, b, 3).
edge(d, c, 5).
edge(e, c, 8).
edge(e, d, 6).
edge(a, h, 2).
edge(h, d, 1).
len([], 0).
len([H|T], N):- len(T, X), N is X+1 .
best_path(Visited, Total):- path(a, a, Visited, Total).
best(Cost-Holder,Bcost-_,Cost-Holder):- Cost<Bcost,!.
best(_,X,X).
pick([Cost-Holder|R],X):- pick(R,Bcost-Bholder),best(Cost-Holder,Bcost-Bholder,X),!.
pick([X],X).
Output :
24 | P a g e
ARTIFICIAL INTELIGENCE(3170716) 200490131056
25 | P a g e