AnyLogic中如何定位发起请求的特定Passenger智能体?
Great question! Let's break this down into clear, actionable parts—first confirming agent identifiers, then solving your core problem of targeting the right passenger for a response.
Yes, All Agents Have Unique IDs
Every agent in AnyLogic comes with a built-in unique identifier via the getId() method. This returns a long value guaranteed to be unique for every agent in your model. That said, you don't need to rely on this ID for your use case—there's a far more straightforward way to reference the exact passenger who sent the request.
The Correct Way to Send the Response Back
Your initial attempts didn't work because you tried accessing passengers via population indices, which aren't tied to the requesting agent. Instead, leverage the message itself to carry a reference to the sender. Here are two reliable approaches:
Approach 1: Use the Built-in Message Sender Reference
AnyLogic's custom message types (like your OrderPassenger) inherit from the base Message class, which includes a getSender() method. This directly returns the agent that sent the message.
When your
Passengersends the request:// Send the OrderPassenger request to your Airport agent send(new OrderPassenger(), main.airport); // Replace main.airport with your actual Airport agent referenceIn your
Airportagent's message reception event (where you handle theOrderPassengerrequest):// Retrieve the incoming request message OrderPassenger receivedRequest = (OrderPassenger) getCurrentMessage(); // Cast the sender to Passenger type (since getSender() returns a generic Agent) Passenger requestingPassenger = (Passenger) receivedRequest.getSender(); // Send the AirportSuggestion back to the exact passenger who initiated the request send(airportSuggestion, requestingPassenger);
Approach 2: Add a Custom Sender Field to Your Message Type
If you prefer explicit control, add a dedicated field to your OrderPassenger message to store the sender:
Edit your
OrderPassengermessage type in AnyLogic's palette:- Add a parameter with type
Passengerand name itsender(or any name you prefer).
- Add a parameter with type
When sending the request from the
Passengeragent:// Pass the current passenger (this) as the sender in the request OrderPassenger request = new OrderPassenger(this); send(request, main.airport);In the
Airportagent's message handling code:OrderPassenger receivedRequest = (OrderPassenger) getCurrentMessage(); // Directly access the sender from the message's custom field send(airportSuggestion, receivedRequest.sender);
Why Your Initial Attempts Failed
main.passengers(orderPassenger.passenger)failed becauseorderPassenger.passengerwasn't an integer index (the population'sget()method expects a position number, not an agent reference).main.passengers(0)only returns the first agent in thepassengerspopulation, which has no connection to the agent who actually sent the request.
Both approaches above ensure you send the AirportSuggestion to the exact Passenger that initiated the request—no guesswork required.
内容的提问来源于stack exchange,提问作者jo_pic

