コード例 #1
0
  @Override
  public void handleEvent(ActivityEndEvent event) {
    try {
      if (isTransitScenario) {
        if (transitDriverIds.contains(event.getPersonId())) return;
      }
      TravellerChain chain = chains.get(event.getPersonId());
      locations.put(event.getPersonId(), network.getLinks().get(event.getLinkId()).getCoord());
      if (chain == null) {
        chain = new TravellerChain();
        chains.put(event.getPersonId(), chain);
        Activity act = chain.addActivity();
        act.setCoord(network.getLinks().get(event.getLinkId()).getCoord());
        act.setEndTime(event.getTime());
        act.setFacility(event.getFacilityId());
        act.setStartTime(0.0);
        act.setType(event.getActType());

      } else if (!event.getActType().equals(PtConstants.TRANSIT_ACTIVITY_TYPE)) {
        Activity act = chain.getActs().getLast();
        act.setEndTime(event.getTime());
      }
    } catch (Exception e) {
      System.err.println(e.getStackTrace());
      System.err.println(event.toString());
    }
  }
コード例 #2
0
 @Override
 public void handleEvent(LinkLeaveEvent event) {
   if (event.getVehicleId().toString().startsWith("tr")) {
     PTVehicle vehicle = ptVehicles.get(event.getVehicleId());
     if (vehicle.in) {
       vehicle.in = false;
       vehicle.incDistance(network.getLinks().get(event.getLinkId()).getLength());
     }
   } else {
     TravellerChain chain = chains.get(event.getDriverId());
     if (chain == null) {
       chain = new TravellerChain();
       chains.put(event.getDriverId(), chain);
       chain.modes.add("car");
       chain.distances.add(0.0);
     } else {
       if (chain.in) {
         chain.in = false;
         if (chain.modes.size() == chain.distances.size()
             && chain.modes.get(chain.modes.size() - 1).equals("car")) {
           double linkDistance = network.getLinks().get(event.getLinkId()).getLength();
           chain.distances.set(
               chain.distances.size() - 1,
               chain.distances.get(chain.distances.size() - 1) + linkDistance);
         } else if (chain.modes.size() == chain.distances.size())
           throw new RuntimeException("Person is leaving a link, but is not in a car mode");
         else
           throw new RuntimeException("Modes list has not the same size of distances list (car)");
       } else {
         chain.modes.add("car");
         chain.distances.add(0.0);
       }
     }
   }
 }
コード例 #3
0
 @Override
 public void handleEvent(TeleportationArrivalEvent event) {
   try {
     if (transitDriverIds.contains(event.getPersonId())) return;
     TravellerChain chain = chains.get(event.getPersonId());
     if (chain.traveledVehicle) chain.traveledVehicle = false;
   } catch (Exception e) {
     System.err.println(e.getStackTrace());
     System.err.println(event.toString());
   }
 }
コード例 #4
0
 @Override
 public void handleEvent(PersonEntersVehicleEvent event) {
   if (pIdsToExclude.contains(event.getPersonId())) {
     return;
   }
   if (!event.getPersonId().toString().startsWith("pt_tr"))
     if (event.getVehicleId().toString().startsWith("tr")) {
       TravellerChain chain = chains.get(event.getPersonId());
       chain.lineId = ptVehicles.get(event.getVehicleId()).lineId;
       chain.routeId = ptVehicles.get(event.getVehicleId()).routeId;
     }
 }
コード例 #5
0
 @Override
 public void handleEvent(PersonStuckEvent event) {
   try {
     if (!transitDriverIds.contains(event.getPersonId())) {
       TravellerChain chain = chains.get(event.getPersonId());
       setStuck(getStuck() + 1);
       if (chain.getJourneys().size() > 0) chain.getJourneys().removeLast();
     }
   } catch (Exception e) {
     System.err.println(e.getStackTrace());
     System.err.println(event.toString());
   }
 }
コード例 #6
0
 @Override
 public void handleEvent(PersonDepartureEvent event) {
   if (pIdsToExclude.contains(event.getPersonId())) {
     return;
   }
   TravellerChain chain = chains.get(event.getPersonId());
   if (chain == null) {
     chain = new TravellerChain();
     chains.put(event.getPersonId(), chain);
   }
   chain.lastTime = event.getTime();
   if (event.getLegMode().equals("transit_walk")) chain.walk = true;
   else chain.walk = false;
 }
コード例 #7
0
  @Override
  public void handleEvent(ActivityStartEvent event) {
    try {
      if (isTransitScenario) {
        if (transitDriverIds.contains(event.getPersonId())) return;
      }
      TravellerChain chain = chains.get(event.getPersonId());
      boolean beforeInPT = chain.isInPT();
      if (event.getActType().equals(PtConstants.TRANSIT_ACTIVITY_TYPE)) {
        chain.setInPT(true);

      } else {
        chain.setInPT(false);
        chain.traveling = false;
        Activity act = chain.addActivity();
        act.setCoord(network.getLinks().get(event.getLinkId()).getCoord());
        act.setFacility(event.getFacilityId());
        act.setStartTime(event.getTime());
        act.setType(event.getActType());
        // end the preceding journey
        Journey journey = chain.getJourneys().getLast();
        journey.setDest(act.getCoord());
        journey.setEndTime(event.getTime());
        journey.setToAct(act);
        if (beforeInPT) journey.getWalks().getLast().setEgressWalk(true);
      }
    } catch (Exception e) {
      System.err.println(e.getStackTrace());
      System.err.println(event.toString());
      ;
    }
  }
コード例 #8
0
 @Override
 public void handleEvent(PersonLeavesVehicleEvent event) {
   if (!event.getPersonId().toString().startsWith("pt_tr")) {
     TravellerChain chain = chains.get(event.getPersonId());
     if (event.getVehicleId().toString().startsWith("tr")) {
       if (chain.modes.size() == chain.distances.size()
           && chain.modes.get(chain.modes.size() - 1).equals("pt")) {
         double stageDistance =
             ptVehicles.get(event.getVehicleId()).removePassenger(event.getPersonId());
         chain.distances.set(
             chain.distances.size() - 1,
             chain.distances.get(chain.distances.size() - 1) + stageDistance);
         chain.in = true;
       } else if (chain.modes.size() == chain.distances.size())
         throw new RuntimeException("Person is leaving a pt vehicle, but is not in a pt mode");
       else throw new RuntimeException("Modes list has not the same size of distances list (pt)");
     }
   }
 }
コード例 #9
0
 @Override
 public void handleEvent(PersonEntersVehicleEvent event) {
   try {
     if (transitDriverIds.contains(event.getPersonId())) return;
     if (ptVehicles.keySet().contains(event.getVehicleId())) {
       TravellerChain chain = chains.get(event.getPersonId());
       Journey journey = chain.getJourneys().getLast();
       // first, handle the end of the wait
       journey.getWaits().getLast().setEndTime(event.getTime());
       // now, create a new trip
       ptVehicles.get(event.getVehicleId()).addPassenger(event.getPersonId());
       Trip trip = journey.addTrip();
       PTVehicle vehicle = ptVehicles.get(event.getVehicleId());
       trip.setLine(vehicle.transitLineId);
       trip.setMode(
           transitSchedule
               .getTransitLines()
               .get(vehicle.transitLineId)
               .getRoutes()
               .get(vehicle.transitRouteId)
               .getTransportMode());
       trip.setBoardingStop(vehicle.lastStop);
       trip.setOrig(journey.getWaits().getLast().getCoord());
       trip.setRoute(ptVehicles.get(event.getVehicleId()).transitRouteId);
       trip.setStartTime(event.getTime());
       // check for the end of a transfer
       if (journey.getPossibleTransfer() != null) {
         journey.getPossibleTransfer().setToTrip(trip);
         journey.getPossibleTransfer().setEndTime(event.getTime());
         journey.addTransfer(journey.getPossibleTransfer());
         journey.setPossibleTransfer(null);
       }
     } else {
       // add the person to the map that keeps track of who drives what
       // vehicle
       driverIdFromVehicleId.put(event.getVehicleId(), event.getPersonId());
     }
   } catch (Exception e) {
     System.err.println(e.getStackTrace());
     System.err.println(event.toString());
   }
 }
コード例 #10
0
 @Override
 public void handleEvent(PersonArrivalEvent event) {
   try {
     if (isTransitScenario) {
       if (transitDriverIds.contains(event.getPersonId())) return;
     }
     TravellerChain chain = chains.get(event.getPersonId());
     switch (event.getLegMode()) {
       case "walk":
       case "transit_walk":
         {
           Journey journey = chain.getJourneys().getLast();
           Walk walk = journey.getWalks().getLast();
           walk.setDest(network.getLinks().get(event.getLinkId()).getCoord());
           walk.setEndTime(event.getTime());
           walk.setDistance(walk.getDuration() * walkSpeed);
           break;
         }
       case TransportMode.car:
         {
           Journey journey = chain.getJourneys().getLast();
           journey.setDest(network.getLinks().get(event.getLinkId()).getCoord());
           journey.setEndTime(event.getTime());
           Trip trip = journey.getTrips().getLast();
           trip.setDistance(journey.getDistance());
           trip.setEndTime(event.getTime());
           chain.inCar = false;
           break;
         }
       case "pt":
         if (isTransitScenario) {
           Journey journey = chain.getJourneys().getLast();
           Trip trip = journey.getTrips().getLast();
           trip.setDest(network.getLinks().get(event.getLinkId()).getCoord());
           trip.setEndTime(event.getTime());
           journey.setPossibleTransfer(new Transfer());
           journey.getPossibleTransfer().setStartTime(event.getTime());
           journey.getPossibleTransfer().setFromTrip(trip);
         } else {
           Journey journey = chain.getJourneys().getLast();
           journey.setEndTime(event.getTime());
           journey.setDest(network.getLinks().get(event.getLinkId()).getCoord());
           journey.setEndTime(event.getTime());
         }
         break;
       default:
         Journey journey = chain.getJourneys().getLast();
         journey.setEndTime(event.getTime());
         journey.setDest(network.getLinks().get(event.getLinkId()).getCoord());
         journey.setEndTime(event.getTime());
         break;
     }
   } catch (Exception e) {
     System.err.println(e.getStackTrace());
     System.err.println(event.toString());
   }
 }
コード例 #11
0
  @Override
  public void handleEvent(LinkLeaveEvent event) {
    try {
      if (ptVehicles.keySet().contains(event.getVehicleId())) {
        PTVehicle vehicle = ptVehicles.get(event.getVehicleId());
        if (vehicle.in) vehicle.in = false;
        vehicle.incDistance(network.getLinks().get(event.getLinkId()).getLength());

      } else {
        TravellerChain chain = chains.get(driverIdFromVehicleId.get(event.getVehicleId()));
        if (chain.inCar) {
          Journey journey = chain.getJourneys().getLast();
          journey.incrementCarDistance(network.getLinks().get(event.getLinkId()).getLength());
        }
      }
    } catch (Exception e) {
      System.err.println(e.getStackTrace());
      System.err.println(event.toString());
    }
  }
コード例 #12
0
  @Override
  public void handleEvent(PersonLeavesVehicleEvent event) {
    if (transitDriverIds.contains(event.getPersonId())) return;
    try {
      if (ptVehicles.keySet().contains(event.getVehicleId())) {
        TravellerChain chain = chains.get(event.getPersonId());
        chain.traveledVehicle = true;
        PTVehicle vehicle = ptVehicles.get(event.getVehicleId());
        double stageDistance = vehicle.removePassenger(event.getPersonId());
        Trip trip = chain.getJourneys().getLast().getTrips().getLast();
        trip.setDistance(stageDistance);
        trip.setAlightingStop(vehicle.lastStop);
      } else {
        driverIdFromVehicleId.remove(event.getVehicleId());
      }

    } catch (Exception e) {
      System.err.println(e.getStackTrace());
      System.err.println(event.toString());
    }
  }
コード例 #13
0
 @Override
 public void handleEvent(ActivityStartEvent event) {
   if (pIdsToExclude.contains(event.getPersonId())) {
     return;
   }
   TravellerChain chain = chains.get(event.getPersonId());
   boolean beforeInPT = chain.inPT;
   if (event.getActType().equals(PtConstants.TRANSIT_ACTIVITY_TYPE)) chain.inPT = true;
   else chain.inPT = false;
   if (!chain.inPT && !chain.walk) chain.modes.add("car");
   else if (!chain.inPT && chain.walk && !beforeInPT) chain.modes.add("walk");
   else if (chain.walk) chain.modes.add("transit_walk");
   else
     chain.modes.add(
         getMode(
             transitSchedule
                 .getTransitLines()
                 .get(chain.lineId)
                 .getRoutes()
                 .get(chain.routeId)
                 .getTransportMode(),
             chain.lineId));
   chain.times.add(event.getTime() - chain.lastTime);
 }
コード例 #14
0
  public void writeSimulationResultsToTabSeparated(String path, String appendage)
      throws IOException {
    String actTableName;
    String journeyTableName;
    String transferTableName;
    String tripTableName;
    if (appendage.matches("[a-zA-Z0-9]*[_]*")) {
      actTableName = appendage + "matsim_activities.txt";
      journeyTableName = appendage + "matsim_journeys.txt";
      transferTableName = appendage + "matsim_transfers.txt";
      tripTableName = appendage + "matsim_trips.txt";
    } else {
      if (appendage.matches("[a-zA-Z0-9]*")) appendage = "_" + appendage;
      actTableName = "matsim_activities" + appendage + ".txt";
      journeyTableName = "matsim_journeys" + appendage + ".txt";
      transferTableName = "matsim_transfers" + appendage + ".txt";
      tripTableName = "matsim_trips" + appendage + ".txt";
    }
    BufferedWriter activityWriter = IOUtils.getBufferedWriter(path + "/" + actTableName);

    activityWriter.write(
        "activity_id\tperson_id\tfacility_id\ttype\t"
            + "start_time\tend_time\tx\ty\tsample_selector\n");

    BufferedWriter journeyWriter = IOUtils.getBufferedWriter(path + "/" + journeyTableName);
    journeyWriter.write(
        "journey_id\tperson_id\tstart_time\t"
            + "end_time\tdistance\tmain_mode\tfrom_act\tto_act\t"
            + "in_vehicle_distance\tin_vehicle_time\t"
            + "access_walk_distance\taccess_walk_time\taccess_wait_time\t"
            + "first_boarding_stop\tegress_walk_distance\t"
            + "egress_walk_time\tlast_alighting_stop\t"
            + "transfer_walk_distance\ttransfer_walk_time\t"
            + "transfer_wait_time\tsample_selector\n");

    BufferedWriter tripWriter = IOUtils.getBufferedWriter(path + "/" + tripTableName);
    tripWriter.write(
        "trip_id\tjourney_id\tstart_time\tend_time\t"
            + "distance\tmode\tline\troute\tboarding_stop\t"
            + "alighting_stop\tsample_selector\n");

    BufferedWriter transferWriter = IOUtils.getBufferedWriter(path + "/" + transferTableName);
    transferWriter.write(
        "transfer_id\tjourney_id\tstart_time\t"
            + "end_time\tfrom_trip\tto_trip\twalk_distance\t"
            + "walk_time\twait_time\tsample_selector\n");

    // read a static field that increments with every inheriting object constructed
    Counter counter = new Counter("Output lines written: ");
    for (Entry<Id, TravellerChain> entry : chains.entrySet()) {
      String pax_id = entry.getKey().toString();
      TravellerChain chain = entry.getValue();
      for (Activity act : chain.getActs()) {
        try {
          activityWriter.write(
              String.format(
                  "%d\t%s\t%s\t%s\t%d\t%d\t%f\t%f\t%f\n",
                  act.getElementId(),
                  pax_id,
                  act.getFacility(),
                  act.getType(),
                  (int) act.getStartTime(),
                  (int) act.getEndTime(),
                  act.getCoord().getX(),
                  act.getCoord().getY(),
                  MatsimRandom.getRandom().nextDouble()));
        } catch (Exception e) {
          System.out.println("Couldn't print activity chain!");
        }
      }
      for (Journey journey : chain.getJourneys()) {
        try {
          journeyWriter.write(
              String.format(
                  "%d\t%s\t%d\t%d\t%.3f\t%s\t%d\t%d\t%.3f\t%d\t%.3f\t%d\t%d\t%s\t%.3f\t%d\t%s\t%.3f\t%d\t%d\t%f\n",
                  journey.getElementId(),
                  pax_id,
                  (int) journey.getStartTime(),
                  (int) journey.getEndTime(),
                  journey.getDistance(),
                  journey.getMainMode(),
                  journey.getFromAct().getElementId(),
                  journey.getToAct().getElementId(),
                  journey.getInVehDistance(),
                  (int) journey.getInVehTime(),
                  journey.getAccessWalkDistance(),
                  (int) journey.getAccessWalkTime(),
                  (int) journey.getAccessWaitTime(),
                  journey.getFirstBoardingStop(),
                  journey.getEgressWalkDistance(),
                  (int) journey.getEgressWalkTime(),
                  journey.getLastAlightingStop(),
                  journey.getTransferWalkDistance(),
                  (int) journey.getTransferWalkTime(),
                  (int) journey.getTransferWaitTime(),
                  MatsimRandom.getRandom().nextDouble()));
          counter.incCounter();

          if (!(journey.isCarJourney() || journey.isTeleportJourney())) {
            for (Trip trip : journey.getTrips()) {
              tripWriter.write(
                  String.format(
                      "%d\t%d\t%d\t%d\t%.3f\t%s\t%s\t%s\t%s\t%s\t%f\n",
                      trip.getElementId(),
                      journey.getElementId(),
                      (int) trip.getStartTime(),
                      (int) trip.getEndTime(),
                      trip.getDistance(),
                      trip.getMode(),
                      trip.getLine(),
                      trip.getRoute(),
                      trip.getBoardingStop(),
                      trip.getAlightingStop(),
                      MatsimRandom.getRandom().nextDouble()));
              counter.incCounter();
            }
            for (Transfer transfer : journey.getTransfers()) {
              transferWriter.write(
                  String.format(
                      "%d\t%d\t%d\t%d\t%d\t%d\t%.3f\t%d\t%d\t%f\n",
                      transfer.getElementId(),
                      journey.getElementId(),
                      (int) transfer.getStartTime(),
                      (int) transfer.getEndTime(),
                      transfer.getFromTrip().getElementId(),
                      transfer.getToTrip().getElementId(),
                      transfer.getWalkDistance(),
                      (int) transfer.getWalkTime(),
                      (int) transfer.getWaitTime(),
                      MatsimRandom.getRandom().nextDouble()));

              counter.incCounter();
            }
          } else {
            for (Trip trip : journey.getTrips()) {

              tripWriter.write(
                  String.format(
                      "%d\t%d\t%d\t%d\t%.3f\t%s\t%s\t%s\t%s\t%s\t%f\n",
                      trip.getElementId(),
                      journey.getElementId(),
                      (int) trip.getStartTime(),
                      (int) trip.getEndTime(),
                      journey.isTeleportJourney() ? 0.000 : trip.getDistance(),
                      trip.getMode(),
                      "",
                      "",
                      "",
                      "",
                      MatsimRandom.getRandom().nextDouble()));

              counter.incCounter();
            }
          }
        } catch (NullPointerException e) {
          setStuck(getStuck() + 1);
        }
      }
    }

    activityWriter.close();
    journeyWriter.close();
    tripWriter.close();
    transferWriter.close();
    counter.printCounter();
  }
コード例 #15
0
 @Override
 public void handleEvent(PersonDepartureEvent event) {
   try {
     if (transitDriverIds.contains(event.getPersonId())) return;
     TravellerChain chain = chains.get(event.getPersonId());
     Journey journey;
     Trip trip;
     switch (event.getLegMode()) {
       case TransportMode.walk:
         // fall through to the next
       case TransportMode.transit_walk:
         if (!chain.traveling) {
           chain.traveling = true;
           journey = chain.addJourney();
           journey.setOrig(network.getLinks().get(event.getLinkId()).getCoord());
           journey.setFromAct(chain.getActs().getLast());
           journey.setStartTime(event.getTime());
           Walk walk = journey.addWalk();
           walk.setAccessWalk(true);
           walk.setStartTime(event.getTime());
           walk.setOrig(journey.getOrig());
         } else {
           journey = chain.getJourneys().getLast();
           Walk walk = journey.addWalk();
           walk.setStartTime(event.getTime());
           walk.setOrig(network.getLinks().get(event.getLinkId()).getCoord());
           journey.getPossibleTransfer().getWalks().add(walk);
         }
         break;
       case TransportMode.car:
         chain.inCar = true;
         journey = chain.addJourney();
         journey.setCarJourney(true);
         journey.setOrig(network.getLinks().get(event.getLinkId()).getCoord());
         journey.setFromAct(chain.getActs().getLast());
         journey.setStartTime(event.getTime());
         trip = journey.addTrip();
         trip.setMode("car");
         trip.setStartTime(event.getTime());
         break;
       case TransportMode.pt:
         if (isTransitScenario) {
           // person waits till they enter the vehicle
           journey = chain.getJourneys().getLast();
           Wait wait = journey.addWait();
           if (journey.getWaits().size() == 1) wait.setAccessWait(true);
           wait.setStartTime(event.getTime());
           wait.setCoord(network.getLinks().get(event.getLinkId()).getCoord());
           if (!wait.isAccessWait()) {
             journey.getPossibleTransfer().getWaits().add(wait);
           }
         } else {
           journey = chain.addJourney();
           journey.setTeleportJourney(true);
           journey.setOrig(network.getLinks().get(event.getLinkId()).getCoord());
           journey.setFromAct(chain.getActs().getLast());
           journey.setStartTime(event.getTime());
           journey.setMainmode(event.getLegMode());
           trip = journey.addTrip();
           trip.setMode(event.getLegMode());
           trip.setStartTime(event.getTime());
         }
         break;
       default:
         journey = chain.addJourney();
         journey.setTeleportJourney(true);
         journey.setOrig(network.getLinks().get(event.getLinkId()).getCoord());
         journey.setFromAct(chain.getActs().getLast());
         journey.setStartTime(event.getTime());
         journey.setMainmode(event.getLegMode());
         trip = journey.addTrip();
         trip.setMode(event.getLegMode());
         trip.setStartTime(event.getTime());
         break;
     }
   } catch (Exception e) {
     System.err.println(e.getStackTrace());
     System.err.println(event.toString());
   }
 }