03

Local Search

Chapter 3 • Advanced

120 min

Local Search

Local Search Algorithm

  1. Start with initial solution
  2. Repeatedly move to better neighbor
  3. Stop when no better neighbor exists

Hill Climbing

python.js
def hill_climbing(initial_solution, neighbors, objective):
    """Hill climbing algorithm"""
    current = initial_solution
    current_value = objective(current)
    
    while True:
        best_neighbor = None
        best_value = current_value
        
        for neighbor in neighbors(current):
            value = objective(neighbor)
            if value > best_value:
                best_neighbor = neighbor
                best_value = value
        
        if best_neighbor is None:
            break  # Local optimum
        
        current = best_neighbor
        current_value = best_value
    
    return current

Simulated Annealing

python.js
import random
import math

def simulated_annealing(initial, neighbors, objective, temp, cooling):
    """Simulated annealing"""
    current = initial
    current_value = objective(current)
    best = current
    best_value = current_value
    
    while temp > 0.1:
        neighbor = random.choice(neighbors(current))
        neighbor_value = objective(neighbor)
        
        delta = neighbor_value - current_value
        
        if delta > 0 or random.random() < math.exp(delta / temp):
            current = neighbor
            current_value = neighbor_value
            
            if current_value > best_value:
                best = current
                best_value = current_value
        
        temp *= cooling
    
    return best

Practice Problems

  1. Hill climbing implementation
  2. Simulated annealing
  3. Genetic algorithms
  4. Tabu search