Where the language model stops and the solver starts: shift plans with CP-SAT

A language model is good at understanding a planner's request and explaining a plan. It is bad at meeting forty rules at once. So we split the work: the model turns words into constraint changes, a CP-SAT solver computes the plan, and the model explains the result. This post shows the pattern, simplified.

Đorđe SpasojevićAI EngineerPublished 24 May 2026 · 2 min read · Manufacturing · Python · OR-Tools CP-SAT

01

Why not let the model plan

—

It breaks rules quietly.The plan looks right and puts someone on an early shift after a night, or an uncertified operator on a line.

—

It cannot prove anything.Not that a plan is the best one, and not that no plan exists.

—

It changes its mind.The same request gives a different plan on each run.

02

The split

01

The planner writes in words:"W07 is off Thursday. Line 2 needs a certified welder on nights all next week."

02

The model turns that into structured changes, checked against a schema before anything runs.

03

The solver computes the whole weekwith every rule, old and new.

04

The model explains the planand what changed, in the planner's words. A person approves it.

The model's output in step 2 is data, not a plan:

json
{"changes": [
  {"type": "unavailable", "worker": "W07", "day": "Thu"},
  {"type": "requires_skill", "line": "L2", "shift": "night",
   "skill": "welding_cert", "days": ["Mon", "Tue", "Wed", "Thu", "Fri"]}
]}

03

The solver

One line, one week, three shifts. Every rule is a constraint; fairness is the objective.

python
from ortools.sat.python import cp_model

DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
SHIFTS = ["early", "late", "night"]

def plan(workers, skills, needs, unavailable):
    """skills[w]: set of skills; needs[(day, shift)]: skill required or None;
    unavailable: set of (worker, day)."""
    m = cp_model.CpModel()
    x = {(w, d, s): m.NewBoolVar(f"{w}_{d}_{s}")
         for w in workers for d in DAYS for s in SHIFTS}

    for d in DAYS:
        for s in SHIFTS:
            skill = needs.get((d, s))
            ok = [w for w in workers if skill is None or skill in skills[w]]
            m.AddExactlyOne([x[w, d, s] for w in ok])      # one qualified person per shift
            for w in workers:
                if w not in ok:
                    m.Add(x[w, d, s] == 0)                  # nobody unqualified

    for w in workers:
        for d in DAYS:
            m.AddAtMostOne([x[w, d, s] for s in SHIFTS])    # one shift a day
            if (w, d) in unavailable:
                for s in SHIFTS:
                    m.Add(x[w, d, s] == 0)
        for i in range(len(DAYS) - 1):                      # no early shift after a night
            m.AddImplication(x[w, DAYS[i], "night"], x[w, DAYS[i + 1], "early"].Not())

    load = {w: m.NewIntVar(0, len(DAYS), f"load_{w}") for w in workers}
    for w in workers:
        m.Add(load[w] == sum(x[w, d, s] for d in DAYS for s in SHIFTS))
    peak = m.NewIntVar(0, len(DAYS), "peak")
    m.AddMaxEquality(peak, list(load.values()))
    m.Minimize(peak)                                        # spread the shifts evenly

    solver = cp_model.CpSolver()
    solver.parameters.max_time_in_seconds = 10
    status = solver.Solve(m)
    if status not in (cp_model.OPTIMAL, cp_model.FEASIBLE):
        return None                                         # no plan meets every rule
    return [(w, d, s) for (w, d, s), v in x.items() if solver.Value(v)]

04

When no plan exists

The model must never invent one. The solver can name the requests that cannot all hold at once (CP-SAT assumptions), and the model turns that into a sentence the planner can act on: "Line 2 needs a certified welder on Thursday night, and both are off."

05

Where it fits

This is a Level 4 build: it reads people, skills and line needs from the plant’s own systems, and a person approves every plan before it goes out. The solver does the part that has to be right; the model does the part that has to be understood.

Build Notes · for engineersMore in Insights →

Bring the system you’re trying to make work.

Schedule a call with a partnerFree until 11 December