Search in Complex Environment
Search in Complex Environment
Local search
Definition
Local search algorithms operate by starting from an initial state and then searching
through neighboring states without keeping track of the paths or the set of states that
have already been reached. This means these algorithms are not systematic; they may
never explore certain areas of the search space where a solution could exist. However,
they offer two significant advantages:
Key Characteristics
Neighborhood-Based: These methods explore solutions that are close to the
current one.
Iterative Improvement: They progress to a better solution at each step.
Local Optima: They can become stuck in a local optimum, which may not be the
global optimum.
Efficiency: They are computationally efficient, making them suitable for large
search spaces.
Examples:
Hill-Climbing: Moves to the best neighboring solution.
Simulated Annealing: Allows for moves to worse solutions based on a probability
mechanism.
Tabou Search: Utilizes memory to prevent revisiting recent solutions.
Genetic Algorithms: Combines multiple solutions to explore the search space
effectively.
Applications
Solving optimization problems such as the Traveling Salesman Problem (TSP).
Tuning hyperparameters in machine learning.
Resource allocation and scheduling tasks.
Local search algorithms can also solve optimization problems, in which the aim is to
Optimization problem Objective function find the best state according to an objective
function.
Optimization problem
Finding the best state according to some objective function.
In the state-space landscape, we can solve an optimization problem by locating the
lowest or the highest valley, under the assumption that each state is defined by its
location and elevation (cost or heuristic function).
If elevation represents the global minimum cost, then the goal is to locate the lowest
valley, which signifies the global minimum. Conversely, if elevation refers to the global
maximum of an objective function, the objective is to find the highest peak, indicating
the global maximum.
Local search algorithms explore this landscape, and a complete local search algorithm
will always find a goal if one exists. In contrast, an optimal local search algorithm will
identify a global minimum or maximum if one is present.
Hill-climbing algorithm
Hill climbing is a local search algorithm used to solve optimization problems. This
iterative method begins with a random solution and seeks to improve it through
incremental changes. The name "hill climbing" comes from the analogy of climbing a
hill, where the objective is to reach the highest point (the optimal solution) by
consistently moving upward toward better solutions. The algorithm moves in the
direction that offers the steepest ascent.
Unlike other algorithms, hill climbing does not maintain a search tree; therefore, the
data structure for the current state only needs to record the current state and the
value of the objective function. Additionally, hill climbing only considers the immediate
neighbors of the current state and does not look beyond them.
Key Concepts of Hill-Climbing Search:
Objective Function: The algorithm aims to maximize an objective function, which
evaluates the quality of a solution.
Neighborhood: At each step, the algorithm explores the "neighborhood" of the
current solution, which consists of solutions that can be reached by making small
changes (e.g., flipping a bit, swapping elements, etc.).
Greedy Approach: Hill-climbing always moves to a neighboring solution that
improves the objective function, making it a greedy algorithm.
Local Optima: The algorithm can get stuck in a local optimum, where no
neighboring solution is better, but the global optimum (the best possible solution)
has not been reached.
Problem formulation
PYTHON
nsts = 64*63*62*61*60*59*58*57
print(f"The size of state-space is: {nsts}")
Initial State:
PYTHON
import sys
sys.stdout.reconfigure(encoding='utf-8')
from tabulate import tabulate
import numpy as np
import random
random.seed(0)
In general, the Hill-Climbing algorithm will be stuck in 86% of cases and only reach the
solution state in 14%.
Simple Hill-Climbing:
Moves to the first neighbor that improves the objective function.
Faster but may not find for best solution.
Steepest-Ascent Hill-Climbing:
Evaluates all neighbors and moves to the one with the best improvement.
More computationally expensive but more thorough.
Stochastic Hill-Climbing:
Randomly selects a neighbor and moves to it if it improves the objective
function.
Introduces randomness to escape local optima.
Random-Restart Hill-Climbing:
Runs hill-climbing multiple times with different initial solutions.
Increases the chances of finding the global optimum.
Analysis of Hill-Climbing Search Algorithm performance
To evaluate the Hill-Climbing Search Algorithm, we will analyze it based on three key
criteria:
Completeness
Quality of Solution
Time and spatial complexity
Completeness
Analysis:
Exceptions:
Random-Restart Hill-Climbing:
By restarting the algorithm multiple times with different initial solutions, it
becomes probabilistically complete. Given infinite time, it will eventually find
the global optimum.
Quality of Solution
Quality of Solution refers to how close the algorithm's output is to the optimal
solution.
Analysis:
Initial Solution: A good initial solution increases the likelihood of finding a high-
quality local optimum.
Neighborhood Structure: A well-defined neighborhood can help the algorithm
explore better solutions.
Objective Function: If the objective function has few local optima, the solution
quality is likely to be high.
Complexity
Time Complexity:
Space Complexity:
O(1) for Basic Hill-Climbing:
The algorithm only needs to store the current solution and its evaluation.
Higher for Variants:
Variants like tabu search or random-restart hill-climbing may require
additional memory to store visited solutions or restart points.
Summary of Analysis
Criterion Analysis
Completeness Not complete (can get stuck in local optima). Random-restart is
probabilistically complete.
Quality of Returns local optima. Quality depends on the initial solution and
Solution problem structure. Variants like simulated annealing improve
quality.
Time Average case: O(n ⋅ k). Worst case: Exponential.
Complexity
Space O(1) for basic hill-climbing. Higher for variants like tabu search.
Complexity
Conclusion
Hill-climbing is a simple and efficient algorithm for optimization problems, but it has
limitations like getting stuck in local optima. By understanding its completeness,
solution quality, and complexity, you can decide when and how to use it effectively.
Variants like stochastic hill-climbing, random-restart hill-climbing, and simulated
annealing can help overcome its limitations.
Simulated annealing
Simulated Annealing (SA) is a probabilistic search method used to solve optimization
problems, especially in large and complex search spaces where traditional methods
may struggle. This technique is inspired by the physical process of annealing in
metallurgy, where a material is heated and then gradually cooled to reduce defects
and achieve a stable, low-energy state. Similarly, SA explores the search space by
occasionally allowing "worse" solutions, with a certain probability that decreases over
time. This approach helps the method escape local optima and ultimately converge
toward a global optimum.
Key Concepts
Objective Function:
SA aims to minimize (or maximize) an objective function f(S) , where S
represents a solution in the search space.
Temperature (T ):
A control parameter that influences the probability of accepting worse
solutions.
Starts at a high value and gradually decreases over time according to a
cooling schedule.
Cooling Schedule:
Defines how the temperature T is reduced over time.
Common schedules include exponential decay (e.g.,
T = T0 ⋅ αk
1 if ΔE ≤ 0,
P (ΔE) = { $$W here$ΔE = f(x new ) − f(x current )$isthechangeintheob
e −ΔE/T if ΔE > 0,
else
continue
end if
end if
end for
T ← updateAnnealingTemperature(T ) //Temperature update rule
end while
return S
end procedure
Global vs. Local Optima: Simulated Annealing (SA) differs from simple local search
methods like Hill Climbing because it has the ability to escape local optima. It does this
by accepting worse solutions based on a probability determined by a temperature-
dependent function. When properly tuned, SA can converge to near-optimal or even
globally optimal solutions in many practical scenarios.
Computational Complexity
Space Complexity: SA generally requires O(1) additional space beyond the input data
and solution representation. It stores the current state, candidate state, and
temperature value, making it memory-efficient.
Robustness and Parameter Sensitivity
If α is too high (e.g., 0.99), the system cools too slowly, increasing runtime.
If α is too low (e.g., 0.8), SA cools too quickly, leading to suboptimal solutions.
Problem Dependency:
SA performs well for problems with rugged search landscapes, such as the
Traveling Salesman Problem (TSP) and Job Scheduling.
For smooth landscapes, gradient-based methods might outperform SA.
Summary
Simulated Annealing algorithm is a powerful and versatile search method for solving
optimization problems. By balancing exploration and exploitation through a
temperature-controlled probabilistic acceptance criterion, it can effectively navigate
complex search spaces and find high-quality solutions. However, careful tuning of
parameters and problem-specific adaptations are often necessary for optimal
performance.
Elitism: Preserving the best solutions from one generation to the next.
Local Mutation: Applying small, localized changes to solutions to explore nearby
regions.
Restricted Crossover: Using crossover operators that produce offspring close to
the parents.
Algorithm steps
Initialization: Generate an initial population of random solutions
Evaluation: Calculate the fitness of each individual solution
Selection: Choose individuals for reproduction based on fitness
Crossover: Create new solutions by combining selected parents
Mutation: Randomly alter some solutions to maintain diversity
Replacement: Form a new population from offspring and possibly some parents
Termination: Stop when a termination condition is met (e.g., fitness threshold,
generation limit)
Pseudocode
Algorithm Genetic Algorithm
procedure GeneticAlgorithm(populationSize, maxGenerations, crossoverRate,
mutationRate)
population ← InitializePopulation(populationSize)
generation ← 0
while generation < maxGenerations and not TerminationCondition() do
EvaluateFitness(population)
newP opulation ← ∅
while ∣newP opulation∣ < populationSize do
parent1 ← SelectParent(population)
parent2 ← SelectParent(population)
if Random() < crossoverRate then
child1, child2 ← Crossover(parent1, parent2)
else
child1, child2 ← parent1, parent2
end if
if Random() < mutationRate then
child1 ← Mutate(child1)
end if
if Random() < mutationRate then
child2 ← Mutate(child2)
end if
Add child1, child2 to newP opulation
end while
population ← Replace(population, newP opulation)
generation ← generation + 1
end while
return BestSolution(population)
end procedure
Performance Analysis
Exploration vs. Exploitation: GAs balance exploration (searching new areas) and
exploitation (refining existing solutions). As local search methods, they lean more
towards exploitation.
Convergence: GAs can converge to local optima, especially if the population
diversity is low. Techniques like maintaining population diversity and adaptive
mutation rates can mitigate this.
Scalability: GAs can handle large, complex search spaces, but their performance
may degrade with very high-dimensional problems due to the curse of
dimensionality.
Parallelism: GAs are inherently parallel, allowing for efficient use of computational
resources.
Robustness: GAs are robust to noisy and dynamic environments, making them
suitable for real-world problems where the fitness landscape may change over
time.
Population Initialization
Fitness Evaluation
Selection
Crossover
Mutation
Replacement
Overall Complexity
The overall time complexity of a GAs depends on the number of generations (G) and
the complexity of each generation. The total time complexity is:
O(G ⋅ (N ⋅ T f + N ⋅ L))
Dominant Terms:
Fitness evaluation (O(N ⋅ T f )) is often the most expensive operation,
especially if T f is large.
Genetic operations (crossover and mutation) scale linearly with N and L.
Space Complexity
Optimization Techniques
Parallelization: Fitness evaluation and genetic operations can be parallelized to
reduce runtime.
Elitism: Preserving the best solutions reduces the need for excessive generations.
Adaptive Operators: Dynamically adjusting crossover and mutation rates can
improve efficiency.
Summary of Complexity
Let solve the 8-Queens problem through the genetic algorithms approaches using
binary and numeric chromosomes.
*Conclusion
Genetic Algorithms, when adapted for local search, offer a powerful and flexible
approach to problem-solving. They balance exploration and exploitation, making them
suitable for a wide range of optimization problems. However, careful tuning of
parameters and strategies to maintain diversity are crucial for their effective
performance. Their robustness and parallelism further enhance their applicability in
complex and dynamic environments.
The objective is to identify the best locations for three new football stadiums in
northern Morocco. The goal is to minimize the total distance to nine major cities while
ensuring that these locations are accessible by road. This is a classic optimization
problem within a continuous domain, where the solution space consists of geographic
coordinates (latitude and longitude).
Problem Formulation
Objective: Minimize the total distance from the three stadiums to the nine major
cities in northern Morocco. Let’s assume "distance" refers to road distance (though
Euclidean distance could be a simplifying proxy if road data is unavailable).
State Space: Continuous 2D coordinates (x i , y i ) for each stadium i = 1, 2, 3, where
x i and y i are longitude and latitude within northern Morocco’s geographic bounds
(roughly 34°N to 36°N, -2°W to -7°W).
Variables: Six coordinates total (two per stadium): (x 1 , y 1 ), (x 2 , y 2 ), (x 3 , y 3 ).
Cities: Assume the nine major cities in northern Morocco are Tangier, Tetouan,
Chefchaouen, Al Hoceima, Nador, Oujda, Fez, Meknes, and Taza (common urban
centers in the region). Their coordinates are fixed.
Objective Function:
9
f(x 1 , y 1 , x 2 , y 2 , x 3 , y 3 ) = ∑ min d(c, (x i , y i ))
i=1,2,3
c=1
where d(c, (x i , y i )) is the distance from city c to stadium i. This assumes each city is
"assigned" to its nearest stadium, minimizing the maximum travel distance for
residents.
Constraint: Locations must have road accessibility (e.g., within a certain distance
of existing highways or feasible road-building zones). For simplicity, we’ll assume
all points in the region could be made accessible with reasonable infrastructure,
refining this later if needed.
f i (x i , y i ) = ∑ d(c, (x i , y i ))
c∈C i
.
∂f i y i −y c
∂y i = ∑ c∈Ci √(x c −x i ) 2 +(y c −y i ) 2
∂f i ∂f i
(x i , y i ) ← (x i , y i ) − η ⋅ ( , )
∂x i ∂y i
Step 4: Iterate
Reassign cities to the nearest stadium based on updated positions.
Recalculate gradients and update positions.
Repeat until convergence (e.g., when position changes or objective function
improvement falls below a threshold, like 0.001).
Step 5: Road Accessibility Check
After convergence, verify each stadium’s location against a road network map
(e.g., proximity to highways like A1, A2, or N13). If a location is too far (say, >10 km
from a major road), perturb it toward the nearest accessible point and reoptimize
locally.
Pseudocode
Algorithm Gradient Descent for Stadium Placement in Northern Morocco
Input: City coordinates C = {(cjx , cjy )}9j=1 , learning rate α, max iterations T , tolerance ϵ
Set t ← 0
(0) (0)
Compute initial cost f (S (0) ) = ∑9j=1 mini=1,2,3
(cjx − xi )2 + (cjy − yi )2
(t)
∂f yi −cjy
← ∑j:kj =i
(t+1) (t) ∂f
Update: xi ← xi −α⋅ (t)
∂xi
end for
Compute new cost f (S (t+1) )
if ∣f (S (t+1) ) − f (S (t) )∣ < ϵ then
break
end if
t←t+1
end while
return S (t)
Example Walkthrough
Problem Formulation
Objective:
Minimize the total distance between the stadiums and the nine major cities.
Ensure that the stadiums are located on or near accessible roads.
Decision Variables:
Let (x i , y i ) be the coordinates of the i-th stadium (i = 1, 2, 3).
Constraints:
Each stadium must be located on or near a road (represented as a set of road
segments or a road network).
Cost Function:
The cost function f(x 1 , y 1 , x 2 , y 2 , x 3 , y 3 ) can be defined as:
3 9
f = ∑ ∑ d ij + λ ⋅ RoadPenalty
i=1 j=1
where:
d ij is the Euclidean distance between the i-th stadium and the j-th city.
RoadPenalty is a penalty term that increases if a stadium is far from a
road.
λ is a weighting factor to balance the distance and road accessibility
terms.
Local Search Based on Newton's Method
Newton's method is an iterative optimization technique that uses the gradient and
Hessian of the cost function to find a local minimum. Here's how we can apply it:
Steps
Initialization:
Start with initial guesses for the stadium locations (x 01 , y 01 , x 02 , y 02 , x 03 , y 03 ).
These can be random or based on heuristic locations (e.g., centroid of cities).
Gradient and Hessian Calculation:
Compute the gradient ∇f and Hessian H of the cost function f.
3. Update Rule:
p k+1 = p k − H −1 (p k ) ⋅ ∇f(p k )
4. Constraints Handling:
After each update, project the stadium locations onto the nearest road
segment to ensure road accessibility.
5. Termination:
Implementation Details
Cost Function
Distance Term:
Compute the Euclidean distance between each stadium and each city:
d ij = √(x i − c jx ) 2 + (y i − c jy ) 2
pseudocode
Algorithm Newton's Method for Stadium Placement
Input: City coordinates {(cjx , cjy )}9j=1 , road network R, initial stadium locations p0 , max
iterations G, threshold ϵ
Output: Optimized stadium locations p∗
Initialize:
Set p ← p0
Set k ← 0
while k < G and ∥∇f (p)∥ > ϵ do
Gradient and Hessian Calculation:
Compute ∇f (p) and H(p)
Update Rule:
p ← p − H −1 (p) ⋅ ∇f (p)
Projection onto Roads:
for each stadium location (xi , yi ) in p do
end for
k ←k+1
end while
Return: Optimized stadium locations p∗ ← p
Example
Input:
Output:
Optimized stadium locations that minimize the total distance to cities and ensure
road accessibility.
This is known as Jensen's inequality. It basically says that the function's value at a
weighted average of two points is less than or equal to the weighted average of the
function's values at those points.
Key Concepts
Stochastic Transitions: Instead of S → S ′ , we have S → S ′ with probability
A A
Standard hill climbing picks the neighbor with the best value. With uncertain actions:
E[cost(S ′ )] = ∑ P (S ′ ∣ S, A) ⋅ cost(S ′ )
S′
From our prior discussion, gradient descent adjusts variables continuously. With
uncertain actions:
Stochastic Gradient Descent (SGD): Introduce noise into the gradient (e.g., from
sampling or model uncertainty) and update based on
x t+1 = x t − α ⋅ (∇f(x t ) + noise).
Advantage: Robust to small perturbations, common in machine learning with noisy
data.
Limitation: Assumes continuous space and may struggle with discrete,
probabilistic transitions.
Formally:
Scenario: City distances are fully known, but road accessibility (e.g., traffic delays
or construction feasibility) is only partially observed via noisy surveys.
State: Stadium coordinates S = {(x 1 , y 1 ), (x 2 , y 2 ), (x 3 , y 3 )}.
Observation: A noisy indicator of accessibility (e.g., “good” or “poor” with some
error rate).
Cost: Minimize expected distance, weighted by uncertain accessibility penalties.
Approach: Use SGD over the belief state. For each iteration:
Observe noisy accessibility data.
Update belief about each site’s true cost.
Adjust coordinates with a noisy gradient step.
Let’s explore online local search, a dynamic and adaptive approach to optimization
that’s particularly relevant when the environment evolves over time or information is
revealed incrementally. As an AI expert, I’ll frame this in the context of our prior
discussions—building on uncertain actions and partially observed environments—
while highlighting its unique characteristics and practical implications.
This mirrors scenarios like robotics (navigating unknown terrain), online resource
allocation, or even our Morocco stadium problem if city data or construction feedback
trickles in over time.
Key Features
Incremental Information: You don’t have the full cost function or state space
upfront—observations arrive as you act.
Real-Time Decisions: Moves are made before the problem is fully defined,
balancing immediate gains with future adaptability.
Feedback-Driven: The algorithm uses outcomes of past actions to refine its
strategy, often under time pressure.
Advantage: Naturally fits problems with continuous variables and streaming data.
Limitation: Assumes the cost function’s structure is somewhat stable over time.
Connection to Partial Observability and Uncertainty
Online local search often assumes a partially observed environment by default. New
observations refine your understanding of the state space. It also overlaps with
uncertain actions:
Partial Observability: You act based on a belief state that updates with each
observation (e.g., a robot learns the map as it moves).
Uncertain Actions: Outcomes are probabilistic, but in online settings, you don’t
precompute expectations—you observe and react.
The key difference is time: Online search doesn’t wait to model the full distribution; it
acts and learns concurrently.
Scenario: You’re placing three stadiums, but city population data and road
conditions are revealed one city at a time as surveys complete.
State: Current coordinates S t = {(x 1 , y 1 ), (x 2 , y 2 ), (x 3 , y 3 )}.
Action: Adjust one stadium’s position slightly.
Feedback: At time t, learn the exact distance and accessibility for city t.
Cost: Minimize total distance to known cities, anticipating future data.
Algorithm: Online gradient descent:
1. Start with random S 0 .
2. For each city t = 1 to 9:
Observe city t’s coordinates and accessibility.
Compute a partial gradient based on known cities.
Update S t with a small step.
3. Output final S 9 .