Lecture 16: Local Search © J. Christopher Beck 2008 1 Outline Local Search on Crystal Maze Core Ideas Local Search for Scheduling © J. Christopher Beck 2008 2 Readings P Ch C.5 © J. Christopher Beck 2008 3 And now for something completely different … Local search is quite a different set of ideas from tree search Be prepared to “think different(ly)” © J. Christopher Beck 2008 4 Crystal Maze Place the numbers 1 through 8 in the nodes such that: Each number appears exactly once – No connected ? ? nodes have consecutive numbers ? ? ? © J. Christopher Beck 2008 ? ? ? 5 Local Search Idea Randomly assign values (even if the constraints are “broken”) Initial state will probably be infeasible Make “moves” to try to move toward a solution © J. Christopher Beck 2008 6 Random Initial Solution 1? © J. Christopher Beck 2008 2? 5? 3? 6? 4? 7? 8? 7 Random Initial Solution 1? 2? 5? 3? 6? 4? 7? 8? “Broken” constraint Cost = # of broken constraints © J. Christopher Beck 2008 8 What Should We Do Now? Move: Swap two numbers Which two numbers? Randomly pick a pair The pair that will lead to the biggest decrease in cost Cost: number of broken constraints © J. Christopher Beck 2008 9 What Should We Do Now? Move: Swap two numbers Which two numbers? Randomly pick a pair The pair that will lead to the biggest decrease in cost Cost: number of broken constraints © J. Christopher Beck 2008 10 Random Initial Solution 1? © J. Christopher Beck 2008 2? 5? 3? 6? 4? 7? 8? 11 Cost Difference Table 1 2 3 4 5 6 7 8 1 0 2 3 4 5 6 7 8 0 © J. Christopher Beck 2008 0 0 0 0 0 0 12 Random Initial Solution 1? © J. Christopher Beck 2008 2? 5? 3? 6? 4? 7? 8? 13 Swap 1 & 2 2? © J. Christopher Beck 2008 1? 5? 3? 6? 4? 7? 8? 14 Cost Difference Table 1 2 3 4 5 6 7 8 1 0 2 0 0 © J. Christopher Beck 2008 3 4 5 6 7 8 0 0 0 0 0 0 15 Random Initial Solution 1? © J. Christopher Beck 2008 2? 5? 3? 6? 4? 7? 8? 16 Swap 1 & 3 3? © J. Christopher Beck 2008 2? 5? 1? 6? 4? 7? 8? 17 Cost Difference Table 1 2 3 4 5 6 7 8 1 0 2 0 0 © J. Christopher Beck 2008 3 0 4 5 6 7 8 0 0 0 0 0 0 18 Random Initial Solution 1? © J. Christopher Beck 2008 2? 5? 3? 6? 4? 7? 8? 19 Swap 1 & 4 4? © J. Christopher Beck 2008 2? 5? 3? 6? 1? 7? 8? 20 Cost Difference Table 1 2 3 4 5 6 7 8 1 0 2 0 0 © J. Christopher Beck 2008 3 0 4 -1 5 6 7 8 0 0 0 0 0 0 21 Cost Difference Table 1 2 3 4 5 6 7 8 1 0 2 0 0 © J. Christopher Beck 2008 3 0 -1 0 4 -1 1 0 0 5 0 -1 0 0 0 6 -2 -2 0 0 0 0 7 -3 -1 -1 -1 1 -1 0 8 -2 -3 0 0 -1 0 0 0 22 Cost Difference Table 1 2 3 4 5 6 7 8 1 0 2 0 0 © J. Christopher Beck 2008 3 0 -1 0 4 -1 1 0 0 5 0 -1 0 0 0 6 -2 -2 0 0 0 0 7 -3 -1 -1 -1 1 -1 0 8 -2 -3 0 0 -1 0 0 0 23 Current State 1? © J. Christopher Beck 2008 2? 5? 3? 6? 4? 7? 8? 24 Swap 1 & 7: Cost 3 7? 2? 5? 3? 6? 4? 1? 8? Only made 1 move (so far)! © J. Christopher Beck 2008 25 New Cost Difference Table 1 2 3 4 5 6 7 8 1 0 2 0 0 © J. Christopher Beck 2008 3 0 0 0 4 0 2 0 0 5 2 0 0 0 0 6 0 1 1 1 1 0 7 3 1 1 1 2 0 0 8 0 1 -1 1 0 0 1 0 26 Current State 7? © J. Christopher Beck 2008 2? 5? 3? 6? 4? 1? 8? 27 Swap 3 & 8: Cost 2 7? © J. Christopher Beck 2008 2? 5? 8? 6? 4? 1? 3? 28 Swap 6 & 7: Cost 1 6? © J. Christopher Beck 2008 2? 5? 8? 7? 4? 1? 3? 29 Moves Initial State: Cost 6 Swap 1 & 7: Cost 3 calculate cost difference table Swap 3 & 8: Cost 2 calculate cost difference table calculate cost difference table Swap 6 & 7: Cost 1 calculate cost difference table © J. Christopher Beck 2008 30 Cost Difference Table 1 2 3 4 5 6 7 8 1 0 2 1 0 © J. Christopher Beck 2008 3 1 1 0 4 1 2 1 0 5 2 2 1 2 0 6 2 1 4 1 2 0 7 1 3 1 3 1 1 0 8 1 1 2 1 2 1 1 0 31 Now what? There are no improving moves to make! So far, we have been “hillclimbing” cost © J. Christopher Beck 2008 moves 32 Now what? Options: Restart from a new random state Take the least worse move (increase cost by minimal amount) Try a new style of local search © J. Christopher Beck 2008 33 Now what? For now, let’s just stop In the next lecture, we will see one way to deal with this But if we stop, we haven’t solved the problem No guarantee that a local search will find the best (or even any) solution!! © J. Christopher Beck 2008 34 Core Ideas of Local Search Start with some (random?) assignment of variables Look in “neighborhood” formed by making a small change to the assignment Choose best neighbor Repeat © J. Christopher Beck 2008 35 To Define a Local Search … Define a way to find a starting solution a neighborhood a rule for choosing a neighbor a cost function (if you don’t already have one) (This is a bit of a simplification – more detail in the next lecture) © J. Christopher Beck 2008 36 A Simple Scheduling Problem Jobs Processing times 0 J0R0[15] J0R1[50] 1 J1R1[50] J1R0[50] 2 J2R0[30] J2R1[15] Solve with local search © J. Christopher Beck 2008 37