Search code examples
randomsimulationanylogicparking

Parking randomly in Anylogic


I am using AnyLogic's Traffic Road Library and I know it's possible in the car Source module to define the initial position of the car in a parking lot.

I want the parking lot space to be randomly chosen. I know there is this function randomFreeSpaceIndex() that returns the index of randomly chosen free parking space.

But I do not know how to call it on my car source or use it with the CarMoveTo tool. Here is my model so far.

Any help is appreciated!


Solution

  • I'm pretty sure that you can't with the standard parking lot, so you have to unfortunately create many parkinglots with 1 parking space as you can see in the following image: parking spot

    With that being done, now you can select a random parking space with a model that could look for example like this: model

    In this model I created a collection of parkingLots... all with 1 parking space: collection

    In the select output block you ask if there is space available:

    for(ParkingLot p : parkingSpaces){
        if(p.nFree()>0)
            return true;
    } 
    return false;
    

    And in the carMoveTo you call the function selectRandomParkingSpace(): randomParkingSpace

    selectRandomParkingSpace uses the following code:

    ArrayList <ParkingLot> freeSpaces=new ArrayList();
    for(ParkingLot p : parkingSpaces){
        if(p.nFree()>0){
            freeSpaces.add(p);
        }
    }
    int randomSpace=uniform_discr(0,freeSpaces.size()-1);
    return freeSpaces.get(randomSpace);
    

    That is my solution.