Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Similar presentations


Presentation on theme: "Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC."— Presentation transcript:

1 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC PROGRAMMING (WEEK 8) Eleni E. Mangina Department of Computer Science University College Dublin The idea behind constraint logic programming Using finite domain constraints

2 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD The idea behind constraint logic programming In old-fashioned prolog programming, programs can spend an awful lot of time searching through impossible solutions, because a test is applied after an entire solution has been generated We can partly overcome this problem by using delayed calls to let predicates “lie in wait” for information and substantial improvements can be made – eg. n! To ~ n 2 But we can improve things still further, by reasoning with logical theory about our data SICStus prolog has built in theories for integer finite domains, booleans, real numbers and national numbers

3 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD The idea behind constraint logic programming (2) When we do this, we can think of the constraints as an enhancement of the unification process (there are logical ways of explaining this) instead of as part of the execution process, like delay declarations So if we insist in advance, for example, that X is an integer between 1 and 3, then the attempt to unify X with 5 can be made to fail as soon as it is tried, instead of at some time later Using constraints can cut down the search space solutions by many, many orders of magnitude They can effectively change the algorithm of a program

4 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Finite domains in Prolog Internally, to the advanced programmer, the SICStus constaint system is very complex and powerful However, it can be used very easily to produce striking effects We associate a set of “allowed values” with each variable, can then any attempt to unify it with something “not allowed” will fail We allow the specification of fixed relationships between variables (eg. X < Y) We could do this with delay declarations alone… In CLP (FD) we add a theory of integers so we can solve (mostly linear) equations

5 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Finite domains in prolog (2) This allows prolog to go beyond ordinary unification and actually suggest values in some cases eg. ?- X in 1…10, X #> _Y + 8, _Y = 1 X = 10? yes Like delayed goals, constraints can sometimes leave a useful residue: ?- X in 1…10, X#> _Y + 8, _Y = -2 X in 7…10 yes ?- X in 1…10, X#\=5 X in (1…4)\/(6…10) yes

6 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Finite domains in prolog (3) We can reason about the domains themselves, too: ?- X in 1…10, Y in 8…20, X = Y X = Y X in 8…10 yes And we can enumerate the members of a domain, if we need to: ?- X in 1…3, indomain(X) X = 1? ; X = 2? ; X = 3? ; no

7 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD How finite domains work in prolog First, we define domain constraints A domain constraint is of the form X::I where X is a variable, and I is a finite set of integers A set S of domain constraints is called a store The domain D(X,S) of X in S is the intersection of all I such that X::I is in S S is consistent if D(X,S) is inhabited for all X Otherwise it is contradictory

8 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD How finite domains work in prolog (2) Now, whenever we unify a domain variable with anything we add any constraints produced to the store If the resulting store is consistent, unification succeeds If the resulting store is contradictory, unification fails and we backtrack There is a further system involved with constraint programming called indexicals but we will not cover that here

9 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Domain Specification Syntax Domains are specified using a special kind of integer term, which I will call an integer domain term An integer domain term is one of the following: –An integer (eg 1) –Sup- positive infinity (supremum) –Inf – negative infinity (infimum)

10 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Domain Specification Syntax (2) A range is one of the following: –An interval (eg T1 … T2) –A set of integers (eg {1,20}) –A union of ranges (eg T1…T2 \/{1.10}) –An intersection of ranges (eg T1…T2/\ {2,7,8})

11 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Predefined constraints SICStus prolog’s finite domain package is in library(clpfd) and contains the following predicates (among others): For testing and setting constraints: in/2 associates a range with a variable and checks consistency ~OP/2 where OP is one of >, >=, =, = Y + 2 ~\=/2 makes two expressions arithmetically not equal

12 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Example: SEND MORE MONEY A well known puzzle: Find the values of the variables in sum SEND + MORE MONEY where all the variables have different values We can write a constraint logic program to do this easily

13 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD SEND MORE MONEY (2) First we write down a predicate which will add the numbers appropriately: % we need to allow a carrying slot add(A,B,C) :- add (A,B,C, 0). % add numbers represented as lists of digits add ([],[],[],0). add([A|As], [B|Bs], [C|Cs], Carry):- A in 0…9, B in 0…9, C #= (A + B + NextCarry) mod 10, Carry #= (A + B + nextCarry) /10, add(As,Bs,Cs,NextCarry).

14 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD SEND MORE MONEY (3) And we call ?- add ([0,S,E,N,D], [0,M,O,R,E], [M,O,N,E,Y]), all_different([S,E,N,D,M,OR,Y]), labelling([], [S,E,N,D,M,O,R,Y]). all_different/1 constrains the variables in its argument list to be different labelling/2 enumerates the values of the variables in its second argument, as constraint by the store This version is roughly 2.800 time faster than the best generate and test version

15 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Check the photocopies for extra slides on constraint programming & DCG


Download ppt "Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC."

Similar presentations


Ads by Google