CP-SAT Rostering › Formula
Machine Learning from ScratchAll chapters CP-SAT Rostering › FormulaOne section · Sections in this chapter2 / 8 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Rostering comes down to a series of yes-or-no questions: should Alice work the morning shift on Monday? Should Bob cover the night shift on Friday? Each of these questions demands a binary answer. There's no such thing as "half an assignment." This fundamental observation leads us directly to the mathematical structure that makes CP-SAT rostering effective for this type of problem.
The Foundation: Binary Decision Variables
Imagine you're a manager creating next week's schedule. For every possible combination of employee, shift type, and day, you face a simple decision: assign them or don't. There are no shades of gray. An employee either works a shift or they don't. This binary nature of rostering decisions is precisely what we capture mathematically.
We represent each possible assignment as a binary decision variable:
where:
- : the set of all employees (indexed by )
- : the set of all shift types, such as Morning, Afternoon, Evening, or Night (indexed by )
- : the set of all time periods, typically days of the week (indexed by )
- : binary decision variable equal to 1 if employee is assigned to shift in time period , and 0 otherwise
Why binary variables? Because rostering is inherently discrete. You cannot assign someone to "0.7 of a shift" or "part-time coverage." The binary representation forces our model to make clear, unambiguous decisions that match the reality of workforce scheduling. This mathematical choice isn't arbitrary. It directly reflects the nature of the problem we're solving.
Even a small roster produces an explosion of binary decisions. With 5 employees working across 4 shift types over 7 days, you're making independent yes-or-no decisions. Each decision variable can be either 0 or 1, giving a solution space with possible combinations. This is an astronomically large number that makes brute-force search completely impractical.
To visualize the structure of these decision variables, imagine a three-dimensional space whose dimensions represent the employee, shift type, and time period of an assignment. Each point in this space corresponds to a binary decision variable that can take the value 0 (not assigned) or 1 (assigned).

Bringing Order to Chaos: Constraints
Having decision variables is just the beginning. Without rules, we could assign anyone to any shift at any time, but that would create chaos. Real-world rostering typically needs to satisfy multiple competing requirements simultaneously. These requirements become constraints in our mathematical model: rules that any valid solution should obey.
Think of constraints as the guardrails that keep our solution on track. They transform an impossibly large space of potential assignments into a manageable set of feasible solutions. Each constraint type addresses a different real-world concern, and together they ensure our roster is both operationally viable and legally compliant.
Coverage Constraints: The most fundamental requirement is that every shift should be staffed. Imagine a hospital emergency room that needs at least 3 nurses on the night shift, or a restaurant that requires 2 servers during dinner rush. Coverage constraints ensure we meet these minimum staffing levels:
This formula says: for every shift type and every time period , the sum of all assignments (where ) should equal the required number of employees . The summation counts how many employees are assigned. We're adding up the ones. Why equality rather than "at least"? Because we can also model "exactly employees" if needed, though in practice you might use to allow overstaffing.
where:
- : the minimum number of employees required for shift in time period (also called the staffing requirement)
This constraint is non-negotiable. If coverage isn't met, operations fail. But coverage alone isn't enough. We also need to respect the reality that employees have lives outside work.
Employee Availability: People can't work when they're unavailable. Alice might have classes on Tuesday mornings, Bob might be on vacation, or Charlie might have childcare responsibilities. Availability constraints prevent impossible assignments:
where:
- : the set of all infeasible assignments where employee is unavailable for shift in period (a subset of )
By setting for all , we're telling the solver: "These assignments are off-limits. Don't even consider them." This significantly reduces the search space and helps prevent schedules that violate employee availability. The constraint applies to each infeasible triple individually, effectively removing those decision variables from consideration.
Maximum Shifts per Employee: Fairness matters. We can't have one employee working 10 shifts while another works 2. Maximum shift constraints ensure balanced workloads:
where:
- : the maximum number of shifts employee can work during the planning period (a non-negative integer, typically set based on labor regulations or organizational policies)
The summation counts all assignments for employee across all shifts and time periods. This sum equals the total number of shifts assigned to employee (since each is either 0 or 1). By requiring this sum to be , we cap each employee's total workload. This constraint prevents burnout and ensures compliance with labor regulations that limit working hours.
Rest Periods: Perhaps the most critical constraint for employee well-being is that people need rest. Working consecutive shifts without adequate recovery time is unsafe, illegal in many jurisdictions, and leads to poor performance. The rest period constraint prevents this:
where:
- : the time gap between consecutive shifts (measured in the same units as , typically hours or time periods)
- : the minimum required rest period between shifts (often 8-12 hours, measured in the same units as )
- : two different shift types (the constraint applies to any pair of shifts)
- : the time period of the first shift
- : the time period of the second shift, occurring time units after the first
The constraint works as follows: if an employee works shift at time (so ), and the time gap is less than the minimum rest period , then they cannot work shift at time (forcing ). The sum of these two binary variables cannot exceed 1, meaning at most one of them can be 1. This mathematical relationship directly encodes the physical reality that people need time to rest.
The rest period constraint can be visualized as a timeline showing how consecutive shift assignments are restricted. When an employee works a shift at time , they cannot work another shift at time if the time gap is less than the minimum rest period .

Finding the Best Solution: The Objective Function
Constraints tell us what's allowed, but they don't tell us what's best. Among all feasible solutions, and there may be thousands, which one should we choose? This is where the objective function comes in. It quantifies what "best" means for our specific situation.
Most commonly, we want to minimize costs. Every assignment has a cost: regular wages, overtime premiums, or penalties for assigning someone to an undesirable shift. The objective function sums these costs across all assignments:
where:
- : the cost of assigning employee to shift in period (a non-negative real number, typically measured in currency units or preference scores)
The binary nature of works well here: when (no assignment), the term contributes nothing to the sum. When (assignment made), the cost is added. The triple summation ensures we consider every possible assignment, but only the ones that occur (where ) contribute to the total cost.
The cost can encode various business priorities:
- Labor costs: Base wages for the shift
- Overtime premiums: Higher costs for shifts that push employees over their regular hours
- Preference penalties: Costs that discourage assigning employees to shifts they dislike
- Skill premiums: Costs that reflect the value of assigning highly skilled employees to critical shifts
By minimizing this sum, we find the schedule that satisfies all constraints while keeping total costs as low as possible. But cost isn't the only possible objective. We could maximize fairness, minimize schedule changes from previous periods, or balance multiple objectives simultaneously.
Understanding the Challenge: Mathematical Properties
Now that we've built our mathematical model, let's understand the scale of the problem we're solving. The properties we'll explore explain why sophisticated algorithms like CP-SAT are necessary and why naive approaches fail spectacularly.
Solution Space Size: The combinatorial explosion
With employees, shift types, and time periods, we have binary decision variables. Each variable can independently be 0 or 1, which means the total number of possible assignments is:
where:
- : the set of all possible solutions (before applying constraints)
- : the cardinality (size) of the employee set , i.e., the number of employees
- : the cardinality (size) of the shift type set , i.e., the number of shift types
- : the cardinality (size) of the time period set , i.e., the number of time periods
- : the total number of decision variables (the product of the three set sizes)
This exponential growth is the heart of the challenge. Consider a modest problem: 10 employees, 4 shift types, 7 days. That's decision variables, creating possible solutions. To put this in perspective, there are estimated to be atoms in the observable universe. We're dealing with numbers so large that exhaustive enumeration is computationally infeasible. Even if we could check a billion solutions per second, it would take longer than the age of the universe to explore them all.
This is why constraints are so powerful: they eliminate vast swaths of this solution space. But even with constraints, the remaining feasible space can be enormous. We need algorithms that can intelligently search this space without exploring every possibility.
The exponential growth of the solution space can be visualized to understand why exhaustive search becomes infeasible even for moderately-sized problems. As the number of decision variables increases, the solution space grows at an exponential rate, making it computationally intractable to explore all possibilities.

Constraint Satisfaction Structure: Putting it all together
We can now see the complete mathematical structure. The rostering problem is a constraint satisfaction problem (CSP): we're searching for assignments that satisfy all constraints simultaneously. Formally:
This formulation reveals the structure that CP-SAT exploits. Each constraint type operates on different subsets of variables:
- Coverage constraints link variables across employees for a given shift-time combination
- Availability constraints fix specific variables to zero
- Maximum shift constraints aggregate variables across shifts and times for each employee
- Rest period constraints create relationships between variables at different time points
The power of constraint programming comes from how these constraints interact. When the solver sets one variable, it can immediately infer consequences for related variables through constraint propagation. For example, if Alice is the only available employee for a shift that requires 2 people, the solver immediately knows the problem is infeasible, without exploring millions of other assignments.
Computational Complexity: Why sophisticated algorithms matter
The exponential solution space means that naive approaches, such as trying every combination or even random sampling, are doomed to failure. But the constraint structure gives us hope: by propagating constraints intelligently, CP-SAT can eliminate huge portions of the search space without explicitly exploring them. The solver uses techniques like:
- Constraint propagation: Inferring variable values from constraints
- Conflict-driven learning: Remembering why certain assignments failed to avoid repeating mistakes
- Branching heuristics: Choosing which variables to set first to maximize constraint propagation
These techniques transform an intractable search into a manageable optimization problem. For most practical rostering problems, CP-SAT can find optimal or near-optimal solutions in seconds to minutes, even when the underlying solution space contains trillions of possibilities.