Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constraint Logic Programming (CLP) Luis Tari March 10, 2005.

Similar presentations


Presentation on theme: "Constraint Logic Programming (CLP) Luis Tari March 10, 2005."— Presentation transcript:

1 Constraint Logic Programming (CLP) Luis Tari March 10, 2005

2 Why CLP? “Generate-and-test” approach is a common methodology for logic programming. –Generate possible solutions –Test and eliminate non-solutions Disadvantages of “generate-and-test” approach: –Passive use of constraints to test potential values –Inefficient for combinatorial search problems CLP languages use the global search paradigm. –Actively pruning the search space –Recursively dividing a problem into subproblems until its subproblems are simple enough to be solved

3 Why CLP and AnsProlog? AnsProlog is inefficient in dealing with numerical values, due to –Implementation of the current solvers –“generate-and-test” paradigm Goal of CLP is to pick numerical values from pre-defined domains for certain variables so that the given constraints on the variables are all satisfied. Idea: use CLP to define and reason with numerical constraints and assignments

4 List Notation in Prolog Example of list notation –[eggs, tea, milk, steak, spinach, toothpaste] Head of a list –First element of a list –eggs Tail of a list –The remaining list apart from the head –[tea, milk, steak, spinach, toothpaste] [H|T] –H = eggs –T = [tea, milk, steak, spinach, toothpaste]

5 An example of List Use the predicate append to define appending two lists into one. append([X|XL],YL,[X|ZL]) :- append(XL,YL,ZL). append([],YL,YL). ?- append([1],[2],X). X = [1,2]

6 CLP with Sicstus Prolog To invoke the CLP library: | ?- :- use_module(library(clpfd)). A constraint is called as if it is a Prolog predicate. | ?- X in 1..5, Y in 2..8, X+Y #= T. X in 1..5, Y in 2..8, T in 3..13 The above constraint says that given X,Y, assign values for T so that the constraint is satisfiable. | ?- X in 1..5, T in 3..13, X+Y #= T. X in 1..5, T in 3..13, Y in -2..12

7 Typical Steps of a CLP Program Step 1. Declare the domains of the variables Step 2. Post the problem constraints Step 3. Look for a feasible solution via backtrack search, or look for an optimal solution via branch-and-bound search

8 Constraint Satisfaction Problem Let’s consider the Send More Money puzzle. The variables are the letters S, E, N, D, M, O, R and Y. Each letter represents a digit between 0 and 9. Assign a value to each digit, such that SEND + MORE equals MONEY.

9 Send More Money Example using CLP | ?- mm([S,E,N,D,M,O,R,Y], []). D = 7, E = 5, M = 1, N = 6, O = 0, R = 8, S = 9, Y = 2

10 Reified Constraints To reflect its truth value into a 0/1 variable B, so that: –The constraint is posted if B is set to 1 –The negation of the constraint is posted if B is set to 0 –B is set to 1 if the constraint becomes entailed –B is set to 0 if the constraint becomes disentailed. A reified constraint is written as: | ?- Constraint # B.

11 An example using reified constraint Define exactly(X,L,N) so that it is true if X occurs exactly N times in the list L. If the constraint “element X is the same as element Y” becomes entailed, then assign B to be 1 else 0.

12 Other Constraints Arithmetic constraints –| ?- X in 1..2, Y in 3..5, X#= B. B = 1, X in 1..2, Y in 3..5 –If the constraint “value in X is <= value in Y” becomes entailed, then assign B to be 1 else 0. Propositional constraints –X #= 4 #\/ Y #= 6 –Disjunction of the two equality constraints Many more….

13 Defining Search Variants labeling(:Options, +Variables) –Where Variables is a list of domain variables or integers and Options is a list of search options. | ?- constraints(Variables), labeling([], Variables). %same as [leftmost,step,up,all] –leftmost : the leftmost variable is selected –step : makes a binary choice between X#=B and X#\=B, where B is the lower or upper bound of X. –up : the domain is explored in ascending order –all : all solutions are enumerated by backtracking

14 An Example - Cumulative Scheduling There are 7 tasks where each task has a fixed duration and a fixed amount of used resource. Goal: find a schedule that minimizes the completion time for the schedule while not exceeding the capacity 13 of the resource.

15 Cumulative Scheduling (2) :- use_module(library(clpfd)). :- use_module(library(lists), [append/3]). schedule(Ss, End) :- length(Ss, 7), Ds=[16,6,13,7,5,18,4], Rs=[2,9,3,7,10,1,11], domain(Ss, 1, 30), domain([End], 1, 50), after(Ss, Ds, End), cumulative(Ss, Ds, Rs, 13), append(Ss, [End], Vars), labeling([minimize(End)], Vars). after([], [], _). after([S|Ss], [D|Ds], E) :- E #>= S+D, after(Ss, Ds, E). Constraining the tasks such that for each task j has a start time Ss j, a duration Ds j, and a resource Rs j, so that the total resource consumption does not exceed 13 at any time. Find an assignment that minimizes the domain variable of Vars

16 Cumulative Scheduling (3) Ds = [16,6,13,7, 5,18,4] Rs = [2,9, 3,7,10, 1,11] Capacity = 13 | ?- schedule(Ss, End). Ss = [1,17,10,10,5,5,1], End = 23

17 References Sicstus Prolog Manual Version 3.12, Chapter 34 on Constraint Logic Programming over Finite Domains P. Van Hentenryck, H. Simonis, M. Dincbas, Constraint satisfaction using constraint logic programming, Journal of Artif. Intell., 58(1-3), pages 113--159, 1992.


Download ppt "Constraint Logic Programming (CLP) Luis Tari March 10, 2005."

Similar presentations


Ads by Google