Skip to content
Claudio Cabrera
Chapter 1 · Component 2 · 28:00src/core/physics.py

The Digital Twin Transition Model

Immutable state evolution and the contribution margin objective function

In This Component

The transition function is the mathematical heart of the system. It defines precisely how the world changes as a consequence of each decision and each exogenous event. Without a correct transition model, any optimization is operating on a fantasy.

Implementation: DigitalTwinTransitionModel

The transition model is implemented as a Protocol-conforming class using Python 3.14’s match/case structural pattern matching to cleanly discriminate between event types.

Three-step process per epoch:

Step 1 — Process the decision : If the action is accept_order=True, the system computes and deducts the corresponding birds and biomass from the physical state. The quoted price is locked into the information state.

Step 2 — Process exogenous information :

match event:
    case B2BMarketEvent() if not action.accept_order:
        # Rejection penalty: churn +0.15, CLV × 0.90
    case IoTEdgeEvent(mortality_count=mort, cv_avg_weight_kg=cv_w, ammonia_spike=spike):
        # Biological update: count -= mort, biomass = count × cv_w
        # Ammonia spike: yield_pct × 0.85
    case _:
        pass

Step 3 — Return immutable : Every state object uses @dataclass(frozen=True, slots=True). Transitions use dataclasses.replace() — no mutation, no shared mutable state, full reproducibility.

The Objective Function: MarginObjectiveFunction

The contribution margin implements Robert Phillips’ incremental-cost logic:

Feasibility is checked before scoring: sellable inventory must cover the order quantity, the schedule must not exceed the delivery offset, and the margin must be strictly positive. A reject action always returns .