0% found this document useful (0 votes)
114 views20 pages

Artificial Intelligence Lab

This document provides information about an Artificial Intelligence lab including: 1. The lab uses Visual Prolog as the programming language and is held every alternate week for full marks of 25 distributed between a report, attendance, and a project. 2. The report includes related theory, problem explanation, program code, output, discussion, and conclusion. 3. The document discusses Prolog programming including its declarative nature, domains, predicates, clauses, goals, backtracking, data types, and provides examples of Prolog programs.

Uploaded by

Bulmi Hilme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views20 pages

Artificial Intelligence Lab

This document provides information about an Artificial Intelligence lab including: 1. The lab uses Visual Prolog as the programming language and is held every alternate week for full marks of 25 distributed between a report, attendance, and a project. 2. The report includes related theory, problem explanation, program code, output, discussion, and conclusion. 3. The document discusses Prolog programming including its declarative nature, domains, predicates, clauses, goals, backtracking, data types, and provides examples of Prolog programs.

Uploaded by

Bulmi Hilme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

ARTIFICIAL INTELLIGENCE LAB

Programming Language: Visual Prolog

Lab: Every Alternate week

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

Prolog (Programming in Logic) is a very important tool in programming artificial intelligence


applications and in the development of expert systems. By allowing the programmer to model
the logical relationships among objects and processes, complex problems are inherently easier to
solve, and the resulting program is easier to maintain through its lifecycle. With Visual Prolog,
applications such as customized knowledge bases, expert systems, natural language interfaces,
and smart information management systems are easy to develop. Prolog is what is known as a
declarative language. This means that given the necessary facts and rules, Prolog will use
deductive reasoning to solve the programming problems. This is in contrast to traditional
computer languages, such as C, BASIC and Pascal, which are procedural languages. We can also
use prolog as any other programming languages in a procedural manner.

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.

Prolog enforces a different problem solving paradigm complementary to traditional


programming languages so it is believed that a student of computer should learn programming in
prolog.

Structure of a Prolog Program

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.

For example: If name is declared as string, it is an expression. Every expression is called


predicates. The word before the parenthesis is the name of the relation and the element
within the parenthesis are the arguments of the predicate, which may be objects or
variables.

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

Backtracking​: It is a technique used by PROLOG to process the program starting from


goal. 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.

Data types in prolog

● Atoms and numbers


● Variables
● Structures

Atoms and numbers

Atoms can be constructed in three different ways

1. strings of letters, digits, and the underscore character ‘_’ starting with a lower case letter.

for example:
man, ram, comp_students, pc_ct_059.

2. strings of special characters

for example:
<------->
:::::::::::

Care should be taken not to use the character combination that may have some built in
meaning.

3. strings of characters enclosed in quotes

for example

‘Ram’

‘Bird’

Numbers used in prolog are integers and real numbers.

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:

X, Ram, _weight etc.

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.

Writing programs in prologs

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.

Program 1: A typical prolog program to find larger among two numbers.


PREDICATES

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.

Program 2: Modify program 1 to find smallest among 3 numbers.

Program 2: Write a program to check if a number is even or odd.

Program 3: Write a program to find HCF and LCM of two numbers.

Program 5: Write a program to find factorial of a number.

Assignment: write a program to find n​th​ Fibonacci number.


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.

family(father, mother, children)

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.

A ​list is a built in structure in prolog. It can be thought of as a sequence of elements ordered


linearly however it is internally represented as a binary tree.

For example:

[ram,shyam,hari,sita]

The representation is as follows

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

and T= [shyam, hari,sita]


List is one of the most useful structure in prolog.

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.

Some of Built-in predicates are as follows

1. Control predicates for controlling program execution, forcing or preventing backtracking


2. Reading predicates for reading data from the keyboard or file
3. Writing predicates for writing data to the screen or printer
4. File system predicates for making files

The write predicate

It is an output predicate and is used as

write(“enter name”)

write(X,”probably has”, Y, “.”)

The input predicate

readln string or symbol readln(X)

readint integer readint(Age)

readchar character

readreal real

Program 1: A program to find the length of a list

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)

We can then on use date as a data type to contain the date.

Program 3: More on Structure

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.

retract( C) –deletes a clause that matches C.

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

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.

The problem can be formulated as

P={(x1,y1),(x2,y2),……………………..(x8,y8)}

where (x1,y1) gives the position of the first queen and so on.

So it can be clearly seen that the domains for xi and yi are

Dx = {1,2,3,4,5,6,7,8}and Dy ={1,2,3,4,5,6,7,8} respectively.

The constraints are

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.

(Try goals like solution ([c(1,1),c(2,B),c(3,C),c(4,8),c(5,E),c(6,F),c(7,G),c(8,H)] etc).

Increase the dimension of the chess board and observe the result.

Assignment 2). Try to solve the above problem using C or C++.

Crypto arithmetic problem

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.

For example: SEND+MORE=MONEY

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}.

The constraints are

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.

LAB 4: FIRST ORDER PREDICATE LOGIC


Program 1: A program combining facts and rules.

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.

The goal can be checked with

son( “Ram”,X).

father(X,”Ram”).

First Order Predicate Logic

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.

FOPL has two major extensions over Propositional Logic:

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

-funcion with term(s) as argument(s).

2. Variables must be quantified by either

A meaning "for all" or

E " "there exists"

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).

"Socrates is dead and buried." dead(socrates)&buried(socrates).

"All men are mortal." Ax (man(x) -> mortal(x)).

Conversion of Prolog into FOPL:

Prolog clauses can be directly translated into FOPL, except for a few exceptions like write, !,
is, assert, retract, ...

The three simple rules for conversion are:

1. “,” corresponds to "^"

2. “;” corresponds to “˅”

3. ":-" corresponds to "←"

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:

1. AX,AY,AZ (grandfather(X,Y) <- (father(X,Z) & parent(Z,Y)) )

by "<-" simplification this becomes

= AX,AY,AZ (grandfather(X,Y) V ~(father(X,Z) & parent(Z,Y)) )

using "not" simplification this becomes

= AX,AY,AZ ( grandfather(X,Y) V ~father(X,Z) V ~parent(Z,Y) )

3. AX,AY,AT (member(X,[Y|T]) <- member(X,T))

by "<-" simplification this becomes

= AX,AY,AT (member(X,[Y|T]) V ~member(X,T))

4. AX,AY,AZ ( uncle(X,Y) <- (parent(Z,Y) & brother(X,Y)) )

by "<-" simplification this becomes

= AX,AY,AZ ( uncle(X,Y) V ~(parent(Z,Y) & brother(X,Y)) )

using "not" simplification this becomes

= AX,AY,AZ ( uncle(X,Y) V ~parent(Z,Y) V ~brother(X,Y) )


5. a <- (b & c & d & e)

by "<-" simplification and "not" simplification this becomes

= 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

1. Horses, cows, pigs are mammals.


2. An offspring of a horse is a horse.
3. Bluebeard is a horse.
4. Bluebeard is Charlie’s parent.
5. Offspring and parent are inverse relations.
6. Every mammal has a parent.

QUERY

1. Is Charlie a horse?

PREMISES 2

1. every American who sells weapons to hostile nations is a criminal.


2. every enemy of America is a hostile.
3. iraq has some missiles.
4. all missiles of iraq were sold by george.
5. george is an American
6. iraq is a country.
7. iraq is the enemy of America.
8. missiles are weapons.

QUESTION

1. Is George a criminal ?

PREMISES 3

1. All pompeians are romans.


2. all romans were either loyal to Caesar or hated him.
3. everyone is loyal to someone.
4. people only try to assassinate rulers they are not loyal to.
5. marcus tried to assassinate Caesar.
6. marcus was Pompeian.

QUESTION

1. did marcus hate Caesar?

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

Does Shailendra like chicken.


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:

1. Expert system Design and development


● Chemical gas recognition expert system
● Expert system for Medical Disease Diagnosis, etc
2. Simulation of Search Algorithms
● Shortest path finding
● Travelling salesman problem
● Tic-tac-toe
● Puzzles
● Games, etc
3. Simulation of Logic Gates such as NAND, AND, OR, etc using Artificial Neural
Networks

Size of Project team: Maximum 4 members in a group

Programming Language: Whichever preferable

You might also like