Skip to content

faq 76546051

Billy Charlton edited this page Sep 5, 2018 · 2 revisions

get plan index with PlanImpl

by GregoryM on 2016-08-04 01:34:51


I'm building a population, and would like to send all of my agents back to their home location, where their plan is constrained to have started (my source data is missing the final home activity).

I noticed a `PlanImpl.getFirstActivity` method that would seem to allow me to access the coordinates of the first activity. Perfect!

    // The output file doesn't send anyone home at the end of the day.
     // This loops over the entire population and creates a home activity that 
     // ends at 4 AM the next morning.
     Population pop = scenario.getPopulation(); 
     PopulationFactory pb = pop.getFactory();
    
     for (Person person : pop.getPersons().values()) {     
       // Get coordinates for the first activity
       // first, we get the index of the plan
       int planIndex = person.getPlans().size();
       PlanImpl plan = (PlanImpl) person.getPlans().get(planindex - 1);
       Coord homeLocation = plan.getFirstActivity().getCoord();

       Activity finalHome = pb.createActivityFromCoord("Home", homeLocation);
       finalHome.setEndTime(28 * 3600);
       plan.addActivity(finalHome);

     }

Except when this runs, I get an `IndexOutOfBoundsException` on the `person.getPlans().get()` call. I know that my population has plans (the value of `planIndex` is 1), but no integer in `get()` seems to be accepted.

Am I going about this the right way?


Comments: 1


Re: get plan index with PlanImpl

by GregoryM on 2016-08-04 10:12:38

As I was thinking about this, I realize that I do have some agents that don't make any trips (they stay at home all day), and this could be interfering.

Thanks for the note about the deprecation. For posterity, the following works:

// The output file doesn't send anyone home at the end of the day.
 // This loops over the entire population and creates a home activity that 
 // ends at 4 AM the next morning.
 Population pop = scenario.getPopulation(); 
 PopulationFactory pb = pop.getFactory();
 for (Person person : pop.getPersons().values()) {     

   // Get coordinates for the first activity
   // first, we get the index of the plan
   Plan plan = person.getPlans().get(0);
   int numberActivities = plan.getPlanElements().size();
   if(numberActivities > 1){
     Activity firstHome = (Activity) plan.getPlanElements().get(0);
     Activity finalHome = pb.createActivityFromCoord("Home", firstHome.getCoord());
     finalHome.setEndTime(28 * 3600);
     plan.addActivity(finalHome);
   }

 }
Clone this wiki locally