Anylogic:基于参数实现多类型代理按比例下车配置问询
Hey there! Let's break down how to get your train drop-off logic working smoothly. I see you've started configuring the Drop-off block, but there are a few key tweaks to ensure it correctly removes 20% of children and 40% of teenagers at your specified station.
1. Fix the Quantity Calculation Syntax
First, your current formula children.size(0.2) + teenagers.size(0.4) has a syntax issue: the size() method for agent collections doesn’t accept a percentage parameter. Instead, you need to calculate the number of agents to drop off by multiplying the collection’s total size by your target percentage, then handle rounding to get a whole number (since you can’t drop off a fraction of an agent).
Replace your Quantity formula with this more robust version:
Math.min(Math.round(children.size() * 0.2), children.size()) + Math.min(Math.round(teenagers.size() * 0.4), teenagers.size())
Math.round()converts the percentage-based decimal to an integer (e.g., 2.3 children becomes 2, 2.6 becomes 3)Math.min(..., collection.size())acts as a safety net: it ensures you never try to drop off more agents than actually exist on the train, which prevents runtime errors.
2. Optimize Drop-off Block Configuration (Better Than a Single Total)
Instead of combining the quantities into one total, split the drop-off into separate rules for each agent type—this makes the logic clearer and more reliable:
- Open your Drop-off block’s properties.
- In the Agents to drop off section, select Specific agents.
- Add two separate rows:
- For children: Set
Agent typetochildren, andQuantitytoMath.min(Math.round(children.size() * 0.2), children.size()) - For teenagers: Set
Agent typetoteenagers, andQuantitytoMath.min(Math.round(teenagers.size() * 0.4), teenagers.size())
- For children: Set
This way, the block explicitly targets each group and removes the exact number you specify, rather than just a combined total.
3. Optional: Switch to Probabilistic Drop-offs (More Realistic Behavior)
If you want a more natural, stochastic outcome (each child has a 20% chance to exit, each teenager a 40% chance), use a Select Output block instead of a fixed quantity:
- Place a Select Output block before the Drop-off block.
- For the children branch: Set the condition to
randomTrue(0.2)—this sends 20% of children to the Drop-off. - For the teenagers branch: Use
randomTrue(0.4)for their 40% exit chance. - Connect the remaining output of the Select Output back to the train’s main flow for agents staying on board.
This approach mimics real-world behavior better than fixed percentages of the group size.
4. Test to Validate the Logic
Always validate your setup with quick tests:
- Add a Text element in your model to display
children.size()andteenagers.size()before and after the drop-off station. - Run the model and check that after the station, children count is ~80% of the original, and teenagers are ~60% (or exactly the rounded number if using fixed quantities).
- If you see errors about removing too many agents, double-check that you’re using
Math.min()to cap the quantity at the actual collection size.
内容的提问来源于stack exchange,提问作者Thabang Marote

