OR-Toolsml

Why Google OR-Tools

Mathematical optimization for real-world constraints

v1.1·11 min read·Kenneth Pernyér
or-toolsoptimizationschedulingroutingoperations-research

The Problem

Business operations are full of optimization problems: scheduling, routing, resource allocation, inventory planning. These problems have constraints (capacity, deadlines, dependencies) and objectives (minimize cost, maximize throughput).

Brute force doesn't scale.

A scheduling problem with 20 tasks and 5 resources has millions of possible assignments. A routing problem with 50 stops has more permutations than atoms in the universe. You can't enumerate all possibilities.

Machine learning doesn't help either. ML learns patterns from data; optimization requires satisfying constraints exactly. An ML model might suggest a schedule that violates capacity limits or misses deadlines.

We needed tools that:

  • Find optimal or near-optimal solutions to constraint satisfaction problems
  • Scale to real-world problem sizes
  • Handle hard constraints (must satisfy) and soft constraints (prefer to satisfy)
  • Integrate with our existing tech stack

Current Options

OptionProsCons
HeuristicsHand-coded rules and approximations.
  • Simple to implement
  • Fast execution
  • Easy to understand and debug
  • Quality varies by problem
  • Difficult to handle complex constraints
  • Doesn't guarantee optimality
  • Brittle to requirement changes
Google OR-ToolsOpen-source optimization suite. CP-SAT, routing, linear programming.
  • Handles complex constraints naturally
  • Proves optimality when possible
  • Multiple solver backends
  • Active development, good docs
  • Learning curve for modeling
  • Can be slow on very large problems
  • Python-first (bindings for other languages)
Commercial Solvers (Gurobi, CPLEX)Enterprise optimization with maximum performance.
  • Fastest solvers available
  • Enterprise support
  • Handles massive scale
  • Expensive licensing
  • Overkill for many problems
  • Vendor lock-in

Future Outlook

Optimization is becoming infrastructure, not specialty.

The tools are more accessible than ever.

OR-Tools makes constraint programming approachable. You describe the problem; the solver finds solutions. The abstraction level has risen from "implement Simplex" to "express constraints."

AI and optimization are converging. LLMs can help formulate optimization problems. Learned heuristics can warm-start solvers. The combination is more powerful than either alone.

The future is optimization as a service—APIs that take problem descriptions and return solutions. OR-Tools is the engine behind many such services.

Our Decision

Why we chose this

  • Constraint satisfactionExpress complex rules; solver ensures they're satisfied
  • Proven optimalityKnow when you have the best possible solution
  • Declarative modelingDescribe the problem, not the solution algorithm
  • Open sourceNo licensing costs; active community

×Trade-offs we accept

  • Learning curveConstraint modeling requires new mental models
  • Performance variabilitySome problems solve in seconds, others take hours
  • Integration effortTranslating business rules to constraints takes care

Motivation

Scheduling, routing, and resource allocation are core to our operations. Getting these wrong means wasted resources, missed deadlines, and unhappy customers.

Heuristics got us started, but they don't scale. As operations grew more complex, our hand-coded rules produced increasingly poor results. We needed mathematical rigor.

OR-Tools gives us that rigor without enterprise pricing. We model constraints in code, and the solver finds solutions that satisfy all requirements. When requirements change, we update constraints, not algorithms.

Recommendation

Start with OR-Tools' CP-SAT solver for constraint satisfaction and discrete optimization. It handles most scheduling and allocation problems well.

Modeling approach:

  1. Define decision variables: What choices are you making?
  2. Express constraints: What rules must be satisfied?
  3. Define objective: What are you optimizing?
  4. Solve and interpret: Extract solution, handle infeasibility

For vehicle routing, use OR-Tools' dedicated routing library. It has built-in support for time windows, capacity, and other common constraints.

Test with small instances first. Optimization problems can be sensitive to formulation; a different model of the same problem may solve much faster.

Examples

scheduling/solver.pypython
from ortools.sat.python import cp_model

def schedule_tasks(tasks, resources, deadlines):
    model = cp_model.CpModel()

    # Decision variables: when does each task start?
    starts = {}
    ends = {}
    for task in tasks:
        starts[task.id] = model.NewIntVar(0, max_time, f'start_{task.id}')
        ends[task.id] = model.NewIntVar(0, max_time, f'end_{task.id}')
        model.Add(ends[task.id] == starts[task.id] + task.duration)

    # Constraint: tasks on same resource don't overlap
    for resource in resources:
        resource_tasks = [t for t in tasks if t.resource == resource.id]
        intervals = [
            model.NewIntervalVar(starts[t.id], t.duration, ends[t.id], f'interval_{t.id}')
            for t in resource_tasks
        ]
        model.AddNoOverlap(intervals)

    # Constraint: respect deadlines
    for task in tasks:
        if task.id in deadlines:
            model.Add(ends[task.id] <= deadlines[task.id])

    # Objective: minimize total completion time
    model.Minimize(max(ends[t.id] for t in tasks))

    # Solve
    solver = cp_model.CpSolver()
    status = solver.Solve(model)

    if status == cp_model.OPTIMAL:
        return {t.id: solver.Value(starts[t.id]) for t in tasks}
    return None

CP-SAT solver handles scheduling constraints declaratively. Add constraints, define objective, and the solver finds the optimal assignment.

Related Articles

Stockholm, Sweden

Version 1.1

Kenneth Pernyér signature