Search - kth.se · ID2204, L06, Christian Schulte, ICT, KTH . 4 . Improving Propagation Idea:...
Transcript of Search - kth.se · ID2204, L06, Christian Schulte, ICT, KTH . 4 . Improving Propagation Idea:...
Search Lecture 06, 2015-04-16
ID2204: Constraint Programming
Christian Schulte [email protected] Software and Computer Systems School of Information and Communication Technology KTH – Royal Institute of Technology Stockholm, Sweden
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 2
Overview
Reminder improved constraint propagation events realistic constraint propagation improving constraint propagation further
Search branching exploration
Outlook
Reminder
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 4
Improving Propagation
Idea: propagator narrows domain of some (few) variables
re-propagate only propagators sharing variables
Maintain a set of “dirty” propagators not known whether fixpoint all other propagators have fixpoint computed
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 5
Propagator Variables
Variables var(p) of propagator p variables of interest
No input considered on other variables No output computed on other variables
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 6
Improved Propagation
propagate((V,U,P), so)
s := s0; N := P; while N ≠ do
choose p N; s’ := p(s); N := N – {p}; MV := { x V | s(x) ≠ s’(x) }; DP := { q P | exists x var(q): x MV }; N := N DP; s := s’; return s;
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 7
Loop Invariant
Loop maintains for all p P-N p(s) = s after termination (N = ): for all p P p(s) = s Obligations
holds initially is actually invariant
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 8
What Is Computed
Fixpoint follows from loop invariant
Largest simultaneous fixpoint as for naïve propagation
proofs works exactly as before sequence of stores not strictly decreasing sufficient: store sequence and decreasing and finite (to
prove next)
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 9
Termination
Insight: if MV=, then p removed from N if MV≠, then p(s)<s
Consider pairs (si,Ni) with
si the value of s at i-th iteration Ni the value of N at i-th iteration
strictly decreasing wrt well-founded lexicographic order of (S,<) and (2P,)
Improving Propagation
Further
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 11
Subsumed Propagators
Propagator p subsumed by store s, iff for all s’ ≤ s : p(s’)=s’
all stronger stores are fixpoints p entailed by s s subsumes p (s entails p)
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 12
Weak Idempotence
A function f X X is idempotent on x X if
f(f(x)) = f(x) statement on just one element
For a propagator: if p is idempotent on s, it does not mean that p is idempotent on s’ with s’ ≤ s
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 13
Status Messages
Solution: propagator returns status and tells result
propagator p is function p S SM × S with SM := {fix, nofix, subsumed}
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 14
Propagator with Status
Assume propagator p and store s if p(s) = (fix, s’), then s’ is fixpoint for p if p(s) = (subsumed, s’), then s’ subsumes p if p(s) = (nofix, s’), then no further knowledge always safe (as before)
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 15
Improved Propagation
propagate((V,U,P), so) s := s0; N := P; while N ≠ do choose p N; (ms,s’):= p(s); N := N – {p}; if ms=subsumed then P := P –{p}; end MV := { x V | s(x) ≠ s’(x) }; DP := { q P | exists x var(q): x MV }; if ms=fix then DP := DP –{p}; end N := N DP; s := s’; return (P,s);
Propagation Events
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 17
Propagation Events
Many propagators simple to decide whether still at fixpoint for changed
domain based on how domain has changed
How domain changes described by propagation event
or just event
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 18
Events
Typical events fix(x) x becomes assigned min(x) minimum of x changes max(x) maximum of x changes any(x) domain of x changes
Clearly overlap fix(x) occurs: min(x) or max(x) occur any(x) occurs min(x) or max(x) occur: any(x) occurs
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 19
Events on Store Change
events(s,s') = { any(x) | s'(x) s(x) } { min(x) | min s'(x) > min s(x) } { max(x) | max s'(x) < max s(x) } { fix(x) | |s'(x)|=1 and |s(x)|>1 }
where s' s
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 20
Events are Monotonic
If s'' s' and s' s then events(s,s'') = events(s,s') events(s',s'')
Event occurs on change from s to s''
occurs on change from s to s', or occurs on change from s' to s''
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 21
Propagator for ≤
Propagator p≤ for x ≤ y p≤ (s) = { x { ns(x) | n ≤ max(s(y)) }, y { ns(y) | n ≥ min(s(x)) }}
good one: es(p≤) = { max(y), min(x) } but also: es(p≤) = { any(y), any(x)}
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 22
Propagator for ≠
Propagator p≠ for x ≠ y p≠(s) = { x s(x) - single(s(y)), y s(y) - single(s(x))}
where: single({n}) = {n} single(N) = (otherwise)
good one: es(p≠) = { fix(y), fix(x) } but also: es(p≠) = { any(y), any(x)}
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 23
Taking Advantage from Event Sets
Base decision of propagators to re-propagate on event sets rather than on modified variables
DP := { q P | events(s,s’) es(q) ≠};
More Optimizations
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 25
Priorities
Choose propagator according to cost: cheapest first according to expected impact general: first-in first-out (queue)
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 26
Propagator Rewriting
Another observation: propagator for max(x,y)=z and values for x are smaller than for y Replace by propagator for y=z
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 27
Summary: Optimizing Propagation
Fixpoint knowledge avoids useless execution idempotence, subsumption, events knowledge provided by propagator
More details on optimizing propagation and
propagation in systems Finite Domain Constraint Programming Systems, Christian
Schulte, Mats Carlsson. In: Francesca Rossi, Peter van Beek, Toby Walsh, editors,
Handbook of Constraint Programming, Foundations of Artificial Intelligence, pages 495-526. Elsevier Science Publishers, 2006.
Search
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 29
Search
Branching defines the search tree what are properties for a branching? what is a search tree?
Exploration how to explore the search tree? first solution search best solution search
Branching
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 31
Branching
How to define branching in our model branching suggests new constraints for decomposition operational formulation: new propagators
Branching makes decision based on current
store but also on propagators first-fail: variable with smallest domain most-constrained first
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 32
Properties
Search tree constructed by branching finite tree decreasing no solutions lost complete no solutions duplicated non-overlapping
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 33
Some Notation
Set of propagators
P := S S
functions from store to store contracting monotonic
Set of constraint models
M
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 34
Simplification
In the following we assume that the set of variables V and the universe U is fixed
Propagation is referred to by propagate(P,s) for propagator set P and store s and returns
pair of propagator set and store
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 35
Branching
A branching b is a function from propagators and a store to tuples of finite propagator sets
b(P, s) = (P1, …, Pn) where
Pi P and finite called i-th alternative
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 36
Branching: Properties
Assume (s not failed) A = sol((V, U, P),s) Ai = sol((V, U, PPi),s) complete: A1 … An = A non-overlapping: AiAj = for i≠j decreasing: if propagate(PPi,s)=(P’,s’) then s’ < s
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 37
Extending Models
A constraint model is a quadruple (V, U, P, b)
variables V universe (values) U propagators P branching b
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 38
Search Tree
A search tree for a constraint model (V, U, P, b) consists of nodes
(P’,s) with P’ set of propagators, s store Root of tree is propagate(P,sinit) where sinit(x) := U for all xV
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 39
Search Tree
For all leaves (P,s): either s failed or b(P,s) = () (“solved”)
For all inner nodes (P,s): s is not failed if b(P,s)= (P1,…, Pn) then node has children propagate(PP1,s), …, propagate(PPn,s)
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 40
Search Tree: Properties
Invariant: nodes of search tree (P,s): either s failed or s sim. fixpoint of P
If node (P1,s1) below (P2,s2) on same path: s1 < s2
Search tree is finite due to decreasing
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 41
Optimizing
For constraint propagation do not start by assuming all propagators dirty only those added by branching are dirty
remember: we start from simultaneous fixpoint (search tree invariant)
Propagate function takes set of dirty and non-dirty propagators as input
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 42
Assignments vs. Solved Nodes
Notice difference a solved node does not mean that for all variables x we have that s(x) is singleton!
Assign and Remove Propagators
Needed for branchings are propagators that assign variable x to a value remove value for a variable x
For smallest possible value
assign-min(s,x)(y) := if y=x then {min(s(x))} else s(y) remove-min(s,x)(y) := if y=x then s(x)-{min(s(x))} else s(y)
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 43
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 44
First-fail
b(P,s) := if exists x with card(s(x)) > 1 and
card(s(x)) smallest then
({assign-min(s,x)}, {remove-min(s,x)}) else
()
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 45
Summary: Branching
Branching defines search tree complete non-overlapping decreasing
Search tree consists of propagators and store after propagation leaves are failed or solved is finite
Exploration
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 47
Depth-first Exploration
dfe(P,s) = (P’,s’) := propagate(P,s); if s’ failed then s’ else
case b(P’,s’) of () then s’ [] (P1,P2) then
s’’ := dfe(P’P1,s’); if s’’ failed then dfe(P’P2,s’) else s’’ end
end
end
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 48
Search in Gecode
Objects created for models as spaces Spaces provide methods for search
status returns status clone returns copy commit commits to alternative
Straightforward see documentation
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 49
State Restoration
Search must restore state backtrack to previous state
Approaches trailing remember changes undo changes recomputation recompute state copying store additional copy
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 50
Trailing
Dominating approach Advantages
remembers exactly what has changed
Disadvantages complicated (all operations must be trail aware) requires additional techniques (time-stamping, …) difficult to combine with concurrency
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 51
Recomputation
Remember path for nodes path starts at some copy c recompute by redoing branching on c for all nodes compute single fixpoint known as batch recomputation
Advantage memory usage problem independent
Disadvantage efficiency overhead
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 52
Copying
Advantages simple allows for concurrency/parallelism
Disadvantages needs way too much memory
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 53
State Restoration in Gecode
Hybrid of copying and recomputation Read in “Modeling and Programming with
Gecode” Adaptive recomputation
remember copy in middle of path
Summary: Principles
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 55
Summary: Constraint Propagation and
Search Models for
constraint propagation branching sketch for exploration
Most important concepts CSPs constraint models propagators propagation loop
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 56
More on Models and Principles…
Excellent book with much more detail, inspired some of the material here
Krzysztof Apt, Principles of Constraint Programming. Cambridge University Press, 2003.
Available in FORUM library
Outlook
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 58
Common Constraints
Common constraints reified, arithmetic, element, distinct, regular, … …and other global constraints
How are they used for modeling How are they propagated What makes them global Propagation strength
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 59
Linear Equality
Propagator for
where ai, d integers, ai ≠ 0
How to propagate cheaply bounds information? for each variable xi consider how small and how large it
possibly can be restrict us here to ax + by = d
dxa in
i i 1
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 60
Element Constraint
Element constraint a[[x]] = y array of integers a variables x and y value of y is value of a at x-th position in particular: 0 ≤ x < elements in a
In Gecode element(this, a, x, y);
also for arrays of variables
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 61
Distinct: Basic Question
Propagating in domain-consistent fashion need to consider all solutions naïve: too much memory, too much time
Is there a better way
in general no but for particular constraints: yes! but element is simple, for more difficult?
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 62
Golomb Rulers
Find n ticks ti on ruler such that distance between ticks pairwise distinct length of ruler minimal
Extremely hard problem applications in cristalography, …
t0 t1 t2 t3 t4 t5
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 63
Cardinality Constraint
Distinct: variables can take a particular value at most once
Generalization: lower and upper bounds on how often a value can be taken by variables
Use: shift assignment worker: variables describe shift to work boss: sets minimum and maximum requirements on
workers per shift
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 64
Channeling/Assignment Constraints
Suppose variables xi and yi (0 i < n) channel(xi, yi) holds, iff xi = j yj = i (0 i < n)
Use: dual models (permutation problems)
queen i is in row j row j contains queen i post constraints on both set of variables
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 65
Lex Constraints
Suppose variables xi and yi (0 i < n) lex(xi, yi) holds, iff (x0, …, xn-1) lexicographically smaller than (y0, …, yn-1)
Use: symmetry breaking
in particular: matrix models (magic square)
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 66
Regular Constraint
Suppose variables xi (0 i < n) regular(xi , r) holds, iff the string x0…xn-1 forms a word from the language of the regular expression r
Use: describe patterns formed by variable
sequences
2015-04-16 ID2204, L06, Christian Schulte, ICT, KTH 67
Goal
Understand and apply modeling abstractions modeling techniques
Understand propagation techniques