Food relief system
A food relief system.
- Class:
FoodReliefSystem - Import:
from skcomplex.systems.engineering.processes import FoodReliefSystem - Field:
engineering/processes - Topics:
humanitarian logistics,supply chain,stocks and flows,system dynamics,ode - References:
- Bala et al. 2017
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.engineering.processes import FoodReliefSystem
trajectory = FoodReliefSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
affected |
100000.0 |
ration |
0.0005 |
onset |
10.0 |
shipping_delay |
14.0 |
cover |
7.0 |
stock_time |
7.0 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | continuous |
| Space | none |
| Laws | rate |
| Randomness | deterministic |
| Domains | real |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class FoodReliefSystem(BaseSystem):
"""A food relief system.
A pipeline ships food to a warehouse and distributes it to people in need. A disaster at `onset` leaves `affected`
people who need a daily ration. The relief agency ships food at the need plus corrections that close the gaps
between the warehouse stock and its desired cover of the need, and between the food in transit and the need over the
shipping delay. Shipped food enters the pipeline and arrives at the warehouse through a third-order material delay.
The warehouse distributes the need while its stock lasts. Food is conserved, so almost nothing reaches the people in
the first days after the disaster, and the gap closes as the pipeline fills. The structure follows the relief food
example of Bala, Arshad and Noh (2017, Section 4.11), with illustrative parameters in tonnes and days.
The system has the food stocks `relief.in_transit` and `relief.warehouse` in tonnes, and the flows
`relief.shipping`, `relief.arrivals`, and `relief.distribution` in tonnes per day, over days. The observables of the
system are `relief.need` and `relief.shipments`.
Args:
affected:
The number of people in need after the disaster.
ration:
The daily ration in tonnes per person.
onset:
The day of the disaster.
shipping_delay:
The average time from shipping to arrival, in days.
cover:
The desired warehouse stock in days of need.
stock_time:
The time to correct the warehouse stock, in days.
label:
The label of the system, which names it when it is nested in another one.
solver:
The solver of the problem, or `'auto'` for the one the system chooses.
backend:
The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.
Examples:
>>> from skcomplex.systems.engineering.processes import FoodReliefSystem
>>> FoodReliefSystem(shipping_delay=7.0).prepare().system_.elements[0].shipping_delay
7.0
"""
field = 'engineering/processes'
topics = ('humanitarian logistics', 'supply chain', 'stocks and flows', 'system dynamics', 'ode')
references = ('Bala et al. 2017',)
until = 120.0
dt = 0.5
observables = (
Observable('need', ReliefPipelineInteraction._need, element='relief'),
Observable('shipments', ReliefPipelineInteraction._get_shipments, element='relief'),
)
def __init__(
self,
affected: float = 1e5,
*,
ration: float = 5e-4,
onset: float = 10.0,
shipping_delay: float = 14.0,
cover: float = 7.0,
stock_time: float = 7.0,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.affected = affected
self.ration = ration
self.onset = onset
self.shipping_delay = shipping_delay
self.cover = cover
self.stock_time = stock_time
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set `system_` to the components of the system built from its model parameters."""
super()._prepare(context)
relief = ReliefPipelineElement(
'relief',
affected=self.affected,
ration=self.ration,
onset=self.onset,
shipping_delay=self.shipping_delay,
cover=self.cover,
stock_time=self.stock_time,
)
self.system_ = System(
self.label,
elements=[relief],
interactions=[ReliefPipelineInteraction('flows', target='relief')],
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
ReliefPipelineElement, fromElement
Interactions:
ReliefPipelineInteraction, fromInteraction
class ReliefPipelineElement(Element):
"""Relief supply line that ships food to a warehouse and distributes it.
The food is shipped through a delayed pipeline.
"""
def __init__(
self,
label: str,
*,
in_transit: Annotated[object, RealSet.closed_open(0, oo), Quantity('food')] = 0.0,
warehouse: Annotated[object, RealSet.closed_open(0, oo), Quantity('food')] = 0.0,
affected: Annotated[object, RealSet.closed_open(0, oo)] = 100000.0,
ration: Annotated[object, RealSet.closed_open(0, oo)] = 0.0005,
onset: Annotated[object, RealSet.closed_open(0, oo)] = 10.0,
shipping_delay: Annotated[object, RealSet.open(0, oo)] = 14.0,
cover: Annotated[object, RealSet.closed_open(0, oo)] = 7.0,
stock_time: Annotated[object, RealSet.open(0, oo)] = 7.0,
pipeline_time: Annotated[object, RealSet.open(0, oo)] = 14.0,
distribution_time: Annotated[object, RealSet.open(0, oo)] = 1.0,
) -> None:
super().__init__(label)
self.in_transit = in_transit
self.warehouse = warehouse
self.affected = affected
self.ration = ration
self.onset = onset
self.shipping_delay = shipping_delay
self.cover = cover
self.stock_time = stock_time
self.pipeline_time = pipeline_time
self.distribution_time = distribution_time
class ReliefPipelineInteraction(Interaction):
"""Shipments, arrivals, and distribution of the food of a relief supply line.
The flows read the stocks and the parameters of the element the role `target` binds.
Args:
label:
The label of the interaction.
target:
The path of the element that holds the stocks and the parameters.
Examples:
>>> from skcomplex.systems.engineering.processes import ReliefPipelineInteraction
>>> ReliefPipelineInteraction('flows', target='relief').target
'relief'
"""
law_classes = (RateLaw,)
def __init__(self, label: str, *, target: Role) -> None:
super().__init__(label)
self.target = target
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the flows of the stocks of the target.
Args:
context:
The namespace of the target, the time `t`, and the parameters.
"""
super()._prepare(context)
s = SimpleNamespace(t=context.t, **vars(context.target))
self.laws_: list[BaseLaw] = ReliefPipelineInteraction._get_relief_laws(s)
@staticmethod
def _need(s: SimpleNamespace) -> Expression:
"""Return the need of relief food.
It starts at the onset of the disaster.
"""
return s.ration * s.affected * step(s.t, s.onset)
@staticmethod
def _get_shipments(s: SimpleNamespace) -> Expression:
"""Return the shipments that meet the need and close the gaps.
The gaps are those of the stock and of the pipeline.
"""
need = ReliefPipelineInteraction._need(s)
stock_gap = (s.cover * need - s.warehouse) / s.stock_time
pipeline_gap = (s.shipping_delay * need - s.in_transit) / s.pipeline_time
return clip(need + stock_gap + pipeline_gap, 0)
@staticmethod
def _get_relief_laws(s: SimpleNamespace) -> list[BaseLaw]:
"""Return the shipments, arrivals, and distribution of relief food.
They follow a disaster.
"""
shipments = ReliefPipelineInteraction._get_shipments(s)
return [
RateLaw({s.in_transit: +1}, shipments, label='shipping'),
RateLaw(
{s.in_transit: -1, s.warehouse: +1},
distributed_delay(shipments, s.shipping_delay, 3),
label='arrivals',
),
RateLaw(
{s.warehouse: -1},
minimum(ReliefPipelineInteraction._need(s), s.warehouse / s.distribution_time),
label='distribution',
),
]
Equations
The equations are those that symbolic returns for the system with its default parameters.
relief.in_transit: diff(in_transit_relief(t), t) == -3*_material_delay3_0_flows_2/shipping_delay_relief + where(affected_relief*ration_relief*where(t >= onset_relief, 1.0, 0) + (affected_relief*ration_relief*shipping_delay_relief*where(t >= onset_relief, 1.0, 0) - in_transit_relief(t))/pipeline_time_relief + (affected_relief*cover_relief*ration_relief*where(t >= onset_relief, 1.0, 0) - warehouse_relief(t))/stock_time_relief < 0, 0, affected_relief*ration_relief*where(t >= onset_relief, 1.0, 0) + (affected_relief*ration_relief*shipping_delay_relief*where(t >= onset_relief, 1.0, 0) - in_transit_relief(t))/pipeline_time_relief + (affected_relief*cover_relief*ration_relief*where(t >= onset_relief, 1.0, 0) - warehouse_relief(t))/stock_time_relief)
relief.warehouse: diff(warehouse_relief(t), t) == 3*_material_delay3_0_flows_2/shipping_delay_relief - where(warehouse_relief(t)/distribution_time_relief < affected_relief*ration_relief*where(t >= onset_relief, 1.0, 0), warehouse_relief(t)/distribution_time_relief, affected_relief*ration_relief*where(t >= onset_relief, 1.0, 0))