Friday, June 29, 2012

Constraint satisfaction problems In AI part 03


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.

BackTracking in Artificial Intelligence part 02




Backtracking
As has already been seen prolog has built in backtracking mechanism. It tries to prove a goal with all possible instantiations. Automatic backtracking is a useful programming concept because it reveals the programmer of the burden of backtracking explicitly. However in some cases this feature degrades the efficiency of the program.

For example in cases where one solution is sufficient, backtracking to find all the solutions is not a good idea. Similarly, in case of mutually exclusive rules(clauses) when one rule has been proved then it is known in advance that no other rules can succeed. So this backtracking can be controlled by the use of ‘cut’, (“!”).

The disadvantage of using cut is that we tend to move away from the declarative nature of the prolog because when we have used the cut the order of the clauses may make difference in the result we get.
              



Consider the function shown in the above figure. The relation between X and Y can be specified by the following three rules.
Rule 1: if X<3 then Y=0
Rule 2: if 3=<X <6 then Y=2
Rule 3: if 6<X then Y=4

This can be programmed as
PREDICATES
f(integer,integer)

CLAUSES
f(X,0):-
                X<3.
f(X,2):-
                3<=X,X<6.
               
f(X,4):-
                6<X.
               
GOAL
f(2,X).

 Assignment1.)

Now modify the program using cut and observe the difference between the two modules. Comment on the difference.

Solution:

Introduction to Prolog for AI programming part 01

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 problems. This is in the 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 the different problem solving paradigm complementary to traditional programming languages so it is believed that a student of computer should learn programming in prolog.

With Visual Prolog, applications such as customized knowledge bases, expert systems, natural language interfaces, and smart information management systems are easy to develop. 


Data types in prolog
There are mainly three data types in prolog which are:
a)      Atoms and numbers
b)      Variables
c)      Structures


a) Atoms and numbers
Atoms can be constructed in three different ways
  • Strings of letters, digits, and the underscore character starting with a lower case letter.    
  •  For example: man, ram, comp_students, pc_ct_063
  • Strings of special characters.                                                                                           
    For example: < ----- >                                                                                                    
  •   Care should be taken not to use the character combination that may some built in meaning.
  • Strings of characters enclosed in quotes.