Constraint Programming -- The B-Prolog Project

BPSolver’s Winning Solutions to
ASP Competition Problems
Neng-Fa Zhou
The City University of New York
[email protected]
Neng-Fa Zhou at TUWIEN
1
Outline
 Overview of the ASP competition
 B-Prolog’s features
 BPSolver’s winning solutions
 BPSolver’s hopeful solutions
 BPSolver’s losing solutions
 Observations
Neng-Fa Zhou at TUWIEN
2
Overview of ASP Competition
(Model & Solve)
 Principles
– To foster open comparison of ASP with any
other declarative paradigm
– To foster development of new language
constructs
– To foster development of new heuristics and/or
algorithms
Neng-Fa Zhou at TUWIEN
3
Overview of ASP Competition
(Model & Solve)
 Participants
– Aclasp (Gringo + Clasp + Gecode)
– BPSolver (B-Prolog)
– EZCSP (Gringo + Clasp + B-Prolog)
– Fastdownward (PDDL)
– IDP (grounder Gidl + MinisatID)
– Potassco (Gringo + Clasp + Gecode)
Neng-Fa Zhou at TUWIEN
4
Overview of ASP Competition
(Model & Solve)
 Benchmarks (34)
– P-problems (7)
– NP-problems (19)
– Beyond NP problems (2)
– Optimization problems (6)
Neng-Fa Zhou at TUWIEN
5
Another View of the Results
(Clasp Vs. B-Prolog)
Neng-Fa Zhou at TUWIEN
6
Outline
 Overview of the ASP competition
 B-Prolog’s features
 BPSolver’s winning solutions
 BPSolver’s hopeful solutions
 BPSolver’s losing solutions
 Observations
Neng-Fa Zhou at TUWIEN
7
B-Prolog =
Prolog + Tabling + CLP(FD)
 Prolog enhanced
– Array subscripts
– Loop constructs
with
 Tabling
– Memorize and reuse intermediate results
• Suitable for dynamic programming problems
 CLP(FD)
– Constraint Logic Programming over Finite Domains
• Suitable for constraint satisfaction problems (NP-complete)
Neng-Fa Zhou at TUWIEN
8
Array Subscripts in B-Prolog
 In arithmetic expressions
S is X[1]+X[2]+X[3]
 In arithmetic constraints
X[1]+X[2] #= X[3]
 In calls to @= and @:=
X[1,2] @= 100
X[1,2] @:= 100
 In any other context,
X[I1,…,In] is the same as X^[I1,…,In]
Neng-Fa Zhou at TUWIEN
9
Loop Constructs in B-Prolog
 foreach
– foreach(E1 in D1, . . ., En in Dn, LVars, Goal)
– Example:
• foreach(A
in [a,b], I in 1..2, writeln((A,I))
 List comprehension
– [T : E1 in D1, . . ., En in Dn, LVars, Goal]
– Examples:
• L @= [(A,I): A in [a,b], I in 1..2].
• sum([A[I,J] : I in 1..N, J in 1..N]) #= N*N
Neng-Fa Zhou at TUWIEN
10
Tabling in B-Prolog
 Eliminate infinite loops
:-table path/2.
path(X,Y):-edge(X,Y).
path(X,Y):-edge(X,Z),path(Z,Y).
 Reduce redundant computations
:-table fib/2.
fib(0,1).
fib(1,1).
fib(N,F):N>1,
N1 is N-1,fib(N1,F1),
N2 is N-2,fib(N2,F2),
F is F1+F2.
Neng-Fa Zhou at TUWIEN
11
The Table-All Approach
 Characteristics
– All the arguments of a tabled subgoal are used
in variant checking
– All answers are tabled
 Problems
– The number of answers may be too large or
even infinite for DP and ML problems
– When computing aggregates, tabling
noncontributing answers is a waste
Neng-Fa Zhou at TUWIEN
12
Mode-Directed Tabling in
B-Prolog
 Table mode declaration
:-table p(M1,...,Mn):C.
– C: Cardinality limit
– Modes
• + : input
• - : output
• min: minimized
• max: maximized
Neng-Fa Zhou at TUWIEN
13
Example: Shortest Path Problem
:-table sp(+,+,-,min).
sp(X,Y,[(X,Y)],W) :edge(X,Y,W).
sp(X,Y,[(X,Z)|Path],W) :edge(X,Z,W1),
sp(Z,Y,Path,W2),
W is W1+W2.
 sp(X,Y,P,W)
– P is a shortest path between X and Y with
minimal weight W.
Neng-Fa Zhou at TUWIEN
14
CLP(FD) in B-Prolog
 A rich set of built-in constraints
– Unification and arithmetic constraints (#=, #\=, #>,
#>=, #<, #=<)
– Global constraints
 A glass-box approach to the implementation
– All propagators are described in Action Rules
(TPLP’06)
– Action Rules is open to the user for describing
problem-specific propagators
 A rich set of labeling options
Neng-Fa Zhou at TUWIEN
15
Global Constraints in B-Prolog
 all_different(L) & all_distinct(L)
– post_neqs(L)
 circuit(L)
 count(V,L,RelOp,N)
– exactly(N,L,V)
– atleast(N,L,V)
– atmost(N,L,V)
 cumulative(Starts,Durations,Resources,Limit)
– serialized(Starts,Durations)
 diffn(L)
 element(I,L,V)
 path_from_to(From,To,L)& path_from_to(From,To,L,Lab)
Neng-Fa Zhou at TUWIEN
16
Outline
 Overview of the ASP competition
 B-Prolog’s features
 BPSolver’s winning solutions
 BPSolver’s hopeful solutions
 BPSolver’s losing solutions
 Observations
Neng-Fa Zhou at TUWIEN
17
BPSolver’s Winning Solutions
Neng-Fa Zhou at TUWIEN
18
Winning Solutions in Prolog
 Grammar-Based Information Extraction
– Parsing
 Labyrinth
– State space search
 Tomography
– Set covering
Neng-Fa Zhou at TUWIEN
19
Winning Solutions With Tabling
 Reachability
 Hydraulic Planning
 Hydraulic Leaking
 Airport Pickup
 Hanoi Tower
Neng-Fa Zhou at TUWIEN
20
Reachability
:-table reach/1.
reach(X):start(X).
reach(Y):reach(X),
edge(X,Y).
Neng-Fa Zhou at TUWIEN
21
Hydraulic Planning
:-table pressurize(+,-,min).
pressurize(Node,Plan,Len):full(Node),!, Plan=[],Len=0.
pressurize(Node,[Valve|Plan],Len):link(AnotherNode,Node,Valve),
\+ stuck(Valve),
pressurize(AnotherNode,Plan,Len1),
Len is Len1+1.
Neng-Fa Zhou at TUWIEN
22
Hydraulic Leaking
:-table pressurize(+,-,min).
pressurize(Node,Plan,(Leaks,Len)):full(Node),!,
Plan=[],Leaks=0,Len=0.
pressurize(Node,[Valve|Plan],(Leaks,Len)):link(AnotherNode,Node,Valve),
\+ stuck(Valve),
pressurize(AnotherNode,Plan,(Leaks1,Len1)),
Len is Len1+1,
(leaking(Valve)->
Leaks is Leaks1+1
;
Leaks is Leaks1
).
Neng-Fa Zhou at TUWIEN
23
Airport Pickup
 Passengers at Airport #1 want to move to
Airport #2 and vice versa
 A certain number of vehicles are available
 Some of the locations are gas stations
CITY MAP
Neng-Fa Zhou at TUWIEN
24
Solution to Airport Pickup
:-table move_vehicle(+,+,+,+,max,-).
move_vehicle(X,X,_Cap,GasLevel,Objective,Steps):Objective=(GasLevel,0),Steps=[].
move_vehicle(X,Y,Cap,GasLevel,Objective,[drive(Z)|Steps]):(driveway(X,Z,GasNeeded);driveway(Z,X,GasNeeded)),
GasLevel>=GasNeeded,
GasLevel1 is GasLevel-GasNeeded,
move_vehicle(Z,Y,Cap,GasLevel1,Objective1,Steps),
Objective1=(AfterGasLevel,MLen),
MLen1 is MLen-1,
Objective=(AfterGasLevel,MLen1).
move_vehicle(X,Y,Cap,GasLevel,Objective,[refuel|Steps]):gasstation(X), GasLevel<Cap,
move_vehicle(X,Y,Cap,Cap,Objective1,Steps),
Objective1=(AfterGasLevel,MLen),
MLen1 is MLen-1,
Objective=(AfterGasLevel,MLen1).
Neng-Fa Zhou at TUWIEN
25
Hanoi Tower (4-Pegs)
A
B
C
D
A
B
C
D
Two snapshots from the sequence
by the Frame-Stewart algorithm
Neng-Fa Zhou at TUWIEN
26
Problem Reduction
 If the largest disk is in its final position,
remove it.
A
B
C
D
A
Neng-Fa Zhou at TUWIEN
B
C
D
27
Create an Intermediate State
 Subproblems
Sub-prob-1
A
B
C
D
A
B
C
D
Sub-prob-2
A
B
C
D
A
Neng-Fa Zhou at TUWIEN
B
C
D
28
The Solution(4-Peg Hanoi Tower)
:-table plan4(+,+,+,-,min).
plan4(N,_CState,_GState,Plan,Len):-N=:=0,!,Plan=[],Len=0.
plan4(N,CState,GState,Plan,Len):reduce_prob(N,CState,GState,CState1,GState1),!,
N1 is N-1,
plan4(N1,CState1,GState1,Plan,Len).
plan4(N,CState,GState,Plan,Len):partition_disks(N,CState,GState,ItState,Mid,Peg),
remove_larger_disks(CState,Mid,CState1),
plan4(Mid,CState1,ItState,Plan1,Len1), % sub-prob1
remove_smaller_or_equal_disks(CState,Mid,CState2),
remove_smaller_or_equal_disks(GState,Mid,GState2),
N1 is N-Mid,
plan3(N1,CState2,GState2,Peg,Plan2,Len2), % sub-prob2
remove_larger_disks(GState,Mid,GState1),
plan4(Mid,ItState,GState1,Plan3,Len3), % sub-prob3
append(Plan1,Plan2,Plan3,Plan),
Len is Len1+Len2+Len3.
Neng-Fa Zhou at TUWIEN
29
Winning Solutions in CLP(FD)
 Tangram
 Magic Square Sets (all_different)
 Weight-Assignment Tree (element)
 Knight Tour (circuit)
 Disjunctive Scheduling (post_disjunctive_tasks
 Maximal Clique
Neng-Fa Zhou at TUWIEN
30
Magic Square Sets
semi(Board,N,Magic):foreach(I in 1..N,
sum([Board[I,J] : J in 1..N])#=Magic),
foreach(J in 1..N,
sum([Board[I,J] : I in 1..N])#=Magic).
normal(Board,N,Magic):sum([Board[I,I] : I in 1..N]) #= Magic,
sum([Board[I,N-I+1] : I in 1..N]) #= Magic.
Neng-Fa Zhou at TUWIEN
31
Outline
 Overview of the ASP competition
 B-Prolog’s features
 BPSolver’s winning solutions
 BPSolver’s hopeful solutions
 BPSolver’s losing solutions
 Observations
Neng-Fa Zhou at TUWIEN
32
BPSolver’s Hopeful Solutions
 Graph Coloring (Ranking = 5)
 Sokoban Optimization (Ranking = 4)
Neng-Fa Zhou at TUWIEN
33
Graph Coloring
 Model-1 (neq)
• For each two neighbors i and j, CiCj
 Model-2 (all_distinct)
• For each complete subgraph (clique) {i1,i2,…,ik},
all_distinct([Ci1, Ci2,…, Cik])
• post_neqs(Neqs) in B-Prolog
 Model-3 (Model-2 + symmetry breaking)
– Allen Van Gelder: Another look at graph coloring via
propositional satisfiability
Neng-Fa Zhou at TUWIEN
34
Graph Coloring in B-Prolog
Go:create_vars(Vars),
generate_neqs(Vars,Neqs),
post_neqs(Neqs,Cliques), % new built-in
largest_clique(Cliques,LClique),
(labeling(LClique)->
labeling_ffc(Vars),!;fail),
output(Vars).
Neng-Fa Zhou at TUWIEN
35
Performance Comparison
(Model-2 Vs. Model-3)
Neng-Fa Zhou at TUWIEN
36
BPSolver’s Solution to Sokoban
Neng-Fa Zhou at TUWIEN
37
The Competition Results
Neng-Fa Zhou at TUWIEN
38
BPSolver’s Losing Solutions
(Score)
 Partner Units (0)
 Reverse Folding (0)
 Solitaire (0)
 Strategic Companies (0)
 Company Controls Optimize (0)
 Stable Marriage (5)
 Maze Generation (49)
Neng-Fa Zhou at TUWIEN
39
Observations
Neng-Fa Zhou at TUWIEN
40