Csbs-Ai Lab Manual
Csbs-Ai Lab Manual
2022 – 2023
1. Study of Prolog.
2. Write simple fact for the statements using PROLOG.
3. Write predicates one converts centigrade temperatures to Fahrenheit, the other
checks if a temperature is below freezing.
4. Write a program to solve the Monkey Banana problem.
5. Write a program in turbo prolog for medical diagnosis and show t he advantage
and disadvantage of green and red cuts.
6. Write a program to solve 4-Queen problem.
7. Write a program to solve any problem usingBreadth FirstSearch.
8. Write a program to solve Missionaries and Cannibal problem.
9. Write a program to solve water jug problem using LISP
10. Case study – expert system
PREPARED BY
Mr.A.D.Saravanaprabhu/AP
Ex:No:1 STUDY OF PROLOG
DATE:
PROLOG-PROGRAMMING IN LOGIC:
PROLOG stands for Programming, In Logic — an idea that emerged in the early 1970’s to
use logic as programming language. The early developers of this idea included Robert Kowaiski at
Edinburgh (on the theoretical side), Marrten van Emden at Edinburgh (experimental demonstration) and
Alian Colmerauer at Marseilles (implementation). David D.H. Warren’s efficient
implementation at Edinburgh in the mid -1970’s greatly contributed to the popularity of PROLOG.
PROLOG is a programming language centred around a small set of basic mechanisms, Including
pattern matching, tree based data structuring and automatic backtracking. This Small set constitutes a
surprisingly powerful and flexible programming framework. PROLOG is especially well suited for
problems that involve objects- in particular, structured objects- and relations between them.
SYMBOLIC LANGUAGE
b) RULES:
c) QUERIES:
Given a database of facts and rules such as that above, we may make queries by typing after a
query
a symbol’?’ statements such as:
?-parent(X,sam) Xann
?grandfather(X,Y)X=jo, Y=sam
An expert system is a set of programs that manipulates encoded knowledge to solve problems
in a specialized domain that normally requires human expertise. An expert system’sknowledge is
obtained from expert sources such as texts, journal articles. databases etc and encodedin a form suitable
for the system to use in its inference or reasoning processes.
5
Once a sufficient body of expert knowledge has been acquired, it must be encoded in some
form, loaded into knowledge base, then tested, and refined continually throughout the life of the
system PROLOG serves as a powerful language in designing expert systems because of its following
features.
• Use of knowledge rather than data modification of the knowledge base without
recompilation of the control programs.
•
META PROGRAMMING
A meta-program is a program that takes other programs as data. Interpreters and compilers
are examples of mela-programs. Meta-interpreter is a particular kind of meta-program: an interpreter
for a language written in that language. So a PROLOG interpreter is an interpreter for PROLOG,
itself written in PROLOG. Due to its symbol- manipulation capabilities, PROLOG is a powerful
language for meta-programming. Therefore, it is often used as an implementation language for other
languages. PROLOG is particularly suitable as a language for rapid prototyping where we areinterested
in implementing new ideas quickly. New ideas are rapidly implemented and experimented with.
6
Ex.No :2 SIMPLE FACT FOR THE STATEMENTS USING PROLOG
DATE:
Aim:
To write simple fact for the statements using prolog.
Algorithm:
Step1: In Prolog syntax, we can write − Understand logical programming syntax and semantics.
Design programs in PROLOG language.
Step2: Prolog programs describe relations, defined by means of clauses. Pure prolog is
restrictedto horn clauses. There are two types of clauses: facts and rules. A rule is one of the
form Head:-Body.and is read as “Head is true if Body is true”.
Step3: A rule's body consists of calls to predicates, which are called the rule's goals.
• Ram like s mango.
• Seema is a girl.
• Bill likes Cindy.
• Rose is red.
• John owns gold.
Output:
Goal queries
?-likes(ram,What).What= mango
?-likes(Who,cindy).Who= cindy
?-red(What).What= rose
?-owns(Who,What).
Who= john What= gold.
7
Ex.No:3 CONVERSION FOR WRITING PREDICATES
DATE:
Aim :
To write predicates one converts centigrade temperatures to Fahrenheit, the other
checks if a temperature is below freezing.
Algorithm:
Procedure:
Production rules:Arithmetic:
c_to_f = f is c * 9 / 5 +32freezing = f < = 32
Rules:
c_to_f(C,F) :-
F is C * 9 / 5 + 32.freezing(F) :-
F =< 32.
Output:
Queries:
?- c_to_f(100,X).X = 212
Yes
?- freezing(15)
.Yes
?- freezing(45).N
8
Ex.No:4 SOLVING THE MONKEY BANANA PROBLEM.
DATE:
Aim :
To write a program to solve the monkey banana problem using prolog.
Algorithm :
The monkey can perform the following actions:-
Step1: Walk on the floor.
Step2: Climb the box.
Step3: Push the box around (if it is beside the box).
Step4: Grasp the banana if it is standing on the box directly under the banana.
Step5: Production Rules:
can_reach(clever,close)
get_on:(can_climb)
under(in room,in_room,in_room,can_climb)Close(get_on,under| tall)
Procedure:
Clauses:
in_room(bananas).
in_room(chair).
in_room(monkey).
clever(monkey).
can_climb(monkey, chair).
tall(chair).
can_move(monkey, chair, bananas).
can_reach(X, Y):-
clever(X),close(X, Y).
get_on(X,Y):-
can_climb(X,Y). under(Y,Z):-
in_room(X),
in_room(Y),in_room(Z),can_climb(X,Y,Z)
. close(X,Z):-get_on(X,Y),
under(Y,Z);
tall(Y).
Output:
Queries:
?- can_reach(A, B).A = monkey.
B = banana.
?- can_reach(monkey, banana).Yes.
9
Ex.No:5 MEDICAL DIAGNOSIS WITH GREEN AND
DATE: RED CUTS
Aim:
To write a prolog program for medical diagnosis.
Procedure :
Tuberculosis is a lung disease whose symptoms are persistant cough, constant fatigue, weight loss,
loss of appetite, fever, coughing up blood, night sweats. So it will be stored in knowledge base in the
form of a rule which is as follow:-
You can also take some other examples, which are given below:-
Pneumonia is a disease whose symptoms are cough, fever, shaking chills, shortness of breath.
So itwill be stored in knowledge base as follow:-
Disease (Patient, pneumonia):-
Symptom (Patient, cough),
Symptom (Patient, fever),
Symptom (Patient, shaking_chills),
Symptom (Patient, shortness_of_breath).
Output:
makewindow(1,7,7"Expert Medical Diagnosis",2,2,23,70),
10
Ex.No:6 SOLVING 4-QUEEN PROBLEM.
DATE:
Aim:
To write a program for placing 4 Queens on a chessboard using C programming
Algorithm:
Description:-
In the 4 Queens problem the object is to place 4 queens on a chessboard in sucha
way that no queens can capture a piece. This means that no two queens may be placed
on the same row, column, or diagonal.
Output:
Goal:
?-nqueens(4),nl. board([q(1,2),q(2,4),q(3,1),q(4,3),[],[],[7,4,1],[7,4,1])
yes
11
Ex.no:7 SOLVING A PROBLEM USING
DATE: BREADTH FIRST SEARCH
Aim:
To find the shortest path using BFS algorithm concepts in prolog.
Algorithm:
Step1: Pick any node, visit the adjacent unvisited vertex, mark it as visited, displayit, and
insert it in a queue.
Step2: If there are no remaining adjacent vertices left, remove the first vertex from the
queue.
Step3: Repeat step 1 and step 2 until the queue is empty or the desired node is found.
Procedure:
graph = {
'A' : ['B','C'],
'B' : ['D', 'E'],
'C' : ['F'],
'D' : [],
'E' : ['F'],'F' : []
}
visited = [] # List to keep track of visited nodes.queue = [] #Initialize a queue
def bfs(visited, graph, node):visited.append(node) queue.append(node)
while queue:
s = queue.pop(0) print (s, end = " ")
for neighbour in graph[s]: if neighbour not in visited:visited.append(neighbour)
queue.append(neighbour) # Driver Code
bfs(visited, graph, 'A')
Output : BFS(ABCDEF)
12
Ex.No:8 SOLVING MISSIONARIES AND CANNIBAL PROBLEM
DATE:
Aim:
To write a program to solve Missionaries and Cannibal problem
Algorithm:
Game start now the task is to move all of them to right side of the river rules:
Step1: The boat can carry at most two people
Step2: If cannibals num greater than missionaries then the cannibals would eat the
missionaries.
Step3: The boat cannot cross the river by itself with no people on board M M M
C C C |--- |
Left side -> right side river travel
Description :
Missionaries and Cannibals can be solved by using different search algorithms like Breadth
first and Depth first search algorithm to find the solution. The node of the graph to be
searched is represented by a state space. Each state space can be represent
by: State(no_of_missionaries, no_of_cannibals, side_of_the_boat)
Where, no_of_missonaries are the number of missionaries atleft side of river,
no_of_cannibals are the number of cannibals at the left side of river and side_of_the_boat is the side of
the boat at particular state. For our case
InitialState=>State(3,3,0)an
d Final State => State(0, 0,
1).
Where 0 represents left side and 1 represents right side of river. We should make a graph search
which traverse the graph from initial state and find out the final state in fewest moves. There are many
AI searches that search the graphs like Breadth first search, Depth first search, or iterative deepening
search. Each of these different search methods has different properties such as whether a result is
guaranteed,and how much time and space is needed to carry out the search. This project uses Breadth
first and Depth first search.
13
Production rules for Missionaries and Cannibals problem
Possible Moves
A move is characterized by the number of missionaries and the number of cannibals
taken in the boat at one time. Since the boat can carry no more than two people at once, the only
feasible combinations are:
Carry(2,0).
Carry(1,0).
Carry(1,1).
Carry(0,1).
Carry(0,2).
Where Carry (M, C) means the boat will carry M missionaries and C cannibals on one trip.
Feasible Moves
Once we have found a possible move, we have to confirm that it is feasible. It is not afeasible to
move more missionaries or more cannibals than that are present on one bank.
When the state is state(M1, C1, left) and we try carry (M,C) then M must be true.
When the state is state(M1, C1, right) and we try carry(M, C) then
M + M1 must be true.
Legal Moves
Once we have found a feasible move, we must check that is legal i.e. no missionaries must be
eaten.
Legal(X,X).
Legal(3,X).
Legal(0,X).
The only safe combinations are when there are equal numbers of missionaries and cannibals or all the
missionaries are on one side.
Output:
14
Ex.no:9 SOLVING WATER JUG PROBLEM
DATE: USING LISP
Aim:
To write a program to solve water jug problem using Lisp.
Algorithm:
Step3: Pour water from one jug to the other until one of the jugs is either empty or full,
PROCEDURE :
1. First we will fill the 4 litre jug completely with water.
2. Then optimal approach would be to empty water from 4-litre jug into 3-litre (leaving 1Lwater
in 4L jug and 3L completely full). Hence we got 1L water. Now, Empty water from 3L.
3. Pour the water from 4L jug into 3L jug Now 4L container is completely empty and1L water in
present in 3L litre jug.
5. On transferring water from 4L jug to 3L jug, we will get 2L water in 4L jug which wasour
required quantity.
Output :
Input: X = 3, Y = 5, Z = 4
Output: 6
15
Ex:No:10 CASE STUDY – EXPERT SYSTEM
DATE:
EXPERT SYSTEM:
Expert system needs similar knowledge which is fetched from its knowledge base, and then
interprets it according to user’s problem. All data is inserted in the knowledge base by domain experts
who are expert in specific domain. This computer based applications are used by userswho are
non- experts in this area for obtaining information.
16