Exemplo n.º 1
1
  @Test
  public void testLegAttributesIO() {
    final Population population = PopulationUtils.createPopulation(ConfigUtils.createConfig());

    final Person person = population.getFactory().createPerson(Id.createPersonId("Donald Trump"));
    population.addPerson(person);

    final Plan plan = population.getFactory().createPlan();
    person.addPlan(plan);
    final Leg leg = population.getFactory().createLeg("SUV");
    plan.addActivity(
        population.getFactory().createActivityFromLinkId("speech", Id.createLinkId(1)));
    plan.addLeg(leg);
    plan.addActivity(population.getFactory().createActivityFromLinkId("tweet", Id.createLinkId(2)));

    leg.getAttributes().putAttribute("mpg", 0.000001d);

    final String file = utils.getOutputDirectory() + "/population.xml";
    new PopulationWriter(population).writeV6(file);

    final Scenario readScenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
    new PopulationReader(readScenario).readFile(file);

    final Person readPerson =
        readScenario.getPopulation().getPersons().get(Id.createPersonId("Donald Trump"));
    final Leg readLeg = (Leg) readPerson.getSelectedPlan().getPlanElements().get(1);

    Assert.assertEquals(
        "Unexpected Double attribute in " + readLeg.getAttributes(),
        leg.getAttributes().getAttribute("mpg"),
        readLeg.getAttributes().getAttribute("mpg"));
  }
Exemplo n.º 2
0
  /** The event handler for this group's timers, RtpFork, and calls. */
  public boolean onEvent(LegEvent event) {
    LOG.debug("PageGroup::onEvent got event " + event.getDescription());

    if (event.getDescription().equals("timer: status=fired name=maximum_duration")) {
      // Maximum duration timer fired, end the page.
      end();
    } else if (event.getDescription().equals("timer: status=fired name=beep_start")) {
      if (beep != null) {
        // Start the beep (if any)
        // Add the inbound caller to the RTP mix so he can hear the beep
        rtpFork.addDestination(inboundRtp);
        rtpFork.startLocalAudio(beep);
      }
    } else if (event.getDescription().equals("localAudio end")) {
      // Now that the beep is done, remove the inbound caller to the RTP mix
      // So he does not hear himself
      rtpFork.removeDestination(inboundRtp);
    } else if (event.getDescription().startsWith("dialog bye")) {
      // Someone hung up.
      Leg leg = event.getLeg();
      if (leg == inbound) {
        // Inbound call ended.  End the page.
        end();
      } else {
        // Outbound call ended.  Stop sending rtp to it
        rtpFork.removeDestination(leg.getRemoteRtpAddress());
      }
    } else if (event.getDescription().equals("sdp")) {
      // SDP changed, keep rtpFork informed.
      rtpFork.removeDestination(event.getLeg().getPreviousRtpAddress());
      rtpFork.addDestination(event.getLeg().getRemoteRtpAddress());
    }

    return true;
  }
Exemplo n.º 3
0
  /**
   * End this page, by clearing up all timers, outbound calls, the inbound call, and the RTP forker.
   *
   * <p>synchronized so multiple calls are serialized. Only the first should be needed.
   */
  public synchronized void end() {
    // Remove all timers associated with me
    Timers.removeTimer(this);

    // Hangup on all outbound calls.
    for (Leg outbound : outbounds) {
      try {
        outbound.destroyLeg();
      } catch (Exception e) {
        LOG.error("PageGroup::end outbound", e);
      }
    }
    outbounds.removeAllElements();

    // Stop the RTP processing.
    rtpFork.stop();
    rtpFork.removeAllDestinations();

    // Hangup on the inbound call
    if (inbound != null) {
      try {
        inbound.destroyLeg();
      } catch (Exception e) {

        LOG.error("PageGroup::end inbound", e);
      }
      inbound = null;
    }
    busy = false;
  }
Exemplo n.º 4
0
  /**
   * Trigger a page of all the destinations.
   *
   * @param inbound The inbound Leg (audio from here goes to all destinations)
   * @param inboundRtp The destination to send RTP packets so the inbound caller can hear.
   * @param alertInfoKey The magic key needed for Polycom Auto-Answer
   * @return
   */
  public boolean page(Leg inbound, InetSocketAddress inboundRtp, String alertInfoKey) {
    if (busy == false) {
      LOG.debug("PageGroup::page starting");
      busy = true;
      this.inbound = inbound;
      this.inboundRtp = inboundRtp;

      // Spin up the RTP forker
      rtpFork.start();
      try {
        // Get the originator for the Page.
        InboundLeg origLeg = (InboundLeg) inbound;
        String pageOriginatorAddress;
        pageOriginatorAddress = origLeg.getAddress();

        // Answer the inbound call.
        inbound.acceptCall(rtpPort);

        // Start the timers
        if (maximumDuration > 0) {
          // Start the maximumDuration timer if it is greater than 0
          Timers.addTimer(
              "maximum_duration", maximumDuration, this); // End this page after this many mS
        }
        Timers.addTimer("beep_start", 1000, this); // Start the beep after this much time

        // Place a call to each destination
        for (String destination : destinations) {
          // Compare the originator with the destination.  Only place an outbound call
          // if they aren't the same.  We don't make a call to the same destination that
          // is initiating the page.
          if (destination.compareToIgnoreCase(pageOriginatorAddress) != 0) {
            Leg outbound =
                placeCall(inbound.getDisplayName(), origLeg.getCallId(), destination, alertInfoKey);
            if (outbound != null) {
              // Keep track of them!
              outbounds.add(outbound);
            }
          } else {
            LOG.info(
                String.format("Skipping %s as it is the page originator.", pageOriginatorAddress));
          }
        }
        return true;
      } catch (Throwable t) {
        LOG.warn("PageGroup::page", t);
        end();
      }
    }
    LOG.debug("PageGroup::page failed");
    return false;
  }
  public void CreateChoiceSets() {

    PopulationFactory populationFactory = population.getFactory();
    ArrayList<Person> newPersons = new ArrayList<>();

    /*Clean routes*/
    PlanRouteStripper planRouteStripper = new PlanRouteStripper();
    planRouteStripper.run(population);

    for (Id<Person> personId : population.getPersons().keySet()) {

      Plan plan = population.getPersons().get(personId).getSelectedPlan();
      plan.getCustomAttributes()
          .put(
              "toll",
              population
                  .getPersonAttributes()
                  .getAttribute(personId.toString(), "selectedPlanToll"));
      initialPlans.put(personId, plan);

      population.getPersons().get(personId).createCopyOfSelectedPlanAndMakeSelected();

      Plan planTmp = population.getPersons().get(personId).getSelectedPlan();
      population.getPersons().get(personId).getPlans().clear();
      population.getPersons().get(personId).addPlan(planTmp);
    }

    /*Create optimal walk plan and substitute it in the planMap*/
    OptimalWalkPlanFinder optimalWalkPlanFinder = new OptimalWalkPlanFinder(controler.getConfig());

    for (Id<Person> personId : population.getPersons().keySet()) {

      /* Plan cloning for each mode*/
      HashMap<String, Plan> planMap = new HashMap<>();

      Leg firstLeg =
          (Leg) population.getPersons().get(personId).getSelectedPlan().getPlanElements().get(1);
      String initialMode = firstLeg.getMode();

      // If "No PT"
      if (initialMode == "pt" && simType.equals("noPT")) initialMode = "walk";

      planMap.put(initialMode, population.getPersons().get(personId).getSelectedPlan());

      // String[] modes =
      // controler.getScenario().getConfig().getModule("subtourModeChoice").getValue("modes").split(",");
      // String[] modes = {"car","walk"};

      ArrayList<String> relevantModes = new ArrayList<String>();
      relevantModes.add("car");
      if (!simType.equals("carOnly") && !simType.equals("noPT")) relevantModes.add("pt");

      for (String mode : relevantModes) {
        if (!initialMode.equals(mode) && !mode.equals("walk")) {
          population.getPersons().get(personId).createCopyOfSelectedPlanAndMakeSelected();
          Plan planForModeChange = population.getPersons().get(personId).getSelectedPlan();

          for (int j = 1; j < planForModeChange.getPlanElements().size(); j += 2) {
            Leg leg = (Leg) planForModeChange.getPlanElements().get(j);
            leg.setMode(mode);
          }

          planMap.put(mode, planForModeChange);
        }
      }

      /*Check if walk is a viable alternative and if so, add it to the choice set*/
      if (!initialMode.equals("walk")
          && optimalWalkPlanFinder.getWalkTravelTime(planMap.get(initialMode)) <= 3600.0
          && !simType.equals("carOnly")) {
        population.getPersons().get(personId).createCopyOfSelectedPlanAndMakeSelected();
        Plan planForModeChange = population.getPersons().get(personId).getSelectedPlan();
        planForModeChange = optimalWalkPlanFinder.findOptimalWalkPlan(planForModeChange);
        for (int j = 1; j < planForModeChange.getPlanElements().size(); j += 2) {
          Leg leg = (Leg) planForModeChange.getPlanElements().get(j);
          leg.setMode("walk");
        }

        planMap.put("walk", planForModeChange);
      }
      ;

      // String[] relevantModes = {"car","pt"};

      /* Departure time modification for each mode*/
      for (String mode : relevantModes) {
        Plan basePlan = planMap.get(mode);
        population.getPersons().get(personId).setSelectedPlan(basePlan);

        for (int i = 0; i < 6; i++) {
          population.getPersons().get(personId).createCopyOfSelectedPlanAndMakeSelected();
        }

        population.getPersons().get(personId).setSelectedPlan(basePlan);

        int planModCounter = 0;
        /* Departure time modification */
        for (Plan planToModify : population.getPersons().get(personId).getPlans()) {

          if (((Leg) planToModify.getPlanElements().get(1)).getMode().equals(mode)
              && !PersonUtils.isSelected(planToModify)) {

            // Home departure time + 1h
            if (planModCounter == 0) {
              Activity act = (Activity) planToModify.getPlanElements().get(0);
              act.setEndTime(act.getEndTime() + 3600.0);
            }

            // Home departure time - 1h
            if (planModCounter == 1) {
              Activity act = (Activity) planToModify.getPlanElements().get(0);
              act.setEndTime(act.getEndTime() - 3600.0);
            }

            // Work departure time + 1h
            if (planModCounter == 2) {
              Activity act = (Activity) planToModify.getPlanElements().get(2);
              act.setEndTime(act.getEndTime() + 3600.0);
            }

            // Work departure time - 1h
            if (planModCounter == 3) {
              Activity act = (Activity) planToModify.getPlanElements().get(2);
              act.setEndTime(act.getEndTime() - 3600.0);
            }

            // Home departure time + 1h, Work departure time +1h
            if (planModCounter == 4) {
              Activity act = (Activity) planToModify.getPlanElements().get(0);
              act.setEndTime(act.getEndTime() + 3600.0);

              Activity act2 = (Activity) planToModify.getPlanElements().get(2);
              act2.setEndTime(act2.getEndTime() + 3600.0);
            }

            // Home departure time - 1h, Work departure time -1h
            if (planModCounter == 5) {
              Activity act = (Activity) planToModify.getPlanElements().get(0);
              act.setEndTime(act.getEndTime() - 3600.0);

              Activity act2 = (Activity) planToModify.getPlanElements().get(2);
              act2.setEndTime(act2.getEndTime() - 3600.0);
            }

            //                      NOT REALISTIC
            //						//Home departure time +1h, Work departure time -1h
            //						if (planModCounter == 6) {
            //							Activity act = (Activity) planToModify.getPlanElements().get(0);
            //							act.setEndTime(act.getEndTime() + 3600.0);
            //
            //							Activity act2 = (Activity) planToModify.getPlanElements().get(2);
            //							act2.setEndTime(act2.getEndTime() - 3600.0);
            //						}
            //
            //						//Home departure time - 1h, Work departure time +1h
            //						if (planModCounter == 7) {
            //							Activity act = (Activity) planToModify.getPlanElements().get(0);
            //							act.setEndTime(act.getEndTime() - 3600.0);
            //
            //							Activity act2 = (Activity) planToModify.getPlanElements().get(2);
            //							act2.setEndTime(act2.getEndTime() + 3600.0);
            //						}

            planModCounter++;
          }
        }
      }

      /*Plan split to different persons*/
      int count = 0;
      population.getPersons().get(personId).setSelectedPlan(planMap.get(initialMode));
      for (Plan newPlan : population.getPersons().get(personId).getPlans()) {
        if (personId.toString().equals("1000")) System.out.println();
        if (!PersonUtils.isSelected(newPlan)) {
          count++;
          Person newPerson =
              populationFactory.createPerson(Id.createPersonId(personId.toString() + "_" + count));
          newPerson.addPlan(newPlan);
          newPerson.setSelectedPlan(newPlan);
          newPersons.add(newPerson);
          population
              .getPersonAttributes()
              .putAttribute(
                  newPerson.getId().toString(),
                  "income",
                  population.getPersonAttributes().getAttribute(personId.toString(), "income"));
          population
              .getPersonAttributes()
              .putAttribute(
                  newPerson.getId().toString(),
                  "betaFactor",
                  population.getPersonAttributes().getAttribute(personId.toString(), "betaFactor"));
        }
      }

      /*Clear all plans from initial person*/
      //			population.getPersons().get(personId).getPlans().clear();
      //			population.getPersons().get(personId).addPlan(initialPlans.get(personId));
      //			population.getPersons().get(personId).setSelectedPlan(initialPlans.get(personId));

      /*Clear all plans from initial person*/
      population.getPersons().get(personId).getPlans().clear();

      Person tempPerson = population.getFactory().createPerson(Id.create(20000, Person.class));
      tempPerson.addPlan(initialPlans.get(personId));
      tempPerson.setSelectedPlan(initialPlans.get(personId));
      tempPerson.createCopyOfSelectedPlanAndMakeSelected();
      Plan tempPlan = tempPerson.getSelectedPlan();
      population.getPersons().get(personId).addPlan(tempPlan);
      population.getPersons().get(personId).setSelectedPlan(tempPlan);
    }

    /*Add new agents to the simulation*/
    for (Person newPerson : newPersons) {
      population.addPerson(newPerson);
    }

    /*Write out new population file*/
    //		System.out.println("New number of persons: " + population.getPersons().size());
    //		new org.matsim.core.population.PopulationWriter(population,
    // controler.getScenario().getNetwork()).write("/Volumes/DATA 1 (WD 2
    // TB)/output_SelectExp1_5p_"+simType+"_1000it_Dwell/popText.xml");

  }
Exemplo n.º 6
0
  private HandlingActivity calculateNextExpectedActivity(
      RouteSpecification routeSpecification, Itinerary itinerary) {
    if (!onTrack()) return NO_ACTIVITY;

    if (lastEvent == null)
      return new HandlingActivity(HandlingEvent.Type.RECEIVE, routeSpecification.getOrigin());

    switch (lastEvent.getType()) {
      case LOAD:
        for (Leg leg : itinerary.getLegs()) {
          if (leg.getLoadLocation().equals(lastEvent.getLocation())) {
            return new HandlingActivity(
                HandlingEvent.Type.UNLOAD, leg.getUnloadLocation(), leg.getVoyage());
          }
        }

        return NO_ACTIVITY;

      case UNLOAD:
        for (Iterator<Leg> it = itinerary.getLegs().iterator(); it.hasNext(); ) {
          final Leg leg = it.next();
          if (leg.getUnloadLocation().equals(lastEvent.getLocation())) {
            if (it.hasNext()) {
              final Leg nextLeg = it.next();
              return new HandlingActivity(
                  HandlingEvent.Type.LOAD, nextLeg.getLoadLocation(), nextLeg.getVoyage());
            } else {
              return new HandlingActivity(HandlingEvent.Type.CLAIM, leg.getUnloadLocation());
            }
          }
        }

        return NO_ACTIVITY;

      case RECEIVE:
        final Leg firstLeg = itinerary.getLegs().iterator().next();
        return new HandlingActivity(
            HandlingEvent.Type.LOAD, firstLeg.getLoadLocation(), firstLeg.getVoyage());

      case CLAIM:
      default:
        return NO_ACTIVITY;
    }
  }
Exemplo n.º 7
0
 public void update(float delta, Vector2 pos) {
   mother.getPosition();
 }
Exemplo n.º 8
0
  @Override
  public void run(final Plan p) {
    PlanImpl plan = (PlanImpl) p;
    double dayTime = 0.0;
    double carDayTime = 0.0;
    double ptDayTime = 0.0;
    double wlkDayTime = 0.0;
    double bikeDayTime = 0.0;
    double othersDayTime = 0.0;
    for (PlanElement pe : plan.getPlanElements())
      if (pe instanceof Leg) {
        Leg bl = (Leg) pe;

        ActTypeZrh legIntent = (ActTypeZrh) this.getLegIntent(plan, bl);

        double time = bl.getTravelTime() / 60.0;
        if (time < 0) time = 0;
        // if (bl.getDepartureTime() < 86400) {
        dayTime += time;
        String mode = bl.getMode();
        if (TransportMode.car.equals(mode)) {
          carTime += time;
          carDayTime += time;
          switch (legIntent) {
            case home:
              carHomeTime += time;
              break;
            case work:
              carWorkTime += time;
              break;
            case education:
              carEducTime += time;
              break;
            case shopping:
              carShopTime += time;
              break;
            case leisure:
              carLeisTime += time;
              break;
            default:
              carOtherTime += time;
              break;
          }
          carLegTimeCounts[Math.min(100, (int) time / 2)]++;
        } else if (TransportMode.pt.equals(mode)) {
          ptTime += time;
          ptDayTime += time;
          switch (legIntent) {
            case home:
              ptHomeTime += time;
              break;
            case work:
              ptWorkTime += time;
              break;
            case education:
              ptEducTime += time;
              break;
            case shopping:
              ptShopTime += time;
              break;
            case leisure:
              ptLeisTime += time;
              break;
            default:
              ptOtherTime += time;
              break;
          }
          ptLegTimeCounts[Math.min(100, (int) time / 2)]++;
        } else if (TransportMode.walk.equals(mode)) {
          wlkTime += time;
          wlkDayTime += time;
          switch (legIntent) {
            case home:
              wlkHomeTime += time;
              break;
            case work:
              wlkWorkTime += time;
              break;
            case education:
              wlkEducTime += time;
              break;
            case shopping:
              wlkShopTime += time;
              break;
            case leisure:
              wlkLeisTime += time;
              break;
            default:
              wlkOtherTime += time;
              break;
          }
          wlkLegTimeCounts[Math.min(100, (int) time / 2)]++;
        } else if (TransportMode.bike.equals(mode)) {
          bikeTime += time;
          bikeDayTime += time;
          switch (legIntent) {
            case home:
              bikeHomeTime += time;
              break;
            case work:
              bikeWorkTime += time;
              break;
            case education:
              bikeEducTime += time;
              break;
            case shopping:
              bikeShopTime += time;
              break;
            case leisure:
              bikeLeisTime += time;
              break;
            default:
              bikeOtherTime += time;
              break;
          }
          bikeLegTimeCounts[Math.min(100, (int) time / 2)]++;
        } else {
          othersTime += time;
          othersDayTime += time;
          switch (legIntent) {
            case home:
              othersHomeTime += time;
              break;
            case work:
              othersWorkTime += time;
              break;
            case education:
              othersEducTime += time;
              break;
            case shopping:
              othersShopTime += time;
              break;
            case leisure:
              othersLeisTime += time;
              break;
            default:
              othersOtherTime += time;
              break;
          }
          othersLegTimeCounts[Math.min(100, (int) time / 2)]++;
        }
      }
    for (int i = 0; i <= Math.min(100, (int) dayTime); i++) totalDayEnRouteTimeCounts[i]++;
    for (int i = 0; i <= Math.min(100, (int) othersDayTime); i++) othersDayEnRouteTimeCounts[i]++;
    for (int i = 0; i <= Math.min(100, (int) carDayTime); i++) carDayEnRouteTimeCounts[i]++;
    for (int i = 0; i <= Math.min(100, (int) ptDayTime); i++) ptDayEnRouteTimeCounts[i]++;
    for (int i = 0; i <= Math.min(100, (int) wlkDayTime); i++) wlkDayEnRouteTimeCounts[i]++;
    for (int i = 0; i <= Math.min(100, (int) bikeDayTime); i++) bikeDayEnRouteTimeCounts[i]++;
  }