0% found this document useful (0 votes)
14 views27 pages

Ai Practicals

Uploaded by

patilvinit114
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views27 pages

Ai Practicals

Uploaded by

patilvinit114
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

PRACTICAL -1

AIM: Study Prolog for AI.

Theory:
Prolog as the name itself suggests, is the short form of LOGical PROgramming.
Prolog is a logical and declarative programming language. It is one major
example of the fourth-generation language that supports the declarative
programming paradigm. This is particularly suitable for programs that
involve symbolic or non-numeric computation. This is the main reason to use
Prolog as the programming language in Artificial Intelligence, where symbol
manipulation and inference manipulation are the fundamental tasks.
In Prolog, we need not mention the way how one problem can be solved, we just
need to mention what the problem is, so that Prolog automatically solves it.
However, in Prolog we are supposed to give clues as the solution method.
Prolog language basically has three different elements –

1. Facts − The fact is predicate that is true, for example, if we say, “Tom is
the son of Jack”, then this is a fact.
2. Rules − Rules are extinctions of facts that contain conditional clauses. To
satisfy a rule these conditions should be met.
3. Questions − And to run a prolog program, we need some questions, and
those questions can be answered by the given facts and rules.

Installation Guide:

• Download the exe file and run it.


• You will see the window as shown below, then click on next –
• Select proper directory where you want to install the software,
otherwise let it be installed on the default directory. Then click
on next.
• You will get the below screen, simply go to next.

• You can verify the below screen, and check/uncheck appropriate boxes,
otherwise you can leave it as default. Then click on next.
• In the next step, you will see the below screen, then click on Install.

• Then wait for the installation process to finish.


• Finally click on Finish to start GNU Prolog.

• The GNU prolog is installed successfully as shown below –


PRACTICAL -2

AIM: Write a program to check symptoms of the disease.

Input:

symptoms(flu, [fever, runny_nose, headache]).


symptoms(cold, [runny_nose, sore_throat, cough]).
symptoms(pox, [fever, rash, headache]).

has_symptom(Disease, Symptom) :-
symptoms(Disease, SymptomList),
member(Symptom, SymptomList).

list_symptoms(Disease) :-
symptoms(Disease, SymptomList),
format('The symptoms of ~w are: ~w~n', [Disease, SymptomList]).

find_disease(Symptom) :-
symptoms(Disease, SymptomList),
member(Symptom, SymptomList),
format('The disease with symptom ~w is ~w.~n',[Symptom, Disease]).
Output:

1. Checking if a disease has a specific symptom:


2. ?- has_symptom(flu, fever).
3. true.
4.
5. ?- has_symptom(cold, headache).
6. false.
7. Listing symptoms of a disease:
8. ?- list_symptoms(flu).
9. The symptoms of flu are: [fever, runny_nose, headache]
10.
11. ?- list_symptoms(cold).
12. The symptoms of cold are: [runny_nose, sore_throat, cough]
13. Finding a disease based on a symptom:
14. ?- find_disease(fever).
15. The disease with symptom fever is flu.
16.
17. ?- find_disease(rash).
18. The disease with symptom rash is pox.
PRACTICAL -3

AIM: Write a program to find a factorial of a number.

Input:

factorial(0,1).
factorial(N,Answer) :-
N>0,
N1 is N-1,
factorial(N1, M1),
Answer is N*M1.

Output:
PRACTICAL -4

AIM: Write a program for appending and reversing a list.

For Appending:

Input:

append_list([], L2, L2).

append_list([X | L1], L2, [X | L3]) :-


append_list(L1, L2, L3).

Output:

• append_list([a,b,c],[p,q], Ans).
Ans = [a,b,c,p,q]

• append_list([1,2,7],[3,14,85], Ans).
Ans = [1,2,7,3,14,85]

• append_list([1,2,3],[p,q], Ans).
Ans = [1,2,3,p,q]

• append_list([7,8,a,10],[p,q], Ans).
Ans = [7,8,a,10,p,q]
For Reversing:

Input:

list_concat([],L,L).

list_concat([X1|L1],L2,[X1|L3]) :-

list_concat(L1,L2,L3).

list_rev([],[]).

list_rev([Head|Tail],Reversed) :-

list_rev(Tail, RevTail),

list_concat(RevTail, [Head],Reversed).

Output:

• list_rev([1,2,3,7,8],ReverseList).
ReverseList = [8,7,3,2,1]

• list_rev([1,2,3,7,8],Answer).
Answer = [8,7,3,2,1]

• list_rev([6,8,9,74,5],NewList).
NewList = [5,74,9,8,6]
PRACTICAL -5

AIM: Write a program to find sum of all numbers in a list.

Input:

sum_list([], 0).
sum_list([H|T], Sum) :-
sum_list(T, Rest),
Sum is H + Rest.

Output:
PRACTICAL -6

AIM: Write a program to implement Tic - Tac – Toe game.

Input:

% Start the game

start :-

Board = ['_', '_', '_', '_', '_', '_', '_', '_', '_'],

display_board(Board),

play(Board, 'x').

% Play the game: alternate turns between 'x' and 'o'

play(Board, Player) :-

format('Player ~w, enter your move (1-9): ', [Player]),

read(Position),

(valid_move(Board, Position) ->

move(Board, Position, Player, NewBoard),

display_board(NewBoard),

(win(NewBoard, Player) ->

format('Player ~w wins!~n', [Player])

; draw(NewBoard) ->

write('It\'s a draw!~n')
; switch_player(Player, NextPlayer),

play(NewBoard, NextPlayer))

; write('Invalid move, try again.'), nl,

play(Board, Player)).

% Switch players

switch_player('x', 'o').

switch_player('o', 'x').

% Check for a valid move

valid_move(Board, Position) :-

Position >= 1, Position =< 9,

nth1(Position, Board, '_').

% Make a move

move(Board, Position, Player, NewBoard) :-

replace(Board, Position, Player, NewBoard).

% Replace an element in the list

replace([_|T], 1, X, [X|T]).

replace([H|T], I, X, [H|R]) :- I > 1, NI is I - 1, replace(T, NI, X, R).


% Check for a win

win(Board, Player) :-

(row(Board, Player); col(Board, Player); diag(Board, Player)).

row([X,X,X,_,_,_,_,_,_], X).

row([_,_,_,X,X,X,_,_,_], X).

row([_,_,_,_,_,_,X,X,X], X).

col([X,_,_,X,_,_,X,_,_], X).

col([_,X,_,_,X,_,_,X,_], X).

col([_,_,X,_,_,X,_,_,X], X).

diag([X,_,_,_,X,_,_,_,X], X).

diag([_,_,X,_,X,_,X,_,_], X).

% Check for a draw

draw(Board) :- \+ member('_', Board).

% Display the board

display_board([A, B, C, D, E, F, G, H, I]) :-

format('~w | ~w | ~w~n', [A, B, C]),

write('---------'), nl,
format('~w | ~w | ~w~n', [D, E, F]),

write('---------'), nl,

format('~w | ~w | ~w~n', [G, H, I]), nl.

Output:
PRACTICAL -7

AIM: Write a program to implement 8 puzzle problem.

Input:(Using DFS)

% Define the goal state


goal([1, 2, 3, 4, 5, 6, 7, 8, 0]).

% Check if a state is the goal state


is_goal(State) :- goal(State).

% Define possible moves


move(State, NewState) :-
nth0(BlankIndex, State, 0),
( (NewIndex is BlankIndex - 3, NewIndex >= 0) % Move Up
; (NewIndex is BlankIndex + 3, NewIndex < 9) % Move Down
; (NewIndex is BlankIndex - 1, BlankIndex mod 3 > 0) % Move Left
; (NewIndex is BlankIndex + 1, BlankIndex mod 3 < 2) % Move Right
),
swap(State, BlankIndex, NewIndex, NewState).

% Swap tiles in the state


swap(State, I, J, NewState) :-
nth0(I, State, A),
nth0(J, State, B),
replace(State, I, B, TempState),
replace(TempState, J, A, NewState).
% Replace an element in the list
replace([_|T], 0, X, [X|T]).
replace([H|T], N, X, [H|R]) :-
N > 0,
N1 is N - 1,
replace(T, N1, X, R).

% Depth-first search
dfs(State, Visited, Path) :-
is_goal(State),
reverse(Path, FinalPath),
write('Solution Path: '), write(FinalPath), nl.

dfs(State, Visited, Path) :-


\+ is_goal(State),
move(State, NewState),
\+ member(NewState, Visited),
dfs(NewState, [NewState|Visited], [NewState|Path]).

% Start the DFS from the initial state


solve_dfs(InitialState) :-
dfs(InitialState, [InitialState], [InitialState]).

% Example initial state: [1, 2, 3, 4, 5, 6, 0, 7, 8]


% Call: solve_dfs([1, 2, 3, 4, 5, 6, 0, 7, 8]).
Output:
PRACTICAL - 8

AIM: Write a program to implement water jug problem.

Input:(Using BFS)

% Define the capacities of the jugs and the target amount


capacity(jug1, 4).
capacity(jug2, 3).
target(2).

% BFS algorithm to find the solution


solve :-
bfs([[0, 0]], [], Solution), % Start the BFS with both jugs empty
reverse(Solution, Path), % Reverse the solution to get the proper path
write('Solution path: '), nl,
print_path(Path).

% BFS function
bfs([[X, Y] | _], _, [[X, Y]]) :-
target(Target), (X = Target; Y = Target). % Goal state: one jug has the target
amount

bfs([[X, Y] | Rest], Visited, [[X, Y] | Path]) :-


findall([NewX, NewY], valid_move([X, Y], [NewX, NewY], Visited),
Moves),
append(Rest, Moves, NewQueue),
bfs(NewQueue, [[X, Y] | Visited], Path).
% Valid moves for each state
valid_move([X, Y], [NewX, Y], Visited) :-
capacity(jug1, MaxX),
NewX is MaxX, \+ member([NewX, Y], Visited). % Fill Jug1

valid_move([X, Y], [X, NewY], Visited) :-


capacity(jug2, MaxY),
NewY is MaxY, \+ member([X, NewY], Visited). % Fill Jug2

valid_move([X, Y], [0, Y], Visited) :-


X > 0, \+ member([0, Y], Visited). % Empty Jug1

valid_move([X, Y], [X, 0], Visited) :-


Y > 0, \+ member([X, 0], Visited). % Empty Jug2

valid_move([X, Y], [NewX, NewY], Visited) :-


capacity(jug1, MaxX),
X + Y >= MaxX,
NewX is MaxX,
NewY is Y - (MaxX - X),
\+ member([NewX, NewY], Visited). % Pour Jug2 into Jug1

valid_move([X, Y], [NewX, NewY], Visited) :-


capacity(jug2, MaxY),
X + Y >= MaxY,
NewY is MaxY,
NewX is X - (MaxY - Y),
\+ member([NewX, NewY], Visited). % Pour Jug1 into Jug2

valid_move([X, Y], [NewX, NewY], Visited) :-


capacity(jug1, MaxX),
X + Y =< MaxX,
NewX is X + Y,
NewY is 0,
\+ member([NewX, NewY], Visited). % Pour all Jug2 into Jug1

valid_move([X, Y], [NewX, NewY], Visited) :-


capacity(jug2, MaxY),
X + Y =< MaxY,
NewY is X + Y,
NewX is 0,
\+ member([NewX, NewY], Visited). % Pour all Jug1 into Jug2

% Printing the solution path


print_path([]).
print_path([[X, Y] | Rest]) :-
write('Jug1: '), write(X), write(' liters, Jug2: '), write(Y), write(' liters'), nl,
print_path(Rest).
Output:
PRACTICAL - 9

AIM: Write a program to solve tower of Hanoi problem.

Input:

tower_of_hanoi(0, _, _, _) :- !.

tower_of_hanoi(N, Source, Target, Auxiliary) :-

N1 is N - 1,

tower_of_hanoi(N1, Source, Auxiliary, Target), % Move N-1 disks from


Source to Auxiliary

move_disk(Source, Target), % Move the Nth disk from Source to Target

tower_of_hanoi(N1, Auxiliary, Target, Source). % Move N-1 disks from


Auxiliary to Target

% Move disk from Source to Target

move_disk(Source, Target) :-

format('Move disk from ~w to ~w~n', [Source, Target]).


Output:
PRACTICAL - 10

AIM: Write a program to solve N- Queen problem.

Input:

% Main predicate to solve the N-Queens problem


n_queens(N, Solution) :-
length(Solution, N),
place_queens(Solution, N),
safe_queens(Solution).

% Predicate to place queens on the board


place_queens([], _).
place_queens([Q|Qs], N) :-
place_queens(Qs, N),
between(1, N, Q),
\+ member(Q, Qs).

% Predicate to check if queens are safe


safe_queens([]).
safe_queens([Q|Qs]) :-
safe_queens(Qs),
safe_queen(Q, Qs, 1).

% Predicate to check if a queen is safe from others


safe_queen(_, [], _).
safe_queen(Q, [Q1|Qs], D) :-
Q =\= Q1,
abs(Q - Q1) =\= D,
D1 is D + 1,
safe_queen(Q, Qs, D1).
Output:

You might also like