Dynamic batches

Here’s a little trick I use to generate dynamic batches, which are batches that are of dynamic size and where products that need to be batched come in a disorganized fashion.

Let’s say for instance that we have to batch products in boxes, and each product has a label with the client and each box can contain products only for that client. Also the products arrive randomly and it’s not possible to know to which client the next product has assigned. How can we solve this in AnyLogic?

One way is using the following structure:

And each time a product enters the wait block, using the following code:

List <Product> products=findAll(wait,p->p.client.equals(agent.client)); // create a list of products that belong the same client as the agent that arrived

if(products.size()==agent.batchSize){ //check if the batch size is reached


for(Product p :products){




If you have a client agent and a fixed size for the batch, instead of using that structure, it’s also possible to send the arriving agent to appropriate client through an exit block using something like to take the agent to the enter block in the right client



All Comments:

  1. Andrey

    February 28, 2021

    Hi Felipe,
    I noticed there is one problem with your scheme. If all agents (e.g. products for one client to be packed in a number of boxes) arrive in one time (not one by one) , block WAIT completes one batch (one box) only and other products are stuck in this block. I found that the WAIT block works correctly if agents arrive one by one. My solution was to put block DELAY (with 1 sec delay) between SOURCE and WAIT to ensure one-by-one flow is maintained (this is because my SOURCE reads data from database and I can’t set up one-by-one agent generating directly in the SOURCE). Thank you for this idea!

    • admin

      March 11, 2021

      yeah i use a delay block before the wait sometimes too… you are correct about that, I should update this article… but now that you wrote this comment, I don’t need to.

Your Comment: