Artificial Intelligence Lab
Artificial Intelligence Lab
Full Marks: 25
Report: 5
Attendance: 5
Project: 15
Report includes:
1. Related Theory
2. Problem explanation with algorithms
3. Program code
4. Output
5. Discussion
6. Conclusion
LAB 1: FAMILIARIZATION WITH PROLOG
Introduction to Prolog
So prolog can be viewed as a tool to solve problems in the field of artificial intelligence or it can
be very well used a general programming language.
1. Domains:- A computer always stores a limited knowledge in comparing with the human
brains. In any expert system, knowledge is stored generally for only a single subject area
known as “domain”. The computer can analyze and solve any problems that are relevant to
this domain. Because current microcomputers have very limited amount of memory, it is
generally done for PROLOG applications to confine this domain to as small as possible.
In prolog programming, the domain defines the data type definition such as String,
integer, symbol, real, char, files
For example:
Name = “string” – This indicates that name is a variable which is string.
2. Predicates:- A predicate is a function with a value of true and false. Predicates express a
property or a relationship.
3. Clauses:- The expression in prolog is called clause. In prolog, a clause is terminated with
the period (.). Clauses hold facts and relations.
For example:
The speaker is dead.
In prolog, we write as:
Is(speaker,dead).
4. Goal:- A goal is essentially a question. If prolog can match with a fact in the database, it
succeeds and responds with TRUE. Each time when a goal is specified, one of the
following three results occur.
a. The goal will succeed i.e, it will prove TRUE.
b. The goal will fail i.e. prolog will not be able to match the goal with any facts in
the program.
c. The execution is fail because of an error in the programming
1. strings of letters, digits, and the underscore character ‘_’ starting with a lower case letter.
for example:
man, ram, comp_students, pc_ct_059.
for example:
<------->
:::::::::::
Care should be taken not to use the character combination that may have some built in
meaning.
for example
‘Ram’
‘Bird’
Variables
Variables are strings of letters digits and underscore that start with an underscore or an
upper-case letter. The scope of a variable is one clause only. So the same variable used in
different clauses mean different thing.
For example:
Note here that Ram is a variable unlike the earlier use ‘Ram’ where it was a constant, an atom.
An underscore ‘_’ also known as anonymous variable is used in clauses when a variable need not
be inferred to more than once.
All prolog programs start from the goal. It then uses the facts and clauses to break down the goal
to sub-goals and tries to prove the subgoals. A clause is said to have succeeded if there is a
combination of facts and clause(s) that holds true.
Prolog has built in backtracking mechanism i.e. whenever there are multiple paths, it chooses
one tries to prove it, and comes back to the other choices whether the first one succeeds or fails.
bigger(integer,integer,integer)
CLAUSES
bigger(X,Y,Z):-
X>Y,Z=X.
bigger(X,Y,Z):-
X<Y,Z=Y.
GOAL
bigger(5,7,X).
In program 1 we have defined a predicate that gives us the larger of the two given integers. The
predicate bigger is defined in the PREDICATES section and the relation is defined in the
CLAUSES section. It is customary to put together the predicate definition and clause definition
for each predicate but it is not necessary. However all the clauses for a predicate have to be
written together.
The predicates section defines what types of relations or facts we are going to use. It is somewhat
like declaring functions in programming languages like C. The clauses section holds the relation
and facts and can be compared to function definition.
LAB 2: FAMILIARIZATION WITH PROLOG Contd.
Structures
Structures are objects that have different components. The components can be atoms or yet some
other structures. A functor is used to construct a structure as follows.
Here family is a structure that has father, mother and the children as its elements. The father and
mother may be atoms while the children may be yet another structure or a list of atoms. List is a
special built in structure in prolog.
For example:
[ram,shyam,hari,sita]
The list as such can be broken down into two parts, the HEAD and the TAIL. The head is the
first element of the list and the tail is the remaining list. The above list can be broken down as
[H| T]
Where H= ram
Built in predicates:- Most version of prolog contains variety of built-in standard procedures that
can support a variety of functions such as control and data input and output.
write(“enter name”)
readchar character
readreal real
DOMAINS
int_list=integer*
PREDICATES
length(int_list,integer)
CLAUSES
length([],0).
length([H|T],L):-
length(T,L1),
L=L1+1.
GOAL
length([1,2,5,2,1,6,7],X).
Program 2: A program to read integers into a list and display them.
DOMAINS
list=integer*
PREDICATES
start
read_a_list(list)
insert(integer,list,list)
display(list)
CLAUSES
start:-
write("enter the numbers"),
nl,
write("enter 0 to stop"),nl,
read_a_list([]).
read_a_list(Y):-
readint(X),
insert(X,Y,Z),
read_a_list(Z).
insert(0,Y,_):-
write("these were the elements you inserted"),
nl,
write("["),
display(Y).
insert(X,Y,[X|Y]).
display([ ]):-
write(" ]"),nl.
display([H|T]):-
write(H),write("\t"),display(T).
GOAL
start.
Structure revisited
In prolog we can use structures to define data types of our requirement. For example if we want
to use date as an structure we can define date as a structure in the domains section as follows
date=d(integer,symbol,integer)
DOMAINS
date=d(integer,symbol,integer)
PREDICATES
inquire
display(symbol)
date_of_birth(symbol,date)
CLAUSES
date_of_birth(ram,d(12,july,1983)).
date_of_birth(shyam,d(15,august,1976)).
date_of_birth(hari,d(26,may,1994)).
date_of_birth(sita,d(29,september,1991)).
display(X):-
date_of_birth(X,Y),
write(X),nl,
write(Y).
inquire:-
write("Enter the name"),
readln(X),
display(X).
GOAL
inquire.
Here the goal so proceeds as to ask a name from the user and to display the date of birth of the
person with that name. With a little modification we can write goals which can find out persons
with age below or above certain value, persons born in a month etc as in a relational database.
So the facts of the prolog can be thought of as a database. In fact we use structures to define
certain relations and for all purposes of integrity this can be used similar to a table in a relational
database. We call it the prolog’s internal database. We can update this database during the
execution of the program by using the following keywords.
assert(C) – this keyword can be used to assert a data in the facts base as
asserta(C) and assertz( C) can be used to control the position of insertion, the two asserts at the
beginning and the end respectively.
1. Write a program to add the content of an integer list and display it.
2. Write a program to find the length of a list.
3. Write a program to append two lists.
4. Write a program which takes a list of integers and displays only 1s and 2s. ( If the input
is [1,2,4,5,2,4,5,1,1] the solution list should be [1,2,2,1,1]. )
5. Write a program to delete a given element from the list.
LAB 3: CONSTRAINT SATISFACTION PROBLEMS
Introduction
Constraint programming is a useful tool in formulating and solving problems that can be defined
in terms of constraint among a set of variables. In fact real world problems are constraint
satisfaction problems defined in terms of some variables that bear some constraints. Finding a set
of variables, that are within the constraints given(or observed) is a solution to that problem.
Let us consider a problem, that can be represented by some relations of the variables x, y and z.
We have a domain Dx, Dy, Dz from where the variables can take a value. The constraint is given
by a set C and may have a number of constraints C1,C2,C3,etc each relating some or all of the
variables x,y and z. Now a solution (or solutions) to the problem is a set dx,dy,dz such that dx
Dx, dy Dy and dz Dz and all the constraints of the set C are satisfied.
Eight queens problem is a constraint satisfaction problem. The task is to place eight queens in the
64 available squares in such a way that no queen attacks eachother. So the problem can be
formulated with variables x1,x2,x3,x4,x5,x6,x7,x8 and y1,y2,y3,y4,y5,y6, y7,y8; the xs
represent the rows and ys the column. Now a solution for this problem is to assign values for x
and for y such that the constraint is satisfied.
P={(x1,y1),(x2,y2),……………………..(x8,y8)}
where (x1,y1) gives the position of the first queen and so on.
1. No two queens should be in the same row, i.e yi≠yj for i=1 to 8;j=1 to 8;i≠j
2. No two queens should be in the same column,
i.e xi≠xj for i=1 to 8;j=1 to 8;i≠j
3. There should not be two queens placed on the same diagonal line
i.e (yi-yj) ≠ ±(xi-xj).
Now a solution to this problem is an instance of P wherein the above mentioned constraints are
satisfied.
PREDICATES
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)]).
Assignment 1). Observe the result of the above program and discuss on the result. Test the goal
by placing a few queens explicitly.
Increase the dimension of the chess board and observe the result.
Crypto arithmetic problem is yet another constraint satisfaction problem. We have to assign
numeric values(0 through 9) to the alphabets in the given words in such a way that the sum of the
two words equals the third.
We have to assign values to the individual alphabets in such a manner that the arithmetic rules
are followed, a trivial solution will be to assign 0s to all, but we have a constraint, no two
alphabets should be assigned with the same number.
C4 C3 C2 C1
S E N D
+ M O R E
M O N E Y
So, by now we have the problem in hand. The domain for the alphabets is
S,E,N,D,M,O,R,Y€ {0,1,2,3,4,5,6,7,8,9}.
D+E=Y+10C1
N+R+C1=E+10C2
E+O+C2=N+10C3
S+M+C3=O+10C4
M=C4
C1,C2,C3,C4€{0,1}
And we have the constraint that no two alphabets should be assigned with the same number.
Moreover some AI scientists insist that M should be 1 seeking that the third row will reduce just
to four literals else wise. This might be an extra constraint.
Assignment 4) Find out the solution to the above problem without considering the constraint
M=1. Modify your program adding this as a constraint. Can you imagine solving this in C? Its
just a question. You don’t have to really try it? But we won’t stop you either.
PREDICATES
husband(STRING,STRING)
father(STRING,STRING)
mother(STRING,STRING)
son(STRING,STRING)
CLAUSES
mother("Kaushalya","Ram").
mother("Kaikai","Bharat").
mother("Sumitra","Laxman").
mother("Sumitra","Satrughan").
husband("Dasarath","Kaushalya").
husband("Dasarath","Kaikai").
husband("Dasarath","Sumitra").
son(A,C):-mother(C,A).
son(A,C):-husband(C,B),mother(B,A).
father(A,B):-husband(A,C),mother(C,B).
Goal
son(X,"Kaikai").
Here the predicate mother is used to assert facts. There are four facts in that section. Similarly
the predicate husband is used to assert more facts. The predicates son and father are used to
define rules. We could have defined rules even with mother and husband but we bet that
conciseness for program clarity at this stage.
son( “Ram”,X).
father(X,”Ram”).
First Order Predicate Logic (FOPL) is a generalization of Propositional Logic. Logic Programs
are written in a sub-language of FOPL and therefore derive their meaning and formal properties
from it.
1. Propositions are renamed "predicates" and may possess an internal structure. In fact the
predicates of FOPL have precisely the same syntactic structure as they have in Prolog -
i.e. a predicate name with terms as arguments, each term being a
-constant,
-variable, or
e.g Ex P(x) means "some unspecified constant has property P". Each quantifier has a
SCOPE which is defined as the textual area in which it binds occurrences of its variable.
This scope is usually delimited by brackets, unless it is obvious as in the example
above.
These extensions critically extend the expressive power of logic. Consider the
following sentences which are given a natural corresponding syntax in predicate calculus:
"John is the brother of Jim." brother_of(john,jim).
Prolog clauses can be directly translated into FOPL, except for a few exceptions like write, !,
is, assert, retract, ...
Examples
1. grandfather(X,Y) :- father(X,Z),parent(Z,Y).
In FOPL:
Ax,Ax,Az (grandfather(x,y) <- (father(x,z)&parent(z,y)))
2. uncle(X,Y) :- parent(Z,Y),brother(X,Z).
In FOPL:
Ax,Ay,Az ( uncle(x,y) <- (parent(z,y)&brother(x,z)))
3. member(X,[X|T]).
In FOPL:
Ax,Ay,At (member(x,[x|t]))
4. member(X,[Y|T]) :- member(X,T).
In FOPL:
Ax,Ay,At (member(x,[y|t]) <- member(x,t))
5. a :- b,c,d,e.
In FOPL:
a <- (b & c & d & e)
If we use the laws derived from the meaning of the logical connectives, we can simplify the
expressions in 1, 2, 4, and 5:
= a V ~b V ~c V ~d V ~e
The following should be obvious from the examples:
Any Prolog clause which is translated into FOPL has exactly one positive (i.e. non-negated)
predicate - originally the head of the Prolog clause.
Assignments
PREMISES 1
QUERY
1. Is Charlie a horse?
PREMISES 2
QUESTION
1. Is George a criminal ?
PREMISES 3
QUESTION
PREMISES 4
Bhogendra likes all kinds of food. Oranges are food. Chicken is food. Anything anyone eats and
isn’t killed by is food. If a person likes a food means that person has eaten it. Jogendra eats
peanuts and is still alive. Shailendra eats everything Bhogendra eats.
QUESTION
LAB 5: PROJECT WORK
Every student must submit project work assigned and the project work will be evaluated by
considering following
● Report writing -5
● Output – 5
● Viva -5
The project tasks for the students include these concentration areas: