Genetic Programming for Job Shop Scheduling: An Interactive Tutorial
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.
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.
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.
Highlighted columns are the terminals this rule uses.
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.
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).
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.
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:
- Evaluate each individual with a fitness function.
- Select parents, typically by tournament selection: sample a few individuals at random and choose the fittest.
- 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.
- 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.
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.
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.
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.
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:
- 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
- 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
- WINQ
- work waiting at the job's next machine
- NINQ
- number of operations waiting there
- + − ×
- 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
WorrDD, 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.
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:
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.
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 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.
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.
- 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 design | Training time | Training fitness | Test 0.80 | Test 0.85 | Test 0.90 | Test 0.95 | Test 0.97 |
|---|---|---|---|---|---|---|---|
| Seed rotation, utilisation 0.95 | 1× | 0.905 | 0.972 | 0.958 | 0.939 | 0.906 | 0.891 |
| Seed rotation, utilisation 0.85 | 0.6× | 0.948 | 0.976 | 0.967 | 0.957 | 0.939 | 0.940 |
| Seed rotation, two scenarios (0.85 and 0.95) | 1.7× | 0.948 | 0.976 | 0.963 | 0.952 | 0.928 | 0.921 |
| Fixed training instance, utilisation 0.95 | 0.8× | 0.944 | 0.988 | 0.979 | 0.971 | 0.950 | 0.950 |
| Seed rotation, short runs (200 jobs), utilisation 0.95 | 0.2× | 0.922 | 0.998 | 0.998 | 1.009 | 1.023 | 1.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.
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:
- 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,
PTis in nearly every good rule andWINQin most, whileWandrDDare rarely used. - Switch the objective to mean weighted tardiness: now
Wappears in practically every good rule, andrDDappears too. GP identifies which features matter for each objective. - Untick
PTand 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.
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).
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:
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.
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.
Identifies the relevant terminals, giving a smaller search space and simpler rules.
Multitask GP learns rules for several scenarios simultaneously, transferring knowledge between them.
Multi-objective GP returns a Pareto front of rules, trading off, for example, flowtime against tardiness, or performance against rule size.
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
- The building blocks: the Job Shop Scheduling tutorial and the Genetic Programming tutorial explain the problem and the method in more depth.
- Readable rules: the Explainable AI by Genetic Programming tutorial shows how to keep evolved rules small and interpretable.
- Code: GPJSS is a Java framework for evolving dispatching rules for dynamic job shop scheduling with GP.
- The book: F. Zhang, S. Nguyen, Y. Mei and M. Zhang, Genetic Programming for Production Scheduling: An Evolutionary Learning Approach (Springer, 2021) covers everything here and much more.
- My research: the genetic programming research theme lists our papers on GP for scheduling and routing.