Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

PROLOG Program to Find

Factorial of a Number
predicates
factorial(integer, real)
go
clauses
go if
write(“Enter a positive integer number:”),
readint(N),
factorial(N,Result),
write(“Factorial of”, N, “is=”, Result).
factorial(0, 1)
factorial(N, Result) if N>0,
N1=N-1,
factorial(N1, Res),
Result=N*Res.
[/python]
Output:
[python]goals:
factorial(5, Answer)
Answer=120

Write a prolog program to find the


permutation of a set.
list = symbol*

%predicates

permute(list,list).

del(symbol,list,list).

%clauses

del(X,[X|L1],L1).
del(X,[Y|L1],[Y|L2]):-

del(X,L1,L2).

permute([],[]).

permute(L,[X|P]):-

del(X,L,L1),

permute(L1,P).

Input : permute([a,b,c],P).
Output :
P=["a","b","c"];
P=["a","c","b"];
P=["b","a","c"];
P=["b","c","a"];
P=["c","a","b"];
P=["c","b","a"];

Prolog Program to append/concatenate two Lists.


Program:
domains
list=integer*

predicates
append(list,list,list).

clauses
append([],L,L).
append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).

goal with output


goal: append([1,2,3,4],[5,6,7,8,9],X)

output: X=[1,2,3,4,5,6,7,8,9]
Prolog Program to find greater of two numbers x and y.
max(X,Y):-
(
X=Y ->
write('both are equal')
;
X>Y ->
(
Z is X,
write(‘maximum is ‘,Z)
)
;
(
Z is Y,
write(‘maximum is ‘,Z)
)
).
Output:----
max(3,6)
maximum is 6

Write a Prolog program to find the greatest number


in the list of numbers List.
maxlist([H|T],R):-
length(T,L),
L>0 ->
(
maxlist(T,R1),
(
H > R1 ->
R is H
;
R is R1
)
)
;
R is H.
Output:----

maxlist([1,4,5,2,7,8,9],0)
9
Write a Prolog program to find sum of given list of
numbers.
domains
x = integer
l = integer*
predicates
sum(l,x)
clauses
sum([],0).
sum([X|List],Sum) :-
sum(List,Sum1),
Sum = X + Sum1.

Output :

Goal: sum([1,2,3,4],Sum)
Sum=10
1 Solution

Goal: sum([-2,-1,1,2],Sum)
Sum=0
1 Solution

Goal: sum([],Sum)
Sum=0
1 Solution

Goal: sum([1],Sum)
Sum=1
1 Solution

Prolog Program to Reverse a List


reverse([], Y, R) :-

R = Y.

reverse([H|T] , Y, R) :-

reverse(T, [H|Y], R).

Output

write a program in prolog to solve 8


queens problem
DOMAINS
cell=c(integer,integer)
list=cell*
int_list=integer*
PREDICATES
solution(list)
member(integer,int_list)
nonattack(cell,list)
CLAUSES
solution([]).
solution([c(X,Y)|Others]):-
solution(Others),
member(Y,[1,2,3,4,5,6,7,8]),
nonattack(c(X,Y),Others).
nonattack(_,[]).
nonattack(c(X,Y),[c(X1,Y1)|Others]):-
Y<>Y1,
Y1-Y<>X1-X,
Y1-Y<>X-X1,
nonattack(c(X,Y),Others).
member(X,[X|_]).
member(X,[_|Z]):-
member(X,Z).
GOAL
solution([c(1,A),c(2,B),c(3,C),c(4,D),c(5,E),c(6,F),c(7,G),c(8,H)]).
Output
A=4, B=2, C=7, D=3, E=6, F=8, G=5, H=1
A=5, B=2, C=4, D=7, E=3, F=8, G=6, H=1
A=3, B=5, C=2, D=8, E=6, F=4, G=7, H=1
A=3, B=6, C=4, D=2, E=8, F=5, G=7, H=1
A=5, B=7, C=1, D=3, E=8, F=6, G=4, H=2
A=4, B=6, C=8, D=3, E=1, F=7, G=5, H=2
A=3, B=6, C=8, D=1, E=4, F=7, G=5, H=2
A=5, B=3, C=8, D=4, E=7, F=1, G=6, H=2
A=5, B=7, C=4, D=1, E=3, F=8, G=6, H=2
A=4, B=1, C=5, D=8, E=6, F=3, G=7, H=2
A=3, B=6, C=4, D=1, E=8, F=5, G=7, H=2
A=4, B=7, C=5, D=3, E=1, F=6, G=8, H=2
A=6, B=4, C=2, D=8, E=5, F=7, G=1, H=3
A=6, B=4, C=7, D=1, E=8, F=2, G=5, H=3
A=1, B=7, C=4, D=6, E=8, F=2, G=5, H=3
A=6, B=8, C=2, D=4, E=1, F=7, G=5, H=3
A=6, B=2, C=7, D=1, E=4, F=8, G=5, H=3
A=4, B=7, C=1, D=8, E=5, F=2, G=6, H=3
A=5, B=8, C=4, D=1, E=7, F=2, G=6, H=3
A=4, B=8, C=1, D=5, E=7, F=2, G=6, H=3
A=2, B=7, C=5, D=8, E=1, F=4, G=6, H=3
A=1, B=7, C=5, D=8, E=2, F=4, G=6, H=3
A=2, B=5, C=7, D=4, E=1, F=8, G=6, H=3
A=4, B=2, C=7, D=5, E=1, F=8, G=6, H=3
A=5, B=7, C=1, D=4, E=2, F=8, G=6, H=3
A=6, B=4, C=1, D=5, E=8, F=2, G=7, H=3
A=5, B=1, C=4, D=6, E=8, F=2, G=7, H=3
A=5, B=2, C=6, D=1, E=7, F=4, G=8, H=3
A=6, B=3, C=7, D=2, E=8, F=5, G=1, H=4
A=2, B=7, C=3, D=6, E=8, F=5, G=1, H=4
A=7, B=3, C=1, D=6, E=8, F=5, G=2, H=4
A=5, B=1, C=8, D=6, E=3, F=7, G=2, H=4
A=1, B=5, C=8, D=6, E=3, F=7, G=2, H=4
A=3, B=6, C=8, D=1, E=5, F=7, G=2, H=4
A=6, B=3, C=1, D=7, E=5, F=8, G=2, H=4
A=7, B=5, C=3, D=1, E=6, F=8, G=2, H=4
A=7, B=3, C=8, D=2, E=5, F=1, G=6, H=4
A=5, B=3, C=1, D=7, E=2, F=8, G=6, H=4
A=2, B=5, C=7, D=1, E=3, F=8, G=6, H=4
A=3, B=6, C=2, D=5, E=8, F=1, G=7, H=4
A=6, B=1, C=5, D=2, E=8, F=3, G=7, H=4
A=8, B=3, C=1, D=6, E=2, F=5, G=7, H=4
A=2, B=8, C=6, D=1, E=3, F=5, G=7, H=4
A=5, B=7, C=2, D=6, E=3, F=1, G=8, H=4
A=3, B=6, C=2, D=7, E=5, F=1, G=8, H=4
A=6, B=2, C=7, D=1, E=3, F=5, G=8, H=4
A=3, B=7, C=2, D=8, E=6, F=4, G=1, H=5
A=6, B=3, C=7, D=2, E=4, F=8, G=1, H=5
A=4, B=2, C=7, D=3, E=6, F=8, G=1, H=5
A=7, B=1, C=3, D=8, E=6, F=4, G=2, H=5
A=1, B=6, C=8, D=3, E=7, F=4, G=2, H=5
A=3, B=8, C=4, D=7, E=1, F=6, G=2, H=5
A=6, B=3, C=7, D=4, E=1, F=8, G=2, H=5
A=7, B=4, C=2, D=8, E=6, F=1, G=3, H=5
A=4, B=6, C=8, D=2, E=7, F=1, G=3, H=5
A=2, B=6, C=1, D=7, E=4, F=8, G=3, H=5
A=2, B=4, C=6, D=8, E=3, F=1, G=7, H=5
A=3, B=6, C=8, D=2, E=4, F=1, G=7, H=5
A=6, B=3, C=1, D=8, E=4, F=2, G=7, H=5
A=8, B=4, C=1, D=3, E=6, F=2, G=7, H=5
A=4, B=8, C=1, D=3, E=6, F=2, G=7, H=5
A=2, B=6, C=8, D=3, E=1, F=4, G=7, H=5
A=7, B=2, C=6, D=3, E=1, F=4, G=8, H=5
A=3, B=6, C=2, D=7, E=1, F=4, G=8, H=5
A=4, B=7, C=3, D=8, E=2, F=5, G=1, H=6
A=4, B=8, C=5, D=3, E=1, F=7, G=2, H=6
A=3, B=5, C=8, D=4, E=1, F=7, G=2, H=6
A=4, B=2, C=8, D=5, E=7, F=1, G=3, H=6
A=5, B=7, C=2, D=4, E=8, F=1, G=3, H=6
A=7, B=4, C=2, D=5, E=8, F=1, G=3, H=6
A=8, B=2, C=4, D=1, E=7, F=5, G=3, H=6
A=7, B=2, C=4, D=1, E=8, F=5, G=3, H=6
A=5, B=1, C=8, D=4, E=2, F=7, G=3, H=6
A=4, B=1, C=5, D=8, E=2, F=7, G=3, H=6
A=5, B=2, C=8, D=1, E=4, F=7, G=3, H=6
A=3, B=7, C=2, D=8, E=5, F=1, G=4, H=6
A=3, B=1, C=7, D=5, E=8, F=2, G=4, H=6
A=8, B=2, C=5, D=3, E=1, F=7, G=4, H=6
A=3, B=5, C=2, D=8, E=1, F=7, G=4, H=6
A=3, B=5, C=7, D=1, E=4, F=2, G=8, H=6
A=5, B=2, C=4, D=6, E=8, F=3, G=1, H=7
A=6, B=3, C=5, D=8, E=1, F=4, G=2, H=7
A=5, B=8, C=4, D=1, E=3, F=6, G=2, H=7
A=4, B=2, C=5, D=8, E=6, F=1, G=3, H=7
A=4, B=6, C=1, D=5, E=2, F=8, G=3, H=7
A=6, B=3, C=1, D=8, E=5, F=2, G=4, H=7
A=5, B=3, C=1, D=6, E=8, F=2, G=4, H=7
A=4, B=2, C=8, D=6, E=1, F=3, G=5, H=7
A=6, B=3, C=5, D=7, E=1, F=4, G=2, H=8
A=6, B=4, C=7, D=1, E=3, F=5, G=2, H=8
A=4, B=7, C=5, D=2, E=6, F=1, G=3, H=8
A=5, B=7, C=2, D=6, E=3, F=1, G=4, H=8

Write a program in prolog to solve 8 Puzzle


problems

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 */

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).

Output:

% Queries:

?- 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(tile
5,6),at(tile6,7),at(tile7,8),at(tile8,9)]

false.

Breadth First Search in Prolog


%connected(+Start, +Goal, -Weight)
connected(1,7,1).

connected(1,8,1).

connected(1,3,1).

connected(7,4,1).

connected(7,20,1).

connected(7,17,1).

connected(8,6,1).

connected(3,9,1).

connected(3,12,1).

connected(9,19,1).

connected(4,42,1).

connected(20,28,1).

connected(17,10,1).

connected2(X,Y,D) :- connected(X,Y,D).

connected2(X,Y,D) :- connected(Y,X,D).

next_node(Current, Next, Path) :-

connected2(Current, Next, _),

not(member(Next, Path)).

breadth_first(Goal, Goal, _,[Goal]).

breadth_first(Start, Goal, Visited, Path) :-

findall(X,
(connected2(X,Start,_),not(member(X,Visited))),

[T|Extend]),

write(Visited), nl,

append(Visited, [T|Extend], Visited2),

append(Path, [T|Extend], [Next|Path2]),

breadth_first(Next, Goal, Visited2, Path2).

Output:----

?- breadth_first(1, 28, [1], []).

DFS implement:
%connected(+Start, +Goal, -Weight)

connected(1,7,1).

connected(1,8,1).

connected(1,3,1).

connected(7,4,1).

connected(7,20,1).

connected(7,17,1).

connected(8,6,1).

connected(3,9,1).

connected(3,12,1).

connected(9,19,1).

connected(4,42,1).

connected(20,28,1).
connected(17,10,1).

connected2(X,Y,D) :- connected(X,Y,D).

connected2(X,Y,D) :- connected(Y,X,D).

next_node(Current, Next, Path) :-

connected2(Current, Next, _),

not(member(Next, Path)).

depth_first(Goal, Goal, _, [Goal]).

depth_first(Start, Goal, Visited, [Start|Path]) :-

next_node(Start, Next_node, Visited),

write(Visited), nl,

depth_first(Next_node, Goal, [Next_node|Visited], Path).

Output:----

?- depth_first(1, 28, [1], P).

best first search


transition(city_a, city_b, 10).
transition(city_a, city_c, 5).
transition(city_b, city_d, 15).
transition(city_b, city_e, 20).
transition(city_c, city_e, 10).
transition(city_d, city_f, 5).
transition(city_e, city_f, 8).
transition(city_e, city_g, 12).

% Define the heuristic function. This is


problem-specific.
% In this example, it's the straight-line
distance between cities.
heuristic(city_a, H) :- H is 15. % Assuming
city_a to city_f is 15 units away.
heuristic(city_b, H) :- H is 12. % Assuming
city_b to city_f is 12 units away.
heuristic(city_c, H) :- H is 20. % Assuming
city_c to city_f is 20 units away.
heuristic(city_d, H) :- H is 8. % Assuming
city_d to city_f is 8 units away.
heuristic(city_e, H) :- H is 5. % Assuming
city_e to city_f is 5 units away.
heuristic(city_f, H) :- H is 0. % The goal
state.

% best_first_search/3 is the main


predicate.
best_first_search(Start, Goal, Path) :-
best_first_search([[Start, 0]], [], Goal,
Path).
% best_first_search/4 handles the BFS
algorithm.
best_first_search([[Node, _] | _], _, Goal,
[Node | []]) :- Node = Goal.
best_first_search([[Node, Cost] | Rest],
Visited, Goal, Path) :-
findall([Child, NewCost], (
transition(Node, Child, StepCost),
NewCost is Cost + StepCost,
heuristic(Child, H),
NewCost + H, Child
), Children),
append(Children, Rest, NewOpen),
best_first_search(NewOpen, [Node |
Visited], Goal, Path).

% Sample query:
% best_first_search(city_a, city_f, Path).

Travelling Salesman Problem with


Prolog
domains
/* will allow us cooperate with better names, for me
this is like #typedef in C++ */
town = symbol
distance = unsigned
rib = r(town,town,distance)
tlist = town*
rlist = rib*
predicates
nondeterm way(town,town,rlist,distance)
nondeterm route(town,town,rlist,tlist,distance)
nondeterm route1(town,tlist,rlist,tlist,distance)
nondeterm ribsmember(rib,rlist)
nondeterm townsmember(town,tlist)
nondeterm tsp(town,town,tlist,rlist,tlist,distance)
nondeterm ham(town,town,tlist,rlist,tlist,distance)
nondeterm
shorterRouteExists(town,town,tlist,rlist,distance)
nondeterm alltown(tlist,tlist)
nondeterm write_list(tlist)
clauses
/*
Nothing special with write_list.
If list is empty we do nothing,
and if something there we write head and call
ourselves for tail.
*/
write_list([]).
write_list([H|T]):-
write(H,‘ ‘),
write_list(T).
/* Is true if town X is in list of towns… */
townsmember(X,[X|_]).
townsmember(X,[_|L]):-
townsmember(X,L).
/* Is true if rib X is in list of ribs… */
ribsmember(r(X,Y,D),[r(X,Y,D)|_]).
ribsmember(X,[_|L]):-
ribsmember(X,L).
/* Is true if Route consists of all Towns presented
in second argument */
alltown(_,[]).
alltown(Route,[H|T]):-
townsmember(H,Route),
alltown(Route,T).

/* Is true if there is a way from Town1 to Town2, and


also return distance between them */
way(Town1,Town2,Ways,OutWayDistance):-
ribsmember(r(Town1,Town2,D),Ways),
OutWayDistance = D.

%/*
/* If next is uncommented then we are using non-
oriented graph*/
way(Town1,Town2,Ways,OutWayDistance):-
ribsmember(r(Town2,Town1,D),Ways), /*switching
direction here…*/
OutWayDistance = D.
%*/

/* Is true if we could build route from Town1 to


Town2 */
route(Town1,Town2,Ways,OutRoute,OutDistance):-
route1(Town1,[Town2],Ways,OutRoute,T1T2Distance),
%SWITCH HERE
way(Town2,Town1,Ways,LasDist), /* If you want find
shortest way comment this line*/
OutDistance = T1T2Distance + LasDist. /* And make
this: OutDistance = T1T2Distance.*/

route1(Town1,[Town1|Route1],_,[Town1|
Route1],OutDistance):-
OutDistance = 0.
/* Does the actual finding of route. We take new
TownX town and if it is not member of PassedRoute,
we continue searching with including TownX in the
list of passed towns.*/
route1(Town1,[Town2|
PassedRoute],Ways,OutRoute,OutDistance):-
way(TownX,Town2,Ways,WayDistance),
not(townsmember(TownX,PassedRoute)),
route1(Town1,[TownX,Town2|
PassedRoute],Ways,OutRoute,CompletingRoadDistance),
OutDistance = CompletingRoadDistance + WayDistance.

shorterRouteExists(Town1,Town2,Towns,Ways,Distance):-
ham(Town1,Town2,Towns,Ways,_,Other),
Other < Distance.
/* calling tsp(a,a,…. picks any one connected to a
town and calls another tsp*/
tsp(Town1,Town1,Towns,Ways,BestRoute,MinDistance):-
way(OtherTown,Town1,Ways,_),

tsp(Town1,OtherTown,Towns,Ways,BestRoute,MinDistance).
/*Travelling Salesman Problem is Hammilton way which
is the shortes of other ones.*/
tsp(Town1,Town2,Towns,Ways,BestRoute,MinDistance):-
ham(Town1,Town2,Towns,Ways,Route,Distance),

not(shorterRouteExists(Town1,Town2,Towns,Ways,Distance)
),
BestRoute = Route,
MinDistance = Distance.
/*Hammilton route from Town1 to Town2 assuming that
Town2->Town1 way exists.*/
ham(Town1,Town2,Towns,Ways,Route,Distance):-
route(Town1,Town2,Ways,Route,Distance),
%SWITCH HERE
alltown(Route,Towns), % if you want simple road
without including all towns you could uncomment this
line
write_list(Route),
write(” tD = “,Distance,“n“).
% fail.

goal
/* EXAMPLE 1
AllTowns = [a,b,c,d],
AllWays =
[r(a,b,1),r(a,c,10),r(c,b,2),r(b,c,2),r(b,d,5),r(c,d,3)
,r(d,a,4)],
*/
/* EXAMPLE 2 */
AllTowns = [a,b,c,d,e],

AllWays = [r(a,c,1),r(a,b,6),r(a,e,5),r(a,d,8),r(c,b,2)
,r(c,d,7),r(c,e,10),r(b,d,3),r(b,e,9),r(d,e,4)],
tsp(a,a,AllTowns,AllWays,Route,Distance),
%SWITCH HERE
% tsp(a,b,AllTowns,AllWays,Route,Distance),
write(“Finally:n“),
write_list(Route),
write(” tMIN_D = “,Distance,“n“).

output
aedbc D = 15
aedbc D = 15
adebc D = 24
aebdc D = 25
abedc D = 27
adbec D = 31
abdec D = 24
Finally:
aedbc MIN_D = 15

You might also like