Architected for high-assurance logistics dispatch where AI Capability ≠ AI Authority: untrusted JSON payloads are quarantined at a four-stage zero-float boundary gate, and every routing, axle-torque, and tachograph decision is proven optimal in exact 64-bit integer arithmetic.
Modern freight brokerages increasingly use LLMs to extract spot-market shipment tenders from emails, PDFs, and webhooks. Yet handing an LLM authority to commit a 40-tonne articulated semi-trailer to a multi-stop European LTL route is an operational catastrophe:
- The Diminishing-Load Axle Trap (
EC-B): A 24,000 kg load that is 100% legal at departure (10,786 kgon the tractor drive axle) can become illegal mid-route (11,567 kg > 11,500 kg) the moment an intermediate rear pallet group is unloaded at a stop—because removing weight behind the trailer's rear tri-axle bogie (10,300 mm) removes upward lever counter-torque on the fifth-wheel kingpin (1,600 mm). - Floating-Point Instability in Branch-and-Bound: In dominance-pruned search trees,
0.1 + 0.2 != 0.3and FMA drift across x86-64 vs. ARM64 cause two identical processes to prune different branches, silently dropping optimal routes or violating cross-node audit reproducibility. - EU Regulation (EC) No 561/2006 Tachograph Coupling: Driving budgets (
270 mincontinuous,540 mindaily), mandatory45 minbreaks,660 minovernight rests, and900 minshift amplitude windows interact non-monotonically with warehouse dock waiting times.
OptiFreight-Core solves this by enforcing a strict Imperative Boundary Shell → Pure Functional Core → Continuous AST & Oracle Governance architecture in 100% Python Standard Library (0 external runtime dependencies):
Across every .py file in src/optifreight/ and tests/, there is not a single float literal, complex literal, float() call, or / division operator. Continuous AST fitness function FF-1 parses the syntax tree of the entire repository on every test run and asserts 0 occurrences of ast.Constant(float | complex) and 0 occurrences of ast.Div.
All physical, temporal, and financial quantities live in 64-bit CPU integer registers:
| Domain Dimension | Canonical Internal Unit | Wire Format (optifreight.bundle/v1) |
Exact Integer Conversion |
|---|---|---|---|
| Mass | Kilograms (kg, int) |
Metric Tonnes ("24.000") |
scale = 1000 (24_000 kg) |
| Length / Clearance | Millimeters (mm, int) |
Meters ("13.600", "2.700") |
scale = 1000 (13_600 mm) |
| Road Distance | Meters (m, int) |
Kilometers ("350.000") |
scale = 1000 (350_000 m) |
| Currency | Euro Cents (¢, int) |
Decimal Euros ("1450.50") |
scale = 100 (145_050 ¢) |
| Rates | ¢/km and ¢/(t·km) (int) |
Integer Cents (120, 3) |
Exact integer |
| Duration / Horizon | Minutes (min, int) |
Integer Minutes (270, 900) |
Exact integer |
| Ratios / Probabilities | Permille (‰, 0..1000) |
Integer Permille (850, 500) |
Exact integer (÷ 1000) |
All upward-rounded divisions use a single branchless integer ceiling primitive:
def ceil_div(n: int, d: int) -> int:
return -(-n // d)A Branch-and-Bound solver expanding thousands of partial LTL trajectories cannot afford per-instance __dict__ hash tables or O(d) tuple-copying on every child expansion:
- Zero
__dict__Overhead (FF-3): All 10 domain and boundary classes (CityNode,RoadEdge,ShipmentOffer,TruckSpec,TachoState,RouteStep,TruckState,Leg,Solution,OfferRejection) are C-struct-backed@dataclass(slots=True, frozen=True)objects verified by AST inspection. - Persistent Parent-Pointer DAG (
RouteStep.prev): EachRouteStepholds aprev: RouteStep | None = field(default=None, compare=False, repr=False)reference to its predecessor. Branching allocates one cell in Heap RAM atO(1)cost while sharing the immutable prefix across all sibling branches, andcompare=False, repr=False(Audit Fix A6) preventsO(d)recursive traversal orRecursionErroron deep chains.
Every candidate route step must satisfy all 11 physical/legal filters and 6 edge-case invariants implemented in models.py, graph.py, physics.py, and optimizer.py:
| Rule ID | Subsystem | Mathematical Contract & Physical Invariant |
|---|---|---|
F1 |
Parse, Don't Validate | __post_init__ enforces regex IDs ^[A-Z0-9_-]{1,32}\Z, NUL-byte rejection, type(v) is int (rejecting bool), positive mass/dimensions/revenue, ordered time windows, and static LDM feasibility before any object can exist in memory. |
F2 |
Dual Trailer Capacity | Checked after every PICKUP: ∑ wᵢ ≤ 24,000 kg ∧ ∑ ldmᵢ ≤ 13,600 mm. |
F3 + EC-A |
3D Stackability & Intra-Offer LDM | Clearance is 3,000 mm (MEGA) or 2,700 mm (STANDARD_TILT / REEFER). If pallet_height_mm > clearance, reject. Else tiers = ⌊clearance / h⌋ if is_stackable else 1, and ldm = ⌈pallets / (per_row · tiers)⌉ · len_mm. Cross-shipment stacking is forbidden (EC-A) so LIFO and the lever-arm model remain exact. |
F4 |
Contiguous Rear-Door LIFO Stack | onboard is an ordered tuple (nose ... door). PICKUP pushes to onboard[-1]; DELIVER is legal only for onboard[-1]. Occupied floor is always the contiguous prefix [0, ∑ ldmᵢ). |
F5 + EC-B |
Doubled-Coordinate Lever-Arm Axle Torque | Kingpin at X_kp = 1,600 mm, rear tri-axle bogie at X_ax = 10,300 mm (SPAN2 = 2(X_ax − X_kp) = 17,400 mm). For item i at prefix offset oᵢ, doubled center of mass is X2ᵢ = 2oᵢ + ldmᵢ, and signed moment is M = ∑ wᵢ(2X_ax − X2ᵢ). Kingpin load is K = 2,000 + ⌈max(M, 0) / 17,400⌉ kg (A3), and tractor drive axle load is D = 800 + ⌈850·K / 1000⌉ ≤ 11,500 kg. Checked after every push AND every pop (EC-B). |
F6 + EC-C |
Equipment & Permanent ADR Taint | Equipment matrix SERVES (MEGA serves {STANDARD_TILT, MEGA}; REEFER serves {REEFER, STANDARD_TILT}). Co-loading pairs {ADR_HAZMAT, FOOD_PHARMA} and {ADR_HAZMAT, FRAGILE_ELECTRONICS} are forbidden. Loading any ADR_HAZMAT sets adr_tainted = True for the remainder of the route, permanently blocking FOOD_PHARMA (EC-C) even after hazmat unloading. |
F7 + EC-D |
EU Reg 561/2006 Tachograph FSM | Tracks (clock, cont_drive, day_drive, shift_start). tacho_drive splits long edges mid-leg (EC-D) whenever min(270 − cont, 540 − day, 900 − elapsed) = 0, inserting a 45 min break (or a 660 min overnight rest if day = 540 or elapsed + 45 ≥ 900). |
F8 |
Warehouse Dock & Amplitude Windows | tacho_dock enforces service start ≥ earliest and end ≤ latest while respecting 900 min shift amplitude. Waiting ≥ 45 min resets cont_drive; waiting ≥ 660 min (or forced pre-dock rest when amplitude would overflow) resets the full shift. |
F9 |
Weight-Dependent Edge Costing | Per traversed road edge with payload w kg: cost_e = ⌈d_e · (1000 · base_cpkm + rate_cptkm · w) / 1,000,000⌉ + toll_e. |
F10 |
Terminal Backhaul Expected Value | Upon final delivery at node v with onboard == (): EV(v) = ⌊backhaul_ev_cents(v) · return_prob_permille(v) / 1000⌋. |
F11 |
Cross-Multiplied Profit Floor | Route is feasible iff net ≥ min_net_profit_cents and net · 1000 ≥ min_cents_per_km · total_dist_m (zero division). |
Important
Why We Pair Branch-and-Bound with an Independent Brute-Force DFS Oracle (tests/_oracle.py)
Heuristic search bugs are insidious: an overly aggressive upper bound or dominance rule does not crash—it silently prunes the true optimum and returns a suboptimal route with high confidence. During Phase 1-A and Phase 2-B, our cross-module adversarial audit and 1,050-seed Brute-Force Oracle uncovered and locked 13 subtle mathematical and boundary defects (A1–A13).
- The Trap: In
upper_bound(), the initial implementation filtered unpicked candidate offers usingcompatible(onboard_objs, o, ...). When offerH(ADR_HAZMAT,€ 600) was currently onboard,compatiblesaw a co-loading conflict with unpicked offerF(FRAGILE_ELECTRONICS,€ 1,000) and excludedF's revenue fromr_open. - Why It Failed Admissibility: Co-loading incompatibility with
FRAGILE_ELECTRONICSis temporary—onceHis delivered at the next stop, the trailer is empty again andadr_taintedonly blocksFOOD_PHARMA, notFRAGILE_ELECTRONICS! Underestimatingupper_boundcaused the solver to prune the optimalH -> Fmulti-stop consolidation (€ 1,392.00net) and return single-stopF(€ 796.00net). - The Fix (
optimizer.py:47): Evaluatecompatible((), o, truck.trailer, state.adr_tainted)against an empty onboard tuple so only permanent exclusions (equipment mismatch andEC-Chazmat residue taint) restrict the optimistic relaxation. Locked bytest_a1_upper_bound_admissibility_probe.
- The Trap: Under EU 561/2006 dock rules (
F7/F8), arriving at a dock closer to the900 minshift amplitude ceiling can force an immediate660 minreset rest during an early-arrival wait window, allowing the driver to depart with a fresh0 minshift clock—whereas a state that arrived earlier with "more remaining budget" does not trigger the reset and is forced into a full660 minrest on the next highway leg. Furthermore, if two states have equal profit (pa == pb) anda.dist_m < b.dist_m, pruningbwhen_ev_seq(b) < _ev_seq(a)violatesR0.5lexicographic tie-breaking if both passF11. - The Fix (
optimizer.py:52-61):dominates(a, b)requires exact equality on all four tachograph coordinates(clock, cont_drive, day_drive, shift_start), weak dominance on(profit, dist_m, adr_tainted), strict dominance on at least one, and_ev_seq(a.last_step) <= _ev_seq(b.last_step)wheneverpa == pb(A10). Locked bytest_a10_dominance_respects_r05_lexicographic_tie_break.
- Closed-World Envelope & Surrogate Hardening (
A11): Hardenedintake_gate.pysotype(v) is intrejects JSON booleans masquerading asint(isinstance(True, int)isTruein Python),_keys()enforces strict closed-world key sets oncitiesandedges, lone UTF-16 surrogates ("\ud800") raiseIntakeError("INVALID_UTF8"), and unhashable offer IDs ("id": ["O9"]) isolate cleanly intoOfferRejection. - Decoupled Brute-Force Oracle & Coverage Floors (
A12–A13): Removedlifo_forward_okfromtests/_oracle.pyso the oracle never shares lookahead pruning logic with the solver, sorted edge pairs before RNG sampling for cross-processPYTHONHASHSEEDreproducibility, and enforced non-vacuous coverage floors across 1,050 seeded instances (feasible >= 300,multi >= 50,breaks >= 300,rests >= 100).
Running the built-in CLI visualizer executes a 6-city, 6-offer European freight scenario starting in Berlin (BER) with a STANDARD_TILT semi-trailer (base_cpkm = 100 ¢/km, rate_cptkm = 2 ¢/(t·km)):
$env:PYTHONPATH="src"; python -m optifreight.cli_visualizer================================================================================
OPTIFREIGHT-CORE DISPATCH & ROUTE REPORT
================================================================================
CHRONOLOGICAL ROUTE SCHEDULE:
--------------------------------------------------------------------------------
Step 1 | PICKUP OFFER_B at BER | Arr: 0m | Dock: 0m..30m | Breaks: 0 | Rests: 0
Payload: 8 000 kg | Floor: 4800 mm | Drive Axle: 8 676 kg
Bed: [NOSE|########OFFER_B#########............................................|DOOR>]
--------------------------------------------------------------------------------
Step 2 | PICKUP OFFER_A at BER | Arr: 30m | Dock: 30m..60m | Breaks: 0 | Rests: 0
Payload: 14 000 kg | Floor: 8400 mm | Drive Axle: 10 845 kg
Bed: [NOSE|########OFFER_B##############OFFER_A######..........................|DOOR>]
--------------------------------------------------------------------------------
Step 3 | DELIVER OFFER_A at PRG | Arr: 330m | Dock: 330m..360m | Breaks: 0 | Rests: 0
Payload: 8 000 kg | Floor: 4800 mm | Drive Axle: 8 676 kg
Bed: [NOSE|########OFFER_B#########............................................|DOOR>]
--------------------------------------------------------------------------------
Step 4 | PICKUP OFFER_C at PRG | Arr: 360m | Dock: 360m..390m | Breaks: 0 | Rests: 0
Payload: 13 000 kg | Floor: 7200 mm | Drive Axle: 10 776 kg
Bed: [NOSE|########OFFER_B###########OFFER_C###................................|DOOR>]
--------------------------------------------------------------------------------
Step 5 | DELIVER OFFER_C at VIE | Arr: 675m | Dock: 700m..730m | Breaks: 1 | Rests: 0
Payload: 8 000 kg | Floor: 4800 mm | Drive Axle: 8 676 kg
Bed: [NOSE|########OFFER_B#########............................................|DOOR>]
--------------------------------------------------------------------------------
Step 6 | DELIVER OFFER_B at VIE | Arr: 730m | Dock: 730m..760m | Breaks: 0 | Rests: 0
Payload: 0 kg | Floor: 0 mm | Drive Axle: 2 500 kg
Bed: [NOSE|....................................................................|DOOR>]
--------------------------------------------------------------------------------
Step 7 | PICKUP OFFER_D at VIE | Arr: 760m | Dock: 760m..805m | Breaks: 0 | Rests: 0
Payload: 7 000 kg | Floor: 3600 mm | Drive Axle: 8 314 kg
Bed: [NOSE|#####OFFER_D######..................................................|DOOR>]
--------------------------------------------------------------------------------
Step 8 | DELIVER OFFER_D at MIL | Arr: 2770m | Dock: 2770m..2815m | Breaks: 1 | Rests: 2
Payload: 0 kg | Floor: 0 mm | Drive Axle: 2 500 kg
Bed: [NOSE|....................................................................|DOOR>]
--------------------------------------------------------------------------------
================================================================================
FINANCIAL & PHYSICAL LEDGER:
--------------------------------------------------------------------------------
Gross Revenue: € 9 000,00
Total Leg Costs: € 2 047,80
Backhaul EV: € 250,00
--------------------------------------------------------------------------------
Net Profit: € 7 202,20
Total Distance: 1530 km (1530000 m)
Final Clock: 2815 min (46h 55m)
Delivered Offers: OFFER_A, OFFER_C, OFFER_B, OFFER_D
Search Expansions: 11 nodes
Optimality: PROVEN
================================================================================
Notice how the dispatcher pre-loads OFFER_B (BER → VIE) at the nose before loading OFFER_A (BER → PRG) at the rear door, unloads OFFER_A in Prague (Step 3), consolidates OFFER_C (PRG → VIE) into the freed rear space (Step 4), unloads OFFER_C then OFFER_B in Vienna (Steps 5–6) in strict LIFO order while keeping tractor drive axle load ≤ 10,845 kg, and then hauls OFFER_D (VIE → MIL) across 1 break and 2 mandatory overnight rests (660 min each) for a proven-optimal net profit of € 7,202.20 in just 11 search expansions.
| Module / Test Suite | Path | LOC | Primary Responsibility & Enforced Guarantees | Status |
|---|---|---|---|---|
| Domain Models | src/optifreight/models.py |
96 |
Fixed-point constants, enums, ceil_div, ldm_for, 7 frozen+slots dataclasses (F1, F3) |
✅ |
| Road Digraph | src/optifreight/graph.py |
97 |
Dual deterministic Dijkstra (get_leg, t_min, d_min) with lexicographic tie-breaking |
✅ |
| Physics & Tacho Engine | src/optifreight/physics.py |
112 |
Lever-arm axle load (F5/EC-B), ADR taint (F6/EC-C), EU 561/2006 FSM (F7/F8), costs (F9–F11) |
✅ |
| B&B LTL Optimizer | src/optifreight/optimizer.py |
134 |
Event-space Best-First B&B, admissible UB (A1), exact-tacho + R0.5 dominance (A2, A10) |
✅ |
| CLI Bed Visualizer | src/optifreight/cli_visualizer.py |
119 |
Cumulative integer ASCII trailer-bed renderer (A7), European demo & JSON bundle CLI, financial ledger |
✅ |
| JSON Boundary Gate | src/optifreight/intake_gate.py |
116 |
4 MiB byte cap, O(n) depth-8 scanner, zero-float parse_decimal_to_scaled_int, optifreight.bundle/v1 |
✅ |
| Brute-Force Oracle | tests/_oracle.py |
115 |
Independent physics replay verifier (verify_solution) + unpruned DFS oracle (brute_force_solve) |
✅ |
| AST Fitness & Fuzzer | tests/test_architecture_fitness.py |
104 |
FF-1 (0 floats/divs), FF-2 (import isolation), FF-3 (10 frozen slots), FF-4/FF-4b (1,050-seed oracle) |
✅ |
| Unit & Security Suites | tests/test_*.py |
1,472 |
44 total tests covering F1–F11, EC-A–EC-F, A1–A13, CLI JSON intake, and PYTHONHASHSEED determinism |
44 / 44 (~1.6s) |
# Windows PowerShell
$env:PYTHONPATH="src"; python -m pytest tests/ -v
# Linux / macOS
PYTHONPATH=src python3 -m pytest tests/ -v# Run built-in European corridor scenario
$env:PYTHONPATH="src"; python -m optifreight.cli_visualizer
# Ingest an external JSON bundle
$env:PYTHONPATH="src"; python -m optifreight.cli_visualizer path/to/bundle.jsonBLUEPRINT.md: System equations, physical/legal derivations (F1–F11,EC-A–EC-F), and the Phase 1-A adversarial audit (A1–A9).SPEC.md: Normative module contracts, exact type signatures, and per-module import allowlists.ADR.md: Five formal Architecture Decision Records (ADR-001throughADR-005) covering fixed-point arithmetic, lever-arm physics, event-space B&B, zero-float JSON ingestion, and AST fitness governance.HANDOFF.md: Complete multi-phase engineering ledger and verification audit log.
