Lecture04 LocalSearch
Lecture04 LocalSearch
Lecture04 LocalSearch
2
Optimization problems
3
Global search algorithms
• Global search: explore search spaces systematically
• Keep one or more paths in memory and record which alternatives
have been explored at each point along the path
• Many problems do not fit the “standard” search model.
• The final configuration matters, not the order in which it is formed.
• No “goal test” and no “path cost”, e.g., Darwinian evolution with
“natural” reproductive fitness
4
Optimization problems
• Find the best state according to an objective function.
• E.g., the Knight’s tour, TSP, scheduling, integrated-circuit design,
factory-floor layout, automatic programming, vehicle routing, etc.
5
Local search algorithms
• Operate using a single current node and generally move
only to neighbors of that node
• Not systematic
• The paths followed by the search are typically not retained.
• Use very little memory, usually a constant amount
• Find reasonable solutions in large or infinite (continuous)
state spaces
6
Local search and Optimization
7
State-space landscape
• A landscape has both “location” and “elevation”
• Location state, elevation heuristic cost or objective function
9
Hill-climbing search
10
Hill-climbing search
11
Hill-climbing search
• A loop that continually moves in the direction of increasing
value and terminates when it reaches a “peak”.
• Peaks are where no neighbor has a higher value.
• Not look ahead beyond the immediate neighbors of the
current state
• No search tree maintained, only the state and the objective
function’s value for the current node recorded
• Sometimes called greedy local search
• Grab a good neighbor without thinking ahead about where to go next
12
Hill-climbing: 8-queens problem
• Complete-state formulation
• All 8 queens on the board, one per column
• Successor function
• Move a queen to another square in the same column → 8 × 7 = 56
successors
• Heuristic cost function ℎ(𝑛)
• The number of pairs of queens that are ATTACKING each other,
either directly or indirectly → global minimum has ℎ 𝑛 = 0
• Make rapid progress toward a solution
• 8-queens (88 17 million states): 4 steps on average when succeeds
and 3 when get stuck
13
Hill-climbing: 8-queens problem
• Current state 𝑐1 𝑐2 𝑐3 𝑐4 𝑐5 𝑐6 𝑐7 𝑐8 = (5 6 7 4 5 6 7 6) ℎ 𝑛 = 17
• The best successors has ℎ = 12 → choose randomly among
the set of best successors if there is more than one
14
Hill climbing and local maxima
• Often suboptimal, due to local maxima, ridges and plateau
• E.g., 8-queens: steepest-ascent hill climbing gets stuck 86% of the
time, solving only 14% of problem instances
NP-hard problems typically have an exponential number of local maxima to get stuck on.
15
Hill climbing and local maxima
• Current state (8 3 7 4 2 5 1 6) ℎ 𝑛 = 1
• Every successor has a higher cost → local minimum
16
Solutions: Sideways moves
• If no downhill (uphill) moves, the algorithm can escape with
sideways moves.
• A limit on the possible number of sideways moves required to avoid
infinite loops
• For example, 8-queens problem
• Allow sideways moves with a limit of 100 → percentage of problem
instances solved raises from 14 to 94%
• Success comes at a cost: the algorithm averages roughly 21 steps
for each successful instance and 64 for each failure.
17
Solutions: Hill-climbing variants
• Stochastic hill climbing
• Choose at random from among the uphill moves with a probability of
selection varied with the moves’ steepness
• Usually converge more slowly than steepest ascent, but find better
solutions in some cases
• First-choice hill climbing
• Generate successors randomly until one is generated that is better
than the current state
• Good strategy when a state has many successors (e.g., thousands)
• Random-restart hill climbing
• A series of hill-climbing searches from randomly generated initial
states until a goal is found
18
Random-restart hill climbing
• Find a good solution quickly after a small number of restarts
• The series of hill-climbing searches can be organized flexibly
• For each restart: run until termination vs. run for a fixed time
• Run a fixed number of restarts vs. run indefinitely
• If each search has a probability 𝑝 of success, the expected
number of restarts required is 1/𝑝
• Very effective indeed for 8-queens
19
Quiz 01: 4-queens problem
• Consider the following 4-queen problem
20
Simulated annealing
21
Simulated annealing
• Combine hill climbing with a random walk in some way that
yields both efficiency and completeness
• Shake hard (i.e., at a high temperature) and then gradually
reduce the intensity of shaking (i.e., lower the temperature)
22
Simulated annealing
function SIMULATED-ANNEALING(problem, schedule)
returns a solution state
inputs: problem, a problem
schedule, a mapping from time to “temperature”
current ← MAKE-NODE(problem.INITIAL-STATE)
for t = 1 to ∞ do
T ← schedule(t)
if T = 0 then return current
next ← a randomly selected successor of current
ΔE ← next.VALUE – current.VALUE
if ΔE > 0 then current ← next
else current ← next only with probability eΔE/T
23
Local beam search
24
Local beam search
• Keep track of 𝑘 states rather than just one
• Begin with 𝑘 randomly generated states
• At each step, all successors of all 𝑘 states are generated
• If the goal is not found, select the 𝑘 best successors from the
complete list and repeat
25
Local beam search
• Useful information is passed among the parallel search
threads → major difference from random-restart search
• Possibly suffer from a lack of diversity among the 𝑘 states
• Quickly concentrate in a small region of the state space → an
expensive version of hill climbing
• Stochastic beam search
• Choose 𝑘 successors at random, with the probability of choosing a
given successor being an increasing function of its value
26
Genetic algorithms
27
Genetic algorithms
• A variant of stochastic beam search
• Successor states are generated by combining two parent
states rather than by modifying a single state
• The reproduction are sexual rather than asexual
28
Genetic algorithms: 8-queens
30
Representation of Individuals
• Each state, or individual, is represented as a string over a
finite alphabet – most commonly, a string of 0s and 1s.
• E.g., 8-queens: 8 × log 2 8 = 24 bits
32
Reproduction: Roulette wheel
33
Quiz 02: Calculate fitness scores
• Consider the 4-queens problem, in which each state
has 4 queens, one per column, on the board. The
state can be represented in genetic algorithm as a
sequence of 4 digits, each of which denotes the
position of a queen in its own column (from 1 to 4).
34
Crossover operation
• For each pair to be mated, a crossover point is chosen
randomly from the positions in the string.
36
A workflow of Genetic algorithms
No
Start
New population
39
Comments on Genetic algorithms
• Random exploration find solutions that local search does not
• Via crossover primarily
• Can solve “hard” problem
• Rely on very little domain knowledge
• Appealing connection to human evolution
40
Comments on Genetic algorithms
• Large number of “tunable” parameters
• Difficult to replicate performance from one problem to another
41
THE END
42