Optimization Problems in AI
Optimization Problems
🧭 Overview of Optimization
Optimization is about choosing the best option from a set of possible options. This contrasts with previous topics like classical search (pathfinding from an initial state to a goal) or adversarial search (game-playing). In optimization, the focus is on finding the best solution itself, not necessarily the path to get there.
Two common ways to frame optimization:
Objective function: a function that measures how good a state is; we aim to maximize its value.
Cost function: a function that measures the cost (distance, time, money) of a state; we aim to minimize its value.
🧗 Local Search
What Makes Local Search Different
Previous search algorithms (BFS, A*) maintain many paths simultaneously. Local search maintains only a single node (current state) and moves to a neighboring node. It is most useful when:
The path to the solution does not matter.
The challenge is figuring out what the goal (solution) looks like, not how to reach it.
State-Space Landscape
A state-space landscape visualizes all possible states. Each vertical bar represents a state; its height represents the value of the objective function (or cost function). Goals:
Global maximum: a state with the highest possible value (maximize objective).
Global minimum: a state with the lowest possible cost (minimize cost).
Objective function: a function that measures the quality of a state (to maximize).
Cost function: a function that measures the undesirability of a state (to minimize).
Hill Climbing
Hill climbing is the simplest local search algorithm for maximization. Starting from an initial state, repeatedly move to the highest-valued neighbor until no neighbor is better (reaching a peak). For minimization, move to the lowest-valued neighbor.
Pseudocode (maximization):
function HillClimb(problem):
current = initial state of problem
repeat:
neighbor = highest-valued neighbor of current
if neighbor is not better than current:
return current
current = neighborExample: Placing Hospitals
Imagine a grid with houses and two hospitals. The cost is the sum of Manhattan distances from each house to its nearest hospital. A neighbor is defined as moving one hospital one square left, right, up, or down. Starting from an initial configuration (cost 17), hill climbing finds improvements:
Move hospital left → cost 15 → down → 13 → left → 11. At cost 11, no neighbor improves it, so the algorithm stops.
However, a better arrangement exists (cost 9) that requires moving a hospital diagonally (not a neighbor). The algorithm missed the global minimum because it got stuck at a local minimum — a state better than its neighbors but not the best overall.
Limitations of Hill Climbing
Local maxima/minima: a state better (for maximization) or worse (for minimization) than all neighbors, but not globally optimal.
Plateaus: flat regions where neighbors have equal value. Two types:
Flat local maximum: all neighbors same value (no progress).
Shoulder: flat area that eventually leads upward/downward (slow progress).
Ridges: narrow peaks that require non-adjacent moves.
Variants of Hill Climbing
Steepest-ascent hill climbing: chooses the best neighbor (highest value) among all neighbors.
Stochastic hill climbing: chooses randomly from all better neighbors.
First-choice hill climbing: moves to the first better neighbor found (efficiency gain).
Random-restart hill climbing: runs hill climbing multiple times from random initial states, keeping the best result. Reduces risk of local optima.
Local beam search: keeps track of k current states (not just one), generating all neighbors and selecting the k best. Explores more broadly than single-state hill climbing.
Simulated Annealing
Simulated annealing is inspired by the physical process of annealing (heating then cooling a material). It allows occasional moves to worse states, especially early on, to escape local optima. Over time (as "temperature" decreases), the probability of accepting worse moves decreases.
Algorithm outline:
function SimulatedAnnealing(problem, max):
current = initial state
for t = 1 to max:
temperature = T(t) # decreases over time
neighbor = random neighbor of current
deltaE = neighbor.value - current.value
if deltaE > 0:
current = neighbor
else:
# accept worse move with probability e^(deltaE / temperature)
if random() < e^(deltaE / T):
current = neighbor
return currentTemperature schedule T(t): higher early → more random moves; lower later → fewer random moves. A common schedule: T(t) = remaining time / max time.
deltaE (change in "energy"): positive means neighbor is better, negative means worse. The acceptance probability for worse moves uses eΔE/T, which is high when ΔE is small (neighbor not much worse) or T is high (early in search).
Simulated annealing reduces the risk of getting stuck in local optima and often finds good solutions for large state spaces.
Applications of Local Search
Traveling Salesman Problem (TSP): find the shortest route visiting all cities and returning to the start. A state is a complete route; a neighbor can be formed by swapping two edges (2-opt move). Hill climbing or simulated annealing can find good approximations, even though TSP is NP-complete.
Facility location: placing hospitals, warehouses, etc., to minimize distance to clients.
📐 Linear Programming
Linear programming (LP) optimizes a linear cost function (to minimize) or objective (to maximize) subject to linear constraints (inequalities or equalities) and variable bounds.
General form:
Minimize c1x1+c2x2+⋯+cnxn
Subject to:
a11x1+a12x2+⋯+a1nxn≤b1
a21x1+a22x2+⋯+a2nxn≤b2
⋮
x1,x2,…,xn≥0 (or other bounds)
All expressions are linear (no x2, x, etc.). Constraints can be ≤, =, or ≥ (the latter can be flipped by multiplying by −1).
Example: Factory production
Machine X1 costs 50/hr, X2 costs 80/hr. Minimize total cost: 50x1+80x2.
Labor: X1 needs 5 units/hr, X2 needs 2 units/hr, total available 20 units: 5x1+2x2≤20.
Output: X1 produces 10 units/hr, X2 produces 12 units/hr, need at least 90 units: 10x1+12x2≥90. Convert to ≤: −10x1−12x2≤−90.
Solution algorithms:
Simplex method: one of the first and most popular.
Interior-point methods: efficient for large problems.
Libraries (e.g., SciPy in Python) can solve LPs given coefficients for the cost function, inequality constraints, and bounds.
🔗 Constraint Satisfaction Problems (CSPs)
A constraint satisfaction problem consists of:
Variables: X1,X2,…,Xn
Domains: for each variable, a set of possible values (e.g., {Monday,Tuesday,Wednesday}).
Constraints: restrictions on combinations of variable values (e.g., X1=X2).
Types of Constraints
Unary constraints: involve a single variable, e.g., A=Monday.
Binary constraints: involve two variables, e.g., A=B.
Hard constraints: must be satisfied for a valid solution.
Soft constraints: preferences (can be violated; often used in optimization).
Examples of CSPs:
Exam scheduling: variables = courses, domain = exam days, constraints = no student has two exams on the same day.
Sudoku: variables = empty cells, domain = 1–9, constraints = no duplicate in row, column, or 3×3 box.
Constraint Graph
Variables are nodes; a binary constraint between two variables is an edge. Unary constraints are not usually shown in the graph but are handled separately.
Consistency Techniques
Node Consistency
A variable is node-consistent if every value in its domain satisfies all unary constraints on it. A CSP is node-consistent if all variables are node-consistent.
Enforcing node consistency: remove values that violate unary constraints.
Arc Consistency
A variable X is arc-consistent with respect to another variable Y if, for every value x in X's domain, there exists a value y in Y's domain such that the binary constraint between X and Y is satisfied. (Directed: X→Y)
Revise function: removes values from X's domain that have no consistent Y.
AC-3 algorithm: enforces arc consistency across the entire CSP.
Start with a queue of all arcs (edges with binary constraints).
While queue not empty:
Dequeue arc (X,Y).
If Revise(X,Y) returns True (domain of X changed):
If X's domain becomes empty → return failure.
For each neighbor Z of X except Y, enqueue (Z,X).
Return success.
AC-3 can reduce domains significantly but may not solve the problem completely; search may still be needed.
Backtracking Search
Backtracking search is a depth-first search specialized for CSPs: assign variables one by one, checking consistency; if no value works, backtrack.
Pseudocode (basic):
function Backtrack(assignment, csp):
if assignment is complete: return assignment
var = SelectUnassignedVariable(csp, assignment)
for value in OrderDomainValues(var, assignment, csp):
if value is consistent with assignment:
add {var = value} to assignment
result = Backtrack(assignment, csp)
if result ≠ failure: return result
remove {var = value} from assignment
return failureImproving Backtracking with Inference (Maintaining Arc Consistency)
Every time a new assignment is made, enforce arc consistency using AC-3 on a limited set of arcs (all arcs involving the assigned variable). This propagates constraints and can reduce domains or even deduce necessary assignments, reducing backtracking.
Heuristics for Variable and Value Ordering
Selecting the next variable:
Minimum Remaining Values (MRV): choose the variable with the smallest domain (fewest possible values). This prunes search quickly.
Degree heuristic: if MRV ties, choose the variable with the highest degree (most constraints with other variables). This constrains many neighbors, reducing future options.
Ordering values for a selected variable:
Least Constraining Value (LCV): choose the value that rules out the fewest options for neighboring variables. This leaves the most flexibility, making it more likely to find a solution without backtracking.
Example: Exam Scheduling CSP
Variables: A, B, C, D, E, F, G (courses)
Domain: {Monday, Tuesday, Wednesday}
Constraints: students taking multiple courses create binary inequality constraints (no two exams same day).
Using backtracking without heuristics, one explores many dead ends. With MRV and LCV, plus maintaining arc consistency, the problem can be solved with little or no backtracking.
🧩 Takeaways
Optimization problems are widespread. Three major approaches covered:
Local search (hill climbing, simulated annealing): good when the path is irrelevant and only the solution matters; can get stuck in local optima, but variants and annealing help.
Linear programming: efficient for problems with linear objectives and linear constraints; solved by simplex or interior-point methods.
Constraint satisfaction: solving for variable assignments under constraints; uses backtracking search enhanced with consistency enforcement (AC-3) and heuristics (MRV, degree, LCV).
By formulating a problem as one of these types, existing algorithms can be applied to find solutions efficiently, even for computationally hard problems like TSP or scheduling.

Ask anything
We've read this whole study set, so questions about it get the best answers.