0% found this document useful (0 votes)
32 views

AI Lab Manual

This document contains summaries of several artificial intelligence and logic programming problems including: 1. The water jug problem is solved using depth-first search to determine the steps to pour water between jugs of different capacities. 2. The travelling salesman problem finds the shortest route between cities using roads of given distances between them. 3. The 8-queens problem uses backtracking to place 8 queens on a chessboard so that no two queens attack each other. 4. The monkey-banana problem determines the steps needed for a monkey to get a banana using actions like climbing and moving between positions like on the floor or a box. The document provides sample Prolog code and output for
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)
32 views

AI Lab Manual

This document contains summaries of several artificial intelligence and logic programming problems including: 1. The water jug problem is solved using depth-first search to determine the steps to pour water between jugs of different capacities. 2. The travelling salesman problem finds the shortest route between cities using roads of given distances between them. 3. The 8-queens problem uses backtracking to place 8 queens on a chessboard so that no two queens attack each other. 4. The monkey-banana problem determines the steps needed for a monkey to get a banana using actions like climbing and moving between positions like on the floor or a box. The document provides sample Prolog code and output for
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/ 14

Artificial Intelligence

Lab Manual 2023


Prolog
Superior College, Gujrat
GCUF
1. Water Jug Problem:
 Program:
min(X,Y,X):-X<Y,!.

min(_,Y,Y).

rev(L,R):-revacc(L,[],R).

revacc([],R,R):-!.

revacc([H|T],A,R):-revacc(T,[H|A],R).

%Solve water jug problem using DFS

%X,Y are initial contents, Nx,Ny are final contents of jug1 of capacity _ and jug2 of capacity My
respectively after pouring from jug1 into jug2

chk(_,X,My,Y,Nx,Ny):- X>0,Y<My,Ey is My-Y,min(X,Ey,P),

Nx is X-P,Ny is Y+P.

%Given 3 jugs of capacities Mx,My,Mz and filled with X,Y,Z units of a liquid respectively,give steps so
that finally they contain Fx,Fy,Fz units of the liquid respectively.

jug(Mx,My,Mz,X,Y,Z,Fx,Fy,Fz):-jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,[],['Initially']).

jug(_,Fx,_,Fy,_,Fz,Fx,Fy,Fz,T,R):-!,rev([[Fx,Fy,Fz],[Fx,Fy,Fz]|T],TR),rev(['Finally'|R],RR),display(TR,RR).

jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-chk(Mx,X,My,Y,Nx,Ny),not(member([Nx,Ny,Z],T))
,jug(Mx,Nx,My,Ny,Mz,Z,Fx,Fy,Fz,[[X,Y,Z]|T],['Pour liquid from jug1 into jug2'|R]).

jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-chk(Mx,X,Mz,Z,Nx,Nz),not(member([Nx,Y,Nz],T))
,jug(Mx,Nx,My,Y,Mz,Nz,Fx,Fy,Fz,[[X,Y,Z]|T],['Pour liquid from jug1 into jug3'|R]).

jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-chk(My,Y,Mz,Z,Ny,Nz),not(member([X,Ny,Nz],T))
,jug(Mx,X,My,Ny,Mz,Nz,Fx,Fy,Fz,[[X,Y,Z]|T],['Pour liquid from jug2 into jug3'|R]).

jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-chk(My,Y,Mx,X,Ny,Nx),not(member([Nx,Ny,Z],T))
,jug(Mx,Nx,My,Ny,Mz,Z,Fx,Fy,Fz,[[X,Y,Z]|T],['Pour liquid from jug2 into jug1'|R]).

jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-chk(Mz,Z,Mx,X,Nz,Nx),not(member([Nx,Y,Nz],T))
,jug(Mx,Nx,My,Y,Mz,Nz,Fx,Fy,Fz,[[X,Y,Z]|T],['Pour liquid from jug3 into jug1'|R]).

jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-chk(Mz,Z,My,Y,Nz,Ny),not(member([X,Ny,Nz],T))
,jug(Mx,X,My,Ny,Mz,Nz,Fx,Fy,Fz,[[X,Y,Z]|T],['Pour liquid from jug3 into jug2'|R]).
display([],[]):-!.

display([T1|T],[R1|R]):-write(R1),write(' : '),write(T1),nl,display(T,R).

 Sample Output:
?- jug(8,5,3,8,0,0,4,4,0).

Initially : [8,0,0]

Pour liquid from jug1 into jug2 : [3,5,0]

Pour liquid from jug1 into jug3 : [0,5,3]

Pour liquid from jug2 into jug1 : [5,0,3]

Pour liquid from jug3 into jug2 : [5,3,0]

Pour liquid from jug1 into jug3 : [2,3,3]

Pour liquid from jug3 into jug2 : [2,5,1]

Pour liquid from jug2 into jug1 : [7,0,1]

Pour liquid from jug3 into jug2 : [7,1,0]

Pour liquid from jug1 into jug3 : [4,1,3]

Pour liquid from jug3 into jug2 : [4,4,0]

Finally : [4,4,0]

true

2. Travelling Salesman Problem


 Program:
road("tampa","houston",200).

road("gordon","tampa",300).

road("houston","gordon",100).

road("houston","kansas_city",120).

road("gordon","kansas_city",130).

route(Town1,Town2,Distance):-

road(Town1,Town2,Distance).
route(Town1,Town2,Distance):-

road(Town1,X,Dist1),

route(X,Town2,Dist2),

Distance=Dist1+Dist2,!.

 Sample Output:
?- route("tampa", "kansas_city", X).

X = 200+120.

3. 8-Queens problem
 Program:
% Solve the 8 Queens problem

solve_queens(Board) :-

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

valid_board(Board),

place_queens(Board).

% Check if the board is valid

valid_board([]).

valid_board([X|Xs]) :-

valid_board(Xs),

member(X, [1, 2, 3, 4, 5, 6, 7, 8]).

% Place the queens on the board

place_queens([]).

place_queens([X|Xs]) :-

place_queens(Xs),

member(X, [1, 2, 3, 4, 5, 6, 7, 8]),

no_attack(X, Xs, 1).


% Check if a queen at column X attacks any queens in the previous columns

no_attack(_, [], _).

no_attack(X, [Y|Ys], N) :-

X =\= Y,

X + N =\= Y,

X - N =\= Y,

N1 is N + 1,

no_attack(X, Ys, N1).

% Print the board

print_board([]).

print_board([X|Xs]) :-

print_row(X),

print_board(Xs).

print_row(X) :-

print_row(X, 1).

print_row(_, 9) :- nl.

print_row(X, N) :-

(X =:= N -> write('Q ') ; write('. ')),

N1 is N + 1,

print_row(X, N1).

 Sample Output:
?- solve_queens(Board), print_board(Board).

...Q....

.Q......

......Q.
..Q.....

.....Q..

.......Q

....Q...

Q.......

Board = [4, 2, 7, 3, 6, 8, 5, 1]

4. Monkey-Banana Problem:
 Program:
% Define the initial state of the problem

initial_state(state(at_door, on_floor, at_window, has_not)).

% Define the goal state of the problem

goal_state(state(_, _, _, has)).

% Define the available actions

action(climb, state(on_floor, on_floor, on_floor, Has), state(on_box, on_floor, on_floor, Has)).

action(push_box, state(at_window, on_floor, at_window, Has), state(at_window, on_floor, at_window,


Has)).

action(grab_banana, state(at_window, on_box, at_window, has_not), state(at_window, on_box,


at_window, has)).

action(walk, state(P, on_floor, P, Has), state(P1, on_floor, P1, Has)) :- can_walk(P, P1).

% Define the possible positions

position(at_window).

position(at_door).

position(on_floor).

position(on_box).

% Define the valid movements


can_walk(P, P1) :- walkable(P), walkable(P1), P \= P1.

walkable(on_floor).

walkable(on_box).

% Define the possible states after performing an action

possible_states(State, NewState) :- action(_, State, NewState).

% Define the path to the goal state

path_to_goal(State, Path) :- path_to_goal(State, [], Path).

path_to_goal(State, Path, Path) :- goal_state(State).

path_to_goal(State, AccPath, Path) :-

possible_states(State, NewState),

\+ member(NewState, AccPath),

path_to_goal(NewState, [NewState|AccPath], Path).

% Print the path to the goal state

print_path(Path) :- reverse(Path, ReversedPath), print_path(ReversedPath, 0).

print_path([], _).

print_path([State|States], Step) :-

Step1 is Step + 1,

format('Step ~w: ~w~n', [Step1, State]),

print_path(States, Step1).

 Sample Output:
?- initial_state(InitialState), path_to_goal(InitialState, Path), print_path(Path).

false.

5. Medical diagnosis:
 Program:
% Define the symptoms
symptom(fever).

symptom(cough).

symptom(sore_throat).

symptom(rash).

% Define the diseases and their associated symptoms

disease(measles, [fever, cough, rash]).

disease(cold, [cough, sore_throat]).

disease(influenza, [fever, cough]).

% Define the rules for diagnosis

diagnose(Disease) :-

symptom(Symptom),

\+ has_symptom(Disease, Symptom),

!,

fail.

diagnose(Disease) :- disease(Disease, _).

% Define the predicate to check if a disease has a symptom

has_symptom(Disease, Symptom) :- disease(Disease, Symptoms), member(Symptom, Symptoms).

% Define the predicate to get all possible diagnoses

possible_diagnoses(Diagnoses) :- findall(Disease, diagnose(Disease), Diagnoses).

% Define the predicate to print the possible diagnoses

print_diagnoses([]).

print_diagnoses([Diagnosis|Diagnoses]) :-

format("~w~n", [Diagnosis]),

print_diagnoses(Diagnoses).
 Sample Output:
?- possible_diagnoses(Diagnoses), print_diagnoses(Diagnoses).

measles

cold

influenza

Diagnoses = [measles, cold, influenza].

6. 8-Puzzle Problem:
 Program:
% Simple Prolog Planner for the 8 Puzzle Problem

% This predicate initialises the problem states. The first argument

% of solve/3 is the initial state, the 2nd the goal state, and the

% third the plan that will be produced.

test(Plan):-

write('Initial state:'),nl,

Init= [at(tile4,1), at(tile3,2), at(tile8,3), at(empty,4), at(tile2,5), at(tile6,6), at(tile5,7), at(tile1,8),


at(tile7,9)],

write_sol(Init),

Goal= [at(tile1,1), at(tile2,2), at(tile3,3), at(tile4,4), at(empty,5), at(tile5,6), at(tile6,7), at(tile7,8),


at(tile8,9)],

nl,write('Goal state:'),nl,

write(Goal),nl,nl,

solve(Init,Goal,Plan).

solve(State, Goal, Plan):-

solve(State, Goal, [], Plan).

%Determines whether Current and Destination tiles are a valid move.


is_movable(X1,Y1) :- (1 is X1 - Y1) ; (-1 is X1 - Y1) ; (3 is X1 - Y1) ; (-3 is X1 - Y1).

% This predicate produces the plan. Once the Goal list is a subset

% of the current State the plan is complete and it is written to

% the screen using write_sol/1.

solve(State, Goal, Plan, Plan):-

is_subset(Goal, State), nl,

write_sol(Plan).

solve(State, Goal, Sofar, Plan):-

act(Action, Preconditions, Delete, Add),

is_subset(Preconditions, State),

\+ member(Action, Sofar),

delete_list(Delete, State, Remainder),

append(Add, Remainder, NewState),

solve(NewState, Goal, [Action|Sofar], Plan).

% The problem has three operators.

% 1st arg = name

% 2nd arg = preconditions

% 3rd arg = delete list

% 4th arg = add list.

% Tile can move to new position only if the destination tile is empty & Manhattan distance = 1

act(move(X,Y,Z),

[at(X,Y), at(empty,Z), is_movable(Y,Z)],

[at(X,Y), at(empty,Z)],
[at(X,Z), at(empty,Y)]).

% Utility predicates.

% Check is first list is a subset of the second

is_subset([H|T], Set):-

member(H, Set),

is_subset(T, Set).

is_subset([], _).

% Remove all elements of 1st list from second to create third.

delete_list([H|T], Curstate, Newstate):-

remove(H, Curstate, Remainder),

delete_list(T, Remainder, Newstate).

delete_list([], Curstate, Curstate).

remove(X, [X|T], T).

remove(X, [H|T], [H|R]):-

remove(X, T, R).

write_sol([]).

write_sol([H|T]):-

write_sol(T),

write(H), nl.

append([H|T], L1, [H|L2]):-


append(T, L1, L2).

append([], L, L).

member(X, [X|_]).

member(X, [_|T]):-

member(X, T).

 Sample Output:
?- test(Plan).

Initial state:

at(tile7,9)

at(tile1,8)

at(tile5,7)

at(tile6,6)

at(tile2,5)

at(empty,4)

at(tile8,3)

at(tile3,2)

at(tile4,1)

Goal state:

[at(tile1,1),at(tile2,2),at(tile3,3),at(tile4,4),at(empty,5),at(tile5,6),at(tile6,7),at(tile7,8),at(tile8,9)]

false.

7. Factorial of a given Number


 Program:
% Define the base case: factorial of 0 is 1

factorial(0, 1).
% Define the recursive case: factorial of N is N multiplied by factorial of N-1

factorial(N, Result) :-

N > 0,

N1 is N - 1,

factorial(N1, SubResult),

Result is N * SubResult.

 Sample Output:
?- factorial(5, Result).

Result = 120

8. Converting Temperature
 Program:
% Convert Celsius to Fahrenheit

celsius_to_fahrenheit(Celsius, Fahrenheit) :-

Fahrenheit is (Celsius * 9/5) + 32.

% Convert Fahrenheit to Celsius

fahrenheit_to_celsius(Fahrenheit, Celsius) :-

Celsius is (Fahrenheit - 32) * 5/9.

% Convert Celsius to Kelvin

celsius_to_kelvin(Celsius, Kelvin) :-

Kelvin is Celsius + 273.15.

% Convert Kelvin to Celsius

kelvin_to_celsius(Kelvin, Celsius) :-

Celsius is Kelvin - 273.15.

% Convert Fahrenheit to Kelvin


fahrenheit_to_kelvin(Fahrenheit, Kelvin) :-

Celsius is (Fahrenheit - 32) * 5/9,

Kelvin is Celsius + 273.15.

% Convert Kelvin to Fahrenheit

kelvin_to_fahrenheit(Kelvin, Fahrenheit) :-

Celsius is Kelvin - 273.15,

Fahrenheit is (Celsius * 9/5) + 32.

 Sample Output:
?- celsius_to_fahrenheit(25, Result).

Result = 77.

?- fahrenheit_to_celsius(77, Result).

Result = 25.

?- celsius_to_kelvin(25, Result).

Result = 298.15.

?- kelvin_to_celsius(298.15, Result).

Result = 25.0.

?- fahrenheit_to_kelvin(77, Result).

Result = 298.15.

?- kelvin_to_fahrenheit(298.15, Result).

Result = 77.0.

You might also like