# BATCH

Groups parts or orders into batches, pallets or lots of a set size, or after a maximum wait, and sends the batch on as one entity.

Groups entities by count: puts each arrival in the block's store and, as soon as `batch_size` items are waiting, takes exactly that many atomically to form one batch entity. Members leave live_entities and carry `in_batch` until a SEPARATE splits the batch again.

With `max_wait` set, a PARTIAL batch leaves too: the timer is armed by the arrival that starts a new group and releases whatever is waiting once the oldest member has waited that long. TIE RULE: a batch that fills in the same instant as the timer expires wins and the timer releases nothing -- the same zero-delay re-check reneging uses, so the outcome never depends on the order SimPy happened to schedule two events at one timestamp.

With `carrier_type` set, an arrival of that type is a CARRIER (a truck): it is not stored, and loads the stored arrivals one at a time, `member_time` each, until it holds `carrier_capacity` (default `batch_size`) or its `max_wait`, counted from its own arrival, has passed; then it leaves as itself, its members aboard.

With a `pattern` too, the carrier is a pallet: each member takes the next slot of that pallet pattern, and the pallet closes when its slots are full or the next member would pass its weight limit (that member waits for the next pallet). The members carry `slot` and `layer`; the pallet carries `items`, `layers`, `load_weight`, `load_height` and `pattern`.

Family: group. Python builder: `m.batch(...)`.

## Typical uses

Use BATCH when entities travel or are processed together as one unit: a pallet, a lot, a tray, a truck load. It groups any entities by count. If you need one entity of each of several different kinds, such as a part and its kit, use [MATCH](match.md) instead. [SEPARATE](separate.md) splits a batch again.

## Behaviour and rules

Each arrival waits in the block's store. When `batch_size` entities are waiting, the block takes exactly that many and sends one new entity of type `batched_entity_type` down the link. The members stop counting as work in progress and carry the batch id until a SEPARATE splits them. Their costs move into the batch.

With `max_wait` set, a partial batch also leaves. The timer starts when the first member of a new group arrives. A batch that fills in the same instant wins, and the timer releases nothing. Without `max_wait`, entities wait for a full batch for ever.

Setting `carrier_type` turns the block into a loading dock. An arriving entity of that type, such as a truck, is not stored. It loads the waiting entities one at a time, each taking `member_time`, until it holds `carrier_capacity` members or its `max_wait` has passed. Then it leaves as itself, with its members aboard. A negative `carrier_capacity` is an error.

Waiting entities show in the `store_summary` frame, with their average size and time in the store.

## Ports

- Inputs: any number of incoming links.
- Outputs: one outgoing link.

## Parameters

| Parameter | Type | Default | Required | Quantity | Unit key | Description |
|---|---|---|---|---|---|---|
| `batch_size` | int | `2` |  | count |  | Number of entities grouped into one batch entity. |
| `batched_entity_type` | str | `'BATCH'` |  |  |  | Entity type assigned to the batch entity. |
| `max_wait` | str | `''` |  | time | `max_wait_time_unit` | How long a partial batch waits before it leaves anyway, e.g. "30" or "entity.deadline"; measured from the arrival that started the group. A batch that fills in the same instant wins. Empty, 0 or less = wait for a full batch for ever. |
| `max_wait_time_unit` | str | `'base'` |  |  |  | Unit `max_wait` is expressed in: "base" (the model's own unit) or one of seconds/minutes/hours/days. |
| `carrier_type` | str | `''` |  |  |  | Carrier mode: an arriving entity of this type (a truck) is a CARRIER that waits here and loads the other arrivals one at a time, until it is full or its max_wait (the cut-off, measured from the carrier's arrival) runs out, then leaves as itself. Empty = a plain BATCH. |
| `carrier_capacity` | str | `''` |  | count |  | Carrier mode: how many members a carrier takes, e.g. "entity.capacity", evaluated for each carrier when it arrives. Empty = batch_size. |
| `member_time` | str | `''` |  | time | `member_time_unit` | Carrier mode: time to load one member, evaluated in the member's own scope when its load starts. Empty = loading takes no time. |
| `member_time_unit` | str | `'base'` |  |  |  | Unit `member_time` is expressed in: "base" (the model's own unit) or one of seconds/minutes/hours/days. |
| `pattern` | str | `''` |  |  |  | Carrier mode: the carrier is a pallet loaded by this record of "pallet_patterns", named outright ("Euro") or chosen per pallet by an expression in the carrier's scope. Each member takes the next slot; the pallet closes at the pattern's limits, and carrier_capacity is not used. Empty = no pattern. |
| `place_per` | str | `'item'` |  |  |  | With a pattern: "item" loads one member per member_time; "layer" waits for a whole layer and places it in one member_time (a layer-forming palletizer). |
| `robot` | str | `''` |  |  |  | Name of a record in "robots": with a pattern, each item's time is this robot's cycle to that item's own slot, and member_time is not used. Empty = member_time. |
| `robot_at` | list | `[0, 0, 0]` |  |  |  | Where the robot stands, from this block (the pallet's place): [dx, dy, rotation] in the model's length unit and degrees, dx along the block's facing, dy toward the floor plan's +y side at rotation 0. |
| `targets` | dict | `{}` |  |  |  | The robot's named targets, as on MOTION; "slot" (the item's place on the pallet) and "slot_above" (approach higher) are given per slot and are not defined here. |
| `segments` | list | `[]` |  |  |  | The robot's cycle, as on MOTION: ptp and lin moves to targets, with timed pick and place segments, every time in seconds. A move without its own payload_kg carries the pattern's item weight. |
| `approach` | str | `''` |  | length |  | How far slot_above is over the slot, in the model's length unit. Empty = 0.3 m. |

## Statistics

Every run fills `system` and `entity_summary` (see the [KPI reference](../kpis.md)). The example below also fills:

| Frame | Columns |
|---|---|
| [`store_summary`](../kpis.md#store_summary) | `Run`, `Store`, `Capacity`, `Average Size`, `Max Size`, `Mode Size`, `Mode Size %`, `P95 Size`, `P99 Size`, `Std Size`, `Avg Time in Store`, `Mode Time in Store`, `Mode Time in Store %`, `P95 Time in Store`, `P99 Time in Store`, `Std Time in Store`, `Util % (time-weighted)` |
| `material_flow_summary` | `Run`, `From`, `To`, `Entities` |

## Problem codes

A check of the model reports these about this block:

| Code | Meaning |
|---|---|
| `E_BATCH_CARRIER_CAPACITY` | A carrier BATCH's carrier_capacity came out below 0 for a carrier (a run-time error). |
| `E_BATCH_MEMBER_TIME` | A BATCH has a member_time or a carrier_capacity but no carrier_type. |
| `E_INITIAL_CARRIER` | An initial_state entry of the carrier type waits at a carrier BATCH: its cut-off cannot be put back. |
| `E_PALLET_UNKNOWN` | A carrier BATCH names a pallet pattern the model does not declare, or a robot palletizes by a pattern chosen per pallet, which cannot be planned before the run. |
| `E_ROBOT_TARGET` | A robot move names no target of its block, a target is not a joint vector, a frame or a point on a block, or a pattern BATCH or SEPARATE defines the per-slot target slot or slot_above itself. |
| `E_ROBOT_UNKNOWN` | A MOTION block, or a BATCH or SEPARATE that works by a pallet pattern, names a robot the model does not declare, or has robot moves and names none. |

## Example

The snippet seeds itself; its output is from a run made when this page was generated.

```python
from nagasim import build as nb

m = nb.Model(time_limit=200)
m.create(distribution=nb.Constant(value=2), max_arrivals=30) >> m.batch(batch_size=5, batched_entity_type="PALLET") >> m.dispose()

results = m.run(seed=1)
print(int(results.kpi("system/Total Entities Completed")), "completed: 30 parts leave as 6 pallets of 5")
```

```text
6 completed: 30 parts leave as 6 pallets of 5
```

## Used in

Bundled models: [Order-driven warehouse (library assets)](../../gallery/logistics.md), [Automation floor (robot cells, AMR, palletiser)](../../gallery/manufacturing.md), [Make-to-order line (library assets)](../../gallery/manufacturing.md), [Palletizing line](../../gallery/manufacturing.md), [HOLD on a store's contents](../../gallery/teaching.md).

Guides: [Digital twin](../../digital-twin.md), [How do I pack parts on a pallet and unpack them later?](../../how-to/recipes/unpack-pallet.md), [Batching and matching](../../modelling-guide/batching-matching.md), [Coming from AnyLogic](../../coming-from/anylogic.md), [Coming from Arena](../../coming-from/arena.md), [Coming from salabim](../../coming-from/salabim.md) and 8 more.

## See also

- [All blocks](index.md), [KPI reference](../kpis.md), [Model file keys](../schema.md), [Model file format](../../model-file-format.md)
- Same family (group): [DUPLICATE](duplicate.md), [MATCH](match.md), [SEPARATE](separate.md)
