Experiment No. 1 Aim:-Theory:-: Sec, Cse, Washimpage 1
Experiment No. 1 Aim:-Theory:-: Sec, Cse, Washimpage 1
Experiment No. 1 Aim:-Theory:-: Sec, Cse, Washimpage 1
Experiment No. 1
Theory:-
Prolog is a logic based language useful for solving problem existing in a domain of fact
and rules. The name “prolog” stands for programming LOGIC indicating its strong roots in
formal logic.
Prolog won designed in the 1970 by Alian colmeyauer and a leam of researches with the
idea new at the time that it won possible to we logic to represent knowledge and to write the
program more precisely, prolog user a subnet of predicate logic and draw its structure from
theoretical work of earlier logiciaous such an Herbrand (1930) and Robinson(1905) on the
arithmetic of theorem. Prolog was originally intended for the written. Because of its concessioner
and simplicity it’s become popular used beyond this domain and now has adepts in axis such as:
The branch of computer science concerned with making computer behave like human AI
is intelligence exhibited by computer science that develop machines and software intelligence
all research is highly technical and specified and on deeply divided into subject.
The control programs are goals of AI research include reasoning, knowledge, planning,
communication, perception and manipulation objects. General intelligence is still among the
field long term used goal currents popular approaches include statistical method computer
intelligence and traditional symbolic AI.
There are enormous of roots and in AI including resend of search and math optimization
logic method based and probabilities and the claim that a central properly of human intelligence
can be presently simulated by machine.
This raise philosophical uriue about the nature of mind and ethics of creating AI in brain
and begins the and clarify their issues firstly it should be clear that what is the efficiency between
knowledge, intelligence data information.
Goal Of AI
Deduction, reasoning, problem solving.
Knowledge representation
SEC,CSE,WASHIMPage 1
AI Lab Manual
Planning
Learning
Nature language processing
Motion and manipulation
Perception
Social Intelligence
Creatively
General Intelligence
Prolog:-
The name taken from phase programming language. The language was originally
developed on 1972 by alion colnerauoer and D. Raunal at the university of maselar in the form
prologs unique in the its ability to inter drive by formal manners presenting and knowledge about
problems about specifying a almost all language developed for the computers driving few
decodes are known general procedure language. Such an FURTRON, COBOC, BASIC
PASCAL all example of procedural language a algo or procedure must first be defined to store
the problem.
The prolog written using procedural language.
Parts Of Prolog Program:-
Domain:-
The section of code where are we defined the legal value for any type i.e., not defined an
a standard types domain declaration can also be used to defined structure that are not defined by
standard domain.
Predicates:-
The PREDICATES section in where are defined predicate to be used in the CLAUSES
section and define the domain is for their arguments. We found it best to think of predicate
declaration on function prototype.
Clauses:-
Clauses are the heart of the program a clause in an instance of a predicate followed by a
period clauses are of two types
1) Facts
2) Rules
SEC,CSE,WASHIMPage 2
AI Lab Manual
1) Facts:-
Facts in the CLAUSE section are relations that are known to be true by the programmer
self standing basis for interface. A property of an object of a relation between objects.
2) Rules:-
User to inter other fact property or relation known given the fact those some other sets of
relations is known.
Goals:-
Part of program where requires are made can be a singular or compound each part of
compound goals.
Conclusion:-
Hence, we studied the artificial intelligence and prolog
SEC,CSE,WASHIMPage 3
AI Lab Manual
Experiment No. 2
predicates
Display:
Logic:
Write clauses location (city, state) and stays (person, city)
Program:
%clauses
location(amravati,maharashtra).
location(nagpur, maharashtra).
location(jodhpur,rajasthan).
stays(ram, nagpur).
stays(lakshman, amravati).
stays(seeta,jodhpur).
SEC,CSE,WASHIMPage 4
AI Lab Manual
Output:
Conclusion:- Thus we have created program containing facts related to following predicates
1.location (city, state)
SEC,CSE,WASHIMPage 5
AI Lab Manual
Experiment No. 3
Based on the facts , define goals to answer questions related to family tree.
Parent/father
abid male
Program:
male(abid).
male(prince).
male(sopan).
male(sakif).
female(riya).
female(suma).
SEC,CSE,WASHIMPage 6
AI Lab Manual
female(ripa).
parents(abid,prince).
parents(abid,riya).
parents(riya,sopan).
parents(riya,suma).
parents(prince,ripa).
parents(prince,sakif).
father(X,Y):-parents(X,Y),male(X).
mother(X,Y):-parents(X,Y),female(X).
sibling(X,Y):-parents(Z,X),parents(Z,Y),X\=Y.
brother(X,Y):-sibling(X,Y),male(X).
sister(X,Y):-sibling(X,Y),female(X).
grandfather(X,Z):-parents(X,Y),parents(Y,Z),male(X).
grandmother(X,Y):-parents(x,Y),parents(Y,Z),female(X).
SEC,CSE,WASHIMPage 7
AI Lab Manual
Output:
SEC,CSE,WASHIMPage 8
AI Lab Manual
Experiment No. 4
Program:
go:-
write('1:Concatenation'),nl,
write('2:Sub_String'),nl,
write('3:String Length'),nl,
write('Enter a choice'),nl,
read(A),
solve(A).
solve(A):-
A=1,
read(B),
read(C),
string_concat(B,C,D),
write('Concatenation is '),write(D),nl,
go.
solve(A):-
A=2,
read(B),
read(Pos),
SEC,CSE,WASHIMPage 9
AI Lab Manual
read(Len),
sub_string(B,Pos,Len,_,D),
write('Sub_String is '),write(D),nl,
go.
solve(A):-
A=3,
read(B),
string_length(B,D),
go.
SEC,CSE,WASHIMPage 10
AI Lab Manual
Output:-
Some queries are there which you can fire directly on console window; there is no need to write
code for such operations.
1] query:- forall(append(_,[F|S],"GOODMORNING"),format('"~s"~n',[[F|S]])).
"GOODMORNING"
"OODMORNING"
"ODMORNING"
"DMORNING"
"MORNING"
"ORNING"
SEC,CSE,WASHIMPage 11
AI Lab Manual
"RNING"
"NING"
"ING"
"NG"
"G"
true.
SEC,CSE,WASHIMPage 12
AI Lab Manual
Experiment No.5
Theory:
The shortest path problem is the problem of finding a path between two verities (or nodes) in a
graph such that the sum of weight of its constituent edges is minimized.
In this program, we have to find a shortest path between two nodes which is shown in the fig.
Consider that ‘a' is our starting node and 'g' is our goal node and we have to find shortest path
between these two nodes . Now we have three possibilities They are follows:
1)a-b-e-g----->22
2)a-b-d-f-g------->33
3)a-c-d-g---------->24
From above observation ,we conclude that first path a-b-e-g is shortest path among these
3 paths its weight is 22.
Logic:
Logic programming is a programming language par diagram in which logical assertions are
viewed as a programs. There are several logic programming systems in use today. The most
popular of which is PROLOG. Programming in PROLOG has been described as a series of logic
assertion each of which is a horn clause.
PROLOG TERMINOLOGY:
b] Clause: Clauses are the actual rules and facts that constitute the PROLOG program.
c] Do: Do works like a function in PROLOG program. The syntax for 'do' is :do:-
e] Make windows( ): In make windows( ) we specify the size of PROLOG for output with its
name ex. makewindows(10,5,9," shortest path ",0,0,15, 54)
f] write( ) and readln ( ):write ( ) is behave like a printf( ) in C programming and ends with , This
is used for displaying the output.
SEC,CSE,WASHIMPage 13
AI Lab Manual
Readln( ) is behave like scanf( ) in c programming to take input from user and end with.
g] goal do: In the PROLOG programming , program always ends with goal do.
Program:
path(X,Y,[arc(X,Y)]) :-
arc(X,Y).
path(X,Y,[arc(X,Z)|P]) :-
arc(X,Z),
path(Z,Y,P).
Output:
Result :In this way we have performed a program for shortest path distance.
SEC,CSE,WASHIMPage 14
AI Lab Manual
Experiment No. 6
Aim: write a program for Depth First Search using the suitable example.
Algorithm:
1. Place starting node in the queue.
Otherwise
4. Remove and expand the first element and place the children at front of the queue.
5. Return to step2.
Program:
successor(a,b).
successor(b,d).
successor(b,e).
successor(a,c).
SEC,CSE,WASHIMPage 15
AI Lab Manual
successor(c,f).
successor(c,g).
final(d).
final(e).
final(f).
final(g).
member(X,[X|_]).
member(X,[_|T]):-
member(X,T).
solve(Node,Solution):-
depthfirstsearch([],Node,Solution).
depthfirstsearch(Path,Node,[Node|Path]):-final(Node).
depthfirstsearch(Path,Node,Solution):-successor(Node,Node1),
not(member(Node1,Path)),write(Node1),
depthfirstsearch([Node|Path],Node1,Solution).
Output:
SEC,CSE,WASHIMPage 16
AI Lab Manual
Experiment No. 7
Logic:-
In a room there is a banana hanging from the ceiling, a box by the window, & a monkey
by the door.
The monkey must move the box to the centre of the room and climb on it to reach the banana.
Initial State: monkey at door, monkey on floor, box at window, monkey does not have banana
Program code:
on(floor,monkey).
on(floor,box).
in(room,monkey).
in(room,box).
in(room,banana).
at(ceiling,banana).
strong(monkey).
grasp(monkey).
climb(monkey,box).
push(monkey,box):-
strong(monkey).
under(banana,box):-
push(monkey,box).
canreach(banana,monkey):-
at(floor,banana);
SEC,CSE,WASHIMPage 17
AI Lab Manual
at(ceiling,banana),
under(banana,box),
climb(monkey,box).
canget(banana,monkey):-
canreach(banana,monkey),grasp(monkey).
Output:
SEC,CSE,WASHIMPage 18
AI Lab Manual
Experiment No. 8
Theory:
Tower of Hanoi is a recursive algorithm. It contains 3 pages labelled as BEG AUX and END.
Suppose a page BEG these are finite number of disk available with decreasing size consider on
edge firstly at initial position BEG has object. Then we have to move the disks from page BEG
to page END by using page AUX as an auxiliary page.
Only one disk may move at a time specifically only top disk on any larger disk cannot be
placed on the smaller disk. To solve the tower of Hanoi problem using recursion first we
observed that the solution of tower of Hanoi problem for n>1 disk may be reduce to following
sub problem.
Sub problem:
1) Move an n-1 disk from peg BEG to AUX
2) Move to disk from peg BEG to END
3) Move top n-1 disk from peg AUX to END
Let us how introduce general notation
To denote a produce which moves the top n disk from initial peg BEG to find peg END
using peg AUX when,
This gives the single instruction as BEG to END may be reduce to following subprogram.
Algorithm:
Tower (N, BEG, AUX, END)
1) if N=1 then
b. RETURN
5) RETURN
Program:
/*WAP for tower of Hanoi in prolog*/
Output:
?- move(3,a,b,c).
Move disk from a to b
Move disk from a to c
Move disk from b to c
Move disk from a to b
Move disk from c to a
Move disk from c to b
Move disk from a to b
true .
3 ?- move(5,a,b,c).
Move disk from a to b
Move disk from a to c
Move disk from b to c
Move disk from a to b
Move disk from c to a
Move disk from c to b
Move disk from a to b
Move disk from a to c
SEC,CSE,WASHIMPage 20
AI Lab Manual
SEC,CSE,WASHIMPage 21