320 likes | 467 Views
The Use and Implementation of Global Constraints in B-Prolog. Neng-Fa Zhou CUNY Brooklyn College and Graduate Center. Outline. Global constraints element, all_distinct, circuit, cumulative Implementation language Action rules Implementation of propagators with AR
E N D
The Use and Implementation of Global Constraints in B-Prolog Neng-Fa Zhou CUNY Brooklyn College and Graduate Center Neng-Fa Zhou at CSPAST'10 in Fukuoka
Outline • Global constraints • element, all_distinct, circuit, cumulative • Implementation language • Action rules • Implementation of propagators with AR • element, all_distinct, circuit, cumulative • Parallelization of propagators • all_distinct Neng-Fa Zhou at CSPAST'10 in Fukuoka
The element Constraint • element(I,L,X) • L=[X1,X2,…,Xn]. The Ith element of L is X, counting from 1. L[I]=X. Neng-Fa Zhou at CSPAST'10 in Fukuoka
Example An Assignment Problem A factory has four workers w1,w2,w3,w4 and four products p1,p2,p3,p4. The problem is to assign workers to products so that each worker is assigned to one product, each product is assigned to one worker, and the profit maximized. The profit made by each worker working on each product is given in the matrix. Profit matrix is: Neng-Fa Zhou at CSPAST'10 in Fukuoka
A Solution That Uses element go:- Vars=[W1,W2,W3,W4], Vars :: 1..4, all_distinct(Vars), element(W1,[7,1,3,4],Val1), element(W2,[8,2,5,1],Val2), element(W3,[4,3,7,2],Val3), element(W4,[3,1,6,3],Val4), Profit #= Val1+Val2+Val3+Val4, maxof(labeling(Vars),Profit), writeln(Profit). Neng-Fa Zhou at CSPAST'10 in Fukuoka
The all_distinct(L) Constraint • all_distinct([V1,V2,…,Vn]) • The elements V1, V2, …, Vn are pairwise different. For all i,j1..n (i<j) Vi Vj. Neng-Fa Zhou at CSPAST'10 in Fukuoka
Example-1Graph Coloring • Model-1 (neq) • For each two neighbors i and j, CiCj • Model-2 (all_distinct) • For each complete subgraph {i1,i2,…,ik}, all_distinct([Ci1, Ci2,…, Cik]) • post_neqs(Neqs) in B-Prolog Neng-Fa Zhou at CSPAST'10 in Fukuoka
Example-1 (cont.)Benchmarking Results Source of benchmarks: Agostino Dovier, Andrea Formisano, and Enrico Pontelli, A comparison of CLP(FD) and ASP solutions to NP-complete problems, ICLP’05. Neng-Fa Zhou at CSPAST'10 in Fukuoka
Example-2The N-Queens Problem • Model-1 (neq) • For i,j1..n (i < j) Qi Qj Qi-Qj (j-i) Qj-Qi (j-i) • Model-2 (all_distinct) • all_distinct([Q1,…,Qn]),all_distinct([Q1-1,Q2-2,…,Qn-n]),all_distinct([Q1+1,Q2+2,…,Qn+n]) Neng-Fa Zhou at CSPAST'10 in Fukuoka
Example-2 (cont.)Encodings in B-Prolog • Model-1 • Model-2 queens(N,Qs):- length(Qs,N), Qs in 1..N, foreach(I in 1..N-1, J in I+1..N, (Qs[I] #\= Qs[J], abs(Qs[I]-Qs[J]) #\= J-I)). queens(N,Qs):- length(Qs,N), Qs in 1..N, Qs1 @= [Qs[I]-I : I in 1..N], Qs2 @= [Qs[I]+I : I in 1..N], all_distinct(Qs),all_distinct(Qs1), all_distinct(Qs2). Neng-Fa Zhou at CSPAST'10 in Fukuoka
The circuit(L) Constraint • Let L=[X1,…,Xn], where Xi1..n. An assignment (X1/a1,…,Xn/an) satisfies this constraint if {1 a1 ,…,n an} forms a Hamiltonian cycle. Neng-Fa Zhou at CSPAST'10 in Fukuoka
Example-1The TSP Problem tsp(Vars):- Vars=[V1,V2,…,V8], V1 :: [2,3,4], V2 :: [1,3,6,7], … V8 :: [5,6,7], circuit(Vars). Neng-Fa Zhou at CSPAST'10 in Fukuoka
Example-2The Knight Tour Problem ktour(Vars):- Vars=[V1,V2,…,V64], V1 :: [11,18], … V64 :: [47,54], circuit(Vars). Neng-Fa Zhou at CSPAST'10 in Fukuoka
The cumulative Constraint • cumulative(Starts,Durations,Resources,Limit) Starts = [S1,…,Sn], Durations = [D1,…,Dn], Resources = [R1,…,Rn], The resource limit cannot be exceeded at any time • When Resources=[1,…,1] and Limit=1serialized(Starts,Durations) Neng-Fa Zhou at CSPAST'10 in Fukuoka
Example Disjunctive Scheduling • Model-1: use disjunctive constraints • Model-2: use global constraints • post_disjunctive_tasks(Disjs) where Disjs=[disj_tasks(S1,D1,S2,D2),…] • B-Prolog converts disjunctive constraints into serialized VV5+97#=<VV17#\/VV17+52#=<VV5, VV5+97#=<VV26#\/VV26+59#=<VV5, VV5+97#=<VV32#\/VV32+41#=<VV5, VV5+97#=<VV49#\/VV49+63#=<VV5, ….. Neng-Fa Zhou at CSPAST'10 in Fukuoka
Disjunctive Scheduling • Benchmarking results Source of benchmarks:www.cril.univ-artois.fr/~lecoutre/research/benchmarks/benchmarks.html Neng-Fa Zhou at CSPAST'10 in Fukuoka
Outline • Global constraints • element, all_distinct, circuit, cumulative • Implementation language • Action rules • Implementation of propagators with AR • element, all_distinct, circuit, cumulative • Parallelization of propagators • all_distinct Neng-Fa Zhou at CSPAST'10 in Fukuoka
Action Rules • Events • Instantiation: ins(X) • Domain • bound(X), dom(X), dom(X,E), dom_any(X), and dom_any(X,E) • Time : time(X) • GUI • actionPerformed(X), mouseClicked(X,E)… • General • event(X,O) Agent, Condition, {EventSet} => Action Neng-Fa Zhou at CSPAST'10 in Fukuoka
Applications of Action Rules • Lazy evaluation • Constraint propagators freeze(X,G), var(X), {ins(X)} => true. freeze(X,G) => call(G). 'X in C-Y_ac'(X,Y,C),var(X),var(Y), {dom(Y,Ey)} => Ex is C-Ey, domain_set_false(X,Ex). 'X in C-Y_ac'(X,Y,C) => true. Neng-Fa Zhou at CSPAST'10 in Fukuoka
Outline • Global constraints • element, all_distinct, circuit, cumulative • Implementation language • Action rules • Implementation of propagators with AR • element, all_distinct, circuit, cumulative • Parallelization of propagators • all_distinct Neng-Fa Zhou at CSPAST'10 in Fukuoka
Implementation of element • element(I,L,X) • L=[X1,X2,…,Xn]. The Ith element of L is X, counting from 1. • Maintaining consistency I in 1..n,X in min(L)..max(L), X ≠ X1 → I ≠ 1, … X ≠ Xn → I ≠ n Neng-Fa Zhou at CSPAST'10 in Fukuoka
element(I,L,X) : L is ground • from X to I • From I to X element_X_to_I(X,I,Map),var(X), {dom_any(X,E)} => map_get_indexes(Map,E,Indexes), I notin Indexes. element_X_to_I(X,I,Map) => true. element_I_to_X(I,X,Vect,Map),var(I), {dom_any(I,E)} => arg(E,Vect,Le), decrement_counter(Map,X,Le). element_I_to_X(I,X,Vect,Counters) => true. Neng-Fa Zhou at CSPAST'10 in Fukuoka
Implementation of all_distinct • Hall-set finding For the constraint all_distinct([X1,X2,…,Xn] where Xi’s domain is Di. A set H is a Hall set if the number of subsets of H among D1, D2, …, and Dn is greater than or equal to |H|. Neng-Fa Zhou at CSPAST'10 in Fukuoka
Implementation of all_distinct in B-Prolog • Check if an updated domain is a Hall set • Weakness • X1 in [1,2], X2 in [1,3], X3 in [2,3], X4 in [1,2,3,4] • Fail to find the Hall set [1,2,3] outof(X,Left,Right), var(X), {generated,ins(X),bound(X),dom(X)} => check_hall_set(X,Left,Right). outof(X,Left,Right) => exclude_list(X,Left),exclude_list(X,Right). Neng-Fa Zhou at CSPAST'10 in Fukuoka
Channeling Constraints • Primal and dual constraints • More pruning power • X1 in [1,2], X2 in [1,3], X3 in [2,3, X4 in [1,2,3,4] • Y1 in [1,2,4], Y2 in [1,3,4], Y3 in [2,3,4], Y4 in [4] ↓ • X1 in [1,2], X2 in [1,3], X3 in [2,3, X4=4 • Y1 in [1,2], Y2 in [1,3], Y3 in [2,3], Y4=4 • all_distinct([X1,X2,…,Xn]) where Xi in 1..n • all_distinct([Y1,Y2,…,Yn]) where Yi in 1..n Neng-Fa Zhou at CSPAST'10 in Fukuoka
Encoding Channeling Constraints in B-Prolog • The primal-dual relationship is maintained using 2n propagators primal_dual(Xi,I,DualVarVector),var(Xi), {dom_any(Xi,J)} => DualVarVector[J]#\= I. primal_dual(Xi,I,DualVarVector) => true. Neng-Fa Zhou at CSPAST'10 in Fukuoka
Effectiveness of Channeling Constraints N-Queens problem Neng-Fa Zhou at CSPAST'10 in Fukuoka
Implementation of circuit • circuit([X1,X2,…,Xn]) • all_distinct([X1,X2,…,Xn]) • Test reachability whenever the domain of any Xi is updated • Ensure non-existence of sub-cycles whenever any Xi is instantiated • How to test reachability? Neng-Fa Zhou at CSPAST'10 in Fukuoka
Implementation of serialized(Starts,Durations) • Edge finding and not-first/not-last rules • d(Ω ∪ {T}) > lct(Ω ∪ {T})-est(Ω)task T must be started before all tasks in Ω • d(Ω ∪ {T}) > lct(Ω)-est(Ω ∪ {T}) task T must be started after all tasks in Ω • d(Ω ∪ {T}) > lct(Ω)-est(T) task T cannot start first • d(Ω ∪ {T}) > lct(T)-est(Ω) task T cannot start last Neng-Fa Zhou at CSPAST'10 in Fukuoka
Outline • Global constraints • element, all_distinct, circuit, cumulative • Implementation language • Action rules • Implementation of propagators with AR • element, all_distinct, circuit, cumulative • Parallelization of propagators • all_distinct Neng-Fa Zhou at CSPAST'10 in Fukuoka
Parallelizing Propagators for all_distinct • Parallelism • Hall-set finding can be applied to different sets in parallel • For one set, counting of subsets can be done in parallel • Synchronization models • Add a mutex variable into each domain variable • Postpone updates until the next choice point and only let the main thread perform updates • Implementation • Task queues and update queues • Use pthreads Neng-Fa Zhou at CSPAST'10 in Fukuoka
Future Work • More experiments and benchmarking • Integrating with SAT technology • Complete the project on parallelizing all_distinct • Parallelization of other global constraints • Incorporating and/or parallelism Neng-Fa Zhou at CSPAST'10 in Fukuoka