Search code examples
anylogicagent

AnyLogic: how to get 2nd or 3rd nearest agents?


In agent based model, let say I have 10 locations and in each location I have random number of people(10~20). Also, I have 5 facility locations. Now I can send all people from each people-location to nearest facility using getNearestAgent(). But I want to send 70% people to nearest one, 20% to second nearest one and 10% to 3rd nearest facility. How can I do that?

I put all locations in GIS map. And successfully send people to nearest facility.

Actual implementation


Solution

  • Make a function that takes as argument the person and returns a location:

    Location nearest=person.getNearestAgent(locations);
     if(randomTrue(0.7)){
          return nearest;
     }else{
          
          List <Location> otherLocations=findAll(locations,l->!l.equals(nearest));
          Location secondNearest=person.getNearestAgent(otherLocations);
          if(randomTrue(2.0/3.0){
                return secondNearest;
          }else{
               List <Location> otherLocations2=findAll(otherLocations,l->!l.equals(secondNearest));
               return person.getNearestAgent(otherLocations2);
          }
    
     }