A factory floor changes every minute: new orders arrive, machines finish jobs, priorities shift. Each time a machine becomes free, someone must decide which waiting job goes next. Simple dispatching rules make these decisions instantly, but designing a good rule by hand is hard. This tutorial shows how genetic programming (GP) can learn such rules automatically from simulation. It assumes no background in scheduling, evolutionary computation, GP or hyper-heuristics. Every idea comes with a demo you can play with, and every demo has a Show Python code button with a complete, commented program you can copy and run.

~50 minreading and playing time
6interactive demos
Pythoncode for every demo, ready to copy and run
Beginnerno scheduling or GP background needed
1–2The problemDynamic job shop scheduling and dispatching rules
3–4The ideaHyper-heuristics: let GP design the rule
5–9The designFramework, terminals & functions, fitness, training instances, evolution
10The payoffTest on unseen instances and deploy

1. Job Shop Scheduling in Two Minutes

A job shop has machines and jobs. Each job is a sequence of operations that must be processed in a given order (its route), each on a specific machine for a given processing time. Different jobs have different routes. A machine processes at most one operation at a time, without interruption, and operations wait in a queue in front of a busy machine.

In dynamic job shop scheduling (DJSS), new jobs arrive over time, and their arrival times and attributes are unknown until they arrive. A complete schedule cannot be computed in advance, so decisions are made in real time, each time a machine becomes idle. Common objectives (to be minimised) are:

  • Mean flowtime: the average time a job spends in the shop, from arrival to completion.
  • Mean weighted tardiness: the average of each job's weight times its tardiness, where tardiness is how long after its due date the job completes (zero if on time).

New to job shops? The Job Shop Scheduling tutorial lets you build schedules by hand, explains why the problem is hard, and runs a live dynamic shop.

Jobs arrive at random times, unknown in advance timeJ5route M2→M1→M3J6route M1→M3J7route M3→M2 ?future A new job joins the queue of its first machine; after each operation it joins the queue of its next one. waiting queuemachineJ2J3 M1processing J1 J1 then joinsthe queue of M3J4 M2processing J5 busy until itsoperation endsJ7J6J9 M3idle Decision point:which job starts next? After its last operation, a job leaves the shop. Flowtime = completion time − arrival time.Tardiness = max(0, completion time − due date).
A dynamic job shop: stochastic job arrivals, job-specific routes, machine queues, and a decision whenever a machine becomes idle.

2. Dispatching Rules

A dispatching rule makes those decisions. It is a priority function: it computes a priority value for every waiting operation from features of the operation, the job and the shop. The operation with the smallest priority starts first. For example, SPT (shortest processing time) uses the priority PT, so short operations go first, and EDD (earliest due date) uses the time left until the job's due date.

Whenever machines are idle, the rule looks at the candidate pool: all operations waiting at idle machines. It starts the one with the smallest priority, which makes that machine busy, and repeats until the pool is empty. Rules are popular in practice because they are computationally cheap, react immediately to new events and are easy to interpret.

Try it on one decision. Machine M3 has just become idle and four operations are waiting. The table lists the features the rule can use for each one (the terminals, explained in Section 6). Pick a rule or type your own.

Demo 1 · A dispatching rule makes one decision

Highlighted columns are the terminals this rule uses.

Try this: switch between rules. SPT picks the shortest operation (J9), EDD the most urgent job (J7), WSPT weighs length against importance (J4), and PT+WINQ also looks at how busy each job's next machine is (J12). Different rules, different decisions, all from the same information. Then type something like PT / W + NOR and watch the tree compute it.

3. No Rule Is Best: Enter Hyper-heuristics

Dozens of dispatching rules have been designed manually over the decades. Which one should a shop use? Let's compare them by simulation.

Simulation set-up used throughout this tutorial. A dynamic job shop with 6 machines. Each job has 2 to 6 operations on distinct machines in a random order, with integer processing times uniform in [1, 49]. Jobs arrive as a Poisson process whose rate gives the chosen utilisation level (the expected proportion of time machines are busy), here 0.85 or 0.95. Job weights are 1, 2 or 4 (probabilities 0.2, 0.6, 0.2), and due date = arrival time + 4 × total processing time. Statistics are collected after a warm-up period. All rules are evaluated on the same simulated instances (common random numbers). This set-up is deliberately small, so that every demo runs in a browser.

Demo 2 · Compare rules, and write your own

Each number is an average over 5 simulation replications (1,000 jobs measured after a 200-job warm-up). Lower is better; ★ marks the best.

Your own rule: type a priority function or click the buttons (hover for a terminal's meaning).

Try this: run the simulation. For mean flowtime, PT+WINQ is best at utilisation 0.85 but 2PT+WINQ+NPT is best at 0.95. For mean weighted tardiness, WSPT is best. The best rule depends on the objective and on the shop conditions. Can you write a rule that beats them all in one column?

Caveat: this ranking is specific to the simulation set-up described above and 5 replications per rule. It may differ for other shop configurations, due-date settings, utilisation levels or objectives.

This is why researchers turned to hyper-heuristics. A heuristic is a method that quickly finds good, though not necessarily optimal, solutions; a dispatching rule is a heuristic for scheduling. A hyper-heuristic works one level up: it searches the space of heuristics, selecting or generating them, rather than the space of solutions. There are two main kinds:

  • Selection hyper-heuristics choose among existing low-level heuristics, for example switching from SPT to EDD when many jobs are late.
  • Generation hyper-heuristics construct new heuristics from basic components. A genetic programming hyper-heuristic (GPHH) uses GP to do this.

The key shift: instead of searching for a good schedule (which becomes outdated as soon as a new job arrives), GPHH searches for a good rule, which can then make scheduling decisions in real time for any jobs that arrive in the future.

Two levels of search GP hyper-heuristicsearches the space of rulesgenerates Dispatching rulee.g. 2PT + WINQ + NPTmakes decisions Schedulebuilt decision by decision how good is each heuristic/rule? (the fitness of the rule) The hyper-heuristic does not build schedules itself: it searches for rules, and each rule builds schedules for whatever jobs arrive.
A hyper-heuristic works one level above the schedule: it searches the space of heuristics (rules).

4. Genetic Programming in Brief

Genetic programming is an evolutionary computation technique that evolves computer programs, commonly represented as trees: leaf nodes are inputs (terminals) and internal nodes are operators (functions). A dispatching rule is exactly such a program: the tree on the right is the priority function 2 × PT + WINQ + NPT.

GP initialises a population of random trees (individuals) and then repeats, for a number of generations:

  1. Evaluate each individual with a fitness function.
  2. Select parents, typically by tournament selection: sample a few individuals at random and choose the fittest.
  3. Apply genetic operators: subtree crossover swaps randomly chosen subtrees between two parents; subtree mutation replaces a randomly chosen subtree with a new random one; reproduction copies a parent unchanged.
  4. The offspring form the next generation, usually with the best individuals copied over unchanged (elitism).

New to GP? The Genetic Programming tutorial shows each of these steps in action.

A dispatching rule as a GP tree ++NPT×WINQ2PT +function node (operator) PTterminal node (feature) Evaluated bottom-up for each waiting operation, e.g. J4 in Demo 1: PT = 12, WINQ = 45, NPT = 30 priority = 2 × 12 + 45 + 30 = 99 The operation with the smallest priority is processed first. GP searches the space of all such trees built from the terminal and function sets.
A dispatching rule is a GP tree: the priority function 2 × PT + WINQ + NPT.

To use GP for dispatching rules, we must make five design decisions: the overall framework, the terminals and functions, the fitness evaluation, the training instances, and how to test and deploy the result. The rest of this tutorial walks through each one.

5. Design Step 1: The Overall Framework

A GPHH has two phases. In the training phase, which happens offline, GP evolves rules and evaluates each one by running it in a discrete-event simulation of the shop on a set of training instances. The best rule is then evaluated on unseen test instances. In the deployment phase, which happens online, the chosen rule runs in the real shop and makes every decision instantly.

Training phase (offline) Deployment phase (online) Populationcandidate rules (trees) Simulationtraining instances Fitnesse.g. mean flowtime Selection & operatorsoffspring ruleseach rulenext generation Testunseen instancesbest rule Best rulefast, interpretable Real shopjobs arrive, machines become idleprioritiesstate at eachdecision point Offline: thousands of simulation runs to evolve and test rules (minutes to hours). Online: one priority calculation per waiting operation (microseconds).
The GP hyper-heuristic framework for learning dispatching rules.
Why offline?

Evaluating thousands of candidate rules by simulation takes minutes to hours, which is acceptable offline but not at a decision point. Online, the rule only computes a few priority values.

Why simulation?

A rule's quality emerges only over many decisions and many jobs. A simulator can generate thousands of realistic job arrivals in seconds, without disrupting the real shop.

Why separate test instances?

The training instances are only a sample. Evaluating the rule on unseen test instances measures its generalisation, and reveals overfitting to the training instances.

6. Design Step 2: Terminals and Functions

The terminal set defines the features (inputs) a rule can use; the function set defines how they can be combined. Together they define the search space: every rule GP can construct. The terminals used in this tutorial are common in the literature, in three groups:

Operation
PT
processing time of this operation
NPT
processing time of the job's next operation
OWT
how long the operation has waited in this queue
Job
WKR
work remaining for the job
NOR
number of operations remaining
W
weight (importance)
TIS
time since the job arrived
rDD
time left until the due date
Shop
WINQ
work waiting at the job's next machine
NINQ
number of operations waiting there
Functions
+ − ×
arithmetic
÷
protected division: returns 1 when dividing by 0, so every tree can be evaluated
max, min
choose between two quantities, like an if-then rule

Good choices follow a few principles:

  • Sufficiency: the terminals must contain the information a good rule needs. Without W or rDD, no rule can favour important or urgent jobs.
  • Compactness: every extra terminal enlarges the search space, and irrelevant or redundant terminals waste search effort and make rules harder to interpret. Feature selection methods can identify the relevant terminals automatically.
  • Closure: every function must accept the output of every other function and terminal, which is why division is protected.
  • Interpretability: simple, meaningful building blocks give rules that people can understand, verify and trust.

In Demo 5 (Section 9) you can switch terminals off and see how the evolved rules change.

7. Design Step 3: Fitness Evaluation

The fitness of a rule is its performance in simulation: run the simulated shop with the rule and measure the objective, for example the mean flowtime. Because job arrivals are random, this is a stochastic (noisy) fitness evaluation. Several choices matter:

  • Warm-up period: the simulated shop starts empty, which is not representative. The first jobs (here 100 to 200) are discarded, so that statistics are collected closer to steady state.
  • Simulation length and replications: one simulation run (one random seed) is one sample of job arrivals. Longer runs, or several replications with different random seeds, reduce the variance of the fitness estimate at a higher computational cost.
  • Common random numbers: evaluate all rules on the same instances (the same random seeds), so that differences in fitness come from the rules rather than from sampling noise.
  • Which instances to train on: the scenarios, the instances and how they change during evolution are the next design step (Section 8).
  • Computational cost: fitness evaluation dominates the cost of GPHH. A run with a population of 60 for 25 generations needs about 1,500 simulation runs. Surrogate-assisted GP reduces this by approximating fitness with cheaper models.
Demo 3 · Fitness from simulation is noisy

Try this: with one 500-job replication per rule, the evaluation ranks these two similar rules correctly only about two times in three. With 5 replications per rule, or longer runs, it is almost always correct, at a higher cost. Now untick same instances: the comparison becomes close to a coin toss, even with 10 replications each, and even the 30-replication verdict may change. This is why GPHH evaluates rules with common random numbers.

Caveat: the exact percentages depend on the two rules compared, the simulation set-up and the random seeds; they illustrate the effect of noise rather than general values.

8. Design Step 4: Choosing the Training Instances

In machine learning, a model is only as good as its training data. In GPHH, the training data are the simulation instances used to compute fitness, and they need as much care as the terminal set or the GP parameters. Three questions define them:

Which scenarios?

A scenario is a problem configuration: number of machines, utilisation level, due-date tightness, job mix and objective. A rule learns the patterns of the scenarios it is trained on.

Which instances?

Each random seed generates a new instance (a new stream of arriving jobs) of a scenario, like a new training example. Keep the same training instances throughout, or use new ones each generation (seed rotation)?

How much?

How many jobs per simulation run (after the warm-up), and how many replications per fitness evaluation? More gives lower-variance fitness, at a higher cost.

Each choice affects both training (solution quality and computational cost) and generalisation (performance on unseen test instances):

  • Cover the conditions the rule will face, especially the hard ones. A rule trained at low utilisation rarely encounters long queues, so it may handle them poorly. When it is unclear which conditions matter, training on multiple scenarios is a safe choice.
  • Normalise fitness across scenarios. Flowtimes at high utilisation are much larger than at low utilisation, so simply summing them would let the busiest scenario dominate. A common approach is to divide each scenario's objective value by that of a reference rule (here PT+WINQ) on the same instance, then average.
  • Rotate the training instances. Training on the same instances every generation allows GP to overfit them. With seed rotation, a rule must keep performing well on new instances to survive, which favours rules that generalise.
  • Make simulations long enough. Short runs are cheap but have high variance, and they are dominated by the transient phase rather than steady-state behaviour. Selection then favours rules that were lucky, or that perform well only in the transient phase.
  • Mind the computational budget. Every extra scenario or replication multiplies the number of simulation runs. Under a fixed budget, there is a trade-off between more scenarios, longer runs, a larger population and more generations.
  • Always keep unseen test instances, including scenarios outside the training range, to measure generalisation.

Try it yourself. Each run below evolves a rule with the same GP (population size 60, 25 generations, objective mean flowtime; the full algorithm is shown in Section 9) but a different training design, then evaluates it on 10 unseen test instances at each of five utilisation levels, from 0.80 to 0.97.

Demo 4 · How the training instances affect generalisation

Pick a design and press Train and test (1–15 seconds). Runs accumulate, so you can compare designs.

Test performance: mean flowtime on unseen test instances, normalised by PT+WINQ on the same instances (dashed line = 1). Below 1 is better.

Training fitness: the best rule's normalised fitness on its training instance(s). Compare it with its normalised test performance at the training utilisation: the difference is the generalisation gap.

Try this: run each design once or twice (GP is random, so single runs vary), then compare with the averages below.
  • Utilisation 0.85 vs 0.95: rules trained at 0.85 perform comparably at low utilisation but fall behind at high utilisation. The reverse did not happen in our runs: rules trained at 0.95 also performed well at low utilisation, plausibly because high-utilisation instances contain the harder decisions.
  • Mixed 0.85 + 0.95: clearly better than training at 0.85 only when tested at high utilisation, at about 1.7 times the cost. In our runs it did not outperform training at 0.95 only; whether multi-scenario training pays off depends on how different the scenarios are.
  • Short simulations: training is about five times faster and the training fitness looks excellent, but the test performance is worse, often worse than PT+WINQ itself: a large generalisation gap.
  • Fixed training instance: its rules performed consistently worse on the test instances than those trained with seed rotation.

Our results (mean of 6 independent GP runs per design; mean flowtime normalised by PT+WINQ, lower is better; training fitness on the training instances, test columns on 10 unseen test instances per utilisation level):

Training designTraining timeTraining fitnessTest 0.80Test 0.85Test 0.90Test 0.95Test 0.97
Seed rotation, utilisation 0.951×0.9050.9720.9580.9390.9060.891
Seed rotation, utilisation 0.850.6×0.9480.9760.9670.9570.9390.940
Seed rotation, two scenarios (0.85 and 0.95)1.7×0.9480.9760.9630.9520.9280.921
Fixed training instance, utilisation 0.950.8×0.9440.9880.9790.9710.9500.950
Seed rotation, short runs (200 jobs), utilisation 0.950.2×0.9220.9980.9981.0091.0231.031

In this set-up: training scenarios should be at least as demanding as those the rule will meet; seed rotation helped; and very short simulation runs were a false economy. The training fitness of the short-run design (0.922) promised more than its test performance delivered (above 1): exactly the kind of overfitting that the test phase is designed to detect.

Caveat: these results come from 6 runs per design under the simulation set-up of this tutorial (Section 3), a single objective (mean flowtime) and fixed GP parameters. They may not hold for other shop configurations, objectives, terminal sets, GP parameters or numbers of runs; published studies use larger simulations, more runs and statistical significance tests.

9. Putting It Together: Evolve a Dispatching Rule

Here is a complete GPHH running in your browser: ramped half-and-half initialisation, fitness by simulation (200 warm-up jobs, then 1,000 measured jobs), tournament selection of size 5, subtree crossover (80%), subtree mutation (15%), reproduction (5%), elitism of 1, and a maximum tree depth of 6. Following Section 8, it uses seed rotation by default. Because the training instance changes every generation, raw fitness values are not comparable across generations, so the chart shows the best rule's fitness normalised by that of PT+WINQ on the same instance.

Demo 5 · Evolve a dispatching rule with GP
Terminal set:

Press Evolve. The default run takes about 5–15 seconds.

best individual's fitness, normalised by PT+WINQ on the same training instance (= 1). Below 1 means better.

Terminals used by the 10 best rules:

Try this:
  • Run with the defaults. Within a generation or two the best rule outperforms PT+WINQ on the training instance. Look at which terminals the best rules rely on: for flowtime, PT is in nearly every good rule and WINQ in most, while W and rDD are rarely used.
  • Switch the objective to mean weighted tardiness: now W appears in practically every good rule, and rDD appears too. GP identifies which features matter for each objective.
  • Untick PT and run again: without it, GP needs several generations even to match PT+WINQ, and the final rules are clearly worse (sufficiency of the terminal set).
  • Untick seed rotation: the run trains on one fixed instance, the "fixed training instance" design of Section 8. Test the result in Demo 6.

Caveat: the terminal usage patterns and convergence speed described here were observed in a few runs under the simulation set-up of this tutorial (Section 3). They may not hold for other shop configurations, objectives, terminal sets or GP parameter settings.

10. Design Step 5: Test and Deploy

A rule is only useful if it performs well on instances it has never seen. Before deployment, a GPHH rule is:

  • Tested on many unseen instances (new random seeds), ideally across several scenarios such as different utilisation levels, and compared with the best manual rules, with statistical significance tests.
  • Interpreted: GP rules are symbolic expressions, so experts can read them, simplify them and check that they make sense. (The Explainable AI by GP tutorial shows how to keep them small and readable.)
  • Deployed: in the real shop, whenever machines become idle, the rule computes a priority for each waiting operation. This takes microseconds, so decisions are made in real time.
  • Maintained: if the shop changes substantially (new machines, a different product mix), the rule is retrained offline with an updated simulation model.
Demo 6 · Test a GP rule on unseen instances

Utilisation 0.85 (lower is better)

Utilisation 0.95 (lower is better)

Press Test to evaluate the rules on 10 unseen instances at each utilisation level (about a second).

Try this: test the example rule, which GP evolved for mean flowtime at utilisation 0.95: on these test instances it outperforms every manual rule at both utilisation levels. Then evolve your own rule in Demo 5 and test it here, or test a mean-flowtime rule on mean weighted tardiness: a rule is only as good as the objective it was trained for.

Caveat: the example rule was selected from one of our runs and tested on 10 instances of this tutorial's set-up (Section 3). Its advantage over the manual rules may not hold in other shop configurations or objectives, and a different run may evolve a weaker rule.

11. Going Further

This tutorial covered the core of GPHH for dynamic job shop scheduling. Research has extended it in many directions:

Several decisions at once

In flexible job shops an operation can run on several machines, so two rules are needed: a routing rule (which machine) and a sequencing rule (which job). Multi-tree GP evolves both together.

Faster evaluation

Surrogate-assisted GP estimates fitness cheaply, for example from a rule's decisions on a set of sample decision situations, so that only promising rules are fully simulated.

Feature selection

Identifies the relevant terminals, giving a smaller search space and simpler rules.

Multitask learning

Multitask GP learns rules for several scenarios simultaneously, transferring knowledge between them.

Multiple objectives

Multi-objective GP returns a Pareto front of rules, trading off, for example, flowtime against tardiness, or performance against rule size.

Interpretability

Simplification, bloat control and visualisation keep evolved rules interpretable, so that people can trust them.

12. Check Your Understanding

1. A dispatching rule gives priorities 24, 9 and 15 to three waiting operations. Which starts first?

2. What does a GP hyper-heuristic search for?

3. Why is division protected in the function set?

4. Two rules are compared with one short simulation run each, on different random instances. What is the main risk?

5. A rule has excellent training fitness but mediocre test performance on unseen instances. What happened?

6. A shop has low utilisation in the mornings and high utilisation in the afternoons. How should the training scenarios be chosen?

7. Why is training done offline and not in the real shop?

Where to Go Next