Press Ctrl+D to draw

Drawing Tools

Log in for saved annotations

1px

2.4  Constraints and Scheduling

Many engineering problems are naturally framed as assignment or scheduling tasks: assigning robots to jobs, scheduling manufacturing operations, booking shared lab equipment, or allocating resources to projects. While you could model these as state-space search problems, that approach often obscures the problem structure and makes algorithms inefficient. Constraint Satisfaction Problems (CSPs) provide a more natural and powerful framework.

2.4.1 Why CSPs?

Consider scheduling three robots to deliver five packages, each with a time window for pickup and dropoff. A search-based approach would explore paths through enormous state spaces—every possible sequence of assignments, pickups, and deliveries. But the core challenge is not how to reach a goal state; it's which assignments satisfy all constraints simultaneously.

CSPs shift focus from paths to assignments: finding values for variables that satisfy all constraints. This declarative approach:

  • Separates what from how: specify constraints, not search algorithms
  • Exploits problem structure: constraint propagation eliminates large swaths of invalid assignments without search
  • Scales better: specialized CSP solvers outperform generic search for many assignment problems
  • Supports incremental refinement: start with a feasible schedule, then optimize

Manufacturing engineers use CSP techniques for shop-floor scheduling, maintenance planning, and resource allocation. Understanding CSPs gives you both a modeling tool and access to powerful solvers.

2.4.2 CSP Fundamentals

Definition 2.15  Constraint Satisfaction Problem

A constraint satisfaction problem (CSP) consists of: - A set of variables \(X = \{X_1, X_2, \ldots, X_n\}\) - A set of domains \(D = \{D_1, D_2, \ldots, D_n\}\), where \(D_i\) is the set of possible values for \(X_i\) - A set of constraints \(C\) that specify allowable combinations of values

A solution is an assignment of values to all variables that satisfies every constraint. A CSP is satisfiable if at least one solution exists.

2.4.2.1 Example: Lab Equipment Booking

Suppose you manage a mechanical engineering lab with shared equipment: a 3D printer, a laser cutter, and a materials testing machine. Three project teams need to book equipment for specific time slots today:

  • Variables: \(X_1, X_2, X_3\) (equipment assignment for teams 1, 2, 3)
  • Domains: \(D_1 = D_2 = D_3 = \{\text{printer}, \text{laser}, \text{tester}\}\)
  • Constraints:
    • Each team needs different equipment (no conflicts)
    • Team 1 cannot use the laser (safety training incomplete)
    • Team 3 requires either the printer or tester (project constraints)

Formally: - \(X_1 \neq X_2\), \(X_1 \neq X_3\), \(X_2 \neq X_3\) (all-different constraint) - \(X_1 \neq \text{laser}\) (unary constraint) - \(X_3 \in \{\text{printer}, \text{tester}\}\) (unary constraint)

A valid solution: \(X_1 = \text{printer}\), \(X_2 = \text{laser}\), \(X_3 = \text{tester}\).

This toy example illustrates the CSP components. Real scheduling problems add temporal constraints, resource capacities, and preferences.

2.4.3 Types of Constraints

Constraints come in several flavors:

  • Unary constraints: restrict a single variable (\(X_1 \neq \text{laser}\))
  • Binary constraints: relate two variables (\(X_1 \neq X_2\))
  • Global constraints: involve many variables simultaneously
    • All-different: no two variables can have the same value
    • Resource capacity: total usage \(\leq\) available capacity
    • Precedence: task \(i\) must complete before task \(j\) starts

Most CSP solvers focus on binary constraints, but global constraints are crucial for engineering problems. For instance, an all-different constraint for robot assignments is more efficient than pairwise inequality checks.

CSPs are NP-complete in general, so we often use search with constraint propagation to find solutions efficiently.

2.4.4.1 Backtracking Search

The standard CSP algorithm is backtracking search: a depth-first search that assigns variables one at a time and backtracks when a constraint is violated.

Basic backtracking pseudocode:

function BACKTRACK(assignment, csp):
    if assignment is complete:
        return assignment
    var = SELECT-UNASSIGNED-VARIABLE(csp)
    for each value in ORDER-DOMAIN-VALUES(var, 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 failure

Without heuristics, backtracking degenerates to brute-force enumeration. The key is choosing which variable to assign next and which value to try first.

2.4.4.2 Variable Ordering Heuristics

Definition 2.16  Minimum Remaining Values (MRV)

The minimum remaining values heuristic selects the variable with the fewest legal values remaining in its domain. Also called the "most constrained variable" or "fail-first" heuristic.

Intuition: MRV prunes the search tree early by detecting failures fast. If a variable has no legal values, backtrack immediately rather than exploring doomed branches.

Definition 2.17  Degree Heuristic

The degree heuristic selects the variable involved in the most constraints with unassigned variables. Often used as a tie-breaker when multiple variables have the same MRV.

Intuition: Assign variables that constrain others most, reducing future branching.

2.4.4.3 Value Ordering Heuristics

Definition 2.18  Least Constraining Value (LCV)

The least constraining value heuristic tries values that rule out the fewest choices for neighboring variables first.

Intuition: LCV leaves maximum flexibility for future assignments, increasing the chance of finding a solution without backtracking.

2.4.4.4 Constraint Propagation

Rather than waiting for backtracking to detect conflicts, constraint propagation prunes domains in advance by enforcing local consistency.

Definition 2.19  Forward Checking

Forward checking propagates constraints after each assignment: whenever \(X_i = v\), remove inconsistent values from the domains of unassigned variables constrained by \(X_i\).

Forward checking detects some dead ends early, reducing backtracking. For example, if you assign \(X_1 = \text{printer}\) and \(X_1 \neq X_2\), forward checking removes "printer" from \(D_2\).

More sophisticated propagation algorithms like arc consistency (AC-3) recursively enforce consistency across all constraint arcs. AC-3 is more expensive per step but often reduces the search tree dramatically.

When problems are large and finding any solution is hard, local search methods can be effective. These algorithms start with a complete assignment (possibly violating constraints) and iteratively improve it.

Definition 2.20  Min-Conflicts Heuristic

The min-conflicts heuristic selects a variable involved in a constraint violation and reassigns it to the value that minimizes the number of violated constraints.

Algorithm outline:

function MIN-CONFLICTS(csp, max_steps):
    current = random complete assignment
    for i = 1 to max_steps:
        if current satisfies all constraints:
            return current
        var = random variable from the set of conflicted variables
        value = value for var that minimizes conflicts
        set var = value in current
    return failure

Min-conflicts is surprisingly effective for large scheduling problems and can find solutions in linear time on average for many problem classes Russell and Norvig, 2020, § 6.4. It is commonly used in real-world resource allocation systems.

Tradeoffs: Local search can get stuck in local minima and offers no completeness guarantees. For critical systems requiring provable solutions, use systematic search with propagation. For large, over-constrained problems, local search often finds good-enough solutions faster.

2.4.6 Scheduling Robots with CSPs

Let's return to the warehouse scenario from Chapter 1 and model a simplified multi-robot scheduling problem as a CSP.

2.4.6.1 Problem Setup

You have three robots and five delivery jobs, each with: - A pickup location \(p_j\) and dropoff location \(d_j\) - A time window \([e_j, l_j]\) (earliest start, latest finish) - An estimated duration \(t_j\) (travel + handling time)

Goal: Assign each job to a robot such that: - Every job is assigned to exactly one robot - No robot exceeds capacity (max 2 jobs per robot) - Time windows are satisfied - Robots return to charging stations between jobs if battery requires

2.4.6.2 CSP Formulation

  • Variables: \(R_1, R_2, R_3, R_4, R_5\) (robot assignment for jobs 1–5)
  • Domains: \(D_i = \{1, 2, 3\}\) (robot IDs)
  • Constraints:
    • Capacity: \(|\{j : R_j = k\}| \leq 2\) for each robot \(k\) (global constraint)
    • Time windows: if robot \(k\) does jobs \(i\) and \(j\) in sequence, \(t_i + \text{travel}(d_i, p_j) + t_j \leq l_j - e_i\)
    • Battery: cumulative distance for robot \(k \leq\) battery range

This formulation abstracts away path planning (handled by A* from the previous section) and focuses on high-level assignment decisions.

2.4.6.3 Implementation Sketch

We'll build a minimal CSP solver with backtracking, MRV, and forward checking. The code will:

  1. Represent the problem as variables, domains, and constraint functions
  2. Use MRV to select variables and LCV to order values
  3. Propagate constraints with forward checking
  4. Visualize the assignment and schedule

This is a didactic implementation; production systems use libraries like Google OR-Tools, Gurobi, or constraint programming languages. But building it from scratch clarifies how CSP solvers exploit structure.

Hands-On: Robot Scheduling CSP

Goal: Implement a backtracking CSP solver and apply it to robot job assignment.

  1. Define the CSP structure: create a CSP class with variables, domains, and constraints.
  2. Implement MRV variable selection and forward checking.
  3. Encode the robot scheduling problem with capacity and time window constraints.
  4. Run the solver and visualize the resulting schedule as a Gantt chart.
  5. Experiment: add a fourth robot or tighten time windows and observe backtracking behavior.

Reflection: How does forward checking reduce backtracking compared to naive backtracking? What happens when the problem becomes over-constrained?

2.4.7 Feasibility vs Optimality

CSPs distinguish between:

  • Feasibility: finding any solution that satisfies all constraints
  • Optimality: finding the best solution according to some objective (minimize makespan, total distance, etc.)

Standard CSP solvers focus on feasibility. To optimize, you can:

  1. Add constraints iteratively: find a feasible solution, add a constraint requiring better objective value, repeat
  2. Branch-and-bound: use backtracking with pruning based on objective bounds
  3. Use optimization frameworks: Mixed-Integer Programming (MIP), constraint programming with optimization (e.g., MiniZinc), or metaheuristics

For the robot scheduling problem, a feasible schedule might waste time. An optimal schedule minimizes total delivery time or energy use. We will revisit optimization in later chapters when we cover reinforcement learning and trajectory optimization.

2.4.8 When to Use CSPs vs Search vs Learning

Use CSPs when: - The problem is naturally an assignment or scheduling task - Constraints are explicit and structured - You need provable feasibility or want to exploit propagation

**Use state-space search (A*, UCS) when**: - The goal is to find a path or sequence of actions - Costs are additive and heuristics are available - The problem is small to medium scale

Use learning/optimization (later chapters) when: - The environment is stochastic or partially observable - Cost functions are unknown or learned from data - Real-time adaptation is required

Many engineering systems combine all three: use CSPs for high-level scheduling, A* for routing, and RL for low-level control. Understanding each tool's strengths helps you architect robust intelligent systems.

2.4.9 Practical Considerations

Real-world CSP applications require attention to:

  • Modeling fidelity: too abstract and solutions are infeasible; too detailed and solving is intractable
  • Dynamic updates: schedules change when tasks arrive or fail; support incremental repair
  • Partial solutions: sometimes finding 90% assignments is enough; stop early if solving is too slow
  • Soft constraints: preferences vs hard requirements; use weighted CSPs or multi-objective formulations

CSP solvers are workhorses in manufacturing, logistics, and resource management. They are less glamorous than machine learning but indispensable for engineered systems.

2.4.10 Summary

Constraint satisfaction problems provide a declarative framework for assignment and scheduling tasks common in engineering. By separating variables, domains, and constraints, CSPs enable:

  • Specialized search algorithms (backtracking with MRV/LCV/forward checking)
  • Constraint propagation to prune infeasible assignments early
  • Local search for large-scale, over-constrained problems
  • Integration with optimization for objective-driven solutions

You now have the foundations to formulate CSPs, choose appropriate algorithms, and apply them to engineering problems. The next section extends these ideas to multi-agent scenarios, where robot interactions require coordination beyond single-agent assignment.

Bibliography

  1. [AI] Russell, Stuart J. and Peter Norvig. Artificial intelligence: a modern approach. (2020) 4 ed. Prentice Hall. http://aima.cs.berkeley.edu/