1 / 32

Propia and CHRs

Propia and CHRs. Transforming Constraint Specification into Constraint Behaviour. Motivation for Propia and CHRs. Encode constraints in readable form Set up model without leaving choice points Support constraint propagation. NB: Never introduce choice points in constraint setup Why not?

dnorton
Download Presentation

Propia and CHRs

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. Propia and CHRs Transforming Constraint Specification into Constraint Behaviour

  2. Motivation for Propia and CHRs • Encode constraints in readable form • Set up model without leaving choice points • Support constraint propagation • NB: Never introduce choice points in constraint setup • Why not? • repeatedly setting up constraints on different branches • partially blind searching

  3. The noclash Example Specification: noclash(S,T)  (T  S+5)  (S  T) Encoding as ECLiPSe clauses: noclash(S,T) :- ic:(T >= S+5). noclash(S,T) :- ic:(S >= T). Model Setup: setup(S) :- S::1..10, constraint(noclash(S,6)).

  4. constraint(Goal) :- call(Goal). ?- setup(S). yes S = 1 This program leaves a choice point The noclash constraint in ECLiPSe Prolog noclash(S,T) :- ic:(T >= S+5). noclash(S,T) :- ic:(S >= T). setup(S) :- S::1..10, constraint(noclash(S,6)).

  5. constraint(Goal) :- Goal infers most. ?- setup(S). yes S{[1, 6..10]} This program does not create any choice points noclash with Propia noclash(S,T) :- ic:(T >= S+5). noclash(S,T) :- ic:(S >= T). setup(S) :- S::1..10, constraint(noclash(S,6)).

  6. constraint(noclash(S,T)) <=> ic:(T<S+5) | ic:(S >= T). constraint(noclash(S,T)) <=> ic:(S<T) | ic:(T >= S+5). ?- setup(S). yes S{1..10} This program does not create any choice points noclash with CHRs noclash(S,T) :- ic:(T >= S+5). noclash(S,T) :- ic:(S >= T). setup(S) :- S::1..10, constraint(noclash(S,6)).

  7. Choice Points and Efficiency Experiment sum(Products,Raw1,Raw2,Profit) :- ( foreach(Item,Products), foreach(R1,R1List), foreach(R2,R2List), foreach(P,PList) do product(Item,R1,R2,P) ), Raw1 #= sum(R1List), Raw2 #= sum(R2List), Profit #= sum(PList). £1 1pin 19nuts £2 2pins 17nuts £P R1pins R2nuts Products: Raw1 = 1 + 2 + R1 Raw2 = 19 + 17 + R2 Profit= 1 + 2 + P product( 101,1,19,1). product( 102,2,17,2). product( 103,3,15,3).product( 104,4,13,4). product( 105,10,8,5). product( 106,16,4,4). product( 107,17,3,3). product( 108,18,2,2). product( 109,19,1,1).

  8. 95>= R1+R1+R1+R1+R1+R1+R1+R1+R1 95>= R2+R2+R2+R2+R2+R2+R2+R2+R2 40=< P +P +P +P +P +P +P +P +P Time for all 13801 solutions: Time to first solution: 1 hour 33 secs Choice Points and Efficiency product_plan(Products) :- length(Products,9), Raw1 #=< 95, Raw2 #=< 95, Profit #>= 40, sum(Products,Raw1,Raw2,Profit), labeling(Products). sum(Products,Raw1,Raw2,Profit) :- ( foreach(Item,Products), foreach(R1,R1List), foreach(R2,R2List), foreach(P,PList) do product(Item,R1,R2,P) ), Raw1 #= sum(R1List), Raw2 #= sum(R2List), Profit #= sum(PList). infers most

  9. Propia • Annotations • Most Specific Generalisation • Algorithm

  10. Propia’s infers Annotation Propia annotation says what you want to infer: • Goal infersconsistent • Fail as soon as inconsistency can be proven • Goal infersunique • Instantiate as soon as unique solution exists • Goal infersac • Maintain finite domains covering all solutions • Goal infersmost • Use strongest available representation covering all solutions

  11. Annotation Results p(1,1). p(1,3). p(1,4). ?- p(X,Y) infers consistent. No information extracted ?- p(X,3) infers unique. X=1 ?- p(X,Y) infers most. X=1 (assuming ic is not loaded) ?- p(X,Y) infers most. X=1,Y{[1,3,4]} (assuming ic is loaded) ?- p(X,Y) infers ac. X=1,Y{[1,3,4]} (ic must be loaded)

  12. C5 C1 C7 C4 (C1,C4,C5) infers most C3 C6 Constraints C2 C8 Problem More “global” Consistency pp(X,Y,Z) :- p(X,Y), p(Y,Z). ?- pp(X,Y,Z) infers most. X=1, Y=1, Z{[1,3,4]}

  13. Most Specific Generalisation The Opposite of Unification! [1,A] [1,1] [1,1] [2,2] [X,X] [1,_] “most specific generalisations”

  14. Most Specific Generalisation over Finite Domains f(1,2) f(2,4) 1 2 X::{[1,2]} f(X::{1,2},Y::{2,4}) “most specific generalisations”

  15. Naïve Propia Algorithm Goal infers most Find all solutions to Goal, and put them in a set Find the most specific generalisation of all the terms in the set member(X,[1,2,3]) infers most Find all solutions tomember(X,[1,2,3]): {1,2,3} Find the most specific generalisation of {1,2,3}: X::{1,2,3}

  16. Propia Algorithm Goal infers most Find one solutionSto Goal The current most specific generalisationMSG = S Repeat Find a solutionNewSto Goal which is NOT an instance ofMSG Find the most specific generalisationNewMSG ofMSGandNewS MSG := NewMSG until no such solution remains

  17. Example - without finite domains p(1,2). p(1,3). p(2,3). p(2,4). p(X,Y) infers most MSG: 1st Iteration: p(1,2) 2nd Iteration: p(1,_) 3rd Iteration: p(_,_) No more iterations

  18. Propia Algorithm for Arc Consistency p(1,2). p(1,3). p(2,3). p(2,4). This algorithm only works for predicates defined by ground facts p(X,Y) infers ac is implemented as: element(I,[1,1,2,2],X), element(I,[2,3,3,4],Y).

  19. CHR - Constraints Handling Rules • CHR -- set of rewrite rules for constraints • Can be mixed with normal Prolog code • H1...,Hn <=><Guards>|<Goals>. remove “ask” add, “tell” Constraint Store

  20. Constraint Store Goal ?- le(B,C) le(A,B) Clauses, Constraints, Goals Clause le(X,Y1), le(Y2,Z) ==> Y1=Y2 | le(X,Z) Head Guard Body le(B,C) le(A,C)

  21. Constraint Store Goal le(B,A) ?- le(B,A) le(A,B) le(A,B) Simplification Rule Clause le(X,Y), le(Y,X) <=> X=Y Head Body A=B

  22. Constraint Store Goal le(1,2) ?- le(1,2) Definition of “less than or equal” (le) :- constraints le/2. le(X,Y), le(Y,Z) ==> le(X,Z). le(X,Y), le(Y,X) <=> X=Y. le(X,Y) ==> X=<Y. le(X,Y) <=> X=<Y | true. true

  23. Constraint Store Goal ?- min(A,B,Min) le(A,B) Entailment Testing Clause le(X,Y) \ min(X,Y,Z) ==> Z=X Head Body min(A,B,Min) Min=A

  24. Entailment Testing – without CHR’s ge1(X,Y,Z) :- ‘=<‘(X,Y,B), ge2(B,X,Y,Z). delay ge2(B,_,_,_) if var(B). ge2(0,_,Y,Z) :- Z=Y. ge2(1,X,_,Z) :- Z=X. min1(X,Y,Z) :- ic: (Z=<X), ic: (Z=<Y), ge1(X,Y,Z). ?- ic: (X=<Y), ic: (X,Y,B). Yes B::0..1

  25. CHR-Defined Precedence prec(Time1,Duration,Time2) means that Time1 precedes Time2 by at least Duration. • Note that: • Time1andTime2may be variables or numbers • Durationis a number • Durationmay be negative • (prec(S1,-5,S2)means thatS2precedesS1by no more than 5)

  26. Syntax and semantics for CHRs • Simplification rule: • H1...,Hn <=><Guards>|<Goals>. • Propagation rule: • H1...,Hn ==><Guards>|<Goals>. • Simpagation rule: • H1..\..Hn <=><Guards>|<Goals>. • Heads are match only - no binding of external vars. • Goals are any Prolog goals or constraints. • Constraint added to store if no rule applicable.

  27. CHRs for prec/3 %A timepoint precedes itself by a maximum duration of 0prec(S,D,S) <=> D=<0. (implicit guard in the head) %Two timepoints preceding each other by (at least) 0 are the sameprec(S1,0,S2), prec(S2,0,S1) <=> S1=S2. (rule with two atoms in the head) %Transitivity prec(S1,D1,S2),prec(S2,D2,S3) ==> D3 is D1+D2, prec(S1,D3,S3). (propagation rule) % prec(S1,D1,S2) is redundantprec(S1,D1,S2) \ prec(S1,D2,S2) <=> D2>=D1 | true. (simpagation rule)

  28. Complete Precedence Entailment % Assume D is a number noclash(S,T)\ prec(T,D,S)<=> D >= -5 | prec(T,0,S). noclash(S,T)\ prec(S,D,T)<=> D >= 0 | prec(S,5,T). prec(S,D,T) ==> ic:(T>=S+D).

  29. Global Consistency • Reasoning on Combinations of Constraints • Propia and CHRs can apply consistency techniques to combinations of constraints • Propia for a priori combinations • Propia can be applied to program-defined predicates • These predicates combine sets of constraints selected in advance of program execution • CHRs for combining newly posted constraints • CHR multi-headed rules can match combinations of constraints • CHR multi-headed rules can match constraints newly posted during search

  30. CHR Exercise Using CHR, axiomatise constraints “less than” and “minimum” Ensure logical completeness.

  31. Propia and CHR Exercise • Implement three constraints, 'and', 'or' and 'xor' • in Propia • in CHR (if you have time) • The constraints are specified as follows: • All boolean variables have domain [0,1]: • 0 for 'false' • 1 for 'true • and(X,Y,Z) =def (X&Y) = Z • or(X,Y,Z) =def (X or Y) = Z • xor(X,Y,Z) =def ((X & -Y) or (-X & Y)) = Z

  32. Testing Your Solution Suppose your constraints are called cons_and, cons_or and cons_xor Now write enter the following procedure: full_adder(I1,I2,I3,O1,O2) :- cons_xor(I1,I2,X1), cons_and(I1,I2,Y1), cons_xor(X1,I3,O1), cons_and(I3,X1,Y2), cons_or(Y1,Y2,O2). The test is : ?- full_adder(I1,I2,0,O1,1).

More Related