Working example 02 Genetic algorithm

Conflicting operational constraints

“Every change to the schedule creates a problem somewhere else.”

A service centre open 06:00–22:00, seven days a week, must match staff to an uneven demand curve while respecting contracts, rest rules, availability and supervisor cover.

The business context

The problem and the solution

The business problem

A service centre needs enough people at busy times without paying for too much unused capacity. It also needs supervisors, suitable days off and enough rest between shifts. Changing one shift can create a gap elsewhere.

How the solution helps

The solution tries many weekly rotas, keeps the better ones and combines or adjusts them to search for improvements. It scores shortages, excess cover and broken rules so you can see the trade-offs. A lower score is better, but the search does not guarantee the best possible rota or that every rule will be satisfied.

Explore the evidence

The input dataset

Two synthetic tables describe hourly demand and the team. Shifts last eight hours and start at 06:00, 08:00, 10:00, 12:00 or 14:00. Each person should work five days, with 11 hours of rest between consecutive days’ shifts. The demo checks rest within the week, not across the Sunday-to-Monday boundary. One supervisor should cover every open hour. Changing team size updates the staff table; the controls below set search and scoring preferences.

All records are illustrative and generated in your browser. Browse every row using the table controls. Displayed decimals are rounded; calculations use full precision.

How it works

The method in three steps

  1. Encode a rosterEach chromosome holds a shift start (06, 08, 10, 12, 14) or a day off for every person and every day.genes = staff × 7 days, values ∈ {off, 06, 08, 10, 12, 14}
  2. Score every ruleUnderstaffing, overstaffing, rest periods under 11 hours, contracts other than five days, availability and supervisor gaps all add penalties.penalty = 10·under + w·over + 60·rest + 60·contract + 30·supervisor + 60·availability
  3. EvolveTournament selection, crossover of whole personal rosters, and mutations that move a day off or a start time. The two best rosters always survive.population 120 · up to 600 generations · elitism 2

Try it

The working example

Search settings

Run the algorithm and watch the roster improve.

Changing it restarts the search
Understaffing costs 10 per hour
Share of personal rosters altered per child
Generation0
Understaffed hours–
Overstaffed hours–
Rule breaches–

Search progress

Penalty of the best roster in each generation.

Staffing against demand

Rostered staff minus required staff, by day and hour.

Best roster found

Shift start and end times for each person.

StaffMonTueWedThuFriSatSun

Interpretation

From example to engagement

What the example shows

The hand-built roster is reasonable on paper — fixed shifts, fair patterns — yet misses demand peaks and breaks rest rules. The algorithm finds arrangements of the same people that no planner would think to try, and makes the trade-off between service and idle time explicit through the penalty weights.

With real data

  • Forecast demand by interval, contracts, skills, leave and preferences.
  • Hard legal rules kept as constraints; soft preferences as weighted penalties.
  • Alternatives: mixed-integer programming or constraint programming where proofs of optimality matter.