You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AnyLogic交通道路库:如何在Car Source中随机选初始停车位?

How to Randomize Initial Parking Spots in AnyLogic's Traffic Road Library

Got it, let's break this down clearly—using randomFreeSpaceIndex() to assign random initial parking spots in your Car Source is straightforward once you know the setup steps, and pairing it with CarMoveTo to get vehicles moving is easy too. Here's exactly what to do:

Step 1: Confirm Your Parking Lot Setup

First, make sure you have a configured ParkingLot object in your model (let's assume you've named it parkingLot for this example—replace this with your actual parking lot's name everywhere below). Double-check that it has defined parking spaces and that some are set to be free initially.

Step 2: Configure Car Source to Use Random Free Spots

Open your Car Source's properties panel and navigate to the Initial Position section:

  • Select the option At a node/point/area, then click the expression (fx) button next to the position field.
  • Paste this code (adjust the parking lot name if needed):
    int randomSpotIndex = parkingLot.randomFreeSpaceIndex();
    // Handle case where no spots are free to avoid errors
    if (randomSpotIndex != -1) {
        return parkingLot.getSpace(randomSpotIndex).getCenter();
    } else {
        // Fallback to a default point if all spots are occupied
        return new Point(0, 0);
    }
    
  • To set the vehicle's initial direction to match the parking spot (so it's facing the right way when it starts moving), go to the Initial Direction field, click the fx button, and use:
    int randomSpotIndex = parkingLot.randomFreeSpaceIndex();
    return (randomSpotIndex != -1) ? parkingLot.getSpace(randomSpotIndex).getDirection() : 0;
    

Step 3: Pair with CarMoveTo to Exit the Parking Lot

If you want vehicles to leave the parking spot after spawning, here's how to link CarMoveTo:

  1. Add a CarMoveTo module to your Car agent type (name it moveToParkingExit for clarity).
  2. Go back to your Car Source's Action on create field, and add this code to set the target to your parking lot's exit and start the movement:
    int randomSpotIndex = parkingLot.randomFreeSpaceIndex();
    if (randomSpotIndex != -1) {
        // Set target to the parking lot's exit node
        agent.moveToParkingExit.setTarget(parkingLot.getExit());
        // Start the movement
        agent.moveToParkingExit.start();
    }
    

Key Notes to Avoid Issues

  • Always check if randomFreeSpaceIndex() returns -1—this means no spots are free, and using that index will cause errors. The fallback code above prevents this.
  • If you have multiple parking lots, make sure you reference the correct one in all expressions.
  • Test with a small number of spots first and add a trace log (like traceln("Assigned spot index: " + randomSpotIndex)) to verify the randomization is working as expected.

内容的提问来源于stack exchange,提问作者user9463018

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:08:41