79
Parallel Programming Parallel algorithms Combinatorial Search

Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Embed Size (px)

Citation preview

Page 1: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Parallel Programming

Parallel algorithms

Combinatorial Search

Page 2: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Some Combinatorial Search Methods

• Divide and conquer

• Backtrack search

• Branch and bound

• Game tree search (minimax, alpha-beta)

2010@FEUP Parallel Algorithms - Combinatorial Search 2

Page 3: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Terminology

• Combinatorial algorithm: computation

performed on discrete structure

• Combinatorial search: finding one or more

optimal or suboptimal solutions in a

defined problem space

• Kinds of combinatorial search problem

• Decision problem (exists (find 1 solution);

doesn’t exist)

• Optimization problem (the best solution)

2010@FEUP Parallel Algorithms - Combinatorial Search 3

Page 4: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Examples of Combinatorial Search

• Laying out circuits in VLSI

• Find the smallest area

• Planning motion of robot arms

• Smallest distance to move (with or without

constraints)

• Assigning crews to airline flights

• Proving theorems

• Playing games

2010@FEUP Parallel Algorithms - Combinatorial Search 4

Page 5: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Search Tree

• Each node represents a problem or sub-

problem

• Root of tree: initial problem to be solved

• Children of a node created by adding

constraints (one move from the father)

• AND node: to find solution, must solve

problems represented by all children

• OR node: to find a solution, solve any of

the problems represented by the children

2010@FEUP Parallel Algorithms - Combinatorial Search 5

Page 6: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Search Tree (cont.)

• AND tree

• Contains only AND nodes

• Divide-and-conquer algorithms

• OR tree

• Contains only OR nodes

• Backtrack search and branch and bound

• AND/OR tree

• Contains both AND and OR nodes

• Game trees

2010@FEUP Parallel Algorithms - Combinatorial Search 6

Page 7: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Divide and Conquer

• Divide-and-conquer methodology

• Partition a problem into subproblems

• Solve the subproblems

• Combine solutions to subproblems

• Recursive: sub-problems may be solved

using the divide-and-conquer methodology

• Example: quicksort

2010@FEUP Parallel Algorithms - Combinatorial Search 7

Page 8: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Best for Centralized Multiprocessor

• Unsolved subproblems kept in one shared

stack

• Processors needing work can access the

stack

• Processors with extra work can put it on

the stack

• Effective workload balancing mechanism

• Stack can become a bottleneck as number

of processors increases

2010@FEUP Parallel Algorithms - Combinatorial Search 8

Page 9: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Multicomputer Divide and Conquer

• Subproblems must be distributed among

memories of individual processors

• Two designs

• Original problem and final solution stored in

memory of a single processor

• Both original problem and final solution

distributed among memories of all processors

2010@FEUP Parallel Algorithms - Combinatorial Search 9

Page 10: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Design 1

• Algorithm has three phases

• Phase 1: problems divided and propagated

throughout the parallel computer

• Phase 2: processors compute solutions to

their subproblems

• Phase 3: partial results are combined

• Maximum speedup limited by propagation

and combining overhead

2010@FEUP Parallel Algorithms - Combinatorial Search 10

Page 11: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Design 2

• Both original problem and final solution

are distributed among processors’

memories

• Eliminates starting up and winding down

phases of first design

• Allows maximum problem size to increase

with number of processors

• Used this approach for parallel quicksort

algorithms

• Challenge: keeping workloads balanced

among processors

2010@FEUP Parallel Algorithms - Combinatorial Search 11

Page 12: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Backtrack Search

• Uses depth-first search to consider

alternative solutions to a combinatorial

search problem

• Recursive algorithm

• Backtrack occurs when

• A node has no children (“dead end”)

• All of a node’s children have been explored

2010@FEUP Parallel Algorithms - Combinatorial Search 12

Page 13: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Example: Crossword Puzzle Creation

• Given

• Blank crossword puzzle

• Dictionary of words and phrases

• Assign letters to blank spaces so that all

puzzle’s horizontal and vertical “words”

are from the dictionary

• Halt as soon as a solution is found

2010@FEUP Parallel Algorithms - Combinatorial Search 13

Page 14: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Crossword Puzzle Problem

2010@FEUP Parallel Algorithms - Combinatorial Search 14

Given a blank crossword

puzzle and a dictionary ............. find a way to fill in the puzzle.

Page 15: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

A Search Strategy

• Identify longest incomplete word in puzzle

(break ties arbitrarily)

• Look for a word of that length

• If cannot find such a word, backtrack

• Otherwise, find longest incomplete word

that has at least one letter assigned

(break ties arbitrarily)

• Look for a word of that length

• If cannot find such a word, backtrack

• Recurse until a solution is found or all

possibilities have been attempted

2010@FEUP Parallel Algorithms - Combinatorial Search 15

Page 16: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

State Space Tree

Root of tree is initial, blank puzzle.

Choices for word 1

Choices for word 2

Word 3 choices

etc.

2010@FEUP Parallel Algorithms - Combinatorial Search 16

Page 17: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Backtrack Search

?

?

?

?

?

?

?

2010@FEUP Parallel Algorithms - Combinatorial Search 17

Page 18: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Backtrack Search

T

R

? ? O ? ? ? ?

L

L

E

Y

2010@FEUP Parallel Algorithms - Combinatorial Search 18

Page 19: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Backtrack Search

T ?

R ?

C L O S E T S

L ?

L ?

E ?

Y ?

2010@FEUP Parallel Algorithms - Combinatorial Search 19

Page 20: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Backtrack Search

T ?

R ?

C L O S E T S

L ?

L ?

E ?

Y ?

Cannot find word.

Must backtrack.

2010@FEUP Parallel Algorithms - Combinatorial Search 20

Page 21: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Backtrack Search

T

R

? ? O ? ? ? ?

L

L

E

Y

Cannot find word.

Must backtrack.

2010@FEUP Parallel Algorithms - Combinatorial Search 21

Page 22: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Backtrack Search

T ?

R ?

C R O Q U E T

L ?

L ?

E ?

Y ?

2010@FEUP Parallel Algorithms - Combinatorial Search 22

Page 23: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Backtrack Search

T T

R R

C R O Q U E T

L M

? ? L ? P ? ?

E E

Y D

2010@FEUP Parallel Algorithms - Combinatorial Search 23

Page 24: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Time and Space Complexity

• Suppose average branching factor in state

space tree is b

• Searching a tree of depth k requires

examining

nodes in the worst case (exponential time)

• Amount of memory usually required is (k)

)(11

11

2 kk

k bb

bbbbb

2010@FEUP Parallel Algorithms - Combinatorial Search 24

Page 25: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Parallel Backtrack Search

• First strategy: give each processor a

subtree

• Suppose p = bk

• A process searches all nodes to depth k

• It then explores only one of subtrees rooted at

level k

• If d (depth of search) > 2k, time required by

each process to traverse first k levels of state

space tree is negligible

2010@FEUP Parallel Algorithms - Combinatorial Search 25

Page 26: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Parallel Backtrack when p = bk

Subtree

Searched

by

Process 0

Subtree

Searched

by

Process 1

Subtree

Searched

by

Process 2

Subtree

Searched

by

Process 3

2010@FEUP Parallel Algorithms - Combinatorial Search 26

Page 27: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

What If p bk

?

• A process can perform sequential search

to level m (where bm

> p) of state space

tree

• Each process explores its share of the

subtrees rooted by nodes at level m

• As m increases, there are more subtrees

to divide among processes, which can

make workloads more balanced

• Increasing m also increases number of

redundant computations

2010@FEUP Parallel Algorithms - Combinatorial Search 27

Page 28: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Maximum Speedup when p bk

In this example 5 processors are exploring a state space

tree with branching factor 3 and depth 10.

2010@FEUP Parallel Algorithms - Combinatorial Search 28

Page 29: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Disadvantage of Allocating One Subtree per Process

• In most cases state space tree is not

balanced

• Example: in crossword puzzle problem,

some word choices lead to dead ends

quicker than others

• Alternative: make sequential search go

deeper, so that each process handles

many subtrees (cyclic allocation)

2010@FEUP Parallel Algorithms - Combinatorial Search 29

Page 30: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Allocating Many Subtrees per Process

2010@FEUP Parallel Algorithms - Combinatorial Search 30

b = 3; p = 4; m = 3; allocation rule (subtree nr) % p == rank

Page 31: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Backtrack Algorithm

2010@FEUP Parallel Algorithms - Combinatorial Search 31

cutoff_count – nr of nodes at cutoff_depth

cutoff_depth – depth at which subtrees are divided among processes

depth – maximum search depth in the state space tree

moves – records the path to the current node (moves made so far)

p, id – number of processes, process rank

Parallel_Backtrack(node, level)

if (level == depth)

if (node is a solution)

Print_Solution(moves)

else

if (level == cutoff_depth)

cutoff_count ++

if (cutoff_count % p != id)

return

possible_moves = Count_Moves(node) // nr of possible moves from current node

for i = 1 to possible_moves

node = Make_Move(node, i)

moves[ level ] = i

Parallel_Backtrack(node, level+1)

node = Unmake_Move(node, i)

return

Page 32: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Distributed Termination Detection

• Suppose we only want to print one

solution

• We want all processes to halt as soon as

one process finds a solution

• This means processes must periodically

check for messages

• Every process calls MPI_Iprobe every

time search reaches a particular level

(such as the cutoff depth)

• A process sends a message after it has

found a solution

2010@FEUP Parallel Algorithms - Combinatorial Search 32

Page 33: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Simple (Incorrect) Algorithm

• A process halts after one of the following

events has happened:

• It has found a solution and sent a message to

all of the other processes

• It has received a message from another

process

• It has completely searched its portion of the

state space tree

2010@FEUP Parallel Algorithms - Combinatorial Search 33

Page 34: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Why Algorithm Fails

• If a process calls MPI_Finalize before

another active process attempts to send it

a message, we get a run-time error

• How this could happen?

• A process finds a solution after another

process has finished searching its share

of the subtrees

OR

• A process finds a solution after another

process has found a solution

2010@FEUP Parallel Algorithms - Combinatorial Search 34

Page 35: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Distributed Termination Problem

• Distributed termination problem: Ensuring

that

• all processes are inactive AND

• no messages are en route

• Solution developed by Dijkstra, Seijen, and

Gasteren in early 1980s

2010@FEUP Parallel Algorithms - Combinatorial Search 35

Page 36: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Dijkstra et al.’s Algorithm

• Each process has a color and a message

count

• Initial color is white

• Initial message count is 0

• A process that sends a message turns

black and increments its message count

• A process that receives a message turns

black and decrements its message count

• If all processes are white and sum of all

their message counts are 0, there are no

pending messages and we can terminate

the processes

2010@FEUP Parallel Algorithms - Combinatorial Search 36

Page 37: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Dijkstra et al.’s Algorithm (cont.)

• Organize processes into a logical ring

• Process 0 passes a token around the ring

• Token also has a color (initially white) and

count (initially 0)

7

-2

7

5

(c)

7

-2

7

-27

5

(d)

Okay to Terminate-2

2

-2

3

-2

2

-2

0

-2

2

-1

-2

0

(b)

0

1

4

0

1

2 3

(a)

-17

-2

-1

-1

-2

2010@FEUP Parallel Algorithms - Combinatorial Search 37

Page 38: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Dijkstra et al.’s Algorithm (cont.)

• A process receives the token

• If process is black

• Process changes token color to black

• Process changes its color to white

• Process adds its message count to token’s

message count

• A process sends

the token to its

successor in the

logical ring

2010@FEUP Parallel Algorithms - Combinatorial Search 38

7

-2

7

5

(c)

7

-2

7

-27

5

(d)

Okay to Terminate-2

2

-2

3

-2

2

-2

0

-2

2

-1

-2

0

(b)

0

1

4

0

1

2 3

(a)

-17

-2

-1

-1

-2

Page 39: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Dijkstra et al.’s Algorithm (cont.)

• Process 0 receives the token

• Safe to terminate processes if

• Token is white

• Process 0 is white

• Token count + process 0 message count = 0

• Otherwise, process 0 must probe ring of

processes again

2010@FEUP Parallel Algorithms - Combinatorial Search 39

7

-2

7

5

(c)

7

-2

7

-27

5

(d)

Okay to Terminate-2

2

-2

3

-2

2

-2

0

-2

2

-1

-2

0

(b)

0

1

4

0

1

2 3

(a)

-17

-2

-1

-1

-2

Page 40: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Branch and Bound

• Variant of backtrack search

• Takes advantage of information about

optimality of partial solutions to avoid

considering solutions that cannot be

optimal

2010@FEUP Parallel Algorithms - Combinatorial Search 40

Page 41: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Example: 8-puzzle

1 2 3

4 5 6

7 8 Hole

This is the solution state.

Tiles slide up, down, or

sideways into hole.

2010@FEUP Parallel Algorithms - Combinatorial Search 41

Page 42: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

State Space Tree Represents Possible Moves

1 5 2

4 3

7 8 6

1 5 2

4 3

7 8 6

1 5 2

4 3 6

7 8

1 5

4 3 2

7 8 6

1 5

4 3 2

7 8 6

1 5 2

4 3

7 8 6

1 5 2

4 3

7 8 6

1 2

4 5 3

7 8 6

1 5 2

4 3

7 8 6

1 5 2

4 8 3

7 6

1 5 2

4 3

7 8 6

1 5 2

4 3 6

7 8

etc.

2010@FEUP Parallel Algorithms - Combinatorial Search 42

Page 43: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Branch-and-bound Methodology

• Could solve puzzle by pursuing breadth-

first search of state space tree

• We want to examine as few nodes as

possible

• Can speed search if we associate with

each node an estimate of minimum

number of tile moves needed to solve the

puzzle, given moves made so far

2010@FEUP Parallel Algorithms - Combinatorial Search 43

Page 44: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Manhattan (or City Block) Distance

0

4

2

3 2

13

2 1

3 12 2

1

2

3 4

3

2

3

43234

Manhattan distance

from the yellow

intersection.

2010@FEUP Parallel Algorithms - Combinatorial Search 44

Page 45: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

A Lower Bound Function

• A lower bound on number of moves

needed to solve puzzle is sum of

Manhattan distance of each tile’s current

position from its correct position

• Depth of node in state space tree

indicates number of moves made so far

• Adding two values gives lower bound on

number of moves needed for any solution,

given moves made so far

• We always search from node having

smallest value of this function (best-first

search)

2010@FEUP Parallel Algorithms - Combinatorial Search 45

Page 46: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Best-first Search of 8-puzzle

1 5 2

4 3

7 8 6

1 5 2

4 3

7 8 6

1 5 2

4 3 6

7 8

1 5

4 3 2

7 8 6

1 5 2

4 3

7 8 6

1 2

4 5 3

7 8 6

1 5 2

4 3

7 8 6

1 5 2

4 8 3

7 6

1 5 2

4 3

7 8 6

1 5 2

4 3 6

7 8

1 5 2

4 3

7 8 6

1 2

4 5 3

7 8 6

1 2

4 5 3

7 8 6

1 2 3

4 5

7 8 6

1 2

4 5 3

7 8 6

5 7

7

5

755

7 7 7 7 5 7

5 7

1 2

34 5

67 8

7

1 2 3

4 5

67 8

7

1 2 3

4 5 6

7 8

5

Solution

2010@FEUP Parallel Algorithms - Combinatorial Search 46

Page 47: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Pseudocode: Sequential Algorithm

2010@FEUP Parallel Algorithms - Combinatorial Search 47

// initial – initial problem// q – priority queue// u, v – nodes of the search tree

Intialize (q)Insert (q, initial)repeat

u Delete_Min (q)if u is a solution then

Print_solution (u)Halt

elsefor i 1 to Possible_Constraints (u) do

Add constraint i to u, creating vInsert (q, v)

Page 48: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Time and Space Complexity

• In worst case, lower bound function

causes function to perform breadth-first

search

• Suppose branching factor is b and

optimum solution is at depth k of state

space tree

• Worst-case time complexity is (bk)

• On average, b nodes inserted into priority

queue every time a node is deleted

• Worst-case space complexity is (bk)

• Memory limitations often put an upper

bound on the size of the problem that can

be solved

2010@FEUP Parallel Algorithms - Combinatorial Search 48

Page 49: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Parallel Branch and Bound

• We will develop a parallel algorithm

suitable for implementation on a

multicomputer or distributed

multiprocessor

• Conflicting goals

• Want to maximize ratio of local to non-local

memory references

• Want to ensure processors searching

worthwhile portions of state space tree

2010@FEUP Parallel Algorithms - Combinatorial Search 49

Page 50: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Single Priority Queue

• Maintaining a single priority queue not a

good idea

• Communication overhead too great

• Accessing queue is a performance

bottleneck

• Does not allow problem size to scale with

number of processors

2010@FEUP Parallel Algorithms - Combinatorial Search 50

Page 51: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Multiple Priority Queues

• Each process maintains separate priority

queue of unexamined subproblems

• Each process retrieves subproblem with

smallest lower bound to continue search

• Occasionally processes send unexamined

subproblems to other processes

2010@FEUP Parallel Algorithms - Combinatorial Search 51

Page 52: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Start-up Mode

• Process 0 contains original problem in its

priority queue

• Other processes have no work

• After process 0 distributes an unexamined

subproblem, 2 processes have work

• A logarithmic number of

distribution steps are sufficient

to get all processes engaged

2010@FEUP Parallel Algorithms - Combinatorial Search 52

Page 53: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Efficiency

• Conditions for solution to be found and

guaranteed optimal

• At least one solution node must be found

• All nodes in state space tree with smaller

lower bounds must be explored

• Execution time dictated by which of these

events occurs last

• This depends on number of processes,

shape of state space tree, communication

pattern

2010@FEUP Parallel Algorithms - Combinatorial Search 53

Page 54: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Efficiency (cont.)

• Sequential algorithm searches minimum

number of nodes (never explores nodes

with lower bounds greater than cost of

optimal solution)

• Parallel algorithm may examine

unnecessary nodes because each process

searching locally best nodes

• Exchanging subproblems

• promotes distribute of subproblems with good

lower bounds, reducing amount of wasted work

• increases communication overhead

2010@FEUP Parallel Algorithms - Combinatorial Search 54

Page 55: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Halting Conditions

• Distributed termination detection more

complicated than for backtrack search

• Can only halt when

• Have found a solution

• Verified no better solutions exist

2010@FEUP Parallel Algorithms - Combinatorial Search 55

Page 56: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Modifications to DTP Algorithm

• Process turns black if it manipulates an

unexamined subproblem with lower bound

less than cost of best solution found so far

• Add additional fields to termination token

• Cost of best solution found so far

• Solution itself (i.e., moves made to reach

solution)

2010@FEUP Parallel Algorithms - Combinatorial Search 56

Page 57: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Actions When Process Gets Token

• Updates token’s color, count fields

• If locally found solution better than one

carried by token, updates token

• If lower bound of first unexamined

problem in priority queue best solution

found so far, empties priority queue

2010@FEUP Parallel Algorithms - Combinatorial Search 57

View Algorithm

Page 58: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Searching Game Trees

• Best programs for chess, checkers based

on exhaustive search

• Algorithms consider series of moves and

responses, evaluate desirability of

resulting positions, and work their way

back up search tree to determine best

initial move

2010@FEUP Parallel Algorithms - Combinatorial Search 58

Page 59: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Minimax Algorithm

• A form of depth-first search

• Value node = value of position from point

of view of player 1

• Player 1 wants to maximize value of node

• Player 2 want to minimize value of node

2010@FEUP Parallel Algorithms - Combinatorial Search 59

Page 60: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Illustration of Minimax

6

1

-3

7

4

2

1

9

8

0

3

-5

-2

4

7

6

Max

Min

Max

Min

1

10

1 2 0 6

2 61 0 -5 -2-31

2010@FEUP Parallel Algorithms - Combinatorial Search 60

Page 61: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Complexity of Minimax

• Branching factor b

• Depth of search d

• Examination of bd

leaves

• Exponential time in depth of search

• Hence frequently cannot search entire

tree to final positions

• Must rely on evaluation function to

determine value of non-final position

• Space required = linear in depth of search

2010@FEUP Parallel Algorithms - Combinatorial Search 61

Page 62: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Alpha-Beta Pruning

• As a rule, deeper search leads to a higher

quality of play

• Alpha-beta pruning allows game tree

searches to go much deeper (twice as

deep in best case)

• Pruning occurs when it is in the interests

of one of the players to allow play to reach

that position

2010@FEUP Parallel Algorithms - Combinatorial Search 62

Page 63: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Illustration of Alpha-Beta Pruning

6

1

-3 4

2

8

0

3

-5

Max

Min

Max

Min

1

10

1 2 0

2 0 -5-31

2010@FEUP Parallel Algorithms - Combinatorial Search 63

Page 64: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Alpha-Beta Pruning Algorithm

2010@FEUP Parallel Algorithms - Combinatorial Search 64

max_c – Maximum possible moves (children) of a position (node)

pos – position or node of the game tree

a, b – lower and upper values of cutoff; cutoff – flag set when is OK to prune

depth – maximum search depth in the game tree

c[ 1 .. max.c ] – children of current position (node)

val – value of each position (point of view of the root player),

width - nr. of legal moves from the current position

Alpha_Beta(pos, a, b, depth) // initialy called with a = - and b = +

if (depth <= 0) return (Evaluate(pos)) // Evaluate terminal node (point of view of the root player)

width = Generate_Moves(pos) // Fills array c [ ]

if (width == 0) return (Evaluate(pos)) // No more legal moves from this position

cutoff = FALSE;

i = 1

while (i <= width) and (cutoff == FALSE)

val = Alpha_Beta(c[ i ], a, b, depth-1)

if (Max_Node(pos) and val > a) // Root moves

a = val

if (Min_Node(pos) and val < b) // Opponent moves

b = val

if (a > b)

cutoff = TRUE

i ++

if (Max_Node(pos)) return a

else return b

Page 65: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Enhancement Aspiration Search

• Ordinary alpha-beta algorithm begins with

pruning window (–, ) (worst value, best

value)

• Pruning increases as window shrinks

• Goal of aspiration search is to start

pruning sooner

• Make estimate of value v of board position

• Figure probable error e of that estimate

• Call alpha-beta with initial pruning window

(v–e, v+e)

• If search fails, re-do with (–, v–e) or (v+e,

)

2010@FEUP Parallel Algorithms - Combinatorial Search 65

Page 66: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Enhancement Iterative Deepening

• Ply: level of a game tree

• Iterative deepening: use a (d–1)-ply search

to prepare for a d-ply search

• Allows time spent in a search to be

controlled: can iterate deeper and deeper

until allotted time has expired

• Can use results of (d–1)-ply search to help

order nodes for d-ply search, improving

pruning

• Can use value returned from (d–1)-ply

search as center of window for d-ply

aspiration search

2010@FEUP Parallel Algorithms - Combinatorial Search 66

Page 67: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Parallel Alpha-Beta Search

• Perform move generation in parallel and

position evaluation

• CMU’s custom chess machine

• Search the tree in parallel

• IBM’s Deep Blue

• Capable of searching more than 100 millions

positions per second

• Defeated Gary Kasparov in a six-game match

in 1997 by a score of 3.5 - 2.5

2010@FEUP Parallel Algorithms - Combinatorial Search 67

Page 68: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Parallel Aspiration Search

• Create multiple windows, one per

processor

• Allows narrower windows than with a

single processor, increasing pruning

• Chess experiments: maximum expected

speedup usually not more than 5 or 6

• This is because there is a lower bound on

the number of nodes that will be searched,

even with optimal search window

2010@FEUP Parallel Algorithms - Combinatorial Search 68

Page 69: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Parallel Subtree Evaluation

• Processes examine independent subtrees

in parallel

• Search overhead: increase in number of

nodes examined through introduction of

parallelism

• Communication overhead: time spent

coordinating processes performing the

search

• Reducing one kind of overhead is usually

at expense of increasing other kind of

overhead

2010@FEUP Parallel Algorithms - Combinatorial Search 69

Page 70: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Game Trees Are Skewed

• In a perfectly ordered game tree the best

move is always the first move considered

from a node

• In practice, search trees are often not too

far from perfectly ordered

• Such trees are highly skewed: the first

branch takes a disproportionate share of

the computation time

2010@FEUP Parallel Algorithms - Combinatorial Search 70

Page 71: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Alpha-beta Pruning of a Perfectly Ordered Game Tree

1 2 2 3 3 2 2 2 2

3 3

2 2

3221

1 2 2 2

1

2010@FEUP Parallel Algorithms - Combinatorial Search 71

1 – type 1 nodes: root and first child of type 1 nodes

2 – type 2 nodes: other children of type1 nodes and children of

type 3 nodes

3 – type 3 nodes: first child of type 2 nodes

The other than the first child of a type 2 node can be pruned

Page 72: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Distributed Tree Search

• Processes control groups of processors

• At beginning of algorithm, root process is

assigned root node of tree and controls all

processors

• Allocation of processors depends on

location in search tree

2010@FEUP Parallel Algorithms - Combinatorial Search 72

Page 73: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Distributed Tree Search (cont.)

• Type 1 node

• All processors initially allocated to search

leftmost child of node

• When search returns, processors assigned to

remaining children in breadth-first manner

• Type 2 or 3 node: processes assigned to children

in breadth-first manner

• When a process completes searching a subtree,

it returns its allocated processors to its parent

and terminates

• Parents reallocate returned processors to

children that are still active

2010@FEUP Parallel Algorithms - Combinatorial Search 73

Page 74: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Performance of Distributed Tree Search

• Given a uniform game tree with branching

factor b

• If alpha-beta algorithm searches tree with

effective branching factor bx, then DTS

with p processors will achieve a speedup

of O(px)

• Usually x is between 0.5 and 1

2010@FEUP Parallel Algorithms - Combinatorial Search 74

Page 75: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Summary (1/5)

• Combinatorial search used to find solutions to a

variety of discrete decision and optimization problems

• Can categorize problems by type of state space tree

they traverse

• Divide-and-conquer algorithms traverse AND trees

• Backtrack search and Branch-and-Bound search

traverse OR trees

• Minimax and alpha-beta pruning search AND/OR trees

2010@FEUP Parallel Algorithms - Combinatorial Search 75

Page 76: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Summary (2/5)

• Parallel divide and conquer

• If problem starts on a single process and

solution resides on a single process, then

speedup limited by propagation and combining

overhead

• If problem and solution distributed among

processors, efficiency can be much higher, but

balancing workloads can still be a challenge

2010@FEUP Parallel Algorithms - Combinatorial Search 76

Page 77: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Summary (3/5)

• Backtrack search

• Depth-first search applied to state space trees

• Can be used to find a single solution or every

solution

• Does not take advantage of knowledge about the

problem to avoid exploring subtrees that cannot

lead to a solution

• Requires space linear in depth of search (good)

• Challenge: balancing work of exploring subtrees

among processors

• Need to implement distributed termination

detection

2010@FEUP Parallel Algorithms - Combinatorial Search 77

Page 78: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Summary (4/5)

• Branch-and-bound search

• Able to use lower bound information to avoid

exploration of subtrees that cannot lead to

optimal solution

• Need to avoid search overhead without

introducing too much communication overhead

• Also need distributed termination detection

2010@FEUP Parallel Algorithms - Combinatorial Search 78

Page 79: Parallel Programming - GitHub Pagesparallelcomp.github.io/CombinatorialSearch.pdf · Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel

Search (5/5)

• Alpha-beta pruning:

• Preferred method for searching game trees

• Only parallel search of independent subtrees

seems to have enough parallelism to scale to

massively parallel machines

• Distributed tree search algorithm a way to

allocate processors so that both search

overhead and communication overhead are

kept to a reasonable level

2010@FEUP Parallel Algorithms - Combinatorial Search 79