AnyLogic中多物料按指定顺序进入工位及传送带的实现方法咨询
Hey there! Let's work through your two AnyLogic sorting requirements step by step. I get why using just agent.priority didn't cut it—your needs involve multiple independent sequencing rules, which global priority can't handle on its own. Here's how to fix it:
Your goal: Randomly generated materials (types 1, 2, 3) need to reach 3 workstations in distinct repeating orders:
- Station 1: 1 → 2 → 3 → 1...
- Station 2: 2 → 3 → 1 → 2...
- Station 3: 3 → 1 → 2 → 3...
Solution: Use Pull Blocks + State Tracking
This setup lets each workstation actively pull the exact material type it needs next, regardless of generation order:
- Add a type identifier to your material agent:
- Open your agent's properties, add an
intparameter namedtype, and set it to 1, 2, or 3 when generating agents (configure this in your Source block's "On create" action).
- Open your agent's properties, add an
- Create a central queue:
- Add a
Queueblock namedmainQueue—all randomly generated materials first enter this queue.
- Add a
- Add state variables for each workstation:
- In your main agent's properties, create three
intvariables:nextTypeStation1 = 1(Station 1 starts needing type 1)nextTypeStation2 = 2(Station 2 starts needing type 2)nextTypeStation3 = 3(Station 3 starts needing type 3)
- In your main agent's properties, create three
- Add
Pullblocks for each workstation:- Create three
Pullblocks (pullStation1,pullStation2,pullStation3), each linked tomainQueue. - Configure each pull block's Pull Condition:
- For
pullStation1:agent.type == nextTypeStation1 - For
pullStation2:agent.type == nextTypeStation2 - For
pullStation3:agent.type == nextTypeStation3
- For
- Add an On Pulled action to each pull block to update the next needed type:
pullStation1action:nextTypeStation1 = nextTypeStation1 == 3 ? 1 : nextTypeStation1 + 1;pullStation2action:nextTypeStation2 = nextTypeStation2 == 3 ? 1 : nextTypeStation2 + 1;pullStation3action:nextTypeStation3 = nextTypeStation3 == 3 ? 1 : nextTypeStation3 + 1;
- Create three
- Link to workstations:
- Connect each
Pullblock to its corresponding workstation (e.g., aProcessblock).
- Connect each
This way, each workstation will only receive materials in your exact desired order—even if materials are generated randomly.
Your goal: Three Sources generating MyAgent1, MyAgent2, MyAgent3 must enter the final conveyor in the repeating order MyAgent2 → MyAgent1 → MyAgent3, one at a time.
Solution: Queue Blocks + Coordination Event
This ensures strict sequential entry, even if agents are generated at different speeds:
- Add a type identifier to your agents:
- If
MyAgent1/2/3share a parent agent, add anintparameteragentType(set 1 for MyAgent1, 2 for MyAgent2, 3 for MyAgent3). If not, you can useagent instanceof MyAgent2for checks, but a type parameter is cleaner.
- If
- Create per-agent queues:
- Add three
Queueblocks (queueAgent1,queueAgent2,queueAgent3), each linked to its corresponding Source. Set each queue's Release Condition tofalse—we'll manually release agents via code.
- Add three
- Add a state variable for sequencing:
- In your main agent, create an
intvariablenextExpectedAgent = 2(start with MyAgent2).
- In your main agent, create an
- Add a coordination event:
- Add an
Eventblock namedtriggerRelease. Set its Action to:// Release the next expected agent if it's available if (nextExpectedAgent == 2 && !queueAgent2.isEmpty()) { queueAgent2.release(queueAgent2.getFirst()); } else if (nextExpectedAgent == 1 && !queueAgent1.isEmpty()) { queueAgent1.release(queueAgent1.getFirst()); } else if (nextExpectedAgent == 3 && !queueAgent3.isEmpty()) { queueAgent3.release(queueAgent3.getFirst()); } // Update the next expected agent (cycle back to 2 after 3) nextExpectedAgent = switch(nextExpectedAgent) { case 2 -> 1; case 1 -> 3; case 3 -> 2; default -> 2; };
- Add an
- Link to final conveyor and auto-trigger the event:
- Connect all three queues to your final conveyor.
- Add an On Entry action to the final conveyor:
triggerRelease.restart(); // Trigger next release after current agent enters - In your main agent's On startup action, add
triggerRelease.restart();to start the sequence.
This setup will wait for the required agent to be available, release it to the conveyor, then immediately prepare for the next in order—ensuring strict one-at-a-time sequential entry.
内容的提问来源于stack exchange,提问作者Afif

