why should you care writing a csp solver in
play

Why Should You Care? Writing a CSP Solver in Understanding how the - PDF document

Why Should You Care? Writing a CSP Solver in Understanding how the solver works 3 (or 4) Easy Lessons makes better models. Christopher Jefferson Minion already imposes this on you, University of Oxford through its low-level input


  1. Why Should You Care? Writing a CSP Solver in • Understanding how the solver works 3 (or 4) Easy Lessons makes better models. Christopher Jefferson • Minion already imposes this on you, University of Oxford through its low-level input language. • Sorry! Why Should You Care? Talk Aims • These talks will teach you about how • Often have to go “under the hood” and add constraint solvers work. new search methods and constraints. • In particular, how Minion works. • Don’t repeat old mistakes if you write your • There is not one true way, so my biases will own constraint solver! show through! Talk Aims Not Talk Aims • Starting at the Minion Input Language • Will discuss GeCode on occasion, as they • Not considering: have nice descriptions of their algorithms. • Tailor • Other solvers are interesting, but I don’t • ESSENCE’ know what they do. • Flattening, Reformulating, ... • Of course mistakes are mine!

  2. Not Talk Aims • Not going to teach you how to add new: An Aside • Variable / Value orders • Constraints Everyone’s favourite CSP! • Variable Types • But after this talk, its mostly C++ • Happy to talk about this in the lab! 2x2 Sudoku 2x2 Sudoku 4 4 3 4 3 4 � � � � 1 2 1 2 1 2 1 2 � � � � � 3 4 3 4 3 4 3 4 2x2 Sudoku 2x2 Sudoku 4 4 3 4 3 4 � � 1 2 1 2 1 2 1 2 1 2 3 4 3 � � � � � � � � � 3 4 3 4 3 4 3 4 3 4

  3. What Now? Branching 4 4 3 4 4 3 3 4 1 1 2 1 2 4 3 � � � � 3 4 3 4 Branching Branching 4 4 3 4 3 4 1 4 3 4 3 4 4 3 4 2 3 4 1 4 3 4 3 Branching Branching 4 4 3 4 1 2 3 4 1 2 4 3 4 3 1 4 2 4 1 4 2 4 3 4 3 4 3 4 3 4 4 3 4 3 4 3 4 3

  4. That’s It! Stupid Solving Variables Domain Time 6 6 ~0.5 sec • Reduce domains by reasoning with 12 6 ~4 hours constraints. • Branch when stuck. 12 12 ~1,000 years Assume 100,000 nodes per second Why is this hard? What is a CSP? <V, D, C> • Represent domains. • Reduce domains with constraints. • Systematically get all possible reasoning • Branch and backtrack • Do it fast! What is a CSP Really? The ‘Good Old Days’ • What is a • Men were men. X Y • Domain? • Women were women. 1 1 • Constraint? • Constraints were a list of tuples. 2 3 3 4 3 6

  5. Tuples are Well Studied! Tuples are Well Studied! AC 1 2 3 4 5 6 7 3.1 2001 3.2 3.3 AC 1 2 3 4 5 6 7 3.1 2001 3.2 3.3 GAC 4 Schema 2001 GAC 4 Schema 2001 Path Consistency Path Consistency Forward Checking Forward Checking Directed Arc Consistency Directed Arc Consistency K-consistency K-consistency Strong K-consistency Strong K-consistency Propagators X 1 2 3 4 5 6 • How constraints are represented inside a constraint solver. Y 1 2 3 4 5 6 • A black box. • Offers one operation: • Take list of domains, reduce them. X < Y X X 1 2 3 4 5 6 1 2 3 4 5 6 Y Y 1 2 3 4 5 6 1 2 3 4 5 6 X < Y X < Y

  6. X 1 2 3 4 5 6 X 1 2 3 4 5 6 Y 1 2 3 4 5 6 Y 1 2 3 4 5 6 X < Y X < Y Variable Store X 1 2 3 4 5 6 • Store a sub-domain for every variable. Y 1 2 3 4 5 6 • Constraints query and reduce. • The standard way most CP (and SAT) solvers store state during search. X < Y Variable Store Variable Store • Define a partial order on search states: Given a CSP with variables � V 1 ,...,V n � , a search state is a list of sub-domains: S = � D 1 ,...,D n � , S’ = � D 1 ’,...,D n ’ � S = � D 1 ,...,D n � Where D i is a sub-domain of V i S � S’ ! D 1 � D 1 ’ , ... , D n � D n ’

  7. Example Variable Stores Variable Store • Set of states form a lattice . X 1 2 3 Y 1 2 3 • Lots of nice mathematical results. X X 1 2 3 1 2 3 • Propagators are a function from states to Y 1 2 3 Y 1 2 3 states. • S, S’ - some states. X X 1 2 3 1 2 3 • P - a propagator as a function. Y 1 2 3 Y 1 2 3 X 1 2 3 Y 1 2 3 Sensible Requirements Sensible Requirements • Monotonic (does not add back domain • Do not remove solutions: values): assignment � S � assignment � P(S) S � P(S) • Hardest part to get right! • This is usually maintained by the solver! Optional Requirements Bounds Consistency • GAC • BC (Bounds Consistency) • Every domain value left is part of a • Only check bounds solution. • Sortof… • Or: Strongest valid propagator. • Every constraint seems to have a • Easy to show this is well-defined! different definition!

  8. Simplest Algorithm Algorithm Properties • Fixed point will be reached in a finite Apply all Propagators amount of time. • Assuming finite domains! If Any Domain was reduced, • Infinite domains are scary. repeat • Fixed point may vary depending on order constraints are executed in. Standard Requirements Propagators in Practice • Lack of confluence is a pain. • Confluent: S � S’ � P(S) � P(S’) • Reordering propagators can lead to • Lattice Theorem: Whatever order different sized searches. confluent propagators are applied in, • But it still gets the right solutions! same fixed point is reached. The ‘Missing Requirement’ Improving Propagation • Identifies Solutions: If only one value is left in the sub-domain of • Two main areas: each variable, reject if not a solution. • Without this: • Reduce how often propagators are run. • Need an extra pass at the end of search • Speed up propagators. to check every constraint. • “Do Nothing” is a valid propagator.

  9. Constraint Queue Constraint Queue A < B B < C C < D A < B B < C C < D A < C A < D C < E A < C A < D C < E Change A Constraint Queue Constraint Queue A < B B < C C < D A < B B < C C < D A < C A < D C < E A < C A < D C < E Propagate A < B Propagate A < D Change A and D Constraint Queue Ordering the Queue A < B B < C C < D • What order should we do things in? • In theory, it doesn’t matter. A < C A < D C < E • But in practice it does. • There is not yet a ‘One True Way’. Change A and D Should we re-add A < D to the queue?

  10. FIFO vs LIFO Multiple Queues • ‘First In First Out’ faster than • Run the faster things first! ‘Last In First Out’ • Gecode has 5 queues. • Can be faster by magnitudes! • Minion has 2 queues. • Further tuning offers much smaller gains. Who Runs the Who Runs the Queues? - GeCode Queues? - GeCode X X < Y X < Y Alldiff Alldiff X < Y Alldiff X Lower X Fast Fast Slow Slow Fast Slow Bound Fast Slow Fast Slow X < Y Alldiff Queue Queue Fast Queue Queue Slow Who Runs the Who Runs the Queues? - GeCode Queues? - Minion X X X < Y Alldiff X < Y Alldiff Lower X Lower X X Fast Slow Fast Slow Bound Bound Fast Slow Fast Slow Alldiff Queue Queue Slow Queue Queue

  11. Minion Queues Improving the Queue • Avoids copying queues. • Queues are precalculated, allocated and • Sometimes we don’t care if a variable has compressed before search. • Faster, but can’t be changed. changed. • Allow finer-grained events. • Constraints can put themselves on the ‘slow queue’. • AllDiff, gcc, reification Don’t Care! X X 1 2 3 4 5 6 1 2 3 4 5 6 Y 1 2 3 4 5 6 Y 1 2 3 4 5 6 X < Y X < Y Only Important Values Optimising Propagation X 1 2 3 4 5 6 • Let constraints state they only want to know about: Y 1 2 3 4 5 6 • Lower / Upper Bound. • Assignment. • Particular Domain Value. X < Y • Any Change.

  12. Queue Other Optimisations • What exactly goes on the queue? • Merge events. • Changed Variables? • Minion does not try. • Changed Constraints? • The ‘double call problem’. • Variable / Constraint pairs? ‘Double Call Problem’ • When a variable changes, all the constraints Practical Constraints on that variable are added to the queue. • Including the constraint which just changed Minion’s Implementation of X < Y the variable! • It is a pain to get rid of these extra events. • Minion ignores, GeCode doesn’t. Implementing x � y X 1 2 3 4 5 6 LeqConstraint(Var x, Var y) setupConstraint() Y 1 2 3 4 5 6 { addTrigger(0, x, LowerBound) addTrigger(1, y, UpperBound) } X < Y

  13. Implementing x � y Congratulations! LeqConstraint(Var x, Var y) propagateConstraint(int trigger) { • Our solver now supports the optimal < if(trigger == 0) constraint! y.setMin(x.setMin() + 1) • But, this is not the whole story... else x.setMax(y.getMax() - 1) } Sum Constraints 0/1 Sum • b 1 + b 2 + … + b n = c • Well researched area. • b i have domain {0,1}. • We will consider a special case here. • c is constant. • Only sum variables of domain {0,1}. • This contains most of the ideas of Minion’s • Only sum to a constant. full sum. Basic Propagator Basic Propagator • Split variables into 3 sets: • Split variables into 3 sets: • S 0 = {i | Domain(b i ) = {0} } • S 0 = {i | Domain(b i ) = {0} } • S 1 = {i | Domain(b i ) = {1} } • S 1 = {i | Domain(b i ) = {1} } • S 01 = {i | Domain(b i ) = {0,1} } • S 01 = {i | Domain(b i ) = {0,1} } • Eventual sum is between |S 1 | and |S 1 | + |S 01 | • c < S 1 Fail

Recommend


More recommend