Exemple #1
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper tour, int nCallsForTour, int choice = Constants.DEFAULT_VALUE)
        {
            IPersonWrapper    person    = tour.Person;
            IPersonDayWrapper personDay = tour.PersonDay;

            //            var foodRetailServiceMedicalQtrMileLog = tour.DestinationParcel.FoodRetailServiceMedicalQtrMileLogBuffer1();
            //            var mixedUseIndex = tour.DestinationParcel.MixedUse4Index1();
            double k8HighSchoolQtrMileLog = tour.DestinationParcel.K8HighSchoolQtrMileLogBuffer1();
            int    carOwnership           = person.GetCarOwnershipSegment();

            int noCarsFlag         = FlagUtility.GetNoCarsFlag(carOwnership);
            int carCompetitionFlag = FlagUtility.GetCarCompetitionFlag(carOwnership);
            //            var notUsualWorkParcelFlag = tour.DestinationParcel.NotUsualWorkParcelFlag(person.UsualWorkParcelId);

            int votALSegment = tour.GetVotALSegment();

            int    workTaSegment       = tour.DestinationParcel.TransitAccessSegment();
            double workAggregateLogsum = Global.AggregateLogsums[tour.DestinationParcel.ZoneId]
                                         [Global.Settings.Purposes.WorkBased][carOwnership][votALSegment][workTaSegment];

            // NONE_OR_HOME

            ChoiceProbabilityCalculator.Alternative alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.NoneOrHome, true, choice == Global.Settings.Purposes.NoneOrHome);

            alternative.Choice = Global.Settings.Purposes.NoneOrHome;

            alternative.AddUtilityTerm(15, (nCallsForTour > 1).ToFlag());
            alternative.AddUtilityTerm(16, Math.Log(personDay.HomeBasedTours));
            alternative.AddUtilityTerm(18, personDay.TwoOrMoreWorkToursExist().ToFlag());
            //            alternative.AddUtility(19, notUsualWorkParcelFlag);
            alternative.AddUtilityTerm(22, noCarsFlag);
            alternative.AddUtilityTerm(23, carCompetitionFlag);
            alternative.AddUtilityTerm(32, workAggregateLogsum);
            //            alternative.AddUtility(32, mixedUseIndex);

            alternative.AddNestedAlternative(11, 0, 50);

            // WORK

            alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Work, personDay.WorkStops > 0, choice == Global.Settings.Purposes.Work);

            alternative.Choice = Global.Settings.Purposes.Work;

            alternative.AddUtilityTerm(1, 1);

            alternative.AddNestedAlternative(12, 1, 50);

            // SCHOOL

            alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.School, personDay.SchoolStops > 0, choice == Global.Settings.Purposes.School);

            alternative.Choice = Global.Settings.Purposes.School;

            alternative.AddUtilityTerm(3, 1);

            alternative.AddNestedAlternative(12, 1, 50);

            // ESCORT

            alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Escort, personDay.EscortStops > 0, choice == Global.Settings.Purposes.Escort);

            alternative.Choice = Global.Settings.Purposes.Escort;

            alternative.AddUtilityTerm(4, 1);
            alternative.AddUtilityTerm(39, k8HighSchoolQtrMileLog);

            alternative.AddNestedAlternative(12, 1, 50);

            // PERSONAL_BUSINESS

            alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.PersonalBusiness, personDay.PersonalBusinessStops > 0, choice == Global.Settings.Purposes.PersonalBusiness);

            alternative.Choice = Global.Settings.Purposes.PersonalBusiness;

            alternative.AddUtilityTerm(6, 1);

            alternative.AddNestedAlternative(12, 1, 50);

            // SHOPPING

            alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Shopping, personDay.ShoppingStops > 0, choice == Global.Settings.Purposes.Shopping);

            alternative.Choice = Global.Settings.Purposes.Shopping;

            alternative.AddUtilityTerm(8, 1);

            alternative.AddNestedAlternative(12, 1, 50);

            // MEAL

            alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Meal, personDay.MealStops > 0, choice == Global.Settings.Purposes.Meal);

            alternative.Choice = Global.Settings.Purposes.Meal;

            alternative.AddUtilityTerm(10, 1);

            alternative.AddNestedAlternative(12, 1, 50);

            // SOCIAL

            alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Social, personDay.SocialStops > 0, choice == Global.Settings.Purposes.Social);

            alternative.Choice = Global.Settings.Purposes.Social;

            alternative.AddUtilityTerm(13, 1);

            alternative.AddNestedAlternative(12, 1, 50);
        }
            public void SetUtilities(ISampleItem sampleItem, int sampleFrequency)
            {
                if (sampleItem == null)
                {
                    throw new ArgumentNullException("sampleItem");
                }

                ChoiceProbabilityCalculator.Alternative alternative = sampleItem.Alternative;

                IHouseholdWrapper household = _tour.Household;
                IPersonWrapper    person    = _tour.Person;
                IPersonDayWrapper personDay = _tour.PersonDay;
                //                var householdHasChildren = household.HasChildren;

                IParcelWrapper destinationParcel = ChoiceModelFactory.Parcels[sampleItem.ParcelId];

                IParcelWrapper excludedParcel      = person.UsualWorkParcel == null || person.UsualWorkParcelId == household.ResidenceParcelId || _tour.DestinationPurpose != Global.Settings.Purposes.Work || _tour.GetTourCategory() == Global.Settings.TourCategories.WorkBased ? null : person.UsualWorkParcel;
                bool           usualWorkParcel     = (excludedParcel != null && excludedParcel.Id == destinationParcel.Id); // only 1 for oddball alternative on tours with oddball alternative
                int            usualWorkParcelFlag = usualWorkParcel.ToFlag();

                // Comment out these nesting calls when estimating the conditional flat model
                // model is NL with oddball alternative
                if (usualWorkParcelFlag == 0)
                {
                    // this alternative is in the non-oddball nest
                    alternative.AddNestedAlternative(_sampleSize + 2, 0, 60); // associates alternative with non-oddball nest
                }
                else
                {
                    // this is the oddball alternative
                    alternative.AddNestedAlternative(_sampleSize + 3, 1, 60); // associates alternative with oddball nest
                }


                if (!alternative.Available)
                {
                    return;
                }


                // use this block of code to eliminate the oddball alternative for estimation of the conditional model
                //if (usualWorkParcelFlag == 1) {
                //    alternative.Available = false;
                //
                //    return;
                //}

                double fastestTravelTime =
                    ImpedanceRoster.GetValue("ivtime", Global.Settings.Modes.Hov3, Global.Settings.PathTypes.FullNetwork, Global.Settings.ValueOfTimes.DefaultVot, _fastestAvailableTimeOfDay, _tour.OriginParcel, destinationParcel).Variable +
                    ImpedanceRoster.GetValue("ivtime", Global.Settings.Modes.Hov3, Global.Settings.PathTypes.FullNetwork, Global.Settings.ValueOfTimes.DefaultVot, _fastestAvailableTimeOfDay, destinationParcel, _tour.OriginParcel).Variable;

                if (fastestTravelTime >= _maxAvailableMinutes)
                {
                    alternative.Available = false;

                    return;
                }

                alternative.Choice = destinationParcel;

                double tourLogsum;

                if (_tour.IsHomeBasedTour)
                {
                    ChoiceProbabilityCalculator.Alternative nestedAlternative = Global.ChoiceModelSession.Get <WorkTourModeModel>().RunNested(_tour, destinationParcel, household.VehiclesAvailable, person.GetTransitFareDiscountFraction());
                    tourLogsum = nestedAlternative == null ? 0 : nestedAlternative.ComputeLogsum();
                }
                else
                {
                    ChoiceProbabilityCalculator.Alternative nestedAlternative = Global.ChoiceModelSession.Get <WorkBasedSubtourModeModel>().RunNested(_tour, destinationParcel);
                    tourLogsum = nestedAlternative == null ? 0 : nestedAlternative.ComputeLogsum();
                }

                //var purpose = _tour.TourPurposeSegment;
                //var nonWorkPurpose = _tour.DestinationPurpose != Global.Settings.Purposes.Work;
                int carOwnership = person.GetCarOwnershipSegment();
                //var votSegment = _tour.VotALSegment;
                //var transitAccess = destinationParcel.TransitAccessSegment();
                //var aggregateLogsum = Global.AggregateLogsums[destinationParcel.ZoneId][purpose][carOwnership][votSegment][transitAccess];
                //var aggregateLogsumHomeBased = Global.AggregateLogsums[destinationParcel.ZoneId][Global.Settings.Purposes.HomeBasedComposite][carOwnership][votSegment][transitAccess];
                //var aggregateLogsumWorkBased = Global.AggregateLogsums[destinationParcel.ZoneId][Global.Settings.Purposes.Work_BASED][carOwnership][votSegment][transitAccess];

                double distanceFromOrigin = _tour.OriginParcel.DistanceFromOrigin(destinationParcel, _tour.DestinationArrivalTime);
                //                var distanceFromOrigin0 = Math.Max(0, Math.Min(distanceFromOrigin - .5, 1 - .5));
                //                var distanceFromOrigin3 = Math.Max(0, distanceFromOrigin - 1);
                //                var distanceFromOrigin4 = Math.Min(distanceFromOrigin, .10);
                //                var distanceFromOrigin5 = Math.Max(0, Math.Min(distanceFromOrigin - .1, .5 - .1));
                //                var distanceFromOrigin8 = Math.Max(0, Math.Min(distanceFromOrigin - .1, .35 - .1));
                //                var distanceFromOrigin9 = Math.Max(0, Math.Min(distanceFromOrigin - .35, 1 - .35));
                double distanceFromOriginLog = Math.Log(1 + distanceFromOrigin);
                double distanceFromWorkLog   = person.UsualWorkParcel.DistanceFromWorkLog(destinationParcel, 1);
                double distanceFromSchoolLog = person.UsualSchoolParcel.DistanceFromSchoolLog(destinationParcel, 1);
                //                var millionsSquareFeet = destinationZoneTotals.MillionsSquareFeet();

                double timePressure = Math.Log(1 - fastestTravelTime / _maxAvailableMinutes);

                // log transforms of buffers for Neighborhood effects
                double empEduBuffer = Math.Log(destinationParcel.EmploymentEducationBuffer1 + 1.0);
                //                var EMPFOO_B = Math.Log(destinationParcel.EmploymentFoodBuffer1 + 1.0);
                //                var EMPGOV_B = Math.Log(destinationParcel.EmploymentGovernmentBuffer1 + 1.0);
                double empOfcBuffer = Math.Log(destinationParcel.EmploymentOfficeBuffer1 + 1.0);
                //                var EMPRET_B = Math.Log(destinationParcel.EmploymentRetailBuffer1 + 1.0);
                //                var EMPSVC_B = Math.Log(destinationParcel.EmploymentServiceBuffer1 + 1.0);
                //                var EMPMED_B = Math.Log(destinationParcel.EmploymentMedicalBuffer1 + 1.0);
                double empIndBuffer = Math.Log(destinationParcel.EmploymentIndustrialBuffer1 + destinationParcel.EmploymentAgricultureConstructionBuffer1 + 1.0);
                //                var EMPTOT_B = Math.Log(destinationParcel.EmploymentTotalBuffer1 + 1.0);
                double housesBuffer = Math.Log(destinationParcel.HouseholdsBuffer1 + 1.0);
                //                var STUDK12B = Math.Log(destinationParcel.StudentsK8Buffer1 + destinationParcel.StudentsHighSchoolBuffer1 + 1.0);
                //                var STUDUNIB = Math.Log(destinationParcel.StudentsUniversityBuffer1 + 1.0);
                //                var EMPHOU_B = Math.Log(destinationParcel.EmploymentTotalBuffer1 + destinationParcel.HouseholdsBuffer1 + 1.0);

                // parking attributes
                //                var parcelParkingDensity = destinationParcel.ParcelParkingPerTotalEmployment();
                //                var zoneParkingDensity = destinationParcel.ZoneParkingPerTotalEmploymentAndK12UniversityStudents(destinationZoneTotals, millionsSquareFeet);
                //                var ParkingPaidDailyLogBuffer1 = Math.Log(1 + destinationParcel.ParkingOffStreetPaidDailySpacesBuffer1);

                // connectivity attributes
                //                var c34Ratio = destinationParcel.C34RatioBuffer1();

                //                var carDeficitFlag = FlagUtility.GetCarDeficitFlag(carOwnership);  // includes no cars
                //                var carCompetitionFlag = FlagUtility.GetCarCompetitionFlag(carOwnership); // exludes no cars
                int noCarCompetitionFlag = FlagUtility.GetNoCarCompetitionFlag(carOwnership);

                //                var noCarsFlag = FlagUtility.GetNoCarsFlag(carOwnership);

                // Usual location attributes
                alternative.AddUtilityTerm(1, usualWorkParcelFlag);
                alternative.AddUtilityTerm(2, person.IsPartTimeWorker.ToFlag() * usualWorkParcelFlag);
                alternative.AddUtilityTerm(3, person.IsStudentAge.ToFlag() * usualWorkParcelFlag);
                alternative.AddUtilityTerm(4, _primaryFlag * personDay.TwoOrMoreWorkToursExist().ToFlag() * usualWorkParcelFlag);
                alternative.AddUtilityTerm(5, personDay.WorkStopsExist().ToFlag() * usualWorkParcelFlag);
                alternative.AddUtilityTerm(6, _secondaryFlag * usualWorkParcelFlag);

                // non-usual location attributes
                alternative.AddUtilityTerm(11, (!usualWorkParcel).ToFlag() * sampleItem.AdjustmentFactor);
                alternative.AddUtilityTerm(12, _tour.IsHomeBasedTour.ToFlag() * (!usualWorkParcel).ToFlag() * timePressure);

                alternative.AddUtilityTerm(13, (!usualWorkParcel).ToFlag() * person.IsFulltimeWorker.ToFlag() * tourLogsum);
                alternative.AddUtilityTerm(14, (!usualWorkParcel).ToFlag() * person.IsPartTimeWorker.ToFlag() * tourLogsum);
                alternative.AddUtilityTerm(15, (!usualWorkParcel).ToFlag() * person.IsNotFullOrPartTimeWorker.ToFlag() * tourLogsum);
                alternative.AddUtilityTerm(16, (!usualWorkParcel).ToFlag() * person.IsRetiredAdult.ToFlag() * distanceFromOriginLog);

                alternative.AddUtilityTerm(17, (!usualWorkParcel).ToFlag() * distanceFromWorkLog);
                alternative.AddUtilityTerm(18, (!usualWorkParcel).ToFlag() * person.IsStudentAge.ToFlag() * distanceFromSchoolLog);

                alternative.AddUtilityTerm(19, (!usualWorkParcel).ToFlag() * noCarCompetitionFlag * destinationParcel.ParkingHourlyEmploymentCommercialMixBuffer1());

                // non-usual location Neighborhood attributes
                alternative.AddUtilityTerm(31, (!usualWorkParcel).ToFlag() * empEduBuffer);
                alternative.AddUtilityTerm(32, (!usualWorkParcel).ToFlag() * empOfcBuffer);
                alternative.AddUtilityTerm(33, (!usualWorkParcel).ToFlag() * housesBuffer);
                alternative.AddUtilityTerm(34, (!usualWorkParcel).ToFlag() * empIndBuffer);

                // non-usual location Size terms (consider conditioning these by fulltime, parttime, notFTPT, an income (see original sacog spec)
                alternative.AddUtilityTerm(40, (!usualWorkParcel).ToFlag() * destinationParcel.EmploymentEducation);
                alternative.AddUtilityTerm(41, (!usualWorkParcel).ToFlag() * destinationParcel.EmploymentFood);
                alternative.AddUtilityTerm(42, (!usualWorkParcel).ToFlag() * destinationParcel.EmploymentGovernment);
                alternative.AddUtilityTerm(43, (!usualWorkParcel).ToFlag() * destinationParcel.EmploymentOffice);
                alternative.AddUtilityTerm(44, (!usualWorkParcel).ToFlag() * destinationParcel.EmploymentRetail);
                alternative.AddUtilityTerm(45, (!usualWorkParcel).ToFlag() * destinationParcel.EmploymentService);
                alternative.AddUtilityTerm(46, (!usualWorkParcel).ToFlag() * destinationParcel.EmploymentMedical);
                alternative.AddUtilityTerm(47, (!usualWorkParcel).ToFlag() * destinationParcel.EmploymentIndustrial + destinationParcel.EmploymentAgricultureConstruction);
                alternative.AddUtilityTerm(48, (!usualWorkParcel).ToFlag() * destinationParcel.Households);
                alternative.AddUtilityTerm(49, (!usualWorkParcel).ToFlag() * destinationParcel.StudentsUniversity);


                // OD shadow pricing
                if (!usualWorkParcel && Global.Configuration.ShouldUseODShadowPricing)
                {
                    int ori = _tour.OriginParcel.District;
                    int des = destinationParcel.District;
                    //var first = res <= des? res : des;
                    //var second = res <= des? des : res;
                    double shadowPriceConfigurationParameter = ori == des ? Global.Configuration.WorkTourDestinationOOShadowPriceCoefficient : Global.Configuration.WorkTourDestinationODShadowPriceCoefficient;
                    int    odShadowPriceF12Value             = MAX_REGULAR_PARAMETER + Global.Configuration.NumberOfODShadowPricingDistricts * (ori - 1) + des;
                    alternative.AddUtilityTerm(odShadowPriceF12Value, shadowPriceConfigurationParameter);
                }



                //        // usual location size term
                //        alternative.AddUtilityTerm(50, usualWorkParcelFlag * 1);

                //        // Comment out these nesting calls when estimating the conditional flat model
                //        // model is NL with oddball alternative
                //        if (usualWorkParcelFlag == 0) {
                //            // this alternative is in the non-oddball nest
                //            alternative.AddNestedAlternative(_sampleSize + 2, 0, 60); // associates alternative with non-oddball nest
                //        }
                //        else {
                //            // this is the oddball alternative
                //            alternative.AddNestedAlternative(_sampleSize + 3, 1, 60); // associates alternative with oddball nest
                //        }
            }