Skip to content
Advertisement

Anylogic-how to calculate distance between pedestrians

I use the pedestrian library (use the ped source, ped Goto, and ped sink) and want to simulate the sidewalk environment. The goal of this model is to get the data that the distance between pedestrians is less than 1m. So, I try to calculate the distance between pedestrians. In the Anylogic, the information of pedestrians can be collected by using getX(),getY, and getId (can be calculated per second). but I don’t know how to select the pedestrian agents and calculate the distance between them. I mean, if there are 10 pedestrians (id: 1, 2, 3…), how to get the distance between 1 and 2, 1 and 3, 2 and 3 …every second?

Advertisement

Answer

Create an event that, every second, loops through all pedestrians. Make sure all pedestrians are actually added to a custom agent population in your PedSource (makes it easier to loop through them).

In the event, use a nested for-loop:

for (Pedestrian currPed : myPedPopulation) {
    for (Pedestrian currOtherPed : myPedPopulation) {
        if (currPed.equals(currOtherPed) break; // not needed
        double distance = currPed.distanceTo(currOtherPed);
        // do with this what you want :)
    }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement