Document Sample

Introduction to Constraint Logic Programming Introduction to Constraint Programming 1 What is CLP? Many common real-life problems can be viewed as being concerned with minimising or maximising a function (e.g. cost or profit) Set of restrictions or constraints described in terms of a set of characteristics (e.g. processing times, labour availability, physical restrictions) Often these characteristics are decisions A model can be constructed based on the characteristics using integer variables and data Passed to a solving technology which understands the model and can process it to find a solution and perhaps the best solution Introduction to Constraint Programming 2 Problems Addressed The types of problem addressed by Constraint Logic Programming (CLP) Combinatorial Search Problems Discrete Optimisation In areas of Scheduling Planning Resource Allocation Assignment Distribution Typical OR problem areas for which the traditional tool is Integer Programming Introduction to Constraint Programming 3 Overview of Applications Hardware design Financial Problems Placement Cutting Problems Air Traffic Control Frequency Allocation Network Configuration Product Design Production Scheduling Satellite Tasking Maintenance Planning Timetabling Crew/Aircraft Rotation Personnel Assignment Introduction to Constraint Programming 4 Characteristics of these problems There are no general methods or direct algorithms available often NP -hard Different strategies and heuristics have to be tested. Problems have changing characteristics Introduction to Constraint Programming 5 Techniques addressing the same problems Linear and Integer Programming Heuristics Convergent Techniques simulated annealing tabu search genetic algorithms Enumeration Specific algorithms Introduction to Constraint Programming 6 CLP - Solving Technology Programming Language specific for modelling solving discrete optimisation problems Combining of ideas from Operational Research, Artificial Intelligence, Logic Programming, Graph Theory New technology - late 1980‟s Prolog based CHIP V5, Eclipse from Imperial College C++ Based Ilog Solver & OPL studio Introduction to Constraint Programming 7 Constraint Logic Programming Constraint Logic Programming Logic Programming Constraint Solver “CLP combines logic which is used to specify a set of possibilities explored via a simple inbuilt search method, with constraints, which are used to minimise the search by eliminating impossible alternatives in advance.” Introduction to Constraint Programming 8 Constraint Logic Programming Logic Programming declarative, relation based inbuilt search based on enumeration fast and flexible development Constraint Solver ensures consistency of constraints at all times through propagation and domain changes Introduction to Constraint Programming 9 How does CLP work? Definition when a variable is given a value = assignment ifevery model variable has a value = total assignment a total assignment satisfying all constraints = solution the set of all possible total assignments = search space CLP searches over all possible assignments some of which are solutions Introduction to Constraint Programming 10 CLP Search Organises search space by naturally partitioning it with a search tree Enumeration Depth-first traversal of the tree Introduction to Constraint Programming 11 Best solution only guaranteed by searching all the space Introduction to Constraint Programming 12 Characteristics of the CLP Search Complete - effectively searches all possibilities, but Enumeration takes too long for larger search spaces „Prunes‟ parts of the tree ahead which logically cannot contain solutions or better solutions removes possible assignments to variables Less space to search, faster to find the best solution Incomplete - if search space is still too large and only a good solution is required Introduction to Constraint Programming 13 All possibilities Initial Search Space After stating the known constraints, the search space can already be considerably reduced Remaining Search Space By using the dynamic features of CLP; adding constraints at any time and labelling the domain variables, the search space is further reduced. The location of the solution is not known, it has to be searched for. Search space during labelling, at a particular variable assignment The actual solution may be here Introduction to Constraint Programming 14 Example of Domain Reduction Variables X and Y have possible assignments of {2, 4, 5} and {2, 4, 6} respectively X[2, 4, 5] 2 4 5 Y[2, 4, 6] Y[2, 4, 6] Y[2, 4, 6] 2 4 6 2 4 6 2 4 6 (2,2) (2,4) (2,6) (4,2) (4,4) (4,6) (5,2) (5,4) (5,6) Introduction to Constraint Programming 15 Example of Domain Reduction The constraint X > Y added What happens to the search space? Automatically sets Y=[2, 4] X[4, 5] Constraint X>Y 4 5 X=4 automatically sets Y=2 Y[2] Y[2, 4] 2 2 4 (4,2) (5,2) (5,4) Introduction to Constraint Programming 16 Remarks Advantages of CLP flexible for modelling user controlled searching easy to incorporate „expert‟ knowledge good performance on certain types of problems publicdomain CLP solvers available (www.icparc.ic.ac.uk/eclipse) Introduction to Constraint Programming 17 Finite Domain (FD) Variable type used in modelling discrete combinatorial problems (also CSP‟s) Domain variables have values as a finite set of integers. If no possible value then no solution to problem Prolog predicates (relations) define constraints between variables which works in both directions Example code fragment: N :: 1..5, element(N, [5,3,9,1,4], V), V #>=4, N #>=3, Introduction to Constraint Programming 18 Example using Element Constraint Choose which 3 jobs to allocate to 3 machines in order to keep the total setup time below 9 start:- [M1, M2, M3]::1..5, %3 machines,5 jobs alldistinct([M1,M2,M3]), element(M1,[3,2,6,8,9],C1), element(M2,[4,6,2,3,2],C2), element(M3,[6,3,2,5,2],C3), C1 + C2 + C3 = Setup, Setup #<=9. Note M1, M2 and M3 are special finite domain variables. Prolog variables, which are less restricted can still be used normally in CLP e.g. as counters Introduction to Constraint Programming 19 Element Constraint Results: by inspecting the domains of M1, M2, M3 and Cost, Machine M1 can do job 1 or 2 Machine M2 can do all jobs except job2 Machine M3 can do all jobs except job1 The cost is between 6 and 9 These deductions will happen automatically within the CLP system, given the constraints imposed Introduction to Constraint Programming 20 SENDMORY example An alphametic is a peculiar type of mathematical puzzle, in which a set of words is written down in the form of an ordinary "long-hand" addition sum, and it is required that the letters of the alphabet be replaced with decimal digits so that the result is a valid arithmetic sum. S E N D + M O R E MO N E Y The mapping of letters to numbers is one-to-one No leading zeros Introduction to Constraint Programming 21 SENDMORY example ctd sendmore([S,E,N,D], [M,O,R,E], [M,O,N,E,Y]) :- [S,E,N,D,M,O,R,Y] :: 0..9, alldistinct([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]). Introduction to Constraint Programming 22 SENDMORY Result - one unique solution ?- sendmore([S,E,N,D],[M,O,R,E],[M,O,N,E,Y]). S=9 9567 E=5 N=6 +1085 D=7 10652 M=1 O=0 R=8 Y=2; no Introduction to Constraint Programming 23 Different Search Paradigms Prolog Generate and Test Prolog variables must have a value if used in a linear equation Constraint Logic Programming Prune and Generate Using Consistency Techniques such as • Forward Checking algorithms • Lookahead algorithms to actively prune the search space Introduction to Constraint Programming 24 Generate and Test The solution model consists of two parts Generating values for the variables -nondeterministic step (expect to backtrack and resatisfy in order to get other possible values) Testing the constraints -deterministic step (straight decision as to whether the configuration of values satisfies all the constraints) Introduction to Constraint Programming 25 Example: 8 Queens Problem: Place 8 queens on a chessboard in such a way that they don‟t attack each other Solution: There should be exactly one queen in each column, row and diagonal of the board. The queen in column i is represented by a variable Xi. This variable takes possible integer values from 1 to 8. Constraints between variables Xi \= Xj Xi \= Xj + (j-i) Xi \= Xj + (i -j) Introduction to Constraint Programming 26 Generate and Test Approach Within PROLOG a simple generator is generate([]). generate([First|Rest]) :- member(First, [1,2,3,4,5,6,7,8]), generate(Rest). Introduction to Constraint Programming 27 Generating Values It yields on backtracking all possible configurations [1,1,1,1,1,1,1,1] [1,1,1,1,1,1,1,2] ... [1,1,1,1,1,1,1,8] [1,1,1,1,1,1,2,1] [1,1,1,1,1,1,2,2] ... [8,8,8,8,8,8,8,8] Introduction to Constraint Programming 28 Testing Constraints test([]) test([F|Others]) :- noattack(F, Others, 1), test(Others). noattack(F, [], Distance). noattack(F, [Next|Others], Distance) :- checknoattack(F, Next, Distance), NewDistance is Distance + 1, noattack(F, Others, NewDistance). checknoattack(X, Y, D) :- X =\= Y, Y =\= X + D, X =\= Y + D. Introduction to Constraint Programming 29 Complete Program queens(L,B) :- length(L,B), generate(L), test(L). Problems with this program The complete solution space is explored Tests are performed too late No pruning is done at any stage Introduction to Constraint Programming 30 CLP Approach queens_clp(L) :- length(L,8), L::1..8, safe(L), labelling(L). safe([]). safe([F|T]) :- noattack(F,T,1), safe(T). labelling([]). labelling([X|Y]) :- indomain(X), labelling(Y). Introduction to Constraint Programming 31 CLP Approach noattack(X,[],Nb). noattack(X,[Y|Ys],Nb) :- X ## Y, Y ## X + Nb, X ## Y + Nb, Nb1 is Nb + 1, noattack(X,Ys,Nb1). The not-equal constraint is active and „prunes‟ off incompatible values in the domains of the other variable Activates when one side or the other of the constraint becomes ground Introduction to Constraint Programming 32 8 Queens board Introduction to Constraint Programming 33 Assigning values indomain(?Variable) Assigns a value from the current domain of the variable By default this will be the smallest value as domains are usually ordered. On backtracking, all values in the domain are tried. Similar to using member, but more efficient. Introduction to Constraint Programming 34 Search Strategies The time it takes to find a solution depends on Size of search space Ability to reduce that search space Way it is searched (search strategy) In CLP, the search strategy is entirely related to the choice of variable and the choice of value In CLP, there are some built-in search strategies for variable selection based on the predicate deletef/3, otherwise they can be modelled in the language Introduction to Constraint Programming 35 First Fail Strategy This is a general searching strategy which can be applied to CLP It is based on the principle of Trying first where you are most likely to detect failure This will remove parts of the search space early and more of it than if the choice was made later Making important decisions as soon as possible, rather than doing a lot of searching before making the important decisions In CLP terms, this corresponds to choosing the variable with the smallest size of domain (number of possible values) Introduction to Constraint Programming 36 First-Fail using deleteff/3 deleteff(?Var, +List, -Rest) This predicate is used to select a variable from a list of domain variables which has the smallest domain. Var is the selected variable from the list, Rest is the rest of the list without Var. Example label([]). label(ProblemVars) :- deleteff(X,ProblemVars,RestVars), indomain(X), label(RestVars). Introduction to Constraint Programming 37 Other Strategies Rotate values - use different values whenever possible This has been found useful for graph colouring problems Example label_colour([],_). label_colour([First|Rest],Colours) :- member(First, Colours), rotate(Colours,Colours1), label_colour(Rest,Colours1). Introduction to Constraint Programming 38 Constraint Store Location where unresolved constraints are put When a constraint is woken up it is taken from the store and examined The domains of all the variables are altered to make the constraint consistent. If any variable results in the empty domain then the program fails If the constraint can be satisfied no matter what the remaining possible values are, it is removed from the store If the constraint still has possible conflicts then it is put back in the store until woken again Introduction to Constraint Programming 39 Inference Rules (Constraint agents) A description of when a constraint is woken up and what propagation takes place to reduce the domains? Different constraint types, different rules There are several types of inference rules implemented within CLP You can also write your own Introduction to Constraint Programming 40 Propagation Example With a single constraint of X = Y + 1 in the constraint store If Y = 5 during the enumeration search, then propagation ensures that X = 6 Conversely, If X = 3 during the enumeration search, then propagation ensures that Y = 2 Introduction to Constraint Programming 41 Inference Rules Delay: As soon as all the arguments are instantiated, check the constraint. Forward Checking: As soon as only one argument remains unground, solve the constraint by removing all inconsistent values from the domain of it. Lookahead: After each change to the domain of an arguments, check whether there are inconsistent values in the domains of the others Introduction to Constraint Programming 42 Forward Checking A constraint is woken when only one domain variable is left uninstantiated The set of all values in the domain of this variable that satisfy the constraint is then computed If no such value exists, the constraint fails If only one exists, the value is assigned to the variable Otherwise, the domain variable is bound to a new variable whose domain is the new set Introduction to Constraint Programming 43 Forward Checking An example of a constraint which uses the Forward Checking Inference Rule is the ## as soon as X or Y is ground, the rule reduces the domain of the other variable Example X in {1,2,3,4,5} ## 3 for the constraint to remain consistent, the system must remove the value of 3 from the domain of X. Hence X in {1,2,4,5} Introduction to Constraint Programming 44 Lookahead Examples of constraints woken by the Lookahead rule are, #=, #<, #<=, #> and #>= also element/3 Suppose X #<Y If one of the arguments becomes ground, constraint consistency can be ensured by restricting the domain of the other argument e.g. X in {10..20} #< 15 restricts X to values {10..14} Introduction to Constraint Programming 45 Lookahead If both arguments are still domain variables, then remove all impossible values from the domains to ensure consistency e.g. X in {10..20} #< Y in {5..15} restricts X to {10..14} and Y to {11..15} but the constraint is not yet solved, since by assigning X = 14 and Y = 11 is wrong. Therefore, the constraint is woken again if the domains subsequently change Introduction to Constraint Programming 46 Time in Pruning Cost Vs Pruning Delay Forward Checking Lookahead Pruning Cost In small problems exhaustively searching can be quicker than using constraint propagation. Constraints are only useful when they are „effective‟ Introduction to Constraint Programming 47 CLP Methodology Determine a model for the problem in terms of domain variables Apply the constraints over these domain variables Search for a feasible solution and then possibly an optimal one Introduction to Constraint Programming 48 Modelling the Problem This is a crucial phase to produce a concise description through the variables and constraints determine the efficiency of resolution Decide whether the problem can initially be modelled in the computation domain of discrete, continuous or 0/1 variables Decide on the values which these variables can take Decide on the choice of constraints for efficiency of resolution Introduction to Constraint Programming 49 Generating Constraints Scheduling job i has domain variables Si and Di representing start time and duration Data structure: precede(Sj, Dj, Sk, Dk) Meaning: Sj with duration Dj must be scheduled after Sk+Dk Input: A list of such structures Constraint Generation Program: gen_prec([]). gen_prec([precede(S1,D1,S2,D2)|R]):- S1 #>= S2 + D2, gen_prec(R). Introduction to Constraint Programming 50 Searching for a feasible solution labelling(ListOfVars) :- deleteff(Var,ListOfVars,Rest), indomain(Var), labelling(Rest). Searches by attempting to enumerate all the variables in a particular order Can backtrack to find all feasible solutions Introduction to Constraint Programming 51 Optimisation Meta predicates for optimisation min_max(+Goal, +Cost) Minimises a Cost which is either a linear term or a list of linear terms using Goal as a way of searching or enumerating the variables. After each improved cost, the search restarts from the beginning minimize(+Goal, +Term) As min_max, but the search restarts from the current point in the search tree. Goal is commonly a call to a labelling predicate with the list of domain variables Introduction to Constraint Programming 52 Optimisation min_max(+Goal, +Cost, +Low, +High, +Percent, +Timeout) As min_max but, Low indicates the value at which searching can stop if a cost has been reached which is less than Low, High indicates a cost value less than which a solution is searched for, Percent indicates the percentage improvement sought for or bettered on successive improved solutions, Tiemout indicates the time in seconds after which the search is aborted and the best solution found so far returned. Introduction to Constraint Programming 53 Branch & Bound CLP uses Branch & Bound to reduce the search space. When a solution is found, satisfying all the constraints then a new constraint is added indicating that the optimal cost must be strictly less than the current solution cost. The search can either restart from the beginning with this additional constraint or continue from that point. If no other solution is found, then the best so far cost is the optimal. Introduction to Constraint Programming 54 Optimisation Schema From the basic schema without optimisation opt_pb(L,Cost) :- defining_domains(L,Cost), generating_constraints(L), choices(L). the equivalent optimisation is opt_pb(L,Cost) :- defining_domains(L,Cost), generating_constraints(L), min_max(choices(L),Cost). Introduction to Constraint Programming 55 Optimisation Schema With lower and upper bounds opt_pb(L,Cost) :- lower(Lower), goodsolution(Upper), defining_domains(L,Cost), generating_constraints(L), min_max(choices(L),Cost,Lower,Upper). Bounds can be derived externally by heuristic solutions or by theoretical means Introduction to Constraint Programming 56 Machine Example ctd start([M1,M2,M3]):- [M1, M2, M3] :: 1..5, alldistinct([M1,M2,M3]), element(M1,[3,2,6,8,9],C1), element(M2,[4,6,2,3,2],C2), element(M3,[6,3,2,5,2],C3), Cost #= C1 + C2 + C3, min_max(labelling([M1,M2,M 3]) , Cost). labelling([]). labelling([H|T]) :- indomain(H), labelling(T). Introduction to Constraint Programming 57

DOCUMENT INFO

Shared By:

Categories:

Tags:
Constraint Logic Programming, logic programming, constraint solvers, Constraint Handling Rules, Constraint Programming, operational semantics, programming language, constraint solving, Tom Schrijvers, Martin Sulzmann

Stats:

views: | 60 |

posted: | 12/25/2010 |

language: | English |

pages: | 57 |

OTHER DOCS BY wulinqing

How are you planning on using Docstoc?
BUSINESS
PERSONAL

By registering with docstoc.com you agree to our
privacy policy and
terms of service, and to receive content and offer notifications.

Docstoc is the premier online destination to start and grow small businesses. It hosts the best quality and widest selection of professional documents (over 20 million) and resources including expert videos, articles and productivity tools to make every small business better.

Search or Browse for any specific document or resource you need for your business. Or explore our curated resources for Starting a Business, Growing a Business or for Professional Development.

Feel free to Contact Us with any questions you might have.