}
// If the adult has a job and she has to take children to school
if (hasJob && (childrenToTakeToSchool != null)) {
// Get the children's school
School school = childrenToTakeToSchool.get(0).getSchool();
// Estimate distance between home->school and home->work
float distanceHomeSchool = home.getAddress().DistanceFrom(school.getClosestEdge());
float distanceHomeWork = home.getAddress().DistanceFrom(jobLocation);
// Estimate the time needed to travel from school->home->work,
// school->home and home->work
int estimatedTravelTimeSchoolHomeWork = Math.round((distanceHomeSchool + distanceHomeWork) / Constants.averageSpeed);
int estimatedTravelTimeHomeSchool = Math.round(distanceHomeSchool / Constants.averageSpeed);
int estimatedTravelTimeHomeWork = Math.round(distanceHomeWork / Constants.averageSpeed);
int timeBetweenSchoolOpeningAndJobStarting = job.getStartingTime() - school.getTimeOpening();
// Calculate the time to leave the house so that the child will
// be at school on time
int timeOfDepartureFromHouseToSchool = Math.max(0, school.getTimeOpening() - estimatedTravelTimeHomeSchool);
// If the adult cannot go from school back home, stay for 20 minutes
// and then go to work, then go straight from school to work
if ((estimatedTravelTimeSchoolHomeWork + 20 * 60) > timeBetweenSchoolOpeningAndJobStarting) {
// Create route for home->school->work
List<Edge> tripEdges = new ArrayList<Edge>();
tripEdges.add(home.getAddress());
tripEdges.add(school.getClosestEdge());
tripEdges.add(jobLocation);
String route = MultipleTripExpander.ExpandTrips(tripEdges);
// Write the route
routesList.add(new Route(timeOfDepartureFromHouseToSchool, "<vehicle id=\"home-school-work-" + String.valueOf(home.getId()) + "-" + String.valueOf(counter) + "\" type=\"" + vehicle.getName() + "\" depart=\"" + String.valueOf(timeOfDepartureFromHouseToSchool) + "\" color=\"" + color + "\">" + "<route color=\"" + color + "\">" + route + "</route>" + "</vehicle>"));
}
// If there is enough time go back home and later go to work
else {
// Create route for home->school->home
List<Edge> tripEdges = new ArrayList<Edge>();
tripEdges.add(home.getAddress());
tripEdges.add(school.getClosestEdge());
tripEdges.add(home.getAddress());
String route = MultipleTripExpander.ExpandTrips(tripEdges);
// Write the route
routesList.add(new Route(timeOfDepartureFromHouseToSchool, "<vehicle id=\"home-school-home-" + String.valueOf(home.getId()) + "-" + String.valueOf(counter) + "\" type=\"" + vehicle.getName() + "\" depart=\"" + String.valueOf(timeOfDepartureFromHouseToSchool) + "\" color=\"" + color + "\">" + "<route color=\"" + color + "\">" + route + "</route>" + "</vehicle>"));
// Estimate what time the adult has to leave home to be at work
// on time
int timeOfDepartureFromHouseToWork = Math.max(0, job.getStartingTime() - estimatedTravelTimeHomeWork);
// Write the trip home->work
tripsList.add(new Trip(timeOfDepartureFromHouseToWork,"<trip id=\"house-work-" + String.valueOf(home.getId()) + "-" + String.valueOf(counter) + "\" depart=\"" + String.valueOf(timeOfDepartureFromHouseToWork) + "\" from=\"" + home.getAddress().getId() + "\" to=\"" + jobLocation.getId() + "\" color=\"" + color + "\" type=\"" + vehicle.getName() + "\" />\n"));
}
}
// If the adult has a job but doesn't have to take children to school
else if (hasJob && (childrenToTakeToSchool == null)) {
// Just go to work
// Try to estimate the distance between home and work
float distanceToTravel = home.getAddress().DistanceFrom(jobLocation);
// Estimate the time needed to cover this distance
int estimatedTravelTime = Math.round(distanceToTravel / Constants.averageSpeed);
// Calculate the time to leave the house so that the adult will be
// at work on time
int timeOfDepartureFromHouse = Math.max(0, job.getStartingTime() - estimatedTravelTime);
// Create route from house to work
tripsList.add(new Trip(timeOfDepartureFromHouse,"<trip id=\"house-work-" + String.valueOf(home.getId()) + "-" + String.valueOf(counter) + "\" depart=\"" + String.valueOf(timeOfDepartureFromHouse) + "\" from=\"" + home.getAddress().getId() + "\" to=\"" + jobLocation.getId() + "\" color=\"" + color + "\" type=\"" + vehicle.getName() + "\" />\n"));
}
// If the adult is unemployeed and has to take children to school
else if (!hasJob && (childrenToTakeToSchool != null)) {
// Get the children's school
School school = childrenToTakeToSchool.get(0).getSchool();
// Try to estimate the distance that the vehicle will need to
// travel
float distanceHomeSchool = home.getAddress().DistanceFrom(school.getClosestEdge());
// Estimate the time needed to go from school to home
int estimatedTravelTimeHomeSchool = Math.round(distanceHomeSchool / Constants.averageSpeed);
// Calculate the time to leave the house
int timeOfDepartureFromHouse = Math.max(0, school.getTimeOpening() - estimatedTravelTimeHomeSchool);
// Go from home to school and back
List<Edge> tripEdges = new ArrayList<Edge>();
tripEdges.add(home.getAddress());
tripEdges.add(school.getClosestEdge());
tripEdges.add(home.getAddress());
String route = MultipleTripExpander.ExpandTrips(tripEdges);
// Write the route
routesList.add(new Route(timeOfDepartureFromHouse,"<vehicle id=\"home-school-home-" + String.valueOf(home.getId()) + "-" + String.valueOf(counter) + "\" type=\"" + vehicle.getName() + "\" depart=\"" + String.valueOf(timeOfDepartureFromHouse) + "\" color=\"" + color + "\">"+"<route color=\"" + color + "\">" + route + "</route>"+"</vehicle>"));
}
// If the adult has a job and she doesn't have to pick up kids from
// school
if (hasJob && (childrenToPickupFromSchool == null)) {
// Create route from work to home
tripsList.add(new Trip(job.getEndingTime(),"<trip id=\"work-house-" + String.valueOf(home.getId()) + "-" + String.valueOf(counter) + "\" depart=\"" + String.valueOf(job.getEndingTime()) + "\" from=\"" + jobLocation.getId() + "\" to=\"" + home.getAddress().getId() + "\" color=\"" + color + "\" type=\"" + vehicle.getName() + "\" />\n"));
}
// If the adult has a job and she has to pickup kids from school
else if (hasJob && (childrenToPickupFromSchool != null)) {
// Get the children's school
School school = childrenToPickupFromSchool.get(0).getSchool();
// Estimate distance between home-school and school-work
float distanceHomeSchool = home.getAddress().DistanceFrom(school.getClosestEdge());
float distanceHomeWork = home.getAddress().DistanceFrom(jobLocation);
// Estimate the time needed to go from work to school and then from
// school to home
int estimatedTravelTimeWorkSchoolHome = Math.round((distanceHomeSchool + distanceHomeWork) / Constants.averageSpeed);
int timeBetweenJobEndingAndSchoolClosing = school.getTimeClosing() - job.getEndingTime();
// If the adult doesn't have enough time after work to go home for
// 20 minutes
if ((estimatedTravelTimeWorkSchoolHome + 20 * 60) > timeBetweenJobEndingAndSchoolClosing) {
// Go straight from work to school and then home
List<Edge> tripEdges = new ArrayList<Edge>();
tripEdges.add(jobLocation);
tripEdges.add(school.getClosestEdge());
tripEdges.add(home.getAddress());
// Build the route
String route = MultipleTripExpander.ExpandTrips(tripEdges);
routesList.add(new Route(job.getEndingTime(),"<vehicle id=\"work-school-home-" + String.valueOf(home.getId()) + "-" + String.valueOf(counter) + "\" type=\"" + vehicle.getName() + "\" depart=\"" + String.valueOf(job.getEndingTime()) + "\" color=\"" + color + "\">"+"<route color=\"" + color + "\">" + route + "</route>"+"</vehicle>"));
}
// If the adult has enough time to go home after work
else {
// Go from work to home and from home to school and from school
// to home
// Create route from work to home
tripsList.add(new Trip(job.getEndingTime(),"<trip id=\"work-house-" + String.valueOf(home.getId()) + "-" + String.valueOf(counter) + "\" depart=\"" + String.valueOf(job.getEndingTime()) + "\" from=\"" + jobLocation.getId() + "\" to=\"" + home.getAddress().getId() + "\" color=\"" + color + "\" type=\"" + vehicle.getName() + "\" />\n"));
// Estimate the time needed to cover the distance from home to
// school
int estimatedTravelTime = (int) Math.round(distanceHomeSchool / Constants.averageSpeed);
// Calculate the time to leave the house so that the adult will
// be at school on time
int timeOfDepartureFromHouse = Math.max(0, school.getTimeClosing() - estimatedTravelTime);
// Build the route
List<Edge> tripEdges = new ArrayList<Edge>();
tripEdges.add(home.getAddress());
tripEdges.add(school.getClosestEdge());
tripEdges.add(home.getAddress());
String route = MultipleTripExpander.ExpandTrips(tripEdges);
routesList.add(new Route(timeOfDepartureFromHouse,"<vehicle id=\"home-school-home-" + String.valueOf(home.getId()) + "-" + String.valueOf(counter) + "\" type=\"" + vehicle.getName() + "\" depart=\"" + String.valueOf(timeOfDepartureFromHouse) + "\" color=\"" + color + "\">"+"<route color=\"" + color + "\">" + route + "</route>"+"</vehicle>"));
}
}
// If the adult has no job and has children to pick up
else if (!hasJob && (childrenToPickupFromSchool != null)) {
School school = childrenToPickupFromSchool.get(0).getSchool();
// Try to estimate the distance between home and school
float distanceToTravel = home.getAddress().DistanceFrom(school.getClosestEdge());
// Estimate the time needed to cover this distance
int estimatedTravelTime = (int) Math.round(distanceToTravel / Constants.averageSpeed);
// Calculate the time to leave the house so that the adult will be
// at school on time
int timeOfDepartureFromHouse = Math.max(0, school.getTimeClosing() - estimatedTravelTime);
// Create route from house to school and back
List<Edge> tripEdges = new ArrayList<Edge>();
tripEdges.add(home.getAddress());
tripEdges.add(school.getClosestEdge());
tripEdges.add(home.getAddress());
String route = MultipleTripExpander.ExpandTrips(tripEdges);
routesList.add(new Route(timeOfDepartureFromHouse, "<vehicle id=\"home-school-home-" + String.valueOf(home.getId()) + "-" + String.valueOf(counter) + "\" type=\"" + vehicle.getName() + "\" depart=\"" + String.valueOf(timeOfDepartureFromHouse) + "\" color=\"" + color + "\">"+"<route color=\"" + color + "\">" + route + "</route>"+"</vehicle>"));
}