Optimization vs. CSP: Choosing the Right Tool

Unit 4: Optimization and Constraint Satisfaction — Section 4.6

An airline needs to assign crews to hundreds of flights, respecting labor regulations, certification requirements, and rest periods — while minimizing total cost. A chip designer needs to place thousands of components on a silicon wafer to minimize signal delay. A hospital administrator needs to assign nurses to shifts across a week while honoring union rules and staff preferences.

These problems are all solvable with techniques from this unit. But which technique? Knowing when to use optimization (hill climbing, simulated annealing) versus constraint satisfaction (backtracking, AC-3) is a practical skill that requires understanding the structure of each approach.

The Core Distinction

The fundamental question is: what does "success" look like for this problem?

Optimization problems have graded solutions — some solutions are better than others, and the goal is to find the best one. There is no concept of "satisfying all requirements"; instead, every solution has a score, and higher is better.

CSPs have binary solutions — a solution either satisfies all constraints or it does not. The goal is to find any valid assignment, not to rank solutions.

Aspect Local Search / Optimization CSP / Backtracking

Goal

Maximize or minimize an objective function

Satisfy all hard constraints

Solution quality

Graded (better/worse scores)

Binary (valid/invalid)

Solution guarantee

Usually approximate; SA approaches optimal

Complete — finds solution if one exists

Search method

Iterative improvement from a single state

Systematic assignment with backtracking

Memory usage

O(1) for hill climbing / SA; O(population) for GA

O(assignment depth)

Best for

Smooth or continuous problems; "good enough" is acceptable

Structured problems with hard constraints; correctness is required

A Decision Framework

Use this sequence of questions to choose an approach:

Choosing Between Optimization and CSP:

  1. Do you have hard constraints that every solution must satisfy?

    • Yes → lean toward CSP

    • No → lean toward optimization

  2. Is there a clear quality score to maximize or minimize?

    • Yes → optimization is natural

    • No (just "valid" vs. "invalid") → CSP is natural

  3. How large is the state space?

    • Continuous or astronomically large → local search / SA

    • Discrete with structured constraints → CSP with backtracking

  4. Is an approximate answer acceptable, or must you find a provably correct solution?

    • Approximate is fine → optimization / SA

    • Must be correct → backtracking CSP

  5. Does the problem decompose into independent sub-problems?

    • Yes (disconnected constraint graph) → CSP can exploit decomposition

    • No (fully connected) → local search may be more practical

  6. Is the solution time-critical?

    • Real-time required → SA or hill climbing (anytime algorithms)

    • Batch processing → backtracking with MAC

When CSP Wins: Structured Constraint Problems

University Course Scheduling

A university schedule must assign every course to a time slot and room.

  • One instructor cannot teach two classes simultaneously

  • Room capacity must exceed enrollment

  • Students in sequential required courses must not face a conflict

  • Every course must be assigned exactly once

This is a textbook CSP. Every constraint is a hard requirement — a schedule with a single room conflict is invalid, not just "slightly worse." Backtracking with MRV and AC-3 can find valid schedules for hundreds of courses in seconds. A local search algorithm would struggle because moving a single class to fix one conflict might create another, and there is no smooth gradient to follow.

Sudoku and Logic Puzzles

Sudoku’s all-different constraints, discrete domains, and rich constraint graph make it the ideal CSP. AC-3 preprocessing alone can solve easy puzzles by reducing all domains to single values. MRV + LCV + MAC handles hard puzzles efficiently. There is no "approximately solved" Sudoku — either all constraints are satisfied or the puzzle is unsolved.

Hardware and Software Configuration

Modern computers offer dozens of compatibility constraints: CPU socket type must match motherboard, total power draw must not exceed the power supply, RAM type must be supported. Selecting a valid combination of components is a CSP. Each constraint is hard (an incompatible build doesn’t work), and the goal is finding any valid configuration, possibly filtered by price afterward.

When Optimization Wins: Continuous or Scored Problems

Traveling Salesman Problem

Finding the optimal tour through 100 cities is NP-hard — no known algorithm can guarantee the optimal in polynomial time. But finding a near-optimal tour is exactly what SA excels at. SA treats tour quality as a continuous objective, accepts small detours to escape local optima, and consistently finds tours within 1–3% of optimal.

There is no natural CSP formulation for TSP — there are no hard constraints beyond visiting each city exactly once, and satisfaction of that alone does not indicate a good solution.

Neural Network Training

Adjusting millions of weights to minimize prediction error is a continuous optimization problem. The objective function is differentiable, the state space is real-valued, and "good enough" accuracy is the goal. No constraint framework applies here — gradient-based optimization (or SA for non-differentiable functions) is the right tool.

Engineering Design

Designing an antenna shape to maximize signal strength, or a bridge structure to minimize material while meeting safety specifications, involves continuous parameters and a quality metric. Local search or SA navigates the continuous design space effectively.

Hybrid Approaches

Real problems often combine both characteristics. Several hybrid algorithms have emerged:

CSP with Optimization: Two-Phase Solving

First, use CSP techniques to find any valid solution. Then, use local search to improve that solution by relaxing soft constraints or minimizing a secondary objective.

University scheduling uses this approach: first find a schedule with no hard violations, then optimize instructor preferences and student convenience.

Min-Conflicts: Local Search for CSPs

Min-conflicts is a local search algorithm specifically designed for CSPs. It starts with a random (possibly invalid) assignment and repeatedly selects a variable that is involved in a constraint violation, reassigning it to the value that minimizes the total number of conflicts.

Min-Conflicts

A local search algorithm for CSPs that starts with a random complete assignment, then iteratively reassigns variables to minimize constraint violations. Extremely effective on highly constrained problems where most assignments are nearly valid.

Min-conflicts solved million-variable n-queens problems that backtracking could not handle. It is the algorithm of choice for large, tightly constrained real-world scheduling.

Weighted CSPs and Soft Constraints

Many real problems have both hard constraints (must satisfy) and soft constraints (prefer to satisfy, but can violate at a cost). Weighted CSPs assign a cost to each constraint violation; the goal becomes minimizing total cost rather than achieving zero violations. This blends CSP formulation with optimization objectives.

Employee Shift Scheduling:

Hard constraints (CSP): * Each shift must have at least one qualified employee * No employee works more than 40 hours per week * Labor law requires 8 hours between consecutive shifts

Soft constraints (optimization): * Minimize total overtime cost * Honor employee day-off requests when possible * Balance workload fairly

A complete solver handles hard constraints with backtracking and soft constraints with a local search objective function — a hybrid of both paradigms.

Summary: Matching Problems to Approaches

Problem Type Recommended Approach Why

Discrete variables, hard constraints, correctness required

Backtracking CSP + AC-3 + MRV/LCV

Complete; exploits constraint structure

Large continuous space, approximate answer acceptable

Simulated Annealing

Escapes local optima; memory-efficient

Smooth unimodal landscape

Hill Climbing + random restart

Fast; no parameter tuning needed

Complex encoding, natural building-block structure

Genetic Algorithm

Population diversity; combines partial solutions

Large, tightly constrained, near-complete assignments

Min-Conflicts

Local search on near-valid solutions is fast

Mixed hard + soft constraints

Two-phase or weighted CSP

Hard constraints via backtracking; soft constraints via optimization

The Google Maps routing problem finds the shortest path between two locations — this is search (Unit 3). But Google also optimizes the routes of thousands of delivery vehicles to minimize total fuel cost — this is optimization. And Google assigns data center workloads to servers while respecting capacity and fault-tolerance constraints — this is CSP.

All three paradigms live inside the same company, often in the same system. The skill is recognizing which paradigm applies to each sub-problem.

Can you identify a problem in your daily life that would benefit from each approach?

No single algorithm solves all problems well. Local search is fast and memory-efficient but approximate; backtracking CSPs are complete but computationally demanding on large problems. The art of applied AI is matching the right algorithm to the problem structure — and recognizing when a hybrid approach can get the best of both worlds. After completing this unit, you now have a toolkit: hill climbing for smooth problems, simulated annealing for rugged landscapes, and backtracking with AC-3 and smart heuristics for structured constraint problems.

Apply the decision framework to classify real-world problems.


Based on the UC Berkeley CS 188 Online Textbook by Nikhil Sharma, Josh Hug, Jacky Liang, and Henry Zhu, licensed under CC BY-SA 4.0.

This work is licensed under CC BY-SA 4.0.