Rice milling system
A rice milling system.
- Class:
RiceMillingSystem - Import:
from skcomplex.systems.engineering.processes import RiceMillingSystem - Field:
engineering/processes - Topics:
agriculture,milling,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 RiceMillingSystem
trajectory = RiceMillingSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
harvest |
2000.0 |
seasonality |
0.8 |
procurement_delay |
1.0 |
outturn |
0.65 |
capacity |
2000.0 |
expansion_time |
12.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 RiceMillingSystem(BaseSystem):
"""A rice milling system.
A mill buys paddy, mills it into rice and by-products, and sells them. Paddy arrives from the farms through a
third-order procurement delay, at a rate that follows the harvest season around its mean. The mill processes its
paddy stock in a month, as far as its milling capacity allows, and splits the milled paddy by mass into rice, the
outturn, husk, bran, and milling losses. Rice, husk, and bran leave their inventories for the market at a fixed
coverage. The capacity adjusts to the smoothed indicated milling rate, so in the harvest season the capacity lags
and paddy accumulates, and in the lean season the capacity idles. Grain is conserved, every change of the paddy,
rice, husk, and bran stocks is a flow, and over a year the milled paddy and the rice sales match the mean harvest
and its outturn. The structure follows the rice milling case of Bala, Arshad and Noh (2017), with illustrative
parameters in tonnes and months, since the case data are specific to their study.
The system has the grain stocks `mill.paddy`, `mill.rice`, `mill.husk`, and `mill.bran` in tonnes, the capacity
`mill.capacity`, and the flows `mill.paddy_purchases`, `mill.milled_rice`, `mill.husk_output`,
`mill.bran_output`, `mill.milling_losses`, `mill.rice_sales`, `mill.husk_sales`, and `mill.bran_sales` in
tonnes per month, over months.
The observables of the system are `mill.procurement`, `mill.indicated_milling`, and `mill.milling`.
Args:
harvest:
The mean paddy procurement in tonnes per month.
seasonality:
The relative amplitude of the yearly harvest cycle.
procurement_delay:
The average time for purchased paddy to reach the mill, in months.
outturn:
The fraction of the milled paddy that becomes rice.
capacity:
The initial milling capacity in tonnes of paddy per month.
expansion_time:
The time for the capacity to adjust to the perceived milling rate, in months.
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 RiceMillingSystem
>>> RiceMillingSystem(capacity=1500.0).prepare().system_.elements[0].capacity
1500.0
"""
field = 'engineering/processes'
topics = ('agriculture', 'milling', 'stocks and flows', 'system dynamics', 'ode')
references = ('Bala et al. 2017',)
until = 120.0
dt = 0.25
observables = (
Observable('procurement', RiceMillInteraction._get_procurement, element='mill'),
Observable('indicated_milling', RiceMillInteraction._get_indicated_milling, element='mill'),
Observable('milling', RiceMillInteraction._get_milling, element='mill'),
)
def __init__(
self,
*,
harvest: float = 2000.0,
seasonality: float = 0.8,
procurement_delay: float = 1.0,
outturn: float = 0.65,
capacity: float = 2000.0,
expansion_time: float = 12.0,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.harvest = harvest
self.seasonality = seasonality
self.procurement_delay = procurement_delay
self.outturn = outturn
self.capacity = capacity
self.expansion_time = expansion_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)
mill = RiceMillElement(
'mill',
paddy=self.harvest,
rice=self.outturn * self.harvest,
husk=HUSK_SHARE * self.harvest,
bran=BRAN_SHARE * self.harvest,
capacity=self.capacity,
harvest=self.harvest,
seasonality=self.seasonality,
procurement_delay=self.procurement_delay,
outturn=self.outturn,
expansion_time=self.expansion_time,
)
self.system_ = System(self.label, elements=[mill], interactions=[RiceMillInteraction('flows', target='mill')])
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
RiceMillElement, fromElement
Interactions:
RiceMillInteraction, fromInteraction
class RiceMillElement(Element):
"""Rice mill that buys seasonal paddy, mills it, and adjusts its capacity.
The paddy is milled into rice, husk, and bran.
"""
def __init__(
self,
label: str,
*,
paddy: Annotated[object, RealSet.closed_open(0, oo), Quantity('grain')] = 2000.0,
rice: Annotated[object, RealSet.closed_open(0, oo), Quantity('grain')] = 1300.0,
husk: Annotated[object, RealSet.closed_open(0, oo), Quantity('grain')] = 400.0,
bran: Annotated[object, RealSet.closed_open(0, oo), Quantity('grain')] = 160.0,
capacity: Annotated[object, RealSet.closed_open(0, oo)] = 2000.0,
harvest: Annotated[object, RealSet.closed_open(0, oo)] = 2000.0,
seasonality: Annotated[object, RealSet.closed_open(0, oo)] = 0.8,
procurement_delay: Annotated[object, RealSet.open(0, oo)] = 1.0,
milling_time: Annotated[object, RealSet.open(0, oo)] = 1.0,
outturn: Annotated[object, RealSet.closed_open(0, oo)] = 0.65,
husk_share: Annotated[object, RealSet.closed_open(0, oo)] = HUSK_SHARE,
bran_share: Annotated[object, RealSet.closed_open(0, oo)] = BRAN_SHARE,
dispatch_time: Annotated[object, RealSet.open(0, oo)] = 1.0,
by_product_time: Annotated[object, RealSet.open(0, oo)] = 1.0,
perception_time: Annotated[object, RealSet.open(0, oo)] = 6.0,
expansion_time: Annotated[object, RealSet.open(0, oo)] = 12.0,
) -> None:
super().__init__(label)
self.paddy = paddy
self.rice = rice
self.husk = husk
self.bran = bran
self.capacity = capacity
self.harvest = harvest
self.seasonality = seasonality
self.procurement_delay = procurement_delay
self.milling_time = milling_time
self.outturn = outturn
self.husk_share = husk_share
self.bran_share = bran_share
self.dispatch_time = dispatch_time
self.by_product_time = by_product_time
self.perception_time = perception_time
self.expansion_time = expansion_time
class RiceMillInteraction(Interaction):
"""Procurement, milling, and sales of a rice mill, and the adjustment of its capacity.
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 RiceMillInteraction
>>> RiceMillInteraction('flows', target='mill').target
'mill'
"""
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] = RiceMillInteraction._get_rice_mill_laws(s)
@staticmethod
def _get_season(mean: Expression, amplitude: Expression, t: Expression, period: float) -> Expression:
"""Return a seasonal value that oscillates about its mean.
It has a relative amplitude and a period.
"""
return mean * (1 + amplitude * sin(2 * math.pi * t / period))
@staticmethod
def _get_procurement(s: SimpleNamespace) -> Expression:
"""Return the procurement of paddy.
It is the seasonal harvest after the procurement delay.
"""
season = RiceMillInteraction._get_season(s.harvest, s.seasonality, s.t, MONTHS_PER_YEAR)
return distributed_delay(season, s.procurement_delay, 3)
@staticmethod
def _get_indicated_milling(s: SimpleNamespace) -> Expression:
"""Return the indicated milling, the paddy milled over the milling time."""
return s.paddy / s.milling_time
@staticmethod
def _get_milling(s: SimpleNamespace) -> Expression:
"""Return the milling, the indicated milling limited by the capacity."""
return minimum(RiceMillInteraction._get_indicated_milling(s), s.capacity)
@staticmethod
def _get_rice_mill_laws(s: SimpleNamespace) -> list[BaseLaw]:
"""Return the procurement, milling, and sales of a rice mill.
It also returns the adjustment of its milling capacity.
"""
milling = RiceMillInteraction._get_milling(s)
losses = 1 - s.outturn - s.husk_share - s.bran_share
perceived = distributed_delay(RiceMillInteraction._get_indicated_milling(s), s.perception_time, conserved=False)
return [
RateLaw({s.paddy: +1}, RiceMillInteraction._get_procurement(s), label='paddy_purchases'),
RateLaw({s.paddy: -1, s.rice: +1}, s.outturn * milling, label='milled_rice'),
RateLaw({s.paddy: -1, s.husk: +1}, s.husk_share * milling, label='husk_output'),
RateLaw({s.paddy: -1, s.bran: +1}, s.bran_share * milling, label='bran_output'),
RateLaw({s.paddy: -1}, losses * milling, label='milling_losses'),
RateLaw({s.rice: -1}, s.rice / s.dispatch_time, label='rice_sales'),
RateLaw({s.husk: -1}, s.husk / s.by_product_time, label='husk_sales'),
RateLaw({s.bran: -1}, s.bran / s.by_product_time, label='bran_sales'),
RateLaw(
s.capacity,
(perceived - s.capacity) / s.expansion_time,
),
]
Equations
The equations are those that symbolic returns for the system with its default parameters.
mill.paddy: diff(paddy_mill(t), t) == (bran_share_mill + husk_share_mill + outturn_mill - 1)*where(capacity_mill(t) < paddy_mill(t)/milling_time_mill, capacity_mill(t), paddy_mill(t)/milling_time_mill) - bran_share_mill*where(capacity_mill(t) < paddy_mill(t)/milling_time_mill, capacity_mill(t), paddy_mill(t)/milling_time_mill) - husk_share_mill*where(capacity_mill(t) < paddy_mill(t)/milling_time_mill, capacity_mill(t), paddy_mill(t)/milling_time_mill) - outturn_mill*where(capacity_mill(t) < paddy_mill(t)/milling_time_mill, capacity_mill(t), paddy_mill(t)/milling_time_mill) + 3*_material_delay3_0_flows_2/procurement_delay_mill
mill.rice: diff(rice_mill(t), t) == outturn_mill*where(capacity_mill(t) < paddy_mill(t)/milling_time_mill, capacity_mill(t), paddy_mill(t)/milling_time_mill) - rice_mill(t)/dispatch_time_mill
mill.husk: diff(husk_mill(t), t) == husk_share_mill*where(capacity_mill(t) < paddy_mill(t)/milling_time_mill, capacity_mill(t), paddy_mill(t)/milling_time_mill) - husk_mill(t)/by_product_time_mill
mill.bran: diff(bran_mill(t), t) == bran_share_mill*where(capacity_mill(t) < paddy_mill(t)/milling_time_mill, capacity_mill(t), paddy_mill(t)/milling_time_mill) - bran_mill(t)/by_product_time_mill
mill.capacity: diff(capacity_mill(t), t) == (_smoothing_delay1_1_flows_0 - capacity_mill(t))/expansion_time_mill