Пример #1
0
  private List<Link> createActivityLinks() {

    List<Link> links = new LinkedList<Link>();

    for (Activity act : this.activityList) {
      if (act.getLinkId() != null && this.network != null) {
        links.add(this.network.getLinks().get(act.getLinkId()));
      }
    }

    if (links.size() == 0) {
      return null;
    } else {
      return links;
    }
  }
 private void handleActivity(EventsToScore eventsToScore, Fixture f, Activity activity) {
   eventsToScore.handleEvent(
       new ActivityStartEvent(
           activity.getStartTime(),
           f.person.getId(),
           activity.getLinkId(),
           activity.getFacilityId(),
           activity.getType()));
   eventsToScore.handleEvent(
       new ActivityEndEvent(
           activity.getEndTime(),
           f.person.getId(),
           activity.getLinkId(),
           activity.getFacilityId(),
           activity.getType()));
 }
  public FreeFloatingParkingPersonDriverAgentImpl(
      final Person person,
      final Plan plan,
      final Netsim simulation,
      final Scenario scenario,
      final MatsimServices controler,
      ParkingModuleWithFreeFloatingCarSharing parkingModule) {
    this.person = person;
    this.simulation = simulation;
    this.controler = controler;
    this.plan = plan;
    this.scenario = scenario;
    this.parkingModule = parkingModule;

    beelineFactor =
        ((PlansCalcRouteConfigGroup) scenario.getConfig().getModule("planscalcroute"))
            .getBeelineDistanceFactors()
            .get("walk");
    walkSpeed =
        (((PlansCalcRouteConfigGroup) controler.getConfig().getModule("planscalcroute"))
            .getTeleportedModeSpeeds()
            .get("walk"));

    List<? extends PlanElement> planElements = this.plan.getPlanElements();
    if (planElements.size() > 0) {
      this.currentPlanElementIndex = 0;
      Activity firstAct = (Activity) planElements.get(0);
      this.currentLinkId = firstAct.getLinkId();
      this.state = MobsimAgent.State.ACTIVITY;
      calculateAndSetDepartureTime(firstAct);
    }
    throw new RuntimeException(
        "Should this class still be in use?  I think there is a delegated version of this, isn't there?  "
            + "This one here causes additional refactoring work. kai, feb'16");
  }
  private boolean judgeByBeeline(final Activity fromAct, final Activity toAct) {
    if (this.aoiCenter == null) {
      // we cannot use the bee-line decision if we don't know the alternative aoi-center
      return false;
    }
    Coord fromCoord = fromAct.getCoord();
    Coord toCoord = toAct.getCoord();

    if (fromCoord == null) {
      fromCoord = this.network.getLinks().get(fromAct.getLinkId()).getCoord();
    }
    if (toCoord == null) {
      toCoord = this.network.getLinks().get(toAct.getLinkId()).getCoord();
    }

    return (CoordUtils.distancePointLinesegment(fromCoord, toCoord, this.aoiCenter)
        <= this.aoiRadius);
  }
 private void writeAct(final Activity act, final BufferedWriter out) throws IOException {
   out.write("\t\t\t<act type=\"");
   out.write(act.getType());
   out.write("\"");
   if (act.getLinkId() != null) {
     out.write(" link=\"");
     out.write(act.getLinkId().toString());
     out.write("\"");
   }
   if (act.getFacilityId() != null) {
     out.write(" facility=\"");
     out.write(act.getFacilityId().toString());
     out.write("\"");
   }
   if (act.getCoord() != null) {
     final Coord coord = coordinateTransformation.transform(act.getCoord());
     out.write(" x=\"");
     out.write(Double.toString(coord.getX()));
     out.write("\" y=\"");
     out.write(Double.toString(coord.getY()));
     out.write("\"");
   }
   if (act.getStartTime() != Time.UNDEFINED_TIME) {
     out.write(" start_time=\"");
     out.write(Time.writeTime(act.getStartTime()));
     out.write("\"");
   }
   if (act != null) {
     Activity a = act;
     if (a.getMaximumDuration() != Time.UNDEFINED_TIME) {
       out.write(" max_dur=\"");
       out.write(Time.writeTime(a.getMaximumDuration()));
       out.write("\"");
     }
   }
   if (act.getEndTime() != Time.UNDEFINED_TIME) {
     out.write(" end_time=\"");
     out.write(Time.writeTime(act.getEndTime()));
     out.write("\"");
   }
   out.write(" />\n");
 }
  @Override
  public void handleAgentLeg(AgentWithParking aem) {
    Activity nextAct =
        (Activity)
            aem.getPerson().getSelectedPlan().getPlanElements().get(aem.getPlanElementIndex() + 3);

    if (GeneralLib.getDistance(
            getCurrentLink(aem).getCoord(), network.getLinks().get(nextAct.getLinkId()).getCoord())
        < distanceToDestinationForStartingRandomSearch) {
      throughAwayRestOfRoute(aem);
    }
    super.handleAgentLeg(aem);
  }
 @Override
 public final void endActivityAndComputeNextState(final double now) {
   Activity act = (Activity) this.getPlanElements().get(this.currentPlanElementIndex);
   this.simulation
       .getEventsManager()
       .processEvent(
           new ActivityEndEvent(
               now,
               this.getPerson().getId(),
               act.getLinkId(),
               act.getFacilityId(),
               act.getType()));
   advancePlan(now);
 }
Пример #8
0
  private void replaceDoubtfulLegsByOtherMode() {
    for (Person p : scenario.getPopulation().getPersons().values()) {
      for (Plan plan : p.getPlans()) {

        Leg lastleg = null;
        Activity lastActivity = null;
        boolean personb = random.nextBoolean();
        for (PlanElement pe : plan.getPlanElements()) {
          if (pe instanceof Activity) {
            if (lastActivity == null) {
              lastActivity = (Activity) pe;
            } else {
              Coord lastCoord;
              if (lastActivity.getCoord() != null) {
                lastCoord = lastActivity.getCoord();
              } else {
                Link lastLink = scenario.getNetwork().getLinks().get(lastActivity.getLinkId());
                lastCoord = lastLink.getCoord();
              }
              Coord currentCoord;
              if (((Activity) pe).getCoord() != null) {
                currentCoord = ((Activity) pe).getCoord();
              } else {
                currentCoord =
                    scenario.getNetwork().getLinks().get(((Activity) pe).getLinkId()).getCoord();
              }

              double distance = CoordUtils.calcDistance(lastCoord, currentCoord);

              if (distance > 3000 && lastleg.getMode().equals("walk")) {
                lastleg.setMode("pt");
              } else if (distance > 20000 && lastleg.getMode().equals("bike")) {
                lastleg.setMode("pt");
              } else if (distance < 2000 && (lastleg.getMode().equals("pt"))) {
                if (personb == true) lastleg.setMode("walk");
                else lastleg.setMode("bike");
              }

              lastActivity = (Activity) pe;
            }

          } else if (pe instanceof Leg) {
            lastleg = (Leg) pe;
          }
        }
      }
    }
  }
  public AllCSModesPersonDriverAgentImpl(
      final Person person,
      final Plan plan,
      final Netsim simulation,
      final Scenario scenario,
      CarSharingVehicles carSharingVehicles,
      TripRouter tripRouter) {
    this.person = person;
    this.simulation = simulation;
    this.plan = plan;
    this.scenario = scenario;

    this.carSharingVehicles = carSharingVehicles;

    this.tripRouter = tripRouter;

    beelineFactor =
        ((PlansCalcRouteConfigGroup) scenario.getConfig().getModule("planscalcroute"))
            .getBeelineDistanceFactors()
            .get("walk");
    walkSpeed =
        (((PlansCalcRouteConfigGroup) scenario.getConfig().getModule("planscalcroute"))
            .getTeleportedModeSpeeds()
            .get("walk"));
    // carsharingVehicleLocations = new ArrayList<ActivityFacility>();
    mapTW = new HashMap<Link, Link>();
    mapOW = new HashMap<Link, Link>();
    List<? extends PlanElement> planElements = this.plan.getPlanElements();
    if (planElements.size() > 0) {
      this.currentPlanElementIndex = 0;
      Activity firstAct = (Activity) planElements.get(0);
      this.currentLinkId = firstAct.getLinkId();
      this.state = MobsimAgent.State.ACTIVITY;
      calculateAndSetDepartureTime(firstAct);
    }
    throw new RuntimeException(
        "Should this class still be in use?  I think there is a delegated version of this, isn't there?  "
            + "This one here causes additional refactoring work. kai, feb'16");
  }
Пример #10
0
  /**
   * Tests that plans with missing act locations are completed (=xy2links and routed) before the
   * mobsim starts.
   *
   * @author mrieser
   */
  @Test
  public void testCalcMissingActLinks() {
    Config config = this.utils.loadConfig(null);
    Fixture f = new Fixture(config);

    /* Create a person with two plans, driving from link 1 to link 3, starting at 7am.  */
    Population population = f.scenario.getPopulation();
    PopulationFactory factory = population.getFactory();
    Person person1 = null;
    Activity act1a = null;
    Activity act1b = null;
    Activity act2a = null;
    Activity act2b = null;
    Leg leg1 = null;
    Leg leg2 = null;

    person1 = PersonImpl.createPerson(Id.create(1, Person.class));
    // --- plan 1 ---
    Plan plan1 = factory.createPlan();
    person1.addPlan(plan1);
    double x1 = -50.0;
    act1a = factory.createActivityFromCoord("h", new Coord(x1, 10.0));
    act1a.setEndTime(7.0 * 3600);
    plan1.addActivity(act1a);
    leg1 = factory.createLeg(TransportMode.car);
    plan1.addLeg(leg1);
    // DO NOT CREATE A ROUTE FOR THE LEG!!!
    double y1 = -10.0;
    act1b = factory.createActivityFromCoord("h", new Coord(1075.0, y1));
    plan1.addActivity(act1b);
    // --- plan 2 ---
    Plan plan2 = factory.createPlan();
    person1.addPlan(plan2);
    double x = -50.0;
    double y = -10.0;
    act2a = factory.createActivityFromCoord("h", new Coord(x, y));
    act2a.setEndTime(7.9 * 3600);
    plan2.addActivity(act2a);
    leg2 = factory.createLeg(TransportMode.car);
    plan2.addLeg(leg2);
    // DO NOT CREATE A ROUTE FOR THE LEG!!!
    act2b = factory.createActivityFromCoord("h", new Coord(1111.1, 10.0));
    plan2.addActivity(act2b);
    population.addPerson(person1);

    // Complete the configuration for our test case
    // - set scoring parameters
    ActivityParams actParams = new ActivityParams("h");
    actParams.setTypicalDuration(8 * 3600);
    actParams.setPriority(1.0);
    config.planCalcScore().addActivityParams(actParams);
    // - define iterations
    config.controler().setLastIteration(0);
    // - make sure we don't use threads, as they are not deterministic
    config.global().setNumberOfThreads(1);

    // Now run the simulation
    Controler controler = new Controler(f.scenario);
    controler.getConfig().controler().setCreateGraphs(false);
    controler.getConfig().controler().setWriteEventsInterval(0);
    controler.addOverridingModule(
        new AbstractModule() {
          @Override
          public void install() {
            bindMobsim()
                .toProvider(
                    new Provider<Mobsim>() {
                      @Override
                      public Mobsim get() {
                        return new FakeMobsim();
                      }
                    });
          }
        });
    controler.setDumpDataAtEnd(false);
    controler.run();
    /* if something goes wrong, there will be an exception we don't catch and the test fails,
     * otherwise, everything is fine. */

    // check that BOTH plans have their act-locations calculated
    assertEquals(f.link1.getId(), act1a.getLinkId());
    assertEquals(f.link3.getId(), act1b.getLinkId());
    assertEquals(f.link1.getId(), act2a.getLinkId());
    assertEquals(f.link3.getId(), act2b.getLinkId());

    // check that BOTH plans have a route set, even when we only run 1 iteration where only one of
    // them is used.
    // assertNotNull(leg1.getRoute());
    // assertNotNull(leg2.getRoute());
    // but do not assume that the leg will be the same instance...
    for (Plan plan : new Plan[] {plan1, plan2}) {
      assertEquals(
          "unexpected plan length in " + plan.getPlanElements(), 3, plan.getPlanElements().size());
      assertNotNull(
          "null route in plan " + plan.getPlanElements(),
          ((Leg) plan.getPlanElements().get(1)).getRoute());
    }
  }
Пример #11
0
  /** Compares plan and events for each agent. Checks the type of the event and the linkId. */
  protected void checkEventsCorrespondToPlans(final Population population) {
    for (Entry<Id<Person>, List<Event>> entry : eventsByPerson.entrySet()) {
      List<Event> list = entry.getValue();
      Person p = population.getPersons().get(entry.getKey());
      // printEvents(list.get(0).agentId);
      Plan plan = p.getSelectedPlan();
      int index = 0;

      Activity act = null;
      Leg leg = null;
      for (PlanElement pe : plan.getPlanElements()) {
        if (pe instanceof Activity) {
          act = (Activity) pe;

          if (leg != null) {
            // each leg ends with enter on act link
            // => only for non empty car legs and non-cars legs this
            // statement is true
            if (leg.getMode().equals(TransportMode.car)
                && ((NetworkRoute) leg.getRoute()).getLinkIds().size() > 0) {
              assertTrue(list.get(index) instanceof LinkEnterEvent);
              assertTrue(
                  act.getLinkId()
                      .toString()
                      .equalsIgnoreCase(((LinkEnterEvent) list.get(index)).getLinkId().toString()));
              index++;
            }

            // each leg ends with arrival on act link
            assertTrue(list.get(index) instanceof PersonArrivalEvent);
            assertTrue(
                act.getLinkId()
                    .toString()
                    .equalsIgnoreCase(
                        ((PersonArrivalEvent) list.get(index)).getLinkId().toString()));
            index++;

            // each leg ends with arrival on act link
            assertTrue(list.get(index) instanceof ActivityStartEvent);
            assertEquals(act.getLinkId(), ((ActivityStartEvent) list.get(index)).getLinkId());
            index++;
          }
        } else if (pe instanceof Leg) {
          leg = (Leg) pe;

          // act end event
          assertTrue(list.get(index) instanceof ActivityEndEvent);
          assertEquals(act.getLinkId(), ((ActivityEndEvent) list.get(index)).getLinkId());
          index++;

          // each leg starts with departure on act link
          assertTrue(list.get(index) instanceof PersonDepartureEvent);
          assertTrue(
              act.getLinkId()
                  .toString()
                  .equalsIgnoreCase(
                      ((PersonDepartureEvent) list.get(index)).getLinkId().toString()));
          index++;

          // each CAR leg must enter/leave act link
          if (leg.getMode().equals(TransportMode.car)) {

            // if car leg contains empty route, then this check is
            // not applicable
            if (((NetworkRoute) leg.getRoute()).getLinkIds().size() > 0) {
              // the first LinkEnterEvent is a AgentWait2LinkEvent
              assertTrue(list.get(index) instanceof VehicleEntersTrafficEvent);
              assertTrue(
                  act.getLinkId()
                      .toString()
                      .equalsIgnoreCase(
                          ((VehicleEntersTrafficEvent) list.get(index)).getLinkId().toString()));
              index++;

              assertTrue(list.get(index) instanceof LinkLeaveEvent);
              assertTrue(
                  act.getLinkId()
                      .toString()
                      .equalsIgnoreCase(((LinkLeaveEvent) list.get(index)).getLinkId().toString()));
              index++;
            }

            for (Id<Link> linkId : ((NetworkRoute) leg.getRoute()).getLinkIds()) {
              // enter link and leave each link on route
              assertTrue(list.get(index) instanceof LinkEnterEvent);
              assertTrue(linkId.equals(((LinkEnterEvent) list.get(index)).getLinkId()));
              index++;

              assertTrue(list.get(index) instanceof LinkLeaveEvent);
              assertTrue(linkId.equals(((LinkLeaveEvent) list.get(index)).getLinkId()));
              index++;
            }
          }
        }
      }
    }
  }