Reinforcement learning · Supply chain · 2025 – present
An agent that decides, hour by hour, whether to pump phosphate slurry, flush with water or stop, on a pipeline where every decision takes 26 hours to reach the plant. Instead of handing a solver a large constraint model, most of the constraints are written into a simulator, an action space and a reward.
Background: my AnyLogic 3D simulation of the network (head-station tanks, pipeline, terminal tank groups)
The system
In Morocco's phosphate supply chain, ore from the Khouribga mining basin is washed, mixed with water into a pulp, and pumped as slurry to the Jorf Lasfar industrial platform on the Atlantic coast. There it feeds phosphoric-acid units and drying plants. The pipeline replaces a long chain of trains, but scheduling it is a difficult problem in its own right.
Four feeders produce ore at rates that vary from hour to hour.
Pulp is stored in local and export tank groups before injection.
Product, water and stops travel as batches. This is where the agent decides.
Three tank groups serve 12 acid, fertiliser and drying clients.
The problem
A batch pumped at 08:00 arrives at the terminal tanks at 10:00 the next day. In the meantime the operator has to:
The usual approach is a mixed-integer linear program: one binary variable per tank, product and hour for every switch, stoppage, water batch and dispatch decision, all linked by big-M constraints. This gives exact answers for a fixed forecast. It is slow to re-solve when the forecast changes, and it gets much harder with each new grade.
The idea
An RL agent doesn't need every rule written as an inequality. Each MILP constraint goes to one of four places, and only the last of them leaves the agent anything to learn.
The next state is computed, so the agent cannot violate these rules.
A single discrete choice per hour means only one thing can happen at a time.
Illegal moves are removed before the agent chooses.
Soft goals become costs the agent learns to trade off.
By the numbers
The reference is the integrated HS + pipeline + TS MILP from the project's modelling deliverable. Its objective is followed by constraints numbered (2) to (194). The agent is trained on the Gymnasium environment in this repository. Both columns cover one 24-hour plan.
| Goes to | MILP constraints (numbering of the integrated model) | # |
|---|---|---|
| Simulator dynamics | feeder rates (2–8), transfers (11–18), inventory balance (19–23), capacity (24–28), switch indicators (29–33, 55), backlog (34), demand triggers and consumption windows (36–37, 159–162), discharge/injection rates and conservation (38–42, 48, 51, 54, 60–61, 64, 94), mix-batch interface (71–93), shared feeders, repulping and pulp return (113–120, 127–151), contamination detection (163–192), client inventory (193–194) | 138 |
| Action masks | new product only into an empty tank (43–47), water separator (56), single stoppage (66–70), storage priority (97–99), transfer activation (100–110), minimum batch length (111–112), 24 h between stops (121), stops framed by water (122–123) | 30 |
| Action space | one product per feeder or tank (9–10), one product discharged or injected per hour (49–50, 52–53), water batch and stop exclusivity (63, 65, 95–96), one terminal group per hour (152–158) | 17 |
| Reward | objective (1), demand met by tmax (35), wrong-grade volume (57–59), water bounds (62), smooth distribution (124–126) | 8 |
How the counts are derived: constraint numbers come from the integrated model. Variable counts assume 2 product grades, 24 hourly periods, 12 client feeders, 4 washery feeders and 4 clients sharing feeders, so they are estimates of model size rather than solver-reported figures. Assigning each constraint to a bucket is my own design choice. The current environment implements a simplified subset of these dynamics (one grade, 5 tank groups). Mix batches, pulp return and contamination are on the roadmap. The DQN reads an 18-feature state into a 256-128-64 network (46 211 weights); the full Gymnasium observation has 455 features.
Formulation
Pipeline mode {product, water, stop}, plus head-station source, terminal destination, feeder routing and terminal transfers, defined as a Gymnasium MultiDiscrete space.
− 1000 · unmet / 100 − 5 if water − 5 if stop + 0.5 per tank in 20–80 % − 2 per tank < 10 % − 1 per tank > 95 %
Simulation
I built a fluid-library model of the network in AnyLogic: sources, head-station tanks, a selector that sends water or product into the pipeline, and split terminal tank groups. Batches are colour-coded by grade. It serves as a test bench: schedules produced in Python are replayed in it to check that they hold up in a more detailed model, and the next step is to train the agent on it directly.
Agents
Pyomo + GLPK. Optimal for one fixed forecast, and the reference every agent is measured against.
Discretised state, 10 bins per feature. Quick to train on short horizons.
Written from scratch in NumPy: a 256-128-64 network, a 20k replay buffer, a target network and ε-greedy exploration.
PyTorch. Learns from logged transitions and penalises Q-values for actions not in the data, because nobody lets an agent explore on a real pipeline.
Where it stands
In one line