Example #1
0
  /**
   * Calculate and return the tour mode choice logsum for the DMU object.
   *
   * @param mcDmuObject is the tour mode choice model DMU object
   * @return logsum of the tour mode choice alternatives
   */
  public double getModeChoiceLogsum(
      TourModeChoiceDMU mcDmuObject,
      String primaryPurposeName,
      Logger modelLogger,
      String choiceModelDescription,
      String decisionMakerLabel) {

    long check1 = System.nanoTime();

    int modelIndex = purposeModelIndexMap.get(primaryPurposeName.toLowerCase());

    HouseholdIf household = mcDmuObject.getHouseholdObject();

    // log headers to traceLogger
    if (household.getDebugChoiceModels()) {
      mcModel[modelIndex].choiceModelUtilityTraceLoggerHeading(
          choiceModelDescription, decisionMakerLabel);
    }

    mcModel[modelIndex].computeUtilities(mcDmuObject, mcDmuObject.getDmuIndexValues());
    double logsum = mcModel[modelIndex].getLogsum();

    // write UEC calculation results to separate model specific log file
    if (household.getDebugChoiceModels()) {
      if (modelLogger.isDebugEnabled()) {
        String loggingHeader = String.format("%s   %s", choiceModelDescription, decisionMakerLabel);
        mcModel[modelIndex].logUECResults(modelLogger, loggingHeader);
      }
      modelLogger.info(choiceModelDescription + " Logsum value: " + logsum);
      modelLogger.info("");
      modelLogger.info("");
    }

    mcLsTotalTime = System.nanoTime() - check1;

    return logsum;
  }
Example #2
0
  public int getModeChoice(TourModeChoiceDMU mcDmuObject, String primaryPurposeName) {

    int modelIndex = purposeModelIndexMap.get(primaryPurposeName);

    HouseholdIf household = mcDmuObject.getHouseholdObject();

    Logger modelLogger = null;
    if (tourCategory.equalsIgnoreCase(ModelStructure.MANDATORY_CATEGORY))
      modelLogger = tourMCManLogger;
    else modelLogger = tourMCNonManLogger;

    String choiceModelDescription = "";
    String decisionMakerLabel = "";
    String loggingHeader = "";
    String separator = "";

    TourIf tour = mcDmuObject.getTourObject();

    if (household.getDebugChoiceModels()) {

      PersonIf person;
      if (tour.getTourCategory().equalsIgnoreCase(ModelStructure.JOINT_NON_MANDATORY_CATEGORY)) {
        person = tour.getPersonObject();
      } else {
        person = mcDmuObject.getPersonObject();
      }

      choiceModelDescription =
          String.format(
              "%s Tour Mode Choice Model for: primaryPurpose=%s, Orig=%d, OrigSubZ=%d, Dest=%d, DestSubZ=%d",
              tourCategory,
              primaryPurposeName,
              household.getHhTaz(),
              household.getHhWalkSubzone(),
              tour.getTourDestTaz(),
              tour.getTourDestWalkSubzone());
      decisionMakerLabel =
          String.format(
              "HH=%d, PersonNum=%d, PersonType=%s, TourId=%d",
              person.getHouseholdObject().getHhId(),
              person.getPersonNum(),
              person.getPersonType(),
              tour.getTourId());
      loggingHeader = String.format("%s    %s", choiceModelDescription, decisionMakerLabel);

      household.logHouseholdObject(
          "Pre " + tourCategory + " Tour Mode Choice Model HHID=" + household.getHhId(),
          tourMCManLogger);
      household.logPersonObject(decisionMakerLabel, tourMCManLogger, person);

      mcModel[modelIndex].choiceModelUtilityTraceLoggerHeading(
          choiceModelDescription, decisionMakerLabel);

      modelLogger.info(" ");
      for (int k = 0; k < loggingHeader.length(); k++) separator += "+";
      modelLogger.info(loggingHeader);
      modelLogger.info(separator);

      household.logTourObject(loggingHeader, modelLogger, person, tour);
    }

    // create TVPB for tour mode choice model
    setTVPBValues(mcDmuObject, primaryPurposeName, true, household.getDebugChoiceModels());

    mcModel[modelIndex].computeUtilities(mcDmuObject, mcDmuObject.getDmuIndexValues());

    Random hhRandom = household.getHhRandom();
    int randomCount = household.getHhRandomCount();
    double rn = hhRandom.nextDouble();

    // if the choice model has at least one available alternative, make choice.
    int chosen;
    if (mcModel[modelIndex].getAvailabilityCount() > 0)
      chosen = mcModel[modelIndex].getChoiceResult(rn);
    else {
      logger.error(
          String.format(
              "Exception caught for HHID=%d, no available %s tour mode alternatives to choose from in choiceModelApplication.",
              household.getHhId(), tourCategory));
      throw new RuntimeException();
    }

    // debug output
    if (household.getDebugChoiceModels()) {

      double[] utilities = mcModel[modelIndex].getUtilities(); // 0s-indexing
      double[] probabilities = mcModel[modelIndex].getProbabilities(); // 0s-indexing
      boolean[] availabilities = mcModel[modelIndex].getAvailabilities(); // 1s-indexing
      String[] altNames = mcModel[modelIndex].getAlternativeNames(); // 0s-indexing

      PersonIf person;
      if (tour.getTourCategory().equalsIgnoreCase(ModelStructure.JOINT_NON_MANDATORY_CATEGORY)) {
        person = tour.getPersonObject();
      } else {
        person = mcDmuObject.getPersonObject();
      }

      String personTypeString = person.getPersonType();
      int personNum = person.getPersonNum();

      modelLogger.info(
          "Person num: "
              + personNum
              + ", Person type: "
              + personTypeString
              + ", Tour Id: "
              + tour.getTourId());
      modelLogger.info(
          "Alternative                    Utility       Probability           CumProb");
      modelLogger.info(
          "--------------------    --------------    --------------    --------------");

      double cumProb = 0.0;
      for (int k = 0; k < mcModel[modelIndex].getNumberOfAlternatives(); k++) {
        cumProb += probabilities[k];
        String altString = String.format("%-3d  %s", k + 1, altNames[k]);
        modelLogger.info(
            String.format(
                "%-20s%15s%18.6e%18.6e%18.6e",
                altString, availabilities[k + 1], utilities[k], probabilities[k], cumProb));
      }

      modelLogger.info(" ");
      String altString = String.format("%-3d  %s", chosen, altNames[chosen - 1]);
      modelLogger.info(
          String.format("Choice: %s, with rn=%.8f, randomCount=%d", altString, rn, randomCount));

      modelLogger.info(separator);
      modelLogger.info("");
      modelLogger.info("");

      // write choice model alternative info to log file
      mcModel[modelIndex].logAlternativesInfo(choiceModelDescription, decisionMakerLabel);
      mcModel[modelIndex].logSelectionInfo(choiceModelDescription, decisionMakerLabel, rn, chosen);
      mcModel[modelIndex].logLogitCalculations(choiceModelDescription, decisionMakerLabel);

      // write UEC calculation results to separate model specific log file
      mcModel[modelIndex].logUECResults(modelLogger, loggingHeader);
    }

    if (saveUtilsProbsFlag) {

      // get the utilities and probabilities arrays for the tour mode choice model for this tour and
      // save them to the tour object
      double[] dUtils = mcModel[modelIndex].getUtilities();
      double[] dProbs = mcModel[modelIndex].getProbabilities();

      float[] utils = new float[dUtils.length];
      float[] probs = new float[dUtils.length];
      for (int k = 0; k < dUtils.length; k++) {
        utils[k] = (float) dUtils[k];
        probs[k] = (float) dProbs[k];
      }

      tour.setTourModalUtilities(utils);
      tour.setTourModalProbabilities(probs);
    }

    return chosen;
  }