16
IBM Research: Computing as a Service © 2005 IBM Corporation Computing as a Service 1 Combinatorial Problem Solving in C10 How to write programmable solvers declaratively Vijay Saraswat <firstname>@<lastname>.org IBM TJ Watson Sep 9, 2014

The Concurrent Constraint Programming Research Programmes -- Redux (part2)

Embed Size (px)

DESCRIPTION

The Concurrent Constraint Programming Research Programmes -- Redux, Invited Talk CP2014 in Lyon Vijay Saraswat

Citation preview

Page 1: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

IBM Research: Computing as a Service

© 2005 IBM Corporation

Com

putin

g as

a S

ervi

ce

1

Combinatorial Problem Solving in C10

How to write programmable solvers declaratively

Vijay Saraswat<firstname>@<lastname>.orgIBM TJ WatsonSep 9, 2014

Page 2: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

Logic Programming

In the early 80s, Colmerauer / Kowalski and colleagues developed definite clause logic programming based on a procedural interpretation of proofs (“right hand side” computation, backward chaining)– Operationally one gets non-

deterministic user-defined recursive procedures, accumulating constraints over Herbrand terms.

– Logically, given an atom p(X1, …, Xn), the system generates bindings X1=t1,…, Xn=tn sufficient to establish the truth of the atom, given the program clauses.

– Multiple (implicitly disjunctive) answers may be returned.

2

(Goal) G ::= H|G,G|G;G

(Program) P ::= H:-G|P,P

(Atom) H ::= p(t1,…,tn)

X1=t1, …, Xn=tn ?- p(X1,…,Xn)

conjunction disjunction

Query posed by userAnswer returned by system

Page 3: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

Prolog 3 and Constraint Logic Programming

At POPL 87, Jaffar and Lassez showed how this framework could be extended to (essentially) arbitrary constraints, implemented by an embedded (black box) constraint solver– Atomic formulas drawn from some

underlying constraint theory (over a vocabulary disjoint from the user-defined predicates E)

CLP(R) is an exemplar, permitting linear arithmetic constraints over R.

Prolog III can also be viewed as an instance, over a different, rich constraint system

3

G ::= c

c1, …, ck ?- p(X1,…,Xn)

Page 4: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

However, …

This framework is not flexible enough to permit the user to specify propagation rules, or search strategies in logic.

Often constraint solvers are incomplete, or highly combinatorial and user-defined propagation rules are critical (cf cc(FD))

Similarly often critical are search strategies, e.g. run all propagation rules, then expand (non-deterministically) the atom with the fewest choices left (cf CHIP)

4

X in {1,2,3}, X!=Y, Y=2

?- X in {1,3}

Page 5: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

Claim: (Timed) CCP provides that framework

Concurrent Constraint Programming is a logical framework based, dually, on the notion of Agents, not Goals, on “left hand side” computation (“forward chaining”)

Operationally one gets non-deterministic user-defined recursive procedures, accumulating constraints in a shared store.

Implicative agents (if c D) trigger on the presence of a constraint c in the store, and take further actions.

Disjunction is “angelic” – on the LHS, if B entails A, then (A;B) is identical to A, i.e the more general solution (A) is kept.

5

X in {1,2,3}, X!=Y, Y=2

?- X in {1,3}

(Agent) D ::= E|c|D,D

|D;D|if c D

(Program) P ::= E-:D|P,P

(Atom) E ::= p(t1,…,tn)

p(X1,…,Xn) ?- c1 ; … ; ck

Entailed constraints determined by system

Agent proposed by user

Page 6: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

Example: Map Coloring

6

class OzMap(N:Int) {

type Colors=1..N.

enum States={wa,nt,sa,nsw,v,t,q}.

X:Map[States,Colors].

agent map {

X(wa)!= X(nt), X(wa)!=X(sa),

X(nt)!= X(sa), X(nt)!=X(q),

X(sa)!= X(q), X(sa)!=X(nsw),

X(sa)!=X(v),

X(q) != X(nsw),X(nsw)!=X(v),

all (i in X.domain)

choose(X(i), 0, X(i).values)

}

agent choose[T](X:T, i:Int, v:Rail[T]){

if (i < V.size)

X=v(i);

choose(X, i+1, v)

}

}

O=new OzMap(3), O.map |-

X(wa)=1,X(nt)=2, X(sa)=3,

X(qa)=1, X(nsw)=2, X(v)=1,

X(t)=1

?

But no control between propagation and choice!

Note: type-generic choose

Page 7: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

Another example: Zebra

7

class Zebra {

enum Nationalities = {english, spanish, ukrainian, norwegian, japanese}.

enum Colors = {red, green, ivory, yellow, blue}.

enum Animals = {dog, fox, horse, zebra, snails}.

enum Drinks = {coffee, tea, milk, orangeJuice, water}.

enum Cigarettes = {oldGold, kools, chesterfields, luckyStrike, parliaments}.

type Houses = Int(0,4).

vars: Array(0..4,0..4)[Houses].

Nation = variables(0), Color = variables(1), Animal = variables(2),

Drink = variables(3), Smoke = variables(4).

agent rightof(h1:Houses, h2:Houses){ h1=h2+1}

agent nextto(h1:Houses, h2:Houses) { rightof(h1,h2) ; rightof(h2,h2)}

agent middle(h:Houses) {h=3}

agent left(h1:Houses) {h=1}

agent constraints {

alldifferent(Nation), alldifferent(Color), alldifferent(Animal),

alldifferent(Drink), alldifferent(Smoke),

Nation(english) = Color(red), Nation(spaniard) = Animal(dog),

Drink(coffee) = Color(green), Nation(ukrainian) = Drink(tea),

rightof(Color(green), Color(ivory)),

…}

Page 8: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

Zebra – but the “control” is programmable

8

// Alternate

agent alldifferent[T](A:Array[T]) {

all (i in A.domain, j in A.domain{j !=i})

all (k in A(i).domain)

if (A(i) = k) A(j) != k // removes k from A(j)’s domain

}

alldifferent/1 – a “global constraint” – is just a user-defined propagator.

If A(i)=k (for any value k and index i) then k is removed from the domain of all other variables A(j).

agent alldifferent[T](A:Array[T]) {

all (i in A.domain, j in A.domain{j !=i})

A(j) != A(i) // assuming != available as a constraint on two variables

}

Page 9: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

But how do we ensure propagation before choice?

Use time – Timed CCP.

TCC is obtained by extending CC “uniformly” across time. A CC computation is run at time t (starting with t=0) till quiescence. Then all agents A s.t. the current time instant has the agent next A are collected, and executed at the next time instant.– Thus TCC provides a logical way to

arrange executions in a total order.

In TCC, the store may be changed non-monotonically between time instants.

This is not possible in Punctuated CCP – here all constraint tells are done within an always, hence constraints persist across time.

9

(Agent) D ::= next c D

(Program) P ::= E-:D|P,P

(Atom) E ::= p(t1,…,tn)

p(X1,…,Xn) ?-

(c11,next(c1

2,next(…c1m1)…));

…;

(ck1,next(ck

2, next(…ckmk)…))

Entailed constraints, across time determined by the system

Agent proposed by user

The result is (c1m1; …; ck

mk).

Page 10: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

Zebra – but the “control” is programmable

10

agent solve {

(I,J) = argmin((i,j:vars.domain)=>values(vars(i,j)).size),

if (values(vars(I,J)).size > 1) next {

always choose(vars(I,J)),

next solve()

}

}

values is the only “primitive” indexical – returns a rail of values for its argument variable.

solve implements the strategy of alternating between time instants in which propagation happens, and time instants in which the decision is made of the variable to split. It terminates when no more variables are left to split.

(I,J) is the index s.t. values(var(I,J)).size is minimized. If there are k > 1 values, choose (i.e. branch disjunctively k-ways) in the next time instant.

This will automatically cause propagation (in that time instant).

Page 11: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

Zebra – but the “control” is programmable

11

public def agent main(String[Rail]) {

z = new Zebra,

always z.constraints,

always all (i,j in z.vars.domain)

if (vars(i.j).size <=1) choose(vars(I,J))

next z.solve

}

The main method. Creates a new Zebra problem, asserts its constraints in all time instants, and sets up a propagator that forces the value of any variable as soon as it has only one value left in its extent.

Also sets up the solve agent to alternate between propagation and choice

Page 12: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

(Aside: In fact RCC gives you CCP+CLP and more)

Much richer capabilities in RCC, while staying within the paradigm– Fully recursive goals (CLP) are available.– Agents with deep guards permit triggers to

be recursively defined– Goals with deep guards permit conditional

recursive augmentation of the store, for the purposes of answering the goal

– Universal goals permit parametric goal solving

12

(Agent) D ::= E|c|D,D

|D;D|if G D

|all X D

|some X D

(Goal) G ::= H|c|G,G

|G;G|if D G

|all X D

|some X D

(Program) P ::= E-:D|P,P

(Atom) E ::= p(t1,…,tn)

p(X1,…,Xn) ?- c1 ; … ; ck

Page 13: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

Crossgrams

A puzzle suggested to me by Gopal Raghavan.

Find words w in the English language which are such that for each position I in w there is a distinct anagram of w starting with the letter at the i’th position. – Example: emits – the corresponding anagrams are mites, items,

times, smite.

Here we consider the simpler version that drops the distinctness requirement (just to keep the program slightly smaller).

13

Page 14: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

Key to solution

Given a list of N dictionary words W, generate N facts word(W, L, S, Ws) where – L is the first character of W, – Ws is a (possibly non-English word) anagram of W in which

all letters are in increasing sort order, and – S is the first letter of Ws.– E.g. word(emits, e, e, eimst) / word(items, i, e, eimst) / word(mites, m, e, eimst), …

With these clauses, crossgrams can be generated quickly by backtracking (assuming facts are indexed appropriately, as they are in many Prolog systems).

14

Page 15: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

Crossgrams: illustrates use of if D G goals

15

class Crossgram {

@Gentzen def word(Atom, Char, Char, Atom).

goal crossgrams(Dict: List[Atom]) = R!{

if (all (W in Dict) {

W = name(Wls),

Wsls = Wls.msort(),

word(W, Wls.head, Wsls.head, name(Wsls))

})

words(R)

}

goal words(R:List[Atom]) {

word(A,C,C,Ws), A=name(Wls),

R=List(A, Wls.tail.map((L:Char)=>W!{word(W,L,_,Ws)}))

}}

Assert word/4 constraints on the fly, based on the dictionary, add them to the store.

Solve goal in context of generated constraints

This could fail, triggering backtracking.

Page 16: The Concurrent Constraint Programming Research Programmes -- Redux (part2)

CCP Research Programmes

© 2009 IBM Corporation

IBM

Res

earc

h

Conclusion

(Timed) CCP provides a dual logic programming framework which is much closer to more conventional “constraint programming” than CLP.

In particular, TCC permits the use of user-defined propagators and search procedures.

R[T]CC considerably enriches programming power, beyond TCC.

16