Experiment No. 1 Aim:-Theory:-: Sec, Cse, Washimpage 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

AI Lab Manual

Experiment No. 1

Aim:-Introduction to AI and Prolog.

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:

 Formal logic and associated from of programming.


 Reasoning modeling
 Database programming
 Planning and so on

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.

Ex. Jane can eat food if it is a regelable on the doctors list.

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

Aim: Write a Prolog program containing facts related to following

predicates

1.location (city, state)

2.stays (person, city)

Display:

(i) list of person, state and city

(ii) Given person staying in which city and state.

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)

2.stays (person, city)

SEC,CSE,WASHIMPage 5
AI Lab Manual

Experiment No. 3

Aim: Create a family tree program to include following rules –

1. M is the mother of P if she is a parent of P and is female


2. F is the father of P if he is a parent of P and is male
3. X is a sibling of Y if they both have the same parent.
4. Then add rules for grandparents, uncle-aunt, sister and brother.

Based on the facts , define goals to answer questions related to family tree.

Example:- Suppose family tree is as follows-

Parent/father
abid male

Male prince riya female

Female male male female

ripa sukif sopan sam

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:

Conclusion:- Thus we have created famliy tree program sucessfully

SEC,CSE,WASHIMPage 8
AI Lab Manual

Experiment No. 4

Aim: Write a program to perform various string operations.

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,

write('Enter any two Strings'),nl,

read(B),

read(C),

string_concat(B,C,D),

write('Concatenation is '),write(D),nl,

go.

solve(A):-

A=2,

write('Enter any String'),nl,

read(B),

write('Enter Starting position'),nl,

read(Pos),

SEC,CSE,WASHIMPage 9
AI Lab Manual

write('Enter length of Sub_String'),nl,

read(Len),

sub_string(B,Pos,Len,_,D),

write('Sub_String is '),write(D),nl,

go.

solve(A):-

A=3,

write('Enter any String'),nl,

read(B),

string_length(B,D),

write('String length is '),write(D),nl,

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.

CONCLUSION: Thus we have created program to perform operations on string.

SEC,CSE,WASHIMPage 12
AI Lab Manual

Experiment No.5

Aim: Write a program to find shortest path distance.

Software Requirement: SWI-Prolog

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:

a] Predicates: A predicates name is the symbol used to defined a relation .

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:-

d] Clearwindows( ):Clearwindows( ) is used for clear the window screen of PROLOG.

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:

/*WAP to find shortest path distance in prolog*/


arc(a,b).
arc(b,a).
arc(b,c).
arc(c,b).
arc(c,d).
arc(d,c).

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.

Objective: To study depth first search using PROLOG.


DFS (Depth First Search) is an algorithm for traversing or searching a tree, tree structure
or graph. One starts at root (selecting any node at the root of the graph case) and explore as far
along each branch before backtracking. DFS is the uniformed search that progress by expanding
the first child node of search tree that appears and thus going deeper and deeper until a goal node
is found, or until it hit the node which has no children. Then search backtracks, returning to the
most recent node it hadn’t finished exploring. In a non-recursive implementation, all freshly
expanded nodes are added to LIFO stack for exploration. Space complexity of DFS is much
lower than BFS. Depth First Search are performed by dividing downward into a tree as quickly
as possible. It does not always generate a child node from most recently expanded node. If goal
is not find when leaf node is reached, the program backtracks to most recently expanded node
and generates another of its children this process continues until goal is found.

Algorithm:
1. Place starting node in the queue.

2. If queue is empty then return failure and stop.

3. If first node in queue is goal node g, return success and stop.

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:

Conclusion: Depth First Search has been achieved.

SEC,CSE,WASHIMPage 16
AI Lab Manual

Experiment No. 7

Aim: Write a prolog program for monkey banana problem.

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

Final State: ?, ?, ?, monkey has 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:

Conclusion: Thus we have created program for monkey banana problem

SEC,CSE,WASHIMPage 18
AI Lab Manual

Experiment No. 8

Aim: WAP for tower of Hanoi.

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

[ Tower (n, BEG, AUX, END)]

To denote a produce which moves the top n disk from initial peg BEG to find peg END
using peg AUX when,

[ Tower (1, BEG, AUX, END)]

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

a. WRITE: BEG END

b. RETURN

2) call Tower (N-1, BEG, END, AUX)

3) WRITE: BEG END

4) call Tower (N-1, AUX, BEG, END)


SEC,CSE,WASHIMPage 19
AI Lab Manual

5) RETURN

Program:
/*WAP for tower of Hanoi in prolog*/

move(1,X,Y,_):-write('Move disk from '),write(X),write(' to '),write(Y),nl.


move(N,X,Y,Z):-N>1,M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).

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

Move disk from b to c


Move disk from b to a
Move disk from c to a
Move disk from b to 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 c to a
Move disk from b to c
Move disk from b to a
Move disk from c to a
Move disk from c to b
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 .

Result: In this way, we performed a program for tower of Hanoi.

SEC,CSE,WASHIMPage 21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy