Prolog Record

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

PROLOG PROGRAM USING BEST FIRST SEARCH

state_record(State, Parent, G, H, F, [State, Parent, G, H, F]).


precedes([_,_,_,_,F1], [_,_,_,_,F2]) :- F1 =< F2.

% go initializes Open and CLosed and calls path


go(Start, Goal) :-
empty_set(Closed),
empty_sort_queue(Empty_open),
heuristic(Start, Goal, H),
state_record(Start, nil, 0, H, H, First_record),
insert_sort_queue(First_record, Empty_open, Open),
path(Open,Closed, Goal).

path(Open,_,_) :-
empty_sort_queue(Open),
write("graph searched, no solution found").
path(Open, Closed, Goal) :-
remove_sort_queue(First_record, Open, _),
state_record(State, _, _, _, _, First_record),
State = Goal,
write('Solution path is: '), nl,
printsolution(First_record, Closed).

path(Open, Closed, Goal) :-


remove_sort_queue(First_record, Open, Rest_of_open),
(bagof(Child, moves(First_record, Open, Closed, Child, Goal), Children);Children = []),
insert_list(Children, Rest_of_open, New_open),
add_to_set(First_record, Closed, New_closed),
path(New_open, New_closed, Goal),!.

moves(State_record, Open, Closed,Child, Goal) :-


state_record(State, _, G, _,_, State_record),
mov(State, Next),
% not(unsafe(Next)),
state_record(Next, _, _, _, _, Test),
not(member_sort_queue(Test, Open)),
not(member_set(Test, Closed)),
G_new is G + 1,
heuristic(Next, Goal, H),
F is G_new + H,
state_record(Next, State, G_new, H, F, Child).

insert_list([], L, L).
insert_list([State | Tail], L, New_L) :-
insert_sort_queue(State, L, L2),
insert_list(Tail, L2, New_L).
printsolution(Next_record, _):-
state_record(State, nil, _, _,_, Next_record),
write(State), nl.
printsolution(Next_record, Closed) :-
state_record(State, Parent, _, _,_, Next_record),
state_record(Parent, _, _, _, _, Parent_record),
member_set(Parent_record, Closed),
printsolution(Parent_record, Closed),
write(State), nl.
PROLOG PROGRAM TO DEMONSTRATE FAMILY RELATIONSHIP
/* Facts */
male(jack).
male(oliver).
male(ali).
male(james).
male(simon).
male(harry).
female(helen).
female(sophie).
female(jess).
female(lily).

parent_of(jack,jess).
parent_of(jack,lily).
parent_of(helen, jess).
parent_of(helen, lily).
parent_of(oliver,james).
parent_of(sophie, james).
parent_of(jess, simon).
parent_of(ali, simon).
parent_of(lily, harry).
parent_of(james, harry).

/* Rules */
father_of(X,Y):- male(X),
parent_of(X,Y).

mother_of(X,Y):- female(X),
parent_of(X,Y).

Output:
?mother_of(X,lily)

X=helen
PROLOG PROGRAM TO COUNT THE NUMBER OF ELEMENTS IN A LIST

% length of empty list is 0 (base case)


list_length([], 0).
list_length([_ | L], N) :-
list_length(L, N1),
N is N1 + 1.
% If length of L is N1, then length of [_ | L] will be N1 + 1

OUT PUT :
? list_length([a, b, c, d], N).

N=4

PROLOG PROGRAM TO READ ADDRESS OF A PERSON USING COMPOUND


VARIABLE

DOMAINS
    person                         = person(name,address)
    name                           = name(first,last)
    address                       = addr(street,city,state)
    street                          = street(number,street_name)
    city,state,street_name   = string
    first,last                       = string
    number                        = integer
GOAL
    P1 = person(name(jim,mos),addr(street(5,"1st st"),igo,"CA")),
    P1 = person(name(_,mos),Address),
    P2 = person(name(jane,mos),Address),
    write("P1=",P1),nl,
    write("P2=",P2),nl.

OUTPUT:
Jim,mos,5,1st St,igo,CA
Prolog program of Fun to show the Concept of CUT Operator

max(X,Y,X) :- X >= Y,!.


max(X,Y,Y) :- X < Y.

max_find(X,Y,Max) :- X >= Y,!, Max = X; Max = Y.

OUTPUT:
?max_find(10,20,Max).
?Max =20.
PROLOG PROGRAM TO FIND MEMBER OF A SET

list_member(b,[a,[b,c]]).
list_length([a,b,c,d,e,f,g,h,i,j],Len).

OUTPUT :
? list_member(b,[a,[b,c]]).
True

? list_member(f,[a,[b,c]]).
PROLOG PROGRAM TO CONCATENATE TWO SETS

domains    
list=integer*

predicates
 append(list,list,list).

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

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

X = [1, 2, 3, 4, 5, 6, 7, 8, 9]
PROLOG PROGRAM TO FIND PERMUTATION OF A SET

% domains

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). 
 
 
OUTPUT:
? permute([a,b,c],P).

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 SOLVE NQueens PROBLEM

queens(N, Queens) :-
length(Queens, N),
board(Queens, Board, 0, N, _, _),
queens(Board, 0, Queens).

board([], [], N, N, _, _).


board([_|Queens], [Col-Vars|Board], Col0, N, [_|VR], VC) :-
Col is Col0+1,
functor(Vars, f, N),
constraints(N, Vars, VR, VC),
board(Queens, Board, Col, N, VR, [_|VC]).

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:
?queens(8, Queens)

Queens = 








Queens = 
Queens = 








Queens = 

You might also like