Professional Documents
Culture Documents
AI Lab Manual
AI Lab Manual
min(_,Y,Y).
rev(L,R):-revacc(L,[],R).
revacc([],R,R):-!.
revacc([H|T],A,R):-revacc(T,[H|A],R).
%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
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]
Finally : [4,4,0]
true
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) :-
valid_board(Board),
place_queens(Board).
valid_board([]).
valid_board([X|Xs]) :-
valid_board(Xs),
place_queens([]).
place_queens([X|Xs]) :-
place_queens(Xs),
no_attack(X, [Y|Ys], N) :-
X =\= Y,
X + N =\= Y,
X - N =\= Y,
N1 is N + 1,
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) :-
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
goal_state(state(_, _, _, has)).
action(walk, state(P, on_floor, P, Has), state(P1, on_floor, P1, Has)) :- can_walk(P, P1).
position(at_window).
position(at_door).
position(on_floor).
position(on_box).
walkable(on_floor).
walkable(on_box).
possible_states(State, NewState),
\+ member(NewState, AccPath),
print_path([], _).
print_path([State|States], Step) :-
Step1 is Step + 1,
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).
diagnose(Disease) :-
symptom(Symptom),
\+ has_symptom(Disease, Symptom),
!,
fail.
print_diagnoses([]).
print_diagnoses([Diagnosis|Diagnoses]) :-
format("~w~n", [Diagnosis]),
print_diagnoses(Diagnoses).
Sample Output:
?- possible_diagnoses(Diagnoses), print_diagnoses(Diagnoses).
measles
cold
influenza
6. 8-Puzzle Problem:
Program:
% Simple Prolog Planner for the 8 Puzzle Problem
% of solve/3 is the initial state, the 2nd the goal state, and the
test(Plan):-
write('Initial state:'),nl,
write_sol(Init),
nl,write('Goal state:'),nl,
write(Goal),nl,nl,
solve(Init,Goal,Plan).
% This predicate produces the plan. Once the Goal list is a subset
write_sol(Plan).
is_subset(Preconditions, State),
\+ member(Action, Sofar),
% 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)],
[at(X,Z), at(empty,Y)]).
% Utility predicates.
is_subset([H|T], Set):-
member(H, Set),
is_subset(T, Set).
is_subset([], _).
remove(X, T, R).
write_sol([]).
write_sol([H|T]):-
write_sol(T),
write(H), nl.
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.
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_to_celsius(Fahrenheit, Celsius) :-
celsius_to_kelvin(Celsius, Kelvin) :-
kelvin_to_celsius(Kelvin, Celsius) :-
kelvin_to_fahrenheit(Kelvin, Fahrenheit) :-
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.