AnyLogic供应链配送仿真中车辆Agent非运行时间测量方法咨询
Hey there! Let's break down how to track that idle time (non-movement time between tasks) for your 3 delivery vehicles in AnyLogic—both per-vehicle metrics and the fleet average. I’ll cover two practical approaches based on your initial idea and the TimeMeasurement tools you mentioned.
Approach 1: Use AnyLogic's TimeMeasurement API
This is the most straightforward way to directly track idle periods, as it handles start/stop timing automatically.
Step 1: Add a TimeMeasurement tracker to your Vehicle Agent
- Open your Vehicle Agent's Variables panel.
- Add a new variable with:
- Name:
idleTimeTracker - Type:
TimeMeasurement - Initial value:
new TimeMeasurement()(or initialize it in the agent's On Startup action:idleTimeTracker = new TimeMeasurement();)
- Name:
Step 2: Trigger Start/Stop Timing
You need to tell the tracker when the vehicle enters idle mode and when it exits to start a new task:
- Start idle timing: When the vehicle finishes a delivery and has no next task. For example, in the On Arrival action of your
moveTo(ortransport) module (after unloading):idleTimeTracker.start(); - Stop idle timing: Right before the vehicle starts moving to a new customer. For example, in the code block where you assign a new delivery task:
Pro tip: If your vehicle starts idle at simulation launch, addidleTimeTracker.stop(); // Then trigger your movement code, e.g., moveTo(nextCustomer);idleTimeTracker.start();to the Vehicle Agent's On Startup action.
Step 3: Collect and Analyze Data
Per-Vehicle Idle Time
At any point (e.g., simulation end), you can get a single vehicle's total idle time with:
// Replace 'myVehicle' with your specific vehicle agent reference double vehicleIdleTime = myVehicle.idleTimeTracker.getTotalTime();
This returns time in your model's configured unit (e.g., hours, minutes—check your model settings!).
Fleet Average Idle Time
In your main agent (the one holding the fleet), add code to aggregate data when the simulation ends (put this in the main agent's On Simulation End action):
// Store all idle times in a list List<Double> allVehicleIdleTimes = new ArrayList<>(); for (Vehicle vehicle : yourVehiclePopulation) { // Replace with your fleet population reference allVehicleIdleTimes.add(vehicle.idleTimeTracker.getTotalTime()); } // Calculate average double averageIdleTime = 0; if (!allVehicleIdleTimes.isEmpty()) { averageIdleTime = allVehicleIdleTimes.stream() .mapToDouble(Double::doubleValue) .average() .orElse(0); } // You can log this or display it in a Text component traceln("Average fleet idle time: " + averageIdleTime);
Approach 2: Calculate via Total Simulation Time - Total Movement Time
If you prefer your initial idea, this method works by subtracting the vehicle's total travel time from the total simulation time. Note: Adjust this if you need to exclude non-idle, non-movement time (like loading/unloading) — we’ll cover that too.
Step 1: Track Total Movement Time per Vehicle
In your Vehicle Agent's Variables panel, add:
- Name:
totalTravelTime - Type:
double - Initial value:
0 - Optional:
moveStartTime(Type:double) to track when each movement starts.
- Name:
Log movement start/end times:
- When starting a movement: In the code block before triggering
moveTo/transport, add:moveStartTime = time(); - When finishing a movement: In the On Arrival action of the movement module, add:
totalTravelTime += time() - moveStartTime;
- When starting a movement: In the code block before triggering
Step 2: Calculate Idle Time
Per-Vehicle Idle Time
- If "non-movement time" = all time not spent moving (including loading/unloading):
double totalIdleTime = time() - totalTravelTime; - If you want true idle time (excluding task work like loading/unloading):
Add another variabletotalTaskTime(double, initial 0) to track loading/unloading duration. Update it during those tasks, then:double trueIdleTime = time() - totalTravelTime - totalTaskTime;
Fleet Average Idle Time
Similar to Approach 1, aggregate data in your main agent's On Simulation End action:
double totalFleetIdleTime = 0; for (Vehicle vehicle : yourVehiclePopulation) { totalFleetIdleTime += (time() - vehicle.totalTravelTime); // Use vehicle.trueIdleTime if you calculated true idle time } double averageIdleTime = totalFleetIdleTime / yourVehiclePopulation.size(); traceln("Average fleet idle time: " + averageIdleTime);
Key Tips for Implementation
- Time Unit Consistency: Always confirm your model's time unit (found in Model Settings) to avoid misinterpreting values (e.g., a value of 2 could mean 2 hours or 2 minutes).
- Real-Time Visualization: Add a
Textcomponent orBar Chartto your main agent’s canvas, binding it to the idle time variables to monitor progress during simulation. - Edge Cases: Handle vehicles that never receive tasks (their idle time should equal total simulation time) by ensuring your timing logic starts correctly on agent startup.
内容的提问来源于stack exchange,提问作者Natasha Jones

