1 / 54

Chapter 4

Chapter 4. Constraint Logic Programming. Outline. Introduction The constraint logic programming scheme CLP languages and applications CLP(R) CHIP B-Prolog Conclusions. Introduction.

odell
Download Presentation

Chapter 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 Constraint Logic Programming

  2. Outline • Introduction • The constraint logic programming scheme • CLP languages and applications • CLP(R) • CHIP • B-Prolog • Conclusions

  3. Introduction • Given a particular domain, a constraint expresses a desired relationship among one or more of the objects. • Many practical problems can be regarded as constraint satisfaction problems (CSPs). • Logic Programming (LP) languages such as Prolog, with their general purpose Horn clause represen-tation and single clear semantics, are very good at expressing constraints in a declarative fashion. • However, Prolog’s unification mechanism is not sufficiently powerful to handle constraints properly. • Unification is not good at processing constraints non-deterministically.

  4. Limitation of Prolog • Example 1: plus(X,Y,Z) :- Z = X+Y. It is unable to deal satisfactorily with: ?- plus(X,Y,2). • Example 2: fact(0,1). fact(N,F):- N>0, N1 is N -1, fact(N1,F1), F is N*F1. The query ?- fact(2,F). gives the answer F = 2, but Prolog fails with the query ?- fact(N,2).

  5. CLP = logic Programming + Constraint Programming • There has been some effort within the Logic Programming community to extend Prolog to overcome these limitations with unification. • However, despite these improvements, LP language still suffer from significant efficiency problems when dealing with constraint satisfaction. • Their simple backtracking search method implies a generate-and-test procedure in which variable values are generated and the constraints are used passively to check the values. • Recently, there has been a radical approach at extending the Logic Programming paradigm to produce Constraint Logic Programming (CLP) languages. • CLP is a merger of two declarative paradigms: constraint programming and logic programming.

  6. Improvements of CLP • Like Logic Programming, CLP uses the resolution principle. However, unification has been replaced by constraint solving over an application domain. • CLP improves on Logic Programming when dealing with constrained search problems, by providing: • more expressive languages in which a user can work directly with objects from the intended domain and employ the natural constraints for that domain, and • far more efficient execution employing specialized methods and algorithms.

  7. THE CONSTRAINT LOGIC PROGAMMING SCHEME • Jaffar and Lassez have given the formal definition of a class of languages for which there is a simple and unified declarative and operational semantics. This is a general framework from which many CLP languages can be derived. • Each instance of this scheme is obtained by specifying a solution-compact structure of computation. • A structure A consists of: • a set D(A) – the domain of A • a collection  of n-ary function: An A; • a collection  of n-ary relations: An {TRUE, FALSE}. • An A-term is either • a constant in ; • a variable; or • of the form f(X1,…,Xn) where f is a function and the Xi are A-terms.

  8. An constraint is of the form: p(X1,…,Xn) where p  and the Xi are A-terms • An atom is of the form: p(X1,…,Xn) where p is a predicate symbol  and the Xi are A-terms. Countable structures, the structure of real numbers are solution-compact. By specifying a solution-compact structure A, one obtains a programming language CLP(A) which is an instance of the CLP Scheme.

  9. A CLP(A) program consists of a finite number of rules: A0 :- C1, C2,…,Cn, A1, A2,…,Am where n, m  0, Ci are constraints over the A-relations and A-terms, Ai are atoms over the A-terms. • A CLP(A) goal is of the form: ?- C1, C2,…,Cn, A1, A2,…,Am where n, m  0, n+m  1, Ci are constraints Ai are atoms • An atom in a goal is a subgoal. • If A, B are two atoms with the same predicate symbol, A = B is used to signify the collection of equations between corresponding arguments in these two atoms. • Example: If A is resistor(X,Y, 5) and B is resistor(2,8,Z), Then A = B denotes { X = 2, Y = 8, Z = 5}.

  10. The CLP(A) Operational Model • Let P denote a CLP(A) program. Let G1, G2 be two goals containning the collections C1, C2 of constraints. Assume that both C1 and C2 are solvable. • There is a derivation step from G1 to G2 if:  G1 is of the form ?- C1,A1,A2,…,Am, where m>= 1  P contains a rule of the form B0 :- C2,D1,D2,…,Dk. where k >= 0 ( B0can match withAi).  G2 is of the form ?- C1,C2, Ai = B0, A1,A2,…Ai-1,Ai+1,…,Am, D1,D2,…,Dk where 1  i  m • Ai in G1 is called the selected subgoal, or the subgoal in G1 chosen to be reduced.

  11. A derivation sequence is a sequence of goals in which there is a derivation step to each goal from the preceding goal. • A derivation sequence is successful if it is finite and its last goal contains only constraints. Such constraints are called answer constraints. Answer constraints constitute the output of a CLP(A) program. • A finitely-failed sequence is a non-successful finite sequence, where no derivation step is possible from its last goal. • A CLP(A) program and goal are executed by a process of continually reducing any remaining atoms in the subgoal.

  12. A comparison of CLP to LP

  13. CLP LANGUAGES AND APPLICATIONS • In this section, we look at some of the best-known and most successful implementations of the CLP schema. • The design and implementation of constraint solvers for these languages has been of key important: • efficiency in dealing with simple constraints, which are the most commonly occurring. • incrementality, so that solving extra constraints does not involve re-solving the original set. • an internal representation facilities efficient backtracking. • We will study 3 languages • CLP(R) • CHIP • B-Prolog

  14. CLP() • CLP() was developed by Jaffar and Michaylov at IBM, Yorktown Heights and at Monash University, Australia. The domain of computation: reals. • Real constants and variables are arithmetic terms. • If t1, t2 are arithmetic terms, then so are (t1+t2), (t1-t2), (t1*t2) and (t1/t2). • Arithmetic terms, uninterpreted constants and variables are all terms. • If f is an n-ary uninterpreted function and t1,…,tn are terms, then so the tree t(t1,…,tn). • An equation or inequality between arithmetic terms is an arithmetic constraint, solvable when there exists a substitution for its variables that makes it true in the obvious way. • Two trees are equal when they share the same root function and the corresponding subtrees are equal.

  15. The CLP() interpreter • The CLP() interpreter: • employs the Prolog’s left-to-right atom selection rule. • employs Prolog’s top-to-bottom depth-first-search strategy. • Its constraint solver does not determine the solvability of all linear non-constraints. The determination of constraint solvability may be delayed until the constraint is “sufficiently instantiated”. • In CLP(), a non-linear constraint c is delayed until the linear equations in the system entail that sufficient variables in c are ground, so as to make it linear. When a delayed constraint is awoken and found to be unsolvable, the system backtracks as in Prolog. The delaying and awakening process is automatic and is designed to be transparent to the user.

  16. CONSTRAINT SOLVER Input delay Equation Solver Engine Interface Inequation Solver Solve directly Output Module Solve directly The architecture of the CLP(R) interpreter

  17. The inference engine controls the execution of derivation steps and maintains variable bindings. Some simpler constraints can be solved directly by the engine. The remainder is passed on the interface, which will eventually return a Boolean answer to the engine. • The interface evaluates complex arithmetic expression and transforms constraints into a small number of standard forms. It can do some constraint satisfaction of its own, but in the main, passes on the standardized constraints to the constraint solver. • The constraint solver lets the equation solver deal with linear equations, using a form of Gaussian elimination. It calls the inequality solver to handle linear inequalities, using an incremental form of the first phase of the Two-phase Simplex method. It delays non-linear equation, awakening them when they become linear and then passing them to the equation solver.

  18. The output module converts constraints so that they reflect relationships only between variables in the goal, and transform these into a compact standard canonical form. Example Programs: fib(0,1). fib(1,1). fib(N+2, X +Y):- N>= 0, fib(N+1, X), fib(N,Y). Steps in the computation of the goal fib(N0,3) are as follows:

  19. ?- fib(N0,3). ?- N1 >= 0, N0 = N1 + 2, 3 = X1 + Y1, fib(N1+1, X1), fib(N1,Y1). … ?- N1 >= 0, N0 = N1 + 2, 3 = X1 + Y1, N2 >= 0, N1 + 1 = N2 +2, X1 = X2+Y2, fib(N1, Y1), fib(N2+1, X2), fib(N2,Y2). … ?- N1 >= 0, N0 = N1 + 2, 3 = X1 + Y1, N2 >= 0, N1 + 1 = N2 +2, X1 = X2+Y2, N1 = 1, Y1 = 1, fib(N2+1, X2), fib(N2,Y2). … ?- N1 >= 0, N0 = N1 + 2, 3 = X1 + Y1, N2 >= 0, N1 + 1 = N2 +2, X1 = X2+Y2, N1 = 1, Y1 = 1, N2+1=1, X2=1, fib(N2,Y2). … ?- N1 >= 0, N0 = N1 + 2, 3 = X1 + Y1, N2 >= 0, N1 + 1 = N2 +2, X1 = X2+Y2, N1 = 1, Y1 = 1, N2+1=1, X2=1, N2=0,Y2=1. Solving the final constraint set, with respect to N0, we obtain: N0 = 3.

  20. Electrical Circuit CLP() program for selecting components in an electrical circuit. ohmlaw(V, I, R):- V = I*R. kirchoff(L):- sum(L,0). sum([ ],0). sum([H|T], N):- N = H+M, sum(T,M). availres(10). availres(14). availres(27). availres(60). availres(100). availcell(10). availcell(20).

  21. and the query: ?- 14.5< V2, V2< 16.25, availres(R1), availres(R2), avail(V), ohmlaw(V1,I1,R1), ohmlaw(V2, I2, R2), kirchoff([I1, -I2]), kirchoff([-V, V1, V2]). • CLP() computes the three sets of solution: V = 20, R1 = 10, R2 = 27, V = 20, R1 = 14, R2 = 60, V = 20, R1 = 27, R2 = 100. I1 R1 I2 V1 V2 R2 V

  22. The CLP Language CHIP • CHIP(constraint handling in Prolog) was developed at ECRC, Munchen. • There are three domains of computation • finite domains, • Boolean algebra and • rational numbers.

  23. Finite Domains • Constraints may be: • numerical- equations, inequalities and disequations on arithmetic terms over domain variables. • symbolic- e.g., element(N, List, Var) meaning that Var is the N-th element of List. • optimization – e.g. minimize(Term, Function) which uses a branch-and-bound technique to find solution to Goal that minimizes Function. • Constraint solving domain variables are solved through Consistency Techniques, which uses constraints to reduce the domains of variables and so the size of the search space. The following two steps are iterated until a solution is reached: • propagation the constraints as far as possible • making a choice

  24. Cryptarithmetic Puzzle • The well known cryptarithmetic puzzle: S E N D + M O R E -------------- M O N E Y can be represented by the CHIP program: sendmore([S,E,N,D],[M,O,R,E],[M,O,N,E,Y]):- [S, E, N, D, M, O, R, Y]:: 0..9, alldifferent([S, E, N, D, M, O, R, Y]), S 0, M  0, Send = 1000*S + 100*E + 10*N + D, More = 1000*M + 100*O + 10*R + E, Money = 10000*M + 1000*O + 100*N + 10*E + Y, Send + More # = Money, labelling([S, E, N, D, M, O, R, Y]). #= is the equality constraint for linear terms. labelling() generates values for the variables

  25. Cryptarithmetic Puzzle (cont.) • Just by constraint manipulation and propagation, the search space is reduced to: S = 9, M = 1, O = 0, E [4..7], N [5..8], R [2..8], D [2..8], Y [2..8] before enumeration starts.

  26. Rational Arithmetic • Used for modeling continuous problems (where there are an infinite number of points in the search space to explore). • Constraints may be: • numerical – equations, inequalities and disequations on linear rational terms • optimization – e.g., rmax(Func) which finds the maximum value of Func, subject to a set of constraints. • CHIP provides a decision procedure for constraints on linear rational terms that is based on a version of the Simplex algorithm.

  27. The constraint solver • The constraint solver has the following features: • it is embedded fully in the CHIP language, rather than a separate module. • It is incremental, in that adding a new constraint doesn’t mean starting from scratch again; • equations and inequalities are treated in a uniform manner – no need to transform equations into inequalities and introduce artificial variables. • The solution returned is the most general one, allowing an infinite number of solutions to be represented symbolically in a finite way; • The same solver can be used both for deciding whether a set of constraints is satisfiable as well as for finding an optimal solution.

  28. Example Program • Consider the transportation problem, that involves two factories with can supply in total 4 and 6 and three warehouses with total demands of 3,5 and 2. Given a 2  3 matrix of unit transportation costs from factory to warehouse, the aim is to find how much each factory should supply to each warehouse so that the overall transportation costs are kept a minimum, while keeping to the supply and demand constraints.

  29. Transportation problem The problem can be expressed in CHIP as follows: top :- X11 + X21 >= 3, X12 + X22 >= 5, X13 + X23 >= 2, X11 + X12 + X13 <= 4, X21 + X22 + X23 <= 6, Cost = 5*X11 + X12 + X13 + 2*X21 + 6*X22 + 9*X23, rmin(Cost).

  30. B-Prolog • B-Prolog is a system for running Prolog and CLP(FD) programs. It can - runs ISO Prolog programs, and - also supports constraint solving over finite domains and Booleans. • The system can work on SunOS, Solaris, Linux and MSDOS (Windows). • B-Prolog was developed by Neng Fa Zhou. • In general, a CLP(FD) program is composed of three parts: • Variable generation which generates variables and specifies their domains • Constraint generation which specifies constraint over the variables • Labeling which instantiates the variables.

  31. Example 1- Constraint Propagation • Solving a CSP with constraint set = { C< D, E<C, E<D} and variables C, D, E  {1,2,3,4} % File: clp2.pl csp2(L):- L=[C,D,E], L in 1..4, C#< D, E#<C, E#<D, labeling(L). Note: The predicate labeling(L) instantiates the list of variables L by enumeration. L in D means the domain of the list of variables L is D. ?- csp2(L). L=[2,3,1]; L=[2,4,1]; L=[3,4,1]; L=[3,4,2];

  32. Example 2 Constraint Propagation • Solving a CSP with constraint set = { X+Y = 9, 2X+4Y= 24 } and X,Y in 0..9 % File: clp1.pl % Date: 3/2004 csp1(L):- L=[X,Y], L in 0..9, X+Y #= 9, 2*X +4*Y#=24, labeling(L). ?- csp1(L). L = [6,3]

  33. Example 3: Light meal A program for devising light meals whose total of calories is less than 10. lightmeal(A,M,D):- L=[I,J,K], L in 0..10, I+J+K #=<10, appetiser(A,I), main(M,J), dessert(D,K), labeling(L). main(M,I):- meat(M,I); fish(M,I). appetiser(radishes,1). appetiser(pasta,6). meat(beaf,5). meat(pork,7). fish(sole,2). fish(tuna,4). dessert(fruit,2). dessert(icecream,6). ?- lightmeal(A,M,D). A = radishes M = beef D = fruit A = radishes M = pork D = fruit A = radishes M = sole D = fruit A = radishes M = sole D = icecream A = radishes M = tuna D = fruit A = pasta M = sole D = fruit

  34. Example 4 - NQueens % File: clp_queens.pl % Purpose: Solving N-Queens problem queen_N(N,List):- makelist(N,List), List in 1..N, constrain_queens(List), labeling_ff(List). /* labelling with “first fail” variable ordering */ constrain_queens([]). constrain_queens([X|Y]):- safe(X,Y,1), constrain_queens(Y). safe(_,[],_). safe(X,[Y|T],K):- noattack(X,Y,K), K1 is K+1, safe(X,T,K1).

  35. noattack(X,Y,K):- X#\=Y, X+K#\= Y, X-K#\=Y. makelist(0,[]):- !. makelist(N,[_|Rest]):- N1 is N-1, makelist(N1,Rest). Note: The predicate makelist(N, L) generates L, a list of N variables. ?- queen_N(8,L). L=[1,5,8,6,3,7,2,4]

  36. Example 5: Graph Coloring • The problem is to label the vertices of a graph so that no two adjacent vertices are assigned to the same color. % File :clp_color.pl % Date : 3/2004 % Purpose: graph coloring with B-Prolog graphcolor(N,Vars):- makelist(N,Vars), Vect =..[f|Vars], Vars in 0..3, build_vars(N,Vect), labeling(Vars).

  37. 1 2 3 6 5 4 7 8 9 11 12 13 10

  38. build_vars(N,_):- N=<0,!. build_vars(N,Vect):- neighbors(N,Neibs), arg(N,Vect,Var), constrain_color(Var,Neibs,Vect), N1 is N-1, build_vars(N1,Vect). constrain_color(_,[],_). constrain_color(Var,[N|Neibs],Vect):- arg(N,Vect,Var1), Var#\=Var1, constrain_color(Var,Neibs,Vect). makelist(0,[]):- !. makelist(N,[_|Rest]):- N1 is N-1, makelist(N1,Rest).

  39. Graph Coloring (cont.) neighbors(1,[5,6,7,10]). neighbors(2,[6,7,8,10,11]). neighbors(3,[10,11,12]). neighbors(4,[]). neighbors(5,[1,8,11]). neighbors(6,[1,2,7,11,12]). neighbors(7,[1,2,6,11,12]). neighbors(8,[2,5,12]). neighbors(9,[]). neighbors(10,[1,2,3]). neighbors(11,[2,3,5,6,7]). neighbors(12,[3,6,7,8]). neighbors(13,[]). ?- graphcolor(13,V). V = [0,0,0,0,1,1,2,2,0,1,3,3,0] : We assign color 0 to vertices 1,2,3,4,9,13; color 1 to vertices 5,6,10; color 2 to vertices 7,8; color 3 to vertices 11,12.

  40. Example 6 – Scheduling with Precedent Constraints Problem Statement. • We are given • a set of n tasks where each task i has its duration di and • a set of precedent constraints. • A precedence constraint between tasks i and j implies that task j must start after the completion of task i. • The project starts at time 0, and the problem is to find a schedule that minimizes the total duration of the project. • We add a fictitious task of duration 0, called end task, which is preceded by all other tasks.

  41. The durations of tasks can be used to calculate two basic quantities for each tasks: its earliest time and its latest time. • The earliest time for a task is the time at which the task will start if the preceding tasks are started as early as possible. The earliest time point ti to start task i is ti = max(tj +dj) for all tasks j which proceed task i. tendis the earliest time point for the end task and also the minimum duration of the project. • If the duration of the project is tend, the latest time point Ti to start task i is given by Ti = min(Tj - di) for all tasks j s.t. task i precedes task j, assuming that Tend is equal to tend.

  42. So the starting time of a task i can be represented by a domain [ti,Ti]. • The slackmi of task i is defined to be the difference between the earliest starting time and the latest starting time. The tasks whose slacks are 0 are called critical tasks. • Problem Representation To each task we associate a variable Si representing the starting time of task i. Now a precedence constraint between task i and task j can be Sj >= Si + di Example. The following table describes a project with 10 tasks A, B, C, D,E, F, G, H, J, and K.

  43. /* Title : House building scheduling problem *//* House building scheduling problem with 10 tasks */ house(L):- L =[SA,SB,SD,SC,SE,SF,SG,SH,SJ,SK,Send], L in 0..30, SB #>= SA+7, SD #>= SA+7, SC #>= SB+3, SE #>= SC+1, SE #>= SD+8, SG #>= SC+1, SG #>= SD+8, SF #>= SD+8, SF #>= SC+1, SH #>= SF+1, SJ #>= SH+3, SK #>= SG+1, SK #>= SE+2, SK#>= SJ+2, Send #>= SK+1, minof(labeling(L),Send).

  44. Note: • 1.The variables SA,…,SK represent the starting dates of the tasks of the project, and the Send is the starting date of the end task, i.e., the finishing date. • 2.The predicate minof(Goal, Exp) finds a satisfiable instance of Goal such that Exp has the minimum optimal value. Here, Goal is used as generator (e.g., labeling(L)), and Exp is an expression. All satisfiable instances of Goal must be created, and for every such instance, Exp must be an integer expression. • In B-Prolog, the search for an optimum solution uses a kind of depth-first branch and bound technique.

  45. For example, when a solution with a value F0 is found, the constraint (Goal < F0) is dynamically added to the program. While searching a new solution, all branches of the search space where the value of Goal is greater than F0 are pruned. The optimal solution is achieved when no new solution better than the most recent one can be found. ?- house(LD). LD = [0,7,7,10,15,15,15,16,19,21,22]. That means SA = 0, SB = 7, SC=7, SD = 10, SE=15, SF=15, SG=15, SH=16, SJ=19, SK=21, Send = 22.

  46. Example 7: Project selection • A company is considering expansion by building a new factory in either city L or city S, or perhaps in both cities. It also is considering building at most one new warehouse, but the choice of location is restricted to a city where a new factory is being built. • The net present value (total profitability) of each of these alternatives is shown in the fourth column of the following table.

  47. Project selection (cont.) • Capital available: 10 million • The rightmost column gives the capital required for the respective investments. The total capital available is 10 million. • The objective is to find the feasible combination of alternatives that maximizes the total net present value. • Because the last two decisions are mutual exclusive, we need the constraint x3 + x4  1 • Furthermore, decision 3 and 4 are contingent decisions, because they are contingent on decisions 1 and 2. x3  x1 x4  x2.

  48. projs(L):- L= [D1,D2, D3,D4], L in 0..1, 6*D1 +3*D2+5*D3+2*D4 #=< 10, D3 + D4 #=< 1, D3 #=< D1, D4 #=< D2, maxof(labeling(L), 9*D1+5*D2 + 6*D3+4*D4). ?- projs(L). L = [1,1,0,0] • That means the company should build factories in both cites but no warehouse.

  49. Example 8: Assignment Problem • The assignment problem is a problem where assignees are being assigned to perform tasks. • For example, the assignees might be employees who need to be given work assignments. However, the assignees could be machines, or vehicles or plants. • To fit the definition of an assignment problem, the applications need to be formulated in a way that satisfies the following assumptions. • The number of assignees and the number of tasks are the same. (This number is denoted by n.) • Each assignee is to be assigned to exactly one task. • Each task is to be performed by exactly one assignee. • There is a cost cij associated with assignee i (i = 1,2,…,n) performing task j (j =1,2,…,n). • The objective is to determine how all n assignments should be made in order to minimize the total cost.

More Related