Propia and CHRs

Preview:

DESCRIPTION

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? - PowerPoint PPT Presentation

Citation preview

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 setupWhy not?•repeatedly setting up constraints on different branches•partially blind searching

3

The noclash Example

noclash(S,T) (T S+5) (S T)Specification:

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

The noclash constraint in ECLiPSe Prolog

constraint(Goal) :- call(Goal).

This program leaves a choice point

?- setup(S).yes

S = 1

noclash(S,T) :- ic:(T >= S+5).noclash(S,T) :- ic:(S >= T).

setup(S) :- S::1..10, constraint(noclash(S,6)).

5

noclash with Propia

constraint(Goal) :- Goal infers most.

S{[1, 6..10]}

This program does not create any choice points

?- setup(S).yes

noclash(S,T) :- ic:(T >= S+5).noclash(S,T) :- ic:(S >= T).

setup(S) :- S::1..10, constraint(noclash(S,6)).

6

noclash with CHRs

constraint(noclash(S,T)) <=> ic:(T<S+5) | ic:(S >= T).constraint(noclash(S,T)) <=> ic:(S<T) | ic:(T >= S+5).

This program does not create any choice points

?- setup(S).yesS{1..10}

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).

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).

£11pin19nuts

£22pins17nuts

£PR1pinsR2nuts

Raw1 = 1 + 2 + R1Raw2 = 19 + 17 + R2Profit= 1 + 2 + P

Products:

8

Choice Points and Efficiencyproduct_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

33 secs

Time for all 13801 solutions:Time to first solution:

1 hour

95>= R1+R1+R1+R1+R1+R1+R1+R1+R195>= R2+R2+R2+R2+R2+R2+R2+R2+R240=< P +P +P +P +P +P +P +P +P

9

Propia

Annotations Most Specific Generalisation Algorithm

10

Propia’s infers Annotation

Propia annotation says what you want to infer: Goal infers consistent

Fail as soon as inconsistency can be proven

Goal infers uniqueInstantiate as soon as unique solution exists

Goal infers acMaintain finite domains covering all solutions

Goal infers mostUse strongest available representation covering all solutions

11

Annotation Resultsp(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

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]}

C1C5

C4

C2

C3 C6

C7

C8Problem

Constraints

(C1,C4,C5) infers most

13

Most Specific Generalisation

The Opposite of Unification!

[1,A] [1,1]

[1,_]

[1,1] [2,2]

[X,X]

“most specific generalisations”

14

Most Specific Generalisation over Finite Domains

1 2

X::{[1,2]}

f(1,2) f(2,4)

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 setFind the most specific generalisation of all the terms in the set

member(X,[1,2,3]) infers most

Find all solutions to member(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 solution S to Goal The current most specific generalisation MSG = S

Repeat Find a solution NewS to Goal

which is NOT an instance of MSG Find the most specific generalisation NewMSG

of MSG and NewS 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>.

CHR -- set of rewrite rules for constraints Can be mixed with normal Prolog code

H1...,Hn <=> <Guards> | <Goals>.

Constraint StoreConstraint Store

add, “tell”“ask”remove

21

Clauses, Constraints, Goals

le(A,B)

Constraint Store

?- le(B,C)

Goal

le(A,C)

le(X,Y1), le(Y2,Z) ==> Y1=Y2 | le(X,Z)

Clause

Head BodyGuard

le(B,C)

22

Constraint Store

Simplification Rule

le(A,B)

?- le(B,A)

Goal

le(B,A)

le(A,B)

le(X,Y), le(Y,X) <=> X=Y

Clause

Head Body

A=B

24

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. Constraint Store

?- le(1,2)

Goal

le(1,2)true

25

Entailment Testing

Constraint Store

le(A,B)

?- min(A,B,Min)

Goal

min(A,B,Min)

le(X,Y) \ min(X,Y,Z) ==> Z=X

Clause

Head Body

Min=A

26

Entailment Testing – without CHR’s

min1(X,Y,Z) :- ic: (Z=<X), ic: (Z=<Y),ge1(X,Y,Z).

?- ic: (X=<Y), ic: (X,Y,B).YesB::0..1

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.

27

CHR-Defined Precedence

prec(Time1,Duration,Time2)

means that Time1 precedes Time2 by at least Duration.

Note that:•Time1 and Time2 may be variables or numbers• Duration is a number•Duration may be negative (prec(S1,-5,S2) means that S2 precedes S1 by no more than 5)

28

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.

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.

29

CHRs for prec/3

%A timepoint precedes itself by a maximum duration of 0

prec(S,D,S) <=> D=<0. (implicit guard in the head)

%Two timepoints preceding each other by (at least) 0 are the same

prec(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 redundant

prec(S1,D1,S2) \ prec(S1,D2,S2) <=> D2>=D1 | true.(simpagation rule)

30

Complete Precedence Entailment

% Assume D is a numbernoclash(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).

31

Global Consistency

Reasoning on Combinations of ConstraintsPropia and CHRs can apply consistency techniques to combinations of constraints

Propia for a priori combinationsPropia can be applied to program-defined predicates

These predicates combine sets of constraints selected in advance of program execution

CHRs for combining newly posted constraintsCHR multi-headed rules can match combinations of constraints

CHR multi-headed rules can match constraints newly posted during search

32

CHR Exercise

Using CHR, axiomatise constraints “less than” and “minimum”

Ensure logical completeness.

33

Propia and CHR ExerciseImplement 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

34

Testing Your SolutionSuppose 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).

Recommended