25 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming ­An overview Boolean Constraints Complete Solvers for.

Slides:



Advertisements
Similar presentations
Mathematical Preliminaries
Advertisements

3.6 Support Vector Machines
Applications Computational LogicLecture 11 Michael Genesereth Spring 2004.
Adders Used to perform addition, subtraction, multiplication, and division (sometimes) Half-adder adds rightmost (least significant) bit Full-adder.
Constraint Satisfaction Problems
Flexible access control policy specification with constraint logic programming Steve Barker, Peter J. Stuckey Presenter: Vijayant Dhankhar.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Introductory Mathematics & Statistics for Business
Cognitive Radio Communications and Networks: Principles and Practice By A. M. Wyglinski, M. Nekovee, Y. T. Hou (Elsevier, December 2009) 1 Chapter 12 Cross-Layer.
Analysis of Algorithms
February 21, 2002 Simplex Method Continued
Thursday, March 7 Duality 2 – The dual problem, in general – illustrating duality with 2-person 0-sum game theory Handouts: Lecture Notes.
February 7, 2002 A brief review of Linear Algebra Linear Programming Models Handouts: Lecture Notes.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Dr. Alexandra I. Cristea CS 319: Theory of Databases: C3.
Programming Language Concepts
1 Outline relationship among topics secrets LP with upper bounds by Simplex method basic feasible solution (BFS) by Simplex method for bounded variables.
Robust Window-based Multi-node Technology- Independent Logic Minimization Jeff L.Cobb Kanupriya Gulati Sunil P. Khatri Texas Instruments, Inc. Dept. of.
Lecture 15 Linked Lists part 2
Chapter 7: Arrays In this chapter, you will learn about
Pole Placement.
Discrete Math Recurrence Relations 1.
Copyright © Cengage Learning. All rights reserved.
The Game of Algebra or The Other Side of Arithmetic The Game of Algebra or The Other Side of Arithmetic © 2007 Herbert I. Gross by Herbert I. Gross & Richard.
Digital Filter Banks The digital filter bank is set of bandpass filters with either a common input or a summed output An M-band analysis filter bank is.
Minimum Weight Plastic Design For Steel-Frame Structures EN 131 Project By James Mahoney.
Detection Chia-Hsin Cheng. Wireless Access Tech. Lab. CCU Wireless Access Tech. Lab. 2 Outlines Detection Theory Simple Binary Hypothesis Tests Bayes.
Modern Programming Languages, 2nd ed.
Chapter 4 Gates and Circuits.
Chapter 4 Gates and Circuits.
Gates and Circuits Nell Dale & John Lewis (adaptation by Erin Chambers and Michael Goldwasser)
Digital Logic Design Gate-Level Minimization
Chap. 3 Logic Gates and Boolean Algebra
Other Gate Types COE 202 Digital Logic Design Dr. Aiman El-Maleh
CS105 Introduction to Computer Concepts GATES and CIRCUITS
MAT 205 F08 Chapter 12 Complex Numbers.
Real Numbers and Complex Numbers
Outline Minimum Spanning Tree Maximal Flow Algorithm LP formulation 1.
Chapter 4 Systems of Linear Equations; Matrices
Chapter 4 Gates and Circuits.
Direct-Current Circuits
Quadratic Inequalities
How to convert a left linear grammar to a right linear grammar
Constant, Linear and Non-Linear Constant, Linear and Non-Linear
P Preparation for Calculus.
Functions, Graphs, and Limits
The simplex algorithm The simplex algorithm is the classical method for solving linear programs. Its running time is not polynomial in the worst case.
Copyright © Cengage Learning. All rights reserved.
Copyright © Cengage Learning. All rights reserved.
Chapter 6 Equations 6.1 Solving Trigonometric Equations 6.2 More on Trigonometric Equations 6.3 Trigonometric Equations Involving Multiples Angles 6.4.
Chapter 10: The Traditional Approach to Design
Copyright © 2010 Pearson Education, Inc. All rights reserved Sec
Systems Analysis and Design in a Changing World, Fifth Edition
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
12 System of Linear Equations Case Study
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
PSSA Preparation.
1 Functions and Applications
9. Two Functions of Two Random Variables
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
The Pumping Lemma for CFL’s
Copyright © Cengage Learning. All rights reserved.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
SAT Solver CS 680 Formal Methods Jeremy Johnson. 2 Disjunctive Normal Form  A Boolean expression is a Boolean function  Any Boolean function can be.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Presentation transcript:

25 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming ­An overview Boolean Constraints Complete Solvers for SAT Boolean Unification Complete Solvers for Linear Constraints Applications [Dech04] Rina Dechter, Constraint Processing,

25 November 2005 Foundations of Logic and Constraint Programming 2 Boolean Constraints ­Boolean domains (often referred to as 0/1 variables) has many applications, namely  In Digital Circuits oExemple: Half-adder  In problems modelling binary choices oExemple: N-Queens  In problems dealing with sets B G1G1 G2G2 A C S

25 November 2005 Foundations of Logic and Constraint Programming 3 Approaches to Boolean Constraint Solving ­The satisfaction of Boolean constraints may be addresses in various and alternative ways  Simbolically Boolean Unification Complete Solver SAT All constraints are converted into Clauses (CNF) Solution construction (backtracking) or repairing (local search) Finite Domains Booleans are just an instance of a Finite domain (0/1) Techniques common to other Finite Domains

25 November 2005 Foundations of Logic and Constraint Programming 4 Boolean Constraints ­To handle Boolean satisfaction simbolically, it is convenient to convert all Boolean constraints using exclusively equality constraint on Boolean Formulas. Boolean Formulas are composed of  Two Boolean operators # (exclusive or) and · (conjunction),  Boolean constants 0 e 1 ( and possibly other constants, domain dependent)  variábles denoted by upper case letters ­This conversion is always possible, since the set {0, 1, #, ·} is complete. Given arbitrary terms a and b, all the usual operators and constants may be expressed using exclusively the “#” and “ · ” operators.  a= 1 # a ; a  b = a · b ; a  b= a # b # a · b a  b = 1 # a # a · b ; a  b = 1 # a # b

25 November 2005 Foundations of Logic and Constraint Programming 5 Boolean Rings ­More formally, the tuple, where A any domain that includes two elements 0 and 1, is a boolean ring, if the following properties are satisfied  Associativity a#(b#c) = (a#b)#ca·(b·c) = (a·b)·c  Comutativity a # b = b # aa · b = b · a  Distribution a #(b·c) = (a#b)·(a#c)a·(b#c) = a·b#a·c  Neutral Element a#0 = aa·1 = a  Exclusivity and Idempotence a#a = 0 a·a = a  Absorbing Element a·0 = 0

25 November 2005 Foundations of Logic and Constraint Programming 6 Boolean Constraints on Sets ­Although Boolean constraints are usually considered on domains with only two elements, another interesting domain of application is Sets. ­More precisely, considering a domain P of all all sets composed of elements a 1, a 2, a 3,..., a n  Constant 1 corresponds to the Universal set {a1, a2, a3,..., an}  Constant 1 corresponds to the Empty set { }  Operator “#” corresponds to the Set Exclusive Union  Operator “·” corresponds to Set Intersection ­Other constraints on setsmay be converted into equality. For example, given the equivalence a  b  a  b = a  a  b = b the includes constraint “  “can be rewritten as a # b # a·b = b  a # b # b # a·b = b # b a # a·b = 0  a·b = a

25 November 2005 Foundations of Logic and Constraint Programming 7 Constraint Solving through Boolean Unification ­The implementation of a symbolical and complete constraint solver fo Booleans is the core of CLP(B), the instance of the CLP(X) scheme to the Boolean Domain. ­In this solver, Boolean constraints are maintained in a solved form, obtained through Boolean unification. ­A Boolean constraint t 1 = t 2 (where terms t 1 e t 2 are exclusively written has the form with operators “#” and “ · ”. Such constraint can be satisfied iff there is a Boolean Unifier for terms t 1 and t 2. ­A Boolean unifier is a set of pairs x / t where x and t are respectively Boolean variables and terms (much in the same way of unifiers in logic programming), and where none of the variables may appear in the terms. ­As in logic programming, Boolean unifiers will be denoted by greek letters.

25 November 2005 Foundations of Logic and Constraint Programming 8 Boolean Unification: Examples Example 1: Terms t 1 =1 # A and t 2 = A·B admit the following Boolean unifier  = {A/1, B/0} In fact, denoting by t  (or t  ) the application of substitution  to term t, t 1  = (1#A)  {A/1, B/0}= 1 # 1 = 0 t 2  = (A·B)  {A/1, B/0}= 1 · 0 = 0 which guarantees that constraint t 1 = t 2 is satisfiable. Example 2: Terms t 1 = 1 # A·B and t 2 = C # D admit unifiers  = {C/1#A·B#D} and  = {C/1#D, A/0} since t 1  = (1#A·B) {C/1#A·B#D} = 1#A·B t 2  = (C#D ) {C/1#A·B#D} = 1#A·B#D#D = 1#A·B and t 1  = (1#A·B) {C/1#D, A/0} = 1#0·B= 1 t 2  = (C#D) {C/1#D, A/0} = (1#D#D)= 1

25 November 2005 Foundations of Logic and Constraint Programming 9 Multiple Boolean Unifiers ­Like in LP it is convenient to consider a hierarchy of unifiers, related by the “more general” relationship. ­Unifier  is more general then  there is a substitution  such that  Example: As seen above, terms t 1 = 1 # A·B and t 2 = C # D, admit unifiers  = {C/1#A·B#D} and  = {C/1#D, A/0} In this case,  is more general than , since for  {A/0}, it is .  = {C/1#A·B#D } o {A / 0} = {C/1#D, A/0} = 

25 November 2005 Foundations of Logic and Constraint Programming 10 Most General Boolean Unifiers ­In general, given two Boolean terms, t 1 e t 2, there might be not only more than one unifiers, but also more than one most general unifier. Example: Boolean terms t 1 = 1 # A·B and t 2 = C # D may be unified by the most general unifiers  1 = { C / 1 # A·B # D}  2 = { D / 1 # A·B # C}  3 = { A / 1 # C # D, B / 1}  4 = { A / 1, B / 1 # C # D} ­None of the unifiers may be obtained by the composition of any of the others and a substitution.

25 November 2005 Foundations of Logic and Constraint Programming 11 Boolean Unification Algorithm ­Taking into account that constraint t 1 = t 2 is equivalent to t 1 #t 2 = 0, the unification of terms t 1 and t 2 is equivalent to constrain term t 1 #t 2 to 0. The conditions necessary to zero a term t, may be analysed follows: 1.Zeroing a constant term is trivially checkable a.If t = 0 the term is already zero; b.If t = 1 term cannot be zeroed 2.Given distribution, a non constant term t, can always be written in the form a·U#b (where U is one of the variables occurring in t) factoring t in order to U. 3.A term t = a·U # b may onlty be zeroed if either a = 1 or b = 0, or both. Otherwise, i.e. a = 0 and b = 1, it would be t = 1  0, whatever the value chosen for U. 4.Such condition ( a = 1 or b = 0 ) is guaranteed iff the term (1 # a)·b is zeroed.

25 November 2005 Foundations of Logic and Constraint Programming 12 Boolean Unification Algorithm 5.Once condition a = 1 or b = 0 is guaranteed, I.if a = 0 (and b = 0 ), then variable U may take an arbitraru value, since 0 = 0·U # 0 ) II.if a = 1, then variable U must take valur b (in this case, 0 = 1·U # b ) 6.The assignment to U of these terms can be implemented by means of a fresh variable, not occurring in t. Denoting by _U this new variable, the previous condition is equivalent to U = (1#a)·_U # b 7.In fact, a.If a = 0 (and b = 0 ) then U = _U, i.e. U may take any arbitrary value, since variable _U, being a fresh variable, is not constrained anywhere. b.If a = 1 then U = (1#1)·_U # b, i.e. U = b, as intendend.

25 November 2005 Foundations of Logic and Constraint Programming 13 Boolean Unification Procedure The reasoning above can be used in the implementation of predicate unif_bool, shown below. predicate unif_bool(in: t 1, t 2 ; out:  ); t  t 1 # t 2 ; unif_bool  zero(t,  ); end predicate. The predicate inputs terms t 1 and t 2, that are to be unified, and succeeds if predicate zero succeedes, returning unifier  obtained from this predicate.

25 November 2005 Foundations of Logic and Constraint Programming 14 Boolean Unification Procedure Predicate zero can now be implemented as shpown below predicate zero (in: t; out:  ); case t = 0: zero = True,  = {}; case t = 1: zero = False; otherwise: A·u # B  t; s  (1#A)·B; if zero(s,σ) then zero  True;   {U/(1#A)·_U#B} o σ else zero = False end if end case end predicate

25 November 2005 Foundations of Logic and Constraint Programming 15 Boolean Unification Procedure: Example   {U/(1#A)·_U#B} o σ ­Notice that substitution  returned by predicate zero is obtained through the composition of substitutions {U/(1#A)·U#B} and σ obtained from the recursive call of the predicate. Example: Satisfy constraint X # X·Z = Y·Z # 1 Unify X#X·Z and Y·Z#1 zero X#X·Z#Y·Z#1 = (1#Z)·X#Y·Z#1 % A x ·X#B x zero (1#(1#Z))·(Y·Z#1) % (1#A x )·B x = Z·(Y·Z#1) = Z·Y#Z % A y ·Y#B y zero (1#Z)·Z %(1#A y )·B y = 0

25 November 2005 Foundations of Logic and Constraint Programming 16 Boolean Unification Algorithm: Example Now let us analyse the unifier that is returned by the sequence of calls Unify X#X·Z and Y·Z#1 zero X#X·Z#Y·Z#1 = (1#Z)·X#Y·Z#1 zero (1#(1#Z))·(Y·Z#1)=Z·Y#Z %(1#A x )·B x zero (1#Z)·Z = 0 %(1#A y )·B y σ z = {} σ y = {Y/(1#A y )·_Y#B y } o σ z = {Y/(1#Z) ·_Y#Z} o {} = {Y/(1#Z) ·_Y#Z} σ x = {X/(1#A x )·_Y#B x } o σ y = {X/(1#1#Z)·_X # Y·Z#1} o {Y/(1#Z)·_Y#Z} = {X/ Z·_X #Y·Z#1} o {Y/(1#Z)·_Y#Z} = {X/ Z·_X #((1#Z)·_Y#Z)·Z#1, Y/(1#Z)·_Y#Z} = {X/ Z·_X #Z#1, Y/(1#Z)·_Y#Z}  = {X/ Z·_X #Z#1, Y/(1#Z)·_Y#Z}

25 November 2005 Foundations of Logic and Constraint Programming 17 Interpreting Unifiers ­Hence, constraint X#X·Z = Y·Z#1 is satisfiable, since the unification of terms X#X·Z and Y·Z#1 succeeds, returning the most general unifier  = {X/Z·_X #Z#1, Y/(1#Z)·_Y#Z} ­This result can be confirmed: (X#X·Z)   = (Z·_X#Z#1)#(Z·_X#Z#1)·Z = (Z·_X#Z#1)#Z·_X = Z#1 (Y·Z#1)   = ((1#Z)·_Y#Z)·Z#1) = Z#1 ­Analysing the unifier we note that the two terms unify whenever  Z=0 and X = 1 and Y is arbitrary ( Y =_Y )  Z=1 and Y = 1 and X is arbitrary ( X =_X ) ­The unifier  has therefore the ground instances {X/1, Y/0, Z/0}, {X/1, Y/1, Z/0}, {X/0, Y/1, Z/1}, {X/1, Y/1, Z/1}.

25 November 2005 Foundations of Logic and Constraint Programming 18 Digital Circuits Application A simple example: 1.Model circuit below with Boolean constraints 2.How does the output relates (symbolically) with the inputs. ­R 1 : Unifier  1 is obtained by solving R 1 C = 1 # A·B  1 ={C / 1 # A·B} ­R 2 : Then  1 is applied to R 2 R2’: R2   1 :D = (1#A·C)  {C/1#A·B} D = (1#A·(1#A·B)) D = 1 # A # A·B R 1 : C = 1 # A·B R 2 : D = 1 # A·C R 3 : E = 1 # B·C R 4 : F = 1 # D·E C R1R1 F R4R4 D R2R2 E R3R3 B A

25 November 2005 Foundations of Logic and Constraint Programming 19 Digital Circuits Application ­Solving R2’: D = 1 # A # A·B unifier  ’ 2 is obtained  ’ 2 = {D / 1 # A # A·B} ­  2 is obtained by composition of  2 ’ with  1  2 =  1 o  2 ’ = {C/1#A·B} o {D/1#A#A·B} = {C/1#A·B, D/1#A#A·B} ­R 3 : Applying  2 to R 3, then R3’: R3   2 : E = (1#B·C)  {D/1#A#A·B, C/1#A·B} : E = 1#B·(1#A·B) ­Solving R 3 ’, unifier  3 ’ is obtained  3 ’ = {E/1#B#A·B} ­Now,  3 ’ is composed with  2  3 =  2 o  3 ’ = {E/1#B#A·B} o {D/1#A#A·B} o {C/1#A·B} = {E/1#B#A·B, D/1#A#A·B, C/1#A·B}

25 November 2005 Foundations of Logic and Constraint Programming 20 Digital Circuits Application ­R 4 : Finally, before solving R 4,  3 is applied R 4 ’: R 4   3 : F = (1#D·E)  {E/1#B#A·B, D/1#A#A·B, C/1#A·B} : F = 1#(1#A#A·B)·(1#B#A·B) : F = 1#1#B#A·B#A#A·B#A·B#A·B#A·B#A·B : F = A # B ­Now, R 4 ’ is solved and unifier  4 ’ is obtained  4 ’ = {F/A#B} ­Composing  4 ’ with  3, it is obtained  4, a unifier for all the constraints  4 =  3 o  4 ’ = {E/1#B#A·B, D/1#A#A·B, C/1#A·B}o{F/A#B} = {E/1#B#A·B, D/1#A#A·B, C/1#A·B, F/A#B} ­By interpreting  4 we may conclude that this circuit with 4 nand gates implements the exclusive-or, since F / A # B.

25 November 2005 Foundations of Logic and Constraint Programming 21 CLP(B) in SICStus ­Once loaded SICStus Prolog the module on Boolean Constraints is loaded with directive :- use_module(library(clpb)). ­Boolean Constraints may be specified with built-in predicate sat(E), where equality is specified as ‘=:=’ and several Boolean operators can be used, namely C = or(A,B) specified asC =:= A+B C = nor(A,B) specified asC =\= A+B C = xor(A,B) specified asC =\= A#B C = and(A,B) specified asC =:= A*B C = nand(A,B) specified asC =\= A*B C = not(A) specified asC =\= A

25 November 2005 Foundations of Logic and Constraint Programming 22 CLP(B) in SICStus Some Examples: 1.?- sat(A#B=:=F). sat(A =:= B#F)? % Constraint A#B = F is satisfiable with unifier A / B+F 2.?- sat(A*B=:=1#C*D). sat(A=\=C*D#B)? % Constraint A·B=1+C·D is satisfiable with unifier A/1+C·D+B 3.?- sat(A#B=:=1#C*D), sat(C#D=:=B). sat(B=:=C#D), sat(A=\=C*D#C#D) ? % Constraints A+B=1+C·D and B=C+D are satisfiable with unifier {A/1+C·D+C+D, B/C+D}

25 November 2005 Foundations of Logic and Constraint Programming 23 CLP(B) in SICStus A more complete example, related to the previous circuit | ?- circuit(A,B,[C,D,E],1). C = 1, E = A, sat(B=\=A), sat(D=\=A) ? Note:Compare this answer with unifier  5 = {E/1+B, D/B, C/1, F/1, A/1+B } found before. C G1G1 F G4G4 D G2G2 E G3G3 B A :- use_module(library(clpb)). nand_gate(X,Y,Z):- sat(X*Y =:= 1#Z). circuit(A,B,[C,D,E],F):- nand_gate(A,B,C), nand_gate(A,C,D), nand_gate(B,C,E), nand_gate(D,E,F).

25 November 2005 Foundations of Logic and Constraint Programming 24 CLP(Q) Application ­Another domain for which a complete solver exists is that of linear constraints over the rationals or reals. The integration of such a solver with LP forms the CLP(Q) or CLP(R) instance of the CLP(X) scheme. ­CLP(Q) and CLP(R) differ in their approach handle arithmetics:  If the coeficients are rational (division between integers) the solutiona are also rational. Hence CLP(Q) avoids rounding errors by handling properly with arithmetics (“+”,” –”, “*”, “/”) over rationals (with arithmetics without rounding  CLP(R) rounds rationals as floating point numbers, and is more “efficient” if less “precise” ­These constraints are quite common in applications. Even when constraints are not linear, problems are often “linearised” (i.e. modelled with linear constraints such taht the errors are acceptable), given the very efficient way of solving these constraints, based on the SIMPLEX algorithm.

25 November 2005 Foundations of Logic and Constraint Programming 25 CLP(Q) : Example Contraints: % Minimum required flow x  6, z  10 % minimum flow % flow capacity of arcs a  5, b  3, c  7, d  2, e  8, f  6 % processing capacity of nodes x  7, y  9, a + b  9, c  8, a + b + d  9, e + f  13 % flow maintenance x = a, y = b + c, a + b + d = e, c = d + f, e + f = z % non-negative constraints x, y,z, a, b, c, d, e, f  /a 2/d 8/e 6/f 3/b 7/c x y z The following problem illustrates a typical use of linear constraints: Problem (Network Management): Find the value of traffic from points x,y to z such that the traffic does not exceed the flow capacity of each arc, nor the processing capacity of each node.

25 November 2005 Foundations of Logic and Constraint Programming 26 CLP(Q) Solver: SIMPLEX ­The SIMPLEX algorithm aims at converting all linear constraints into a solved form, composed of equality constraints on linear expressions. This conversion is done in two steps. 1.All inequality constraints on decision variables are converted into equality constraints with the addition of “slack” (non-negative) variables a X a n X n ≤ b  a X a n X n + S = b a X a n X n ≥ b  a X a n X n - S = b 2.In general m constraints on n decision variables are converted into m constraints on k+n non-negative variables, where k  m ( k = m if all constraints are inequality constraints). 3.A solved form is obtained if a partition can be found on the k+n variables in m basic variables X i (i  1..n) and p = k+n-m non basic variables Y j (j  1.. p), such that the m constraints can be written as X i = d i + c i1 Y c ip Y p ( for all i  1..n) where all free coefficients d i are non-negative.

25 November 2005 Foundations of Logic and Constraint Programming 27 SIMPLEX: Geometric Interpretation X2 = 0 X2 X1X1 X3 = 0 X5 = 0 X1 = 0 X4 = 0 X3 = X1 - X2 X4 = -3 + X1 + X2 X5 = 5 + X1 - X2 x1 = 1 - x3/3 + x5/3 x2 = 6 - x3/3 - 2 x5/3 x4 = x3/3 - X5/3 X1 = 5 - X3 - X4 X2 = -2 + X3 + 2 X4 X5 = X3 - 3 X4 X2 = 5 + X1 - X5 X3 = X1 + X5 X4 = X1 - X5 2 X1 + X2 ≤ 8 X1 + X2 ≥ 3 X1 - X2 ≥ -5 X1, X2 ≥ 0 2 X1 + X2 + X3 = 8 X1 + X2 - X4 = 3 X1 - X2 - X5 = -5

25 November 2005 Foundations of Logic and Constraint Programming 28 SICStus CLP(Q)/CLP(R) Solvers ­SICStus Prolog supports constraint solving over the Rational/Real numbers. As for Booleans, to handle these constraints, a module for CLP(Q) or CLP(R) must be loaded, with directives: :- use_module(user:library(clpq)). :- use_module(user:library(clpr)). ­Contraints may now be specified with the usual Prolog syntax but inside { }. For example the previous problem may be directly specified as p1([X1,X2]) :- {2*X1 + X2 = = 3, X1 - X2 >= -5 }, { X1 >= 0, X2 >= 0 }. or specified with explicit slack variables p2([X1,X2,X3,X4,X5]) :- {2*X1+X2+X3 = 8, X1+X2–X4 = 3, X1-X2–X5 = -5 }, { X1 >= 0, X2 >= 0, X3 >= 0, X4 >= 0, X5 >= 0 }.

25 November 2005 Foundations of Logic and Constraint Programming 29 SICStus CLP(Q)/CLP(R) Solvers ­Once defined these programs can be run. Answers projected to the called variables, with some extra variables included when necessary p1([X1,X2]) :- {2*X1 + X2 = = 3, X1 - X2 >= -5 }, { X1 >= 0, X2 >= 0 }. | ?- p1([X1,X2]). {X1+1/2*X2=<4}, {X1>=0}, {X2>=0}, {X1+X2>=3}, {X1-X2>= -5} ? X2 >= 0 X2X2 2 X1 + X2 = 8 X1 - X2 = -5 X1 >= 0 X1 + X2 = 3 X1

25 November 2005 Foundations of Logic and Constraint Programming 30 SICStus CLP(Q)/CLP(R) Solvers ­The solved form is more easily (?) identified with the explicit slack variables. p2([X1,X2,X3,X4,X5]) :- {2*X1+X2+X3 = 8, X1+X2–X4 = 3, X1-X2–X5 = -5 }, { X1 >= 0, X2 >= 0, X3 >= 0, X4 >= 0, X5 >= 0 }. | ?- p2([X1,X2,X3,X4,X5]). {X5=9-3/2*X2-1/2*X3}, {X4=1+1/2*X2-1/2*X3}, {X1=4-1/2*X2-1/2*X3}, {X2+X3=<8}, {X2+1/3*X3=<6}, {X2>=0}, {X3>=0}, {X2-X3>= -2} ? ­Solved form identifies X1 = 4, X2 = 0 X2 >= 0 X2X2 2 X1 + X2 = 8 X1 - X2 = -5 X1 >= 0 X1 + X2 = 3 X1

25 November 2005 Foundations of Logic and Constraint Programming 31 SICStus CLP(Q)/CLP(R) Solvers ­Of course, if the problem has no solutions the answer is no! p3([X1,X2]) :- {2*X1 + X2 >= 8, X1 + X2 =< 3, X1-X2 =< -5 }, { X1 >= 0, X2 >= 0}. | ?- p3([X1,X2]). no ? X2 >= 0 X2X2 2 X1 + X2 = 8 X1 - X2 = -5 X1 >= 0 X1 + X2 = 3 X1

25 November 2005 Foundations of Logic and Constraint Programming 32 SICStus CLP(Q)/CLP(R): Example Example: A certain amount of money is lent at some interest rate for a number of time periods, to be payed with constant instalments. Model Assuming that  I is the number of time periods remaining to pay the debt  D i represents the debt at the begining of time period i.  R is the interest rate (in percentage points)  P is the (constant) instalment to be payed at the end of each time period then we have the following relations:  The debt at the begining of a period is the debt at the begining of the previous period, increased by the interest, less the payment made. D i = D i+1 * (1 +R/100) – P for i > 1  No debt should exist at the end of the last time period last time period 0 = D 1 * (1 +R/100) – P

25 November 2005 Foundations of Logic and Constraint Programming 33 SICStus CLP(Q)/CLP(R): Example ­These constraints can be easily expressed in CLP(Q) / CLP(R) as shown in the programs below. ­Notice that such program could not be easily converted to pure Prolog, in statements like V is Exp, since one does not know what values are given and what are know. % 0 = D1 * (1 +R/100) – P debt(Debt, Rate, TimeLeft, Payment):- { TimeLeft = 1, Payment = Debt * (1 + Rate/100) }. % Di = Di+1 * (1 +R/100) – P for i > 1 debt(Debt, Rate, TimeLeft, Payment):- { TimeLeft > 1, NextTimeLeft = TimeLeft - 1, NextDebt = Debt * (1 + Rate/100) – Payment }, debt(NextDebt, Rate, NextTimeLeft, Payment).

25 November 2005 Foundations of Logic and Constraint Programming 34 SICStus CLP(Q)/CLP(R): Example Some interaction with the program  What yearly payment is due when 100 units are lent for 7 years at a 5% interest rate. What is the total payment? | ?- debt(100,5,7,P), {T = 7* P}. P = , T = ?  What amount of money can be borrowed by someone that is willing to pay a fixed instalment of 10 units for 7 years, whne the interest rate is 5%? | ?- debt(D,5,7,10). D = ?  What is the relationship between the yearly payment and the initial debt, when 7 years are used and the interest rate is 5%? | ?- debt(D,5,7,P). {P= *D} ?

25 November 2005 Foundations of Logic and Constraint Programming 35 SICStus CLP(Q)/CLP(R): Example ­It is important to notice that CLP(Q)/CLP(R) is only complete when the constraints are linear. When they are not they are frozen in the constraint store, pending further instantiation  What is the relation between the yearly payment and the initial debt, when 7 years are used and the interest rate is R% | ?- debt(D,R,7,10). clpr:{10.0-D-0.01*(R*D)+_A=0.0},% 7 years to go clpr:{ *(_B*R)-_B=0.0},% 6 years to go clpr:{10.0+_C-0.01*(_A*R)-_A=0.0},% 5 years to go clpr:{10.0+_D-0.01*(_C*R)-_C=0.0},% 4 years to go clpr:{10.0+_E-0.01*(_D*R)-_D=0.0},% 3 years to go clpr:{10.0+_F-0.01*(_E*R)-_E=0.0},% 2 years to go clpr:{10.0+_B-0.01*(_F*R)-_F=0.0} ?% 1 years to go