Beispiel #1
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITripWrapper trip, IEnumerable <dynamic> pathTypeModels, IParcelWrapper originParcel, IParcelWrapper destinationParcel, int departureTime, int choice = Constants.DEFAULT_VALUE)
        {
            var household       = trip.Household;
            var householdTotals = household.HouseholdTotals;
            var person          = trip.Person;
            var tour            = trip.Tour;
            var halfTour        = trip.HalfTour;

            // household inputs
            var onePersonHouseholdFlag  = household.IsOnePersonHousehold.ToFlag();
            var twoPersonHouseholdFlag  = household.IsTwoPersonHousehold.ToFlag();
            var income0To25KFlag        = household.Has0To25KIncome.ToFlag();
            var income25To45KFlag       = household.Has25To45KIncome.ToFlag();
            var childrenAge5Through15   = householdTotals.ChildrenAge5Through15;
            var nonworkingAdults        = householdTotals.NonworkingAdults;
            var retiredAdults           = householdTotals.RetiredAdults;
            var noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(household.VehiclesAvailable);
            var carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(household.VehiclesAvailable);

            // person inputs
            var drivingAgeStudentFlag = person.IsDrivingAgeStudent.ToFlag();
            var maleFlag          = person.IsMale.ToFlag();
            var ageLessThan35Flag = person.AgeIsLessThan35.ToFlag();

            // tour inputs
            var parkAndRideTourFlag       = tour.IsParkAndRideMode().ToFlag();
            var transitTourFlag           = tour.IsTransitMode().ToFlag();
            var schoolBusTourFlag         = tour.IsSchoolBusMode().ToFlag();
            var hov3TourFlag              = tour.IsHov3Mode().ToFlag();
            var hov2TourFlag              = tour.IsHov2Mode().ToFlag();
            var sovTourFlag               = tour.IsSovMode().ToFlag();
            var bikeTourFlag              = tour.IsBikeMode().ToFlag();
            var walkTourFlag              = tour.IsWalkMode().ToFlag();
            var homeBasedWorkTourFlag     = (tour.IsHomeBasedTour && tour.IsWorkPurpose()).ToFlag();
            var homeBasedSchoolTourFlag   = (tour.IsHomeBasedTour && tour.IsSchoolPurpose()).ToFlag();
            var homeBasedEscortTourFlag   = (tour.IsHomeBasedTour && tour.IsEscortPurpose()).ToFlag();
            var homeBasedShoppingTourFlag = (tour.IsHomeBasedTour && tour.IsShoppingPurpose()).ToFlag();
            var homeBasedMealTourFlag     = (tour.IsHomeBasedTour && tour.IsMealPurpose()).ToFlag();
            var homeBasedSocialTourFlag   = (tour.IsHomeBasedTour && tour.IsSocialPurpose()).ToFlag();
            var notHomeBasedTourFlag      = (!tour.IsHomeBasedTour).ToFlag();
//			var homeBasedNotWorkSchoolEscortTourFlag = (tour.IsHomeBasedTour && tour.DestinationPurpose > Global.Settings.Purposes.Escort).ToFlag();
//			var costco = -0.1432 * homeBasedWorkTourFlag - 0.2000 * homeBasedSchoolTourFlag - 0.3000 * homeBasedEscortTourFlag - 0.2000 * homeBasedNotWorkSchoolEscortTourFlag - 0.2000 * notHomeBasedTourFlag;

            // trip inputs
            var originHomeEscortFlag = (trip.IsNoneOrHomePurposeByOrigin() && trip.IsEscortPurposeByDestination()).ToFlag();
            var originWorkEscortFlag = (trip.IsWorkPurposeByOrigin() && trip.IsEscortPurposeByDestination()).ToFlag();

            var destinationHomeEscortFlag = (trip.IsNoneOrHomePurposeByDestination() && trip.IsEscortPurposeByOrigin()).ToFlag();
            var destinationWorkEscortFlag = (trip.IsWorkPurposeByDestination() && trip.IsEscortPurposeByOrigin()).ToFlag();

            // only trip on first half-tour
            var onlyTripOnFirstHalfFlag = (trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips == 1 && trip.IsToTourOrigin).ToFlag();

            // first trip on first half-tour, not only one
            var firstTripOnFirstHalfFlag = (trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips == 1 && !trip.IsToTourOrigin).ToFlag();

            // last trip first half-tour, not only one
            var lastTripOnFirstHalfFlag = (trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips > 1 && trip.IsToTourOrigin).ToFlag();

            // only trip on second half-tour
            var onlyTripOnSecondHalfFlag = (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips == 1 && trip.IsToTourOrigin).ToFlag();

            // first trip on second half-tour, not only one
            var firstTripOnSecondHalfFlag = (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips == 1 && !trip.IsToTourOrigin).ToFlag();

            // last trip second half-tour, not only one
            var lastTripOnSecondHalfFlag = (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips > 1 && trip.IsToTourOrigin).ToFlag();

            // remaining inputs
            var originMixedDensity        = originParcel.MixedUse4Index1();
            var originIntersectionDensity = originParcel.NetIntersectionDensity1();
            var destinationParkingCost    = destinationParcel.ParkingCostBuffer1(2);
            var amPeriodFlag      = departureTime.IsLeftExclusiveBetween(Global.Settings.Times.SixAM, Global.Settings.Times.TenAM).ToFlag();
            var middayPeriodFlag  = departureTime.IsLeftExclusiveBetween(Global.Settings.Times.TenAM, Global.Settings.Times.ThreePM).ToFlag();
            var pmPeriodFlag      = departureTime.IsLeftExclusiveBetween(Global.Settings.Times.ThreePM, Global.Settings.Times.SevenPM).ToFlag();
            var eveningPeriodFlag = (departureTime > Global.Settings.Times.SevenPM).ToFlag();

            // availability
            var tripModeAvailable = new bool[Global.Settings.Modes.TotalModes];

            var isLastTripInTour = (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips == 1 && trip.IsToTourOrigin) || (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips > 1 && trip.IsToTourOrigin);
            var frequencyPreviousTripModeIsTourMode =
                trip.IsHalfTourFromOrigin
                                        ? tour.HalfTourFromOrigin.Trips.Where(x => x.Sequence < trip.Sequence).Count(x => tour.Mode == x.Mode)
                                        : tour.HalfTourFromOrigin.Trips.Union(tour.HalfTourFromDestination.Trips.Where(x => x.Sequence < trip.Sequence)).Count(x => tour.Mode == x.Mode);

            // if a park and ride tour, only car is available
            if (tour.Mode == Global.Settings.Modes.ParkAndRide)
            {
                tripModeAvailable[Global.Settings.Modes.Sov]  = tour.Household.VehiclesAvailable > 0 && tour.Person.IsDrivingAge;
                tripModeAvailable[Global.Settings.Modes.Hov2] = !tripModeAvailable[Global.Settings.Modes.Sov];
            }
            // if the last trip of the tour and tour mode not yet used, only the tour mode is available
            else if (isLastTripInTour && frequencyPreviousTripModeIsTourMode == 0)
            {
                tripModeAvailable[tour.Mode] = true;
            }
            else
            {
                // set availability based on tour mode
                for (var mode = Global.Settings.Modes.Walk; mode <= tour.Mode; mode++)
                {
                    tripModeAvailable[mode] = true;
                }
            }

            // school bus is a special case - use HOV3 impedance and only available for school bus tours
            var pathTypeExtra              = pathTypeModels.First(x => x.Mode == Global.Settings.Modes.Hov3);
            var modeExtra                  = Global.Settings.Modes.SchoolBus;
            var availableExtra             = pathTypeExtra.Available && tour.IsSchoolBusMode() && tripModeAvailable[modeExtra];
            var generalizedTimeLogsumExtra = pathTypeExtra.GeneralizedTimeLogsum;

            var alternative = choiceProbabilityCalculator.GetAlternative(modeExtra, availableExtra, choice == modeExtra);

            alternative.Choice = modeExtra;

            alternative.AddNestedAlternative(_nestedAlternativeIds[modeExtra], _nestedAlternativeIndexes[modeExtra], THETA_PARAMETER);

            if (availableExtra)
            {
                //	case Global.Settings.Modes.SchoolBus:
                alternative.AddUtilityTerm(2, generalizedTimeLogsumExtra * tour.TimeCoefficient);
                alternative.AddUtilityTerm(18, 1);
                alternative.AddUtilityTerm(100, schoolBusTourFlag);
                alternative.AddUtilityTerm(102, (schoolBusTourFlag * onlyTripOnFirstHalfFlag));
                alternative.AddUtilityTerm(103, (schoolBusTourFlag * onlyTripOnSecondHalfFlag));
                alternative.AddUtilityTerm(104, (schoolBusTourFlag * firstTripOnFirstHalfFlag));
                alternative.AddUtilityTerm(105, (schoolBusTourFlag * firstTripOnSecondHalfFlag));
                alternative.AddUtilityTerm(106, (schoolBusTourFlag * lastTripOnFirstHalfFlag));
                alternative.AddUtilityTerm(107, (schoolBusTourFlag * lastTripOnSecondHalfFlag));
                alternative.AddUtilityTerm(112, parkAndRideTourFlag);
                alternative.AddUtilityTerm(113, transitTourFlag);
            }

            foreach (var pathTypeModel in pathTypeModels)
            {
                var mode                  = pathTypeModel.Mode;
                var available             = pathTypeModel.Available && tripModeAvailable[mode];
                var generalizedTimeLogsum = pathTypeModel.GeneralizedTimeLogsum;

                alternative        = choiceProbabilityCalculator.GetAlternative(mode, available, choice == mode);
                alternative.Choice = mode;

                alternative.AddNestedAlternative(_nestedAlternativeIds[mode], _nestedAlternativeIndexes[mode], THETA_PARAMETER);

                if (!available)
                {
                    continue;
                }

                alternative.AddUtilityTerm(2, generalizedTimeLogsum * tour.TimeCoefficient);

                if (mode == Global.Settings.Modes.Transit)
                {
                    alternative.AddUtilityTerm(20, 1);
                    alternative.AddUtilityTerm(22, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(100, transitTourFlag);
                    alternative.AddUtilityTerm(102, (transitTourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (transitTourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (transitTourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (transitTourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (transitTourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (transitTourFlag * lastTripOnSecondHalfFlag));
                }
                else if (mode == Global.Settings.Modes.Hov3)
                {
                    alternative.AddUtilityTerm(30, 1);
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT3));
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    alternative.AddUtilityTerm(34, (nonworkingAdults + retiredAdults));
                    alternative.AddUtilityTerm(36, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(37, twoPersonHouseholdFlag);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(100, hov3TourFlag);
                    alternative.AddUtilityTerm(102, (hov3TourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (hov3TourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (hov3TourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (hov3TourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (hov3TourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (hov3TourFlag * lastTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(114, parkAndRideTourFlag);
                    alternative.AddUtilityTerm(115, transitTourFlag);
                    alternative.AddUtilityTerm(116, schoolBusTourFlag);
                    alternative.AddUtilityTerm(149, homeBasedWorkTourFlag);
                    alternative.AddUtilityTerm(150, homeBasedSchoolTourFlag);
                    alternative.AddUtilityTerm(152, homeBasedEscortTourFlag);
                    alternative.AddUtilityTerm(153, homeBasedShoppingTourFlag);
                    alternative.AddUtilityTerm(154, homeBasedMealTourFlag);
                    alternative.AddUtilityTerm(155, homeBasedSocialTourFlag);
                    alternative.AddUtilityTerm(161, (destinationWorkEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(162, (originWorkEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(163, (originHomeEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(164, (originHomeEscortFlag * middayPeriodFlag));
                    alternative.AddUtilityTerm(165, (originHomeEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(166, (originHomeEscortFlag * eveningPeriodFlag));
                    alternative.AddUtilityTerm(167, (destinationHomeEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(168, (destinationHomeEscortFlag * middayPeriodFlag));
                    alternative.AddUtilityTerm(169, (destinationHomeEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(170, (destinationHomeEscortFlag * eveningPeriodFlag));
                }
                else if (mode == Global.Settings.Modes.Hov2)
                {
                    alternative.AddUtilityTerm(40, 1);
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT2));
                    alternative.AddUtilityTerm(32, (childrenAge5Through15 * (1 - homeBasedEscortTourFlag)));
                    alternative.AddUtilityTerm(34, ((nonworkingAdults + retiredAdults) * (1 - homeBasedEscortTourFlag)));
                    alternative.AddUtilityTerm(38, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(100, hov2TourFlag);
                    alternative.AddUtilityTerm(102, (hov2TourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (hov2TourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (hov2TourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (hov2TourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (hov2TourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (hov2TourFlag * lastTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(118, transitTourFlag);
                    alternative.AddUtilityTerm(119, schoolBusTourFlag);
                    alternative.AddUtilityTerm(120, hov3TourFlag);
                    alternative.AddUtilityTerm(149, homeBasedWorkTourFlag);
                    alternative.AddUtilityTerm(150, homeBasedSchoolTourFlag);
                    alternative.AddUtilityTerm(152, homeBasedEscortTourFlag);
                    alternative.AddUtilityTerm(153, homeBasedShoppingTourFlag);
                    alternative.AddUtilityTerm(154, homeBasedMealTourFlag);
                    alternative.AddUtilityTerm(155, homeBasedSocialTourFlag);
                    alternative.AddUtilityTerm(161, (destinationWorkEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(162, (originWorkEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(163, (originHomeEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(164, (originHomeEscortFlag * middayPeriodFlag));
                    alternative.AddUtilityTerm(165, (originHomeEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(166, (originHomeEscortFlag * eveningPeriodFlag));
                    alternative.AddUtilityTerm(167, (destinationHomeEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(168, (destinationHomeEscortFlag * middayPeriodFlag));
                    alternative.AddUtilityTerm(169, (destinationHomeEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(170, (destinationHomeEscortFlag * eveningPeriodFlag));
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    alternative.AddUtilityTerm(50, 1);
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient));
                    alternative.AddUtilityTerm(52, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(54, income0To25KFlag);
                    alternative.AddUtilityTerm(55, income25To45KFlag);
                    alternative.AddUtilityTerm(59, drivingAgeStudentFlag);
                    alternative.AddUtilityTerm(100, sovTourFlag);
                    alternative.AddUtilityTerm(102, (sovTourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (sovTourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (sovTourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (sovTourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (sovTourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (sovTourFlag * lastTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(121, parkAndRideTourFlag);
                    alternative.AddUtilityTerm(122, transitTourFlag);
                    alternative.AddUtilityTerm(124, hov3TourFlag);
                    alternative.AddUtilityTerm(125, hov2TourFlag);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    alternative.AddUtilityTerm(60, 1);
                    alternative.AddUtilityTerm(61, maleFlag);
                    alternative.AddUtilityTerm(62, ageLessThan35Flag);
                    alternative.AddUtilityTerm(65, originIntersectionDensity);
                    alternative.AddUtilityTerm(100, bikeTourFlag);
                    alternative.AddUtilityTerm(102, (bikeTourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (bikeTourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (bikeTourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (bikeTourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (bikeTourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (bikeTourFlag * lastTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(127, transitTourFlag);
                    alternative.AddUtilityTerm(128, schoolBusTourFlag);
                    alternative.AddUtilityTerm(130, hov2TourFlag);
                    alternative.AddUtilityTerm(131, sovTourFlag);
                    alternative.AddUtilityTerm(147, notHomeBasedTourFlag);
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    alternative.AddUtilityTerm(72, ageLessThan35Flag);
                    alternative.AddUtilityTerm(75, originIntersectionDensity);
                    alternative.AddUtilityTerm(78, originMixedDensity);                     // origin and destination mixed use measures - geometric avg. - half mile from cell, in 1000s
                    alternative.AddUtilityTerm(100, walkTourFlag);
                    alternative.AddUtilityTerm(102, (walkTourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (walkTourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (walkTourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (walkTourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (walkTourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (walkTourFlag * lastTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(141, homeBasedWorkTourFlag);
                    alternative.AddUtilityTerm(142, homeBasedSchoolTourFlag);
                }
            }
        }
Beispiel #2
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper tour, IEnumerable <dynamic> pathTypeModels, IParcelWrapper destinationParcel, int choice = Constants.DEFAULT_VALUE)
        {
            var household       = tour.Household;
            var householdTotals = household.HouseholdTotals;
            var person          = tour.Person;
            var personDay       = tour.PersonDay;

            // household inputs
            var income0To25KFlag        = household.Has0To25KIncome.ToFlag();
            var childrenUnder5          = householdTotals.ChildrenUnder5;
            var childrenAge5Through15   = householdTotals.ChildrenAge5Through15;
            var nonworkingAdults        = householdTotals.NonworkingAdults;
            var retiredAdults           = householdTotals.RetiredAdults;
            var onePersonHouseholdFlag  = household.IsOnePersonHousehold.ToFlag();
            var twoPersonHouseholdFlag  = household.IsTwoPersonHousehold.ToFlag();
            var noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(household.VehiclesAvailable);
            var carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(household.VehiclesAvailable);
            var carsLessThanWorkersFlag = household.GetFlagForCarsLessThanWorkers(household.VehiclesAvailable);

            // person inputs
            var maleFlag = person.IsMale.ToFlag();
            var ageBetween51And98Flag = person.AgeIsBetween51And98.ToFlag();

            // tour inputs
            var shoppingTourFlag           = (tour.DestinationPurpose == Global.Settings.Purposes.Shopping).ToFlag();
            var mealTourFlag               = (tour.DestinationPurpose == Global.Settings.Purposes.Meal).ToFlag();
            var socialOrRecreationTourFlag = (tour.DestinationPurpose == Global.Settings.Purposes.Social).ToFlag();

            // remaining inputs
            var originParcel           = tour.OriginParcel;
            var parkingDuration        = ChoiceModelUtility.GetParkingDuration(person.IsFulltimeWorker);
            var destinationParkingCost = destinationParcel.ParkingCostBuffer1(parkingDuration);

            double escortPercentage;
            double nonEscortPercentage;

            ChoiceModelUtility.SetEscortPercentages(personDay, out escortPercentage, out nonEscortPercentage, true);

            //foreach (var pathTypeModel in pathTypeModels) {
            foreach (dynamic pathTypeModel in pathTypeModels)
            {
                IPathTypeModel ipathTypeModel        = pathTypeModel;
                var            mode                  = pathTypeModel.Mode;
                var            available             = pathTypeModel.Mode != Global.Settings.Modes.ParkAndRide && pathTypeModel.Available;
                var            generalizedTimeLogsum = pathTypeModel.GeneralizedTimeLogsum;

                var alternative = choiceProbabilityCalculator.GetAlternative(mode, available, choice == mode);
                alternative.Choice = mode;

                alternative.AddNestedAlternative(_nestedAlternativeIds[pathTypeModel.Mode], _nestedAlternativeIndexes[pathTypeModel.Mode], THETA_PARAMETER);

                if (!available)
                {
                    continue;
                }

                alternative.AddUtilityTerm(2, generalizedTimeLogsum * tour.TimeCoefficient);

                if (mode == Global.Settings.Modes.Transit)
                {
                    alternative.AddUtilityTerm(20, 1);
                    alternative.AddUtilityTerm(21, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(120, shoppingTourFlag);
//						alternative.AddUtility(121, mealTourFlag);
//						alternative.AddUtility(129, destinationParcel.MixedUse2Index1());
                    alternative.AddUtilityTerm(128, destinationParcel.TotalEmploymentDensity1());
//						alternative.AddUtility(127, destinationParcel.NetIntersectionDensity1());
//						alternative.AddUtility(126, originParcel.NetIntersectionDensity1());
//						alternative.AddUtility(125, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(124, originParcel.MixedUse2Index1());
//						alternative.AddUtility(123, Math.Log(destinationParcel.StopsTransitBuffer1+1));
//						alternative.AddUtility(122, Math.Log(originParcel.StopsTransitBuffer1+1));
                }
                else if (mode == Global.Settings.Modes.Hov3)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT3));
                    alternative.AddUtilityTerm(30, 1);
                    alternative.AddUtilityTerm(31, childrenUnder5);
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    alternative.AddUtilityTerm(34, nonworkingAdults + retiredAdults);
                    alternative.AddUtilityTerm(35, ((double)pathTypeModel.PathDistance).AlmostEquals(0) ? 0 : Math.Log(pathTypeModel.PathDistance));
                    alternative.AddUtilityTerm(38, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(39, twoPersonHouseholdFlag);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(43, carsLessThanWorkersFlag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                    alternative.AddUtilityTerm(136, shoppingTourFlag);
                    alternative.AddUtilityTerm(137, mealTourFlag);
                    alternative.AddUtilityTerm(138, socialOrRecreationTourFlag);
                }
                else if (mode == Global.Settings.Modes.Hov2)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT2));
                    alternative.AddUtilityTerm(31, childrenUnder5);
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    alternative.AddUtilityTerm(34, nonworkingAdults + retiredAdults);
                    alternative.AddUtilityTerm(35, ((double)pathTypeModel.PathDistance).AlmostEquals(0) ? 0 : Math.Log(pathTypeModel.PathDistance));
                    alternative.AddUtilityTerm(40, 1);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(43, carsLessThanWorkersFlag);
                    alternative.AddUtilityTerm(48, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                    alternative.AddUtilityTerm(136, shoppingTourFlag);
                    alternative.AddUtilityTerm(137, mealTourFlag);
                    alternative.AddUtilityTerm(138, socialOrRecreationTourFlag);
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient));
                    alternative.AddUtilityTerm(50, 1);
                    alternative.AddUtilityTerm(52, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(54, income0To25KFlag);
                    alternative.AddUtilityTerm(131, escortPercentage);
                    alternative.AddUtilityTerm(132, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    double class1Dist = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions ?
                                        ImpedanceRoster.GetValue("class1distance", mode, Global.Settings.PathTypes.FullNetwork,
                                                                 Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable : 0;
                    double class2Dist = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions ?
                                        ImpedanceRoster.GetValue("class2distance", mode, Global.Settings.PathTypes.FullNetwork,
                                                                 Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable : 0;
                    double worstDist = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions ?
                                       ImpedanceRoster.GetValue("worstdistance", mode, Global.Settings.PathTypes.FullNetwork,
                                                                Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable : 0;

                    alternative.AddUtilityTerm(60, 1);
                    alternative.AddUtilityTerm(61, maleFlag);
                    alternative.AddUtilityTerm(63, ageBetween51And98Flag);
                    alternative.AddUtilityTerm(160, socialOrRecreationTourFlag);
                    alternative.AddUtilityTerm(169, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(168, destinationParcel.TotalEmploymentDensity1());
//						alternative.AddUtility(167, destinationParcel.NetIntersectionDensity1());
//						alternative.AddUtility(166, originParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(165, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(164, originParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(161, (class1Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(162, (class2Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(163, (worstDist > 0).ToFlag());
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    alternative.AddUtilityTerm(73, ageBetween51And98Flag);
                    alternative.AddUtilityTerm(171, mealTourFlag);
                    alternative.AddUtilityTerm(172, socialOrRecreationTourFlag);
//						alternative.AddUtility(179, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(178, destinationParcel.HouseholdDensity1());
//						alternative.AddUtility(177, destinationParcel.NetIntersectionDensity1());
//						alternative.AddUtility(176, originParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(175, originParcel.HouseholdDensity1());
//						alternative.AddUtility(174, originParcel.MixedUse4Index1());
                }
            }
        }
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, IHouseholdDayWrapper householdDay, ITourWrapper tour,
                              IParcelWrapper destinationParcel, int householdCars,
                              int constrainedMode, int constrainedArrivalTime, int constrainedDepartureTime, HTourModeTime choice = null)
        {
            IHouseholdWrapper household = tour.Household;
            IPersonWrapper    person    = tour.Person;
            IPersonDayWrapper personDay = tour.PersonDay;

            Framework.DomainModels.Models.IHouseholdTotals householdTotals = household.HouseholdTotals;

            // household inputs
            int childrenUnder5         = householdTotals.ChildrenUnder5;
            int childrenAge5Through15  = householdTotals.ChildrenAge5Through15;
            int nonworkingAdults       = householdTotals.NonworkingAdults;
            int retiredAdults          = householdTotals.RetiredAdults;
            int onePersonHouseholdFlag = household.IsOnePersonHousehold.ToFlag();
            int twoPersonHouseholdFlag = household.IsTwoPersonHousehold.ToFlag();
            //var householdCars = household.VehiclesAvailable; MABADD now an input parameter
            int noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(householdCars);
            int carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(householdCars);
            int carsLessThanWorkersFlag = household.GetFlagForCarsLessThanWorkers(householdCars);
            int income0To25KFlag        = household.Has0To25KIncome.ToFlag();
            int income100KPlusFlag      = household.Has100KPlusIncome.ToFlag();

            // person inputs
            int partTimeWorkerFlag     = person.IsPartTimeWorker.ToFlag();
            int nonworkingAdultFlag    = person.IsNonworkingAdult.ToFlag();
            int universityStudentFlag  = person.IsUniversityStudent.ToFlag();
            int retiredAdultFlag       = person.IsRetiredAdult.ToFlag();
            int drivingAgeStudentFlag  = person.IsDrivingAgeStudent.ToFlag();
            int fulltimeWorkerFlag     = person.IsFulltimeWorker.ToFlag();
            int childAge5Through15Flag = person.IsChildAge5Through15.ToFlag();
            int childUnder5Flag        = person.IsChildUnder5.ToFlag();
            int maleFlag              = person.IsMale.ToFlag();
            int ageUnder30Flag        = person.AgeIsLessThan30.ToFlag();
            int ageBetween51And98Flag = person.AgeIsBetween51And98.ToFlag();
            int adultFlag             = person.IsAdult.ToFlag();

            // person-day inputs
            int homeBasedToursOnlyFlag          = (personDay == null) ? 1 : personDay.OnlyHomeBasedToursExist().ToFlag();
            int firstSimulatedHomeBasedTourFlag = (personDay == null) ? 1 : personDay.IsFirstSimulatedHomeBasedTour().ToFlag();
            int laterSimulatedHomeBasedTourFlag = (personDay == null) ? 0 : personDay.IsLaterSimulatedHomeBasedTour().ToFlag();
            int totalStops              = (personDay == null) ? 0 : personDay.GetTotalStops();
            int totalSimulatedStops     = (personDay == null) ? 0 : personDay.GetTotalSimulatedStops();
            int escortStops             = (personDay == null) ? 0 : personDay.EscortStops;
            int homeBasedTours          = (personDay == null) ? 1 : personDay.HomeBasedTours;
            int simulatedHomeBasedTours = (personDay == null) ? 0 : personDay.SimulatedHomeBasedTours;

            // tour inputs
            int            escortTourFlag           = tour.IsEscortPurpose().ToFlag();
            int            shoppingTourFlag         = tour.IsShoppingPurpose().ToFlag();
            int            mealTourFlag             = tour.IsMealPurpose().ToFlag();
            int            socialTourFlag           = tour.IsSocialPurpose().ToFlag();
            int            personalBusinessTourFlag = tour.IsPersonalBusinessPurpose().ToFlag();
            int            recreationTourFlag       = tour.IsRecreationPurpose().ToFlag();
            int            medicalTourFlag          = tour.IsMedicalPurpose().ToFlag();
            IParcelWrapper originParcel             = tour.OriginParcel;
            //var destinationParcel = tour.DestinationParcel; MABADD now an input parameter
            int jointTourFlag        = (tour.JointTourSequence > 0) ? 1 : 0;
            int partialHalfTour1Flag = (tour.PartialHalfTour1Sequence > 0) ? 1 : 0;
            int partialHalfTour2Flag = (tour.PartialHalfTour2Sequence > 0) ? 1 : 0;
            int fullHalfTour1Flag    = (tour.FullHalfTour1Sequence > 0) ? 1 : 0;
            int fullHalfTour2Flag    = (tour.FullHalfTour2Sequence > 0) ? 1 : 0;
            int parentTourMode       = tour.ParentTour == null ? 0 : tour.ParentTour.Mode;


            // remaining inputs
            // Higher priority tour of 2+ tours for the same purpose
            int highPrioritySameFlag = (personDay == null) ? 1 : (tour.GetTotalToursByPurpose() > tour.GetTotalSimulatedToursByPurpose() && tour.GetTotalSimulatedToursByPurpose() == 1).ToFlag();

            // Lower priority tour(s) of 2+ tours for the same purpose
            int lowPrioritySameFlag = (personDay == null) ? 0 : (tour.GetTotalSimulatedToursByPurpose() > 1).ToFlag();

            // Higher priority tour of 2+ tours for different purposes
            int highPriorityDifferentFlag = (personDay == null) ? 0 : (personDay.IsFirstSimulatedHomeBasedTour() && personDay.HomeBasedToursExist()).ToFlag() * (1 - highPrioritySameFlag);

            // Lower priority tour of 2+ tours for different purposes
            int lowPriorityDifferentFlag = (personDay == null) ? 0 : (personDay.IsLaterSimulatedHomeBasedTour() && personDay.HomeBasedToursExist()).ToFlag() * (1 - lowPrioritySameFlag);

            ITimeWindow timeWindow = (householdDay == null) ? new TimeWindow() : tour.GetRelevantTimeWindow(householdDay);
            int         totalMinutesAvailableInDay = timeWindow.TotalAvailableMinutes(1, 1440);


            int bigPeriodCount = DayPeriod.H_BIG_DAY_PERIOD_TOTAL_TOUR_TIMES;
            int nPeriodCombs   = bigPeriodCount * (bigPeriodCount + 1) / 2;


            //set components
            int componentIndex = 0;
            int periodComb     = -1;

            bool useTimeComponents = Global.Configuration.IsInEstimationMode || constrainedArrivalTime == 0 || constrainedDepartureTime == 0;

            if (useTimeComponents)
            {
                for (int arrivalPeriodIndex = 0; arrivalPeriodIndex < bigPeriodCount; arrivalPeriodIndex++)
                {
                    MinuteSpan arrivalPeriod = DayPeriod.HBigDayPeriods[arrivalPeriodIndex];
                    int        arrivalPeriodAvailableMinutes = timeWindow.TotalAvailableMinutes(arrivalPeriod.Start, arrivalPeriod.End);

                    for (int departurePeriodIndex = arrivalPeriodIndex; departurePeriodIndex < bigPeriodCount; departurePeriodIndex++)
                    {
                        MinuteSpan departurePeriod = DayPeriod.HBigDayPeriods[departurePeriodIndex];
                        int        departurePeriodAvailableMinutes = timeWindow.TotalAvailableMinutes(departurePeriod.Start, departurePeriod.End);

                        if (arrivalPeriod == departurePeriod)
                        {
                            componentIndex = arrivalPeriodIndex;
                            choiceProbabilityCalculator.CreateUtilityComponent(componentIndex);
                            ChoiceProbabilityCalculator.Component arrivalComponent = choiceProbabilityCalculator.GetUtilityComponent(componentIndex);

                            if (arrivalPeriodAvailableMinutes > 0)
                            {
                                double hoursArrival = arrivalPeriod.Middle / 60.0;
                                int    firstCoef    = 300;
                                arrivalComponent.AddUtilityTerm(300, Math.Log(arrivalPeriodAvailableMinutes));
                                //arrival shift variables
                                arrivalComponent.AddUtilityTerm(firstCoef + 2, partTimeWorkerFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 3, nonworkingAdultFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 4, universityStudentFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 5, retiredAdultFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 6, drivingAgeStudentFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 7, childAge5Through15Flag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 8, childUnder5Flag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 9, escortTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 10, shoppingTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 11, mealTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 12, socialTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 13, personalBusinessTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 14, recreationTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 15, medicalTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 16, income0To25KFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 17, income100KPlusFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 18, highPrioritySameFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 19, lowPrioritySameFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 20, highPriorityDifferentFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 21, lowPriorityDifferentFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 22, jointTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 23, partialHalfTour1Flag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 24, fullHalfTour1Flag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 25, partialHalfTour2Flag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 26, fullHalfTour2Flag * hoursArrival);
                            }

                            componentIndex = bigPeriodCount + departurePeriodIndex;
                            choiceProbabilityCalculator.CreateUtilityComponent(componentIndex);
                            ChoiceProbabilityCalculator.Component departureComponent = choiceProbabilityCalculator.GetUtilityComponent(componentIndex);


                            if (departurePeriodAvailableMinutes > 0)
                            {
                                departureComponent.AddUtilityTerm(300, Math.Log(departurePeriodAvailableMinutes));
                            }
                        }
                        // set period combination component
                        periodComb++;
                        componentIndex = 2 * bigPeriodCount + periodComb;
                        choiceProbabilityCalculator.CreateUtilityComponent(componentIndex);
                        ChoiceProbabilityCalculator.Component combinationComponent = choiceProbabilityCalculator.GetUtilityComponent(componentIndex);

                        if (arrivalPeriodAvailableMinutes > 0 && departurePeriodAvailableMinutes > 0)
                        {
                            double hoursDuration = (departurePeriod.Middle - arrivalPeriod.Middle) / 60.0;

                            int firstCoef = 700;
                            //combination constants
                            combinationComponent.AddUtilityTerm(firstCoef + periodComb, 1.0);
                            // duration shift variables
                            combinationComponent.AddUtilityTerm(firstCoef + 31, escortTourFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 32, shoppingTourFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 33, mealTourFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 34, socialTourFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 35, personalBusinessTourFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 36, recreationTourFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 37, medicalTourFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 38, highPrioritySameFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 39, lowPrioritySameFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 40, highPriorityDifferentFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 41, lowPriorityDifferentFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 42, partTimeWorkerFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 43, jointTourFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 44, partialHalfTour1Flag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 45, fullHalfTour1Flag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 46, partialHalfTour2Flag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 47, fullHalfTour2Flag * hoursDuration);
                            // peak-to-peak variables
                            if (arrivalPeriod.Index == DayPeriod.AM_PEAK && departurePeriod.Index == DayPeriod.PM_PEAK)
                            {
                                combinationComponent.AddUtilityTerm(firstCoef + 48, fulltimeWorkerFlag);
                                combinationComponent.AddUtilityTerm(firstCoef + 49, income0To25KFlag);
                                combinationComponent.AddUtilityTerm(firstCoef + 50, income100KPlusFlag);
                            }
                        }
                    }
                }
            }

            for (int mode = Global.Settings.Modes.Walk; mode <= Global.Settings.Modes.SchoolBus; mode++)
            {
                componentIndex = 2 * bigPeriodCount + nPeriodCombs + mode - 1;
                choiceProbabilityCalculator.CreateUtilityComponent(componentIndex);
                ChoiceProbabilityCalculator.Component modeComponent = choiceProbabilityCalculator.GetUtilityComponent(componentIndex);

                if (mode == Global.Settings.Modes.SchoolBus)
                {
                    modeComponent.AddUtilityTerm(10, 1);
                    modeComponent.AddUtilityTerm(11, childUnder5Flag);
                    modeComponent.AddUtilityTerm(12, adultFlag);
                }
                else if (mode == Global.Settings.Modes.ParkAndRide)
                {
                    modeComponent.AddUtilityTerm(10, 1);
                    modeComponent.AddUtilityTerm(16, noCarsInHouseholdFlag);
                    modeComponent.AddUtilityTerm(17, carsLessThanWorkersFlag);
                    modeComponent.AddUtilityTerm(129, destinationParcel.MixedUse2Index1());
                    modeComponent.AddUtilityTerm(129, destinationParcel.TotalEmploymentDensity1() / 5000.0);
                    modeComponent.AddUtilityTerm(129, destinationParcel.NetIntersectionDensity1() / 50.0);
                    //modeComponent.AddUtilityTerm(123, Math.Log(destinationParcel.StopsTransitBuffer1 + 1));
                }
                else if (mode == Global.Settings.Modes.Transit)
                {
                    modeComponent.AddUtilityTerm(20, 1);
                    //        modeComponent.AddUtilityTerm(21, maleFlag);
                    modeComponent.AddUtilityTerm(22, ageUnder30Flag);
                    //    modeComponent.AddUtilityTerm(23, ageBetween51And98Flag);
                    modeComponent.AddUtilityTerm(24, income0To25KFlag);
                    //    modeComponent.AddUtilityTerm(25, income100KPlusFlag);
                    modeComponent.AddUtilityTerm(26, noCarsInHouseholdFlag);
                    modeComponent.AddUtilityTerm(27, carsLessThanDriversFlag);
                    modeComponent.AddUtilityTerm(129, destinationParcel.MixedUse2Index1());
                    modeComponent.AddUtilityTerm(129, destinationParcel.TotalEmploymentDensity1() / 5000.0);
                    modeComponent.AddUtilityTerm(129, destinationParcel.NetIntersectionDensity1() / 50.0);
                    //    modeComponent.AddUtilityTerm(124, originParcel.NetIntersectionDensity1()/50.0);
                    //    modeComponent.AddUtilityTerm(124, originParcel.HouseholdDensity1()/1000.0);
                    //    modeComponent.AddUtilityTerm(124, originParcel.MixedUse2Index1());
                    //modeComponent.AddUtilityTerm(123, Math.Log(destinationParcel.StopsTransitBuffer1 + 1));
                    //modeComponent.AddUtilityTerm(122, Math.Log(originParcel.StopsTransitBuffer1 + 1));
                }
                else if (mode == Global.Settings.Modes.Hov3)
                {
                    modeComponent.AddUtilityTerm(30, 1);
                    modeComponent.AddUtilityTerm(31, childrenUnder5);
                    modeComponent.AddUtilityTerm(32, childrenAge5Through15);
                    modeComponent.AddUtilityTerm(34, nonworkingAdults + retiredAdults);
                    modeComponent.AddUtilityTerm(38, onePersonHouseholdFlag);
                    modeComponent.AddUtilityTerm(39, twoPersonHouseholdFlag);
                    modeComponent.AddUtilityTerm(36, noCarsInHouseholdFlag);
                    modeComponent.AddUtilityTerm(37, carsLessThanDriversFlag);
                }
                else if (mode == Global.Settings.Modes.Hov2)
                {
                    modeComponent.AddUtilityTerm(31, childrenUnder5);
                    modeComponent.AddUtilityTerm(32, childrenAge5Through15);
                    modeComponent.AddUtilityTerm(34, nonworkingAdults + retiredAdults);
                    modeComponent.AddUtilityTerm(36, noCarsInHouseholdFlag);
                    modeComponent.AddUtilityTerm(37, carsLessThanDriversFlag);
                    modeComponent.AddUtilityTerm(40, 1);
                    modeComponent.AddUtilityTerm(41, onePersonHouseholdFlag);
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    //    modeComponent.AddUtilityTerm(50, 1);
                    //    modeComponent.AddUtilityTerm(54, income0To25KFlag);
                    modeComponent.AddUtilityTerm(55, income100KPlusFlag);
                    modeComponent.AddUtilityTerm(57, carsLessThanWorkersFlag);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    modeComponent.AddUtilityTerm(60, 1);
                    modeComponent.AddUtilityTerm(61, maleFlag);
                    modeComponent.AddUtilityTerm(62, ageUnder30Flag);
                    modeComponent.AddUtilityTerm(63, ageBetween51And98Flag);
                    //        modeComponent.AddUtilityTerm(64, income0To25KFlag);
                    //      modeComponent.AddUtilityTerm(65, income100KPlusFlag);
                    modeComponent.AddUtilityTerm(66, noCarsInHouseholdFlag);
                    modeComponent.AddUtilityTerm(67, carsLessThanDriversFlag);
                    modeComponent.AddUtilityTerm(169, destinationParcel.MixedUse4Index2());
                    modeComponent.AddUtilityTerm(169, destinationParcel.TotalEmploymentDensity2() / 20000.0);
                    modeComponent.AddUtilityTerm(169, destinationParcel.NetIntersectionDensity2() / 200.0);
                    modeComponent.AddUtilityTerm(164, originParcel.NetIntersectionDensity2() / 200.0);
                    modeComponent.AddUtilityTerm(164, originParcel.HouseholdDensity2() / 4000.0);
                    modeComponent.AddUtilityTerm(164, originParcel.MixedUse4Index2());
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    modeComponent.AddUtilityTerm(70, 1.0);
                    modeComponent.AddUtilityTerm(71, maleFlag);
                    modeComponent.AddUtilityTerm(72, ageUnder30Flag);
                    //        modeComponent.AddUtilityTerm(73, ageBetween51And98Flag);
                    //        modeComponent.AddUtilityTerm(74, income0To25KFlag);
                    modeComponent.AddUtilityTerm(75, income100KPlusFlag);
                    //        modeComponent.AddUtilityTerm(76, noCarsInHouseholdFlag);
                    modeComponent.AddUtilityTerm(77, carsLessThanDriversFlag);
                    modeComponent.AddUtilityTerm(179, destinationParcel.MixedUse4Index1());
                    modeComponent.AddUtilityTerm(179, destinationParcel.TotalEmploymentDensity1() / 5000.0);
                    modeComponent.AddUtilityTerm(179, destinationParcel.NetIntersectionDensity1() / 50.0);
                    modeComponent.AddUtilityTerm(179, originParcel.NetIntersectionDensity1() / 50.0);
                    modeComponent.AddUtilityTerm(179, originParcel.HouseholdDensity1() / 1000.0);
                    modeComponent.AddUtilityTerm(179, originParcel.MixedUse4Index1());
                }

                if (mode == Global.Settings.Modes.Walk || mode == Global.Settings.Modes.Bike || mode == Global.Settings.Modes.Hov2 ||
                    mode == Global.Settings.Modes.Hov3 || mode == Global.Settings.Modes.Transit)
                {
                    int firstCoef = 200 + 10 * mode;
                    //modeComponent.AddUtilityTerm(firstCoef + 0, escortTourFlag);
                    //modeComponent.AddUtilityTerm(firstCoef + 1, shoppingTourFlag);
                    //modeComponent.AddUtilityTerm(firstCoef + 2, mealTourFlag);
                    //modeComponent.AddUtilityTerm(firstCoef + 3, socialTourFlag);
                    //modeComponent.AddUtilityTerm(firstCoef + 4, personalBusinessTourFlag);
                    //modeComponent.AddUtilityTerm(firstCoef + 5, recreationTourFlag);
                    //    modeComponent.AddUtilityTerm(firstCoef + 6, medicalTourFlag);
                    //modeComponent.AddUtilityTerm(firstCoef + 7, jointTourFlag);
                    modeComponent.AddUtilityTerm(firstCoef + 8, Math.Min(partialHalfTour1Flag + partialHalfTour2Flag, 1.0));
                    modeComponent.AddUtilityTerm(firstCoef + 9, Math.Min(fullHalfTour1Flag + fullHalfTour2Flag, 1.0));

                    modeComponent.AddUtilityTerm(290 + mode, mode == parentTourMode ? 1 : 0);
                }
                modeComponent.AddUtilityTerm(298, mode >= Global.Settings.Modes.Sov && mode <= Global.Settings.Modes.Hov3 && parentTourMode == Global.Settings.Modes.Sov ? 1 : 0);
                modeComponent.AddUtilityTerm(299, mode >= Global.Settings.Modes.Sov && mode <= Global.Settings.Modes.Hov3 && parentTourMode >= Global.Settings.Modes.Hov2 && parentTourMode <= Global.Settings.Modes.Hov3 ? 1 : 0);
            }



            //loop on all alternatives, using modeTimes objects
            {
                foreach (HTourModeTime modeTimes in HTourModeTime.ModeTimes[ParallelUtility.threadLocalAssignedIndex.Value])
                {
                    MinuteSpan arrivalPeriod = modeTimes.ArrivalPeriod;
                    int        arrivalPeriodAvailableMinutes = timeWindow.TotalAvailableMinutes(arrivalPeriod.Start, arrivalPeriod.End);

                    MinuteSpan departurePeriod = modeTimes.DeparturePeriod;
                    int        departurePeriodAvailableMinutes = timeWindow.TotalAvailableMinutes(departurePeriod.Start, departurePeriod.End);
                    periodComb = modeTimes.PeriodCombinationIndex;

                    int mode = modeTimes.Mode;

                    int altIndex = modeTimes.Index;

                    //set availabillity based on time window variables and any constrained choices
                    bool available = (modeTimes.LongestFeasibleWindow != null) && (mode > 0) &&
                                     (mode != Global.Settings.Modes.Sov || (tour.Person.IsDrivingAge && tour.Household.VehiclesAvailable > 0)) &&
                                     (constrainedMode <= 0 || constrainedMode == mode) &&
                                     (constrainedArrivalTime <= 0 || (constrainedArrivalTime >= arrivalPeriod.Start && constrainedArrivalTime <= arrivalPeriod.End)) &&
                                     (constrainedDepartureTime <= 0 || (constrainedDepartureTime >= departurePeriod.Start && constrainedDepartureTime <= departurePeriod.End));


                    ChoiceProbabilityCalculator.Alternative alternative = choiceProbabilityCalculator.GetAlternative(altIndex, available,
                                                                                                                     choice != null && choice.Index == altIndex);

                    alternative.Choice = modeTimes; // JLB added 20130420

                    //alternative.AddNestedAlternative(HTourModeTime.TOTAL_TOUR_MODE_TIMES + periodComb + 1, periodComb, THETA_PARAMETER);

                    if (Global.Configuration.IsInEstimationMode && choice != null && altIndex == choice.Index)
                    {
                        Global.PrintFile.WriteLine("Aper Dper Mode {0} {1} {2} Travel Times {3} {4} Window {5} {6}",
                                                   arrivalPeriod.Index, departurePeriod.Index, mode,
                                                   modeTimes.ModeAvailableToDestination ? modeTimes.TravelTimeToDestination : -1,
                                                   modeTimes.ModeAvailableFromDestination ? modeTimes.TravelTimeFromDestination : -1,
                                                   modeTimes.LongestFeasibleWindow != null ? modeTimes.LongestFeasibleWindow.Start : -1,
                                                   modeTimes.LongestFeasibleWindow != null ? modeTimes.LongestFeasibleWindow.End : -1);
                    }

                    /*                if (altIndex == 0)
                     *                  {
                     *                      alternative.AddUtilityTerm(991, tour.Household.Id);
                     *                      alternative.AddUtilityTerm(992, tour.Person.Id);
                     *                      alternative.AddUtilityTerm(993, tour.PersonDay.Day);
                     *                      alternative.AddUtilityTerm(994, tour.Sequence);
                     *                      alternative.AddUtilityTerm(995, constrainedMode);
                     *                      alternative.AddUtilityTerm(996, constrainedArrivalTime);
                     *                      alternative.AddUtilityTerm(997, constrainedDepartureTime);
                     *                      alternative.AddUtilityTerm(998, tour.DestinationPurpose);
                     *                      alternative.AddUtilityTerm(999, (tour.ParentTour == null) ? 0 : 1);
                     *                  }
                     */
                    //if in application mode and combination is not available, can skip the rest
                    if (!Global.Configuration.IsInEstimationMode && !alternative.Available)
                    {
                        continue;
                    }
                    if (useTimeComponents)
                    {
                        // arrival period utility component
                        alternative.AddUtilityComponent(choiceProbabilityCalculator.GetUtilityComponent(arrivalPeriod.Index));

                        // departure period utility component
                        alternative.AddUtilityComponent(
                            choiceProbabilityCalculator.GetUtilityComponent(bigPeriodCount + departurePeriod.Index));

                        // period combination utility component
                        alternative.AddUtilityComponent(choiceProbabilityCalculator.GetUtilityComponent(2 * bigPeriodCount + periodComb));
                    }
                    // mode utility component
                    alternative.AddUtilityComponent(
                        choiceProbabilityCalculator.GetUtilityComponent(2 * bigPeriodCount + nPeriodCombs + mode - 1));

                    //even in estimation mode, do not need the rest of the code if not available
                    if (!alternative.Available)
                    {
                        continue;
                    }

                    // set parking cost for period combination
                    double parkingDuration = (departurePeriod == arrivalPeriod
                                               ? (arrivalPeriod.End - arrivalPeriod.Start) / 2.0
                                               : departurePeriod.Middle - arrivalPeriod.Middle) / 60.0;

                    // parking at work is free if no paid parking at work and tour goes to usual workplace
                    double destinationParkingCost = (!Global.Configuration.IsInEstimationMode &&
                                                     Global.Configuration.ShouldRunPayToParkAtWorkplaceModel &&
                                                     tour.Person.UsualWorkParcel != null &&
                                                     destinationParcel == tour.Person.UsualWorkParcel &&
                                                     person.PaidParkingAtWorkplace == 0)
                                                     ? 0.0
                                                     : destinationParcel.ParkingCostBuffer1(parkingDuration);
                    double parkingCostFraction = (mode == Global.Settings.Modes.Sov)
                                                  ? 1.0
                                                  : (mode == Global.Settings.Modes.Hov2)
                                                        ? 1.0 / Global.Configuration.Coefficients_HOV2CostDivisor_Work
                                                        : (mode == Global.Settings.Modes.Hov3)
                                                              ? 1.0 / Global.Configuration.Coefficients_HOV3CostDivisor_Work
                                                              : 0.0;


                    double minimumTimeNeeded = modeTimes.TravelTimeToDestination + modeTimes.TravelTimeFromDestination +
                                               Global.Settings.Times.MinimumActivityDuration;

                    alternative.AddUtilityTerm(1, modeTimes.GeneralizedTimeToDestination + modeTimes.GeneralizedTimeFromDestination);
                    //alternative.AddUtilityTerm(2, destinationParkingCost*parkingCostFraction);
                    //alternative.AddUtilityTerm(3,
                    //                           Math.Log(Math.Min(1140, modeTimes.LongestFeasibleWindow.End - modeTimes.LongestFeasibleWindow.Start -
                    //                                                     minimumTimeNeeded + 1.0 )));
                    // JLB 20140204 replaced coeff 3 with a different time window formulation:  time pressure
                    //    instead of having positive utility for increasing time window, have negative utility for decreasing time window
                    alternative.AddUtilityTerm(3,
                                               Math.Log(Math.Max(Constants.EPSILON, 1 -
                                                                 Math.Pow(minimumTimeNeeded / (Math.Min(1140, modeTimes.LongestFeasibleWindow.End - modeTimes.LongestFeasibleWindow.Start)), 0.3)
                                                                 )));

                    //alternative.AddUtilityTerm(4, Math.Log((totalMinutesAvailableInDay + 1.0)/(minimumTimeNeeded + 1.0)));
                    //alternative.AddUtilityTerm(4,
                    //                    Math.Log(Math.Max(Constants.EPSILON, 1 - minimumTimeNeeded/(Math.Min(1140, totalMinutesAvailableInDay)))));

                    //alternative.AddUtilityTerm(5,
                    //                           (maleFlag == 0 && mode == Global.Settings.Modes.Walk &&
                    //                            arrivalPeriod.Index >= DayPeriod.EVENING)
                    //                                   ? 1
                    //                                   : 0);
                    //    alternative.AddUtilityTerm(5,
                    //                               (maleFlag == 0 && mode == Global.Settings.Modes.Walk &&
                    //                                departurePeriod.Index >= DayPeriod.EVENING)
                    //                                   ? 1
                    //                                   : 0);
                }
            }
        }
Beispiel #4
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper tour, IEnumerable <IPathTypeModel> pathTypeModels, IParcelWrapper destinationParcel, int householdCars, int choice = Constants.DEFAULT_VALUE)
        {
            IHouseholdWrapper household = tour.Household;
            IPersonWrapper    person    = tour.Person;
            IPersonDayWrapper personDay = tour.PersonDay;

            // household inputs
            int income0To25KFlag        = household.Has0To25KIncome.ToFlag();
            int income25To50KFlag       = household.Has25To50KIncome.ToFlag();
            int income75KPlusFlag       = household.Has75KPlusIncome.ToFlag();
            int incomeOver100Flag       = household.Has100KPlusIncome.ToFlag();
            int onePersonHouseholdFlag  = household.IsOnePersonHousehold.ToFlag();
            int twoPersonHouseholdFlag  = household.IsTwoPersonHousehold.ToFlag();
            int noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(householdCars);
            int carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(householdCars);
            int carsLessThanWorkersFlag = household.GetFlagForCarsLessThanWorkers(householdCars);

            // person inputs
            int childUnder5Flag       = person.IsChildUnder5.ToFlag();
            int adultFlag             = person.IsAdult.ToFlag();
            int drivingAgeStudentFlag = person.IsDrivingAgeStudent.ToFlag();
            int maleFlag = person.IsMale.ToFlag();

            // remaining inputs
            IParcelWrapper originParcel           = tour.OriginParcel;
            double         destinationParkingCost = destinationParcel.ParkingCostBuffer1(6);

            ChoiceModelUtility.SetEscortPercentages(personDay, out double escortPercentage, out double nonEscortPercentage);

            // school bus is a special case - use HOV3 impedance
            {
                IPathTypeModel pathTypeExtra              = pathTypeModels.First(x => x.Mode == Global.Settings.Modes.Hov3);
                int            modeExtra                  = Global.Settings.Modes.SchoolBus;
                bool           availableExtra             = pathTypeExtra.Available;
                double         generalizedTimeLogsumExtra = pathTypeExtra.GeneralizedTimeLogsum;

                ChoiceProbabilityCalculator.Alternative alternative = choiceProbabilityCalculator.GetAlternative(modeExtra, availableExtra, choice == modeExtra);
                alternative.Choice = modeExtra;

                alternative.AddNestedAlternative(_nestedAlternativeIds[modeExtra], _nestedAlternativeIndexes[modeExtra], THETA_PARAMETER);

                if (availableExtra)
                {
                    //    case Global.Settings.Modes.SchoolBus:
                    alternative.AddUtilityTerm(2, generalizedTimeLogsumExtra * tour.TimeCoefficient);
                    alternative.AddUtilityTerm(10, 1);
                    alternative.AddUtilityTerm(11, noCarsInHouseholdFlag);   // for calibration
                    alternative.AddUtilityTerm(13, carsLessThanDriversFlag); // for calibration
                    alternative.AddUtilityTerm(17, childUnder5Flag);
                    alternative.AddUtilityTerm(18, adultFlag);
                }
            }


            foreach (IPathTypeModel pathTypeModel in pathTypeModels)
            {
                int  mode      = pathTypeModel.Mode;
                bool available = (pathTypeModel.Mode != Global.Settings.Modes.ParkAndRide || Global.Configuration.IncludeParkAndRideInSchoolTourModeModel) &&
                                 (pathTypeModel.Mode != Global.Settings.Modes.PaidRideShare || Global.Configuration.PaidRideShareModeIsAvailable) &&
                                 pathTypeModel.Available;
                double generalizedTimeLogsum = pathTypeModel.GeneralizedTimeLogsum;

                ChoiceProbabilityCalculator.Alternative alternative = choiceProbabilityCalculator.GetAlternative(mode, available, choice == mode);
                alternative.Choice = mode;

                alternative.AddNestedAlternative(_nestedAlternativeIds[mode], _nestedAlternativeIndexes[mode], THETA_PARAMETER);

                if (!available)
                {
                    continue;
                }

                double modeTimeCoefficient = (Global.Configuration.AV_IncludeAutoTypeChoice && household.OwnsAutomatedVehicles > 0 && mode >= Global.Settings.Modes.Sov && mode <= Global.Settings.Modes.Hov3) ?
                                             tour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) :
                                             (mode == Global.Settings.Modes.PaidRideShare && Global.Configuration.AV_PaidRideShareModeUsesAVs) ?
                                             tour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) : tour.TimeCoefficient;
                alternative.AddUtilityTerm(2, generalizedTimeLogsum * modeTimeCoefficient);


                if (mode == Global.Settings.Modes.ParkAndRide)
                {
                    alternative.AddUtilityTerm(5, 1);
                    alternative.AddUtilityTerm(6, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(7, carsLessThanWorkersFlag);
                    alternative.AddUtilityTerm(129, destinationParcel.MixedUse2Index1());
                }
                else if (mode == Global.Settings.Modes.Transit)
                {
                    alternative.AddUtilityTerm(20, 1);
                    alternative.AddUtilityTerm(21, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(22, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(27, childUnder5Flag);
                    alternative.AddUtilityTerm(28, adultFlag);
                    alternative.AddUtilityTerm(29, drivingAgeStudentFlag);
                    alternative.AddUtilityTerm(129, destinationParcel.MixedUse2Index1());
                    alternative.AddUtilityTerm(128, destinationParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(123, (tour.OriginParcel.DistanceToFerry > 0 && tour.OriginParcel.DistanceToFerry <= 0.5).ToFlag());
                    alternative.AddUtilityTerm(124, (destinationParcel.DistanceToFerry > 0 && destinationParcel.DistanceToFerry <= 0.5).ToFlag());
                    //alternative.AddUtilityTerm(125, 0.001 * originParcel.NetIntersectionDensity1()
                    //                                         + 0.0001 * originParcel.HouseholdDensity1()
                    //                                         + 1.0 * originParcel.MixedUse4Index1());
                    //alternative.AddUtilityTerm(126, 1.0 * destinationParcel.MixedUse4Index1()
                    //                                         + 0.00001 * destinationParcel.TotalEmploymentDensity1()
                    //                                         + 0.001 * destinationParcel.NetIntersectionDensity1());

                    //                        alternative.AddUtilityTerm(127, destinationParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtilityTerm(126, originParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtilityTerm(125, originParcel.HouseholdDensity1());
                    //                        alternative.AddUtilityTerm(124, originParcel.MixedUse2Index1());
                    //                        alternative.AddUtilityTerm(123, Math.Log(destinationParcel.StopsTransitBuffer1+1));
                    alternative.AddUtilityTerm(122, Math.Log(originParcel.StopsTransitBuffer1 + 1));
                }
                else if (mode == Global.Settings.Modes.Hov3)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT3));
                    alternative.AddUtilityTerm(30, 1);
                    alternative.AddUtilityTerm(37, twoPersonHouseholdFlag);
                    alternative.AddUtilityTerm(37, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(36, noCarsInHouseholdFlag);   // for calibration of hov3 vs hov2
                    alternative.AddUtilityTerm(35, carsLessThanDriversFlag); // for calibration of hov3 vs hov2
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(44, income0To25KFlag);
                    alternative.AddUtilityTerm(45, income25To50KFlag);
                    alternative.AddUtilityTerm(47, childUnder5Flag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Hov2)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT2));
                    alternative.AddUtilityTerm(38, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(40, 1);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(42, carsLessThanDriversFlag); // for calibration
                    alternative.AddUtilityTerm(44, income0To25KFlag);
                    alternative.AddUtilityTerm(45, income25To50KFlag);
                    alternative.AddUtilityTerm(47, childUnder5Flag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient));
                    alternative.AddUtilityTerm(50, 1);
                    alternative.AddUtilityTerm(52, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(54, income0To25KFlag);
                    alternative.AddUtilityTerm(56, income75KPlusFlag);
                    alternative.AddUtilityTerm(59, drivingAgeStudentFlag);
                    alternative.AddUtilityTerm(131, escortPercentage);
                    alternative.AddUtilityTerm(132, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    double class1Dist =
                        Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                  ? ImpedanceRoster.GetValue("class1distance", mode, Global.Settings.PathTypes.FullNetwork,
                                             Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                  : 0;

                    double class2Dist =
                        Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                  ? ImpedanceRoster.GetValue("class2distance", mode, Global.Settings.PathTypes.FullNetwork,
                                             Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                  : 0;

                    double worstDist =
                        Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                  ? ImpedanceRoster.GetValue("worstdistance", mode, Global.Settings.PathTypes.FullNetwork,
                                             Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                  : 0;

                    alternative.AddUtilityTerm(60, 1);
                    alternative.AddUtilityTerm(61, maleFlag);
                    alternative.AddUtilityTerm(67, noCarsInHouseholdFlag);   //for calibration
                    alternative.AddUtilityTerm(68, carsLessThanDriversFlag); //for calibration
                    alternative.AddUtilityTerm(69, adultFlag);
                    alternative.AddUtilityTerm(169, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(168, destinationParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(167, destinationParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(166, originParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(165, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(164, originParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(161, (class1Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(162, (class2Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(163, (worstDist > 0).ToFlag());
                    alternative.AddUtilityTerm(170, 1.0 * destinationParcel.MixedUse4Index1()
                                               + 0.00002 * destinationParcel.TotalEmploymentDensity1()
                                               + 0.001 * destinationParcel.NetIntersectionDensity1()
                                               + 0.001 * originParcel.NetIntersectionDensity1()
                                               + 0.0002 * originParcel.HouseholdDensity1()
                                               + 1.0 * originParcel.MixedUse4Index1());
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    alternative.AddUtilityTerm(70, 1);
                    alternative.AddUtilityTerm(77, noCarsInHouseholdFlag);   //for calibration
                    alternative.AddUtilityTerm(78, carsLessThanDriversFlag); //for calibration
                    alternative.AddUtilityTerm(79, adultFlag);
                    alternative.AddUtilityTerm(179, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(178, destinationParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(177, destinationParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(176, originParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(175, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(179, originParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(181, 1.0 * destinationParcel.MixedUse4Index1()
                                               + 0.00001 * destinationParcel.TotalEmploymentDensity1()
                                               + 0.001 * destinationParcel.NetIntersectionDensity1()
                                               + 0.001 * originParcel.NetIntersectionDensity1()
                                               + 0.0001 * originParcel.HouseholdDensity1()
                                               + 1.0 * originParcel.MixedUse4Index1());
                }
                else if (mode == Global.Settings.Modes.PaidRideShare)
                {
                    if (Global.Configuration.PaidRideshare_UseEstimatedInsteadOfAssertedCoefficients)
                    {
                        alternative.AddUtilityTerm(80, 1.0);
                        alternative.AddUtilityTerm(81, noCarsInHouseholdFlag);   //for calibration
                        alternative.AddUtilityTerm(82, carsLessThanDriversFlag); //for calibration
                        alternative.AddUtilityTerm(83, tour.Person.AgeIsBetween26And35.ToFlag());
                        alternative.AddUtilityTerm(83, tour.Person.AgeIsBetween18And25.ToFlag());
                        alternative.AddUtilityTerm(84, (tour.Person.Age < 18).ToFlag());            //alternative.AddUtilityTerm(81, tour.Person.AgeIsBetween26And35.ToFlag());
                        alternative.AddUtilityTerm(85, originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2);
                        alternative.AddUtilityTerm(86, destinationParcel.HouseholdsBuffer2 + destinationParcel.StudentsUniversityBuffer2 + destinationParcel.EmploymentTotalBuffer2);
                        alternative.AddUtilityTerm(87, income0To25KFlag);
                        alternative.AddUtilityTerm(88, incomeOver100Flag);
                    }
                    else
                    {
                        double modeConstant = Global.Configuration.AV_PaidRideShareModeUsesAVs
                        ? Global.Configuration.AV_PaidRideShare_ModeConstant
                                              + Global.Configuration.AV_PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2,
                                                                                                                    (Global.Configuration.PaidRideShare_DensityMeasureCapValue > 0) ? Global.Configuration.PaidRideShare_DensityMeasureCapValue : 6000)
                                              + Global.Configuration.AV_PaidRideShare_AVOwnerCoefficient * (household.OwnsAutomatedVehicles > 0).ToFlag()
                        : Global.Configuration.PaidRideShare_ModeConstant
                                              + Global.Configuration.PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2,
                                                                                                                 (Global.Configuration.PaidRideShare_DensityMeasureCapValue > 0) ? Global.Configuration.PaidRideShare_DensityMeasureCapValue : 6000);

                        alternative.AddUtilityTerm(90, modeConstant);
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age26to35Coefficient * tour.Person.AgeIsBetween26And35.ToFlag());
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age18to25Coefficient * tour.Person.AgeIsBetween18And25.ToFlag());
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_AgeOver65Coefficient * (tour.Person.Age >= 65).ToFlag());
                    }
                }

                RegionSpecificCustomizations(alternative, tour, pathTypeModel.PathType, mode, destinationParcel);
            }
        }
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper tour, IEnumerable <IPathTypeModel> pathTypeModels, IParcelWrapper destinationParcel, int choice = Constants.DEFAULT_VALUE)
        {
            var household       = tour.Household;
            var householdTotals = household.HouseholdTotals;
            var person          = tour.Person;
            var personDay       = tour.PersonDay;

            // household inputs
            var income0To25KFlag        = household.Has0To25KIncome.ToFlag();
            var childrenUnder5          = householdTotals.ChildrenUnder5;
            var childrenAge5Through15   = householdTotals.ChildrenAge5Through15;
            var nonworkingAdults        = householdTotals.NonworkingAdults;
            var retiredAdults           = householdTotals.RetiredAdults;
            var onePersonHouseholdFlag  = household.IsOnePersonHousehold.ToFlag();
            var twoPersonHouseholdFlag  = household.IsTwoPersonHousehold.ToFlag();
            var noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(household.VehiclesAvailable);
            var carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(household.VehiclesAvailable);
            var carsLessThanWorkersFlag = household.GetFlagForCarsLessThanWorkers(household.VehiclesAvailable);

            // person inputs
            var maleFlag = person.IsMale.ToFlag();
            var ageBetween51And98Flag = person.AgeIsBetween51And98.ToFlag();
            var univStudentFlag       = person.IsUniversityStudent.ToFlag();

            // tour inputs
            var shoppingTourFlag           = (tour.DestinationPurpose == Global.Settings.Purposes.Shopping).ToFlag();
            var mealTourFlag               = (tour.DestinationPurpose == Global.Settings.Purposes.Meal).ToFlag();
            var socialOrRecreationTourFlag = (tour.DestinationPurpose == Global.Settings.Purposes.Social).ToFlag();

            // remaining inputs
            var originParcel           = tour.OriginParcel;
            var parkingDuration        = ChoiceModelUtility.GetParkingDuration(person.IsFulltimeWorker);
            var destinationParkingCost = destinationParcel.ParkingCostBuffer1(parkingDuration);

            double escortPercentage;
            double nonEscortPercentage;

            ChoiceModelUtility.SetEscortPercentages(personDay, out escortPercentage, out nonEscortPercentage, true);

            // paidRideShare is another special case  - set in config file - use HOV2 impedance
            if (Global.Configuration.PaidRideShareModeIsAvailable)
            {
                var pathTypeExtra              = pathTypeModels.First(x => x.Mode == Global.Settings.Modes.PaidRideShare);
                var modeExtra                  = Global.Settings.Modes.PaidRideShare;
                var availableExtra             = pathTypeExtra.Available;
                var generalizedTimeLogsumExtra = pathTypeExtra.GeneralizedTimeLogsum;
                var distanceExtra              = pathTypeExtra.PathDistance;

                var alternative = choiceProbabilityCalculator.GetAlternative(modeExtra - 2, availableExtra, choice == modeExtra);
                alternative.Choice = modeExtra;

                alternative.AddNestedAlternative(_nestedAlternativeIds[modeExtra], _nestedAlternativeIndexes[modeExtra], THETA_PARAMETER);

                if (availableExtra)
                {
                    var extraCostPerMile = Global.Configuration.AV_PaidRideShareModeUsesAVs ?
                                           Global.Configuration.AV_PaidRideShare_ExtraCostPerDistanceUnit : Global.Configuration.PaidRideShare_ExtraCostPerDistanceUnit;
                    var fixedCostPerRide = Global.Configuration.AV_PaidRideShareModeUsesAVs ?
                                           Global.Configuration.AV_PaidRideShare_FixedCostPerRide : Global.Configuration.PaidRideShare_FixedCostPerRide;
                    var autoTimeCoefficient = Global.Configuration.AV_PaidRideShareModeUsesAVs ?
                                              tour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) : tour.TimeCoefficient;
                    alternative.AddUtilityTerm(2, generalizedTimeLogsumExtra * autoTimeCoefficient);
                    alternative.AddUtilityTerm(2, distanceExtra * extraCostPerMile * tour.CostCoefficient);
                    alternative.AddUtilityTerm(2, fixedCostPerRide * tour.CostCoefficient);

                    var modeConstant = Global.Configuration.AV_PaidRideShareModeUsesAVs
                      ? Global.Configuration.AV_PaidRideShare_ModeConstant
                                       + Global.Configuration.AV_PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2, 6000)
                                       + Global.Configuration.AV_PaidRideShare_AVOwnerCoefficient * (household.OwnsAutomatedVehicles > 0).ToFlag()
                      : Global.Configuration.PaidRideShare_ModeConstant
                                       + Global.Configuration.PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2, 6000);


                    alternative.AddUtilityTerm(90, modeConstant);
                    alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age26to35Coefficient * tour.Person.AgeIsBetween26And35.ToFlag());
                    alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age18to25Coefficient * tour.Person.AgeIsBetween18And25.ToFlag());
                    alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_AgeOver65Coefficient * (tour.Person.Age >= 65).ToFlag());
                }
            }


            //foreach (var pathTypeModel in pathTypeModels) {
            foreach (IPathTypeModel pathTypeModel in pathTypeModels)
            {
                IPathTypeModel ipathTypeModel        = pathTypeModel;
                var            mode                  = pathTypeModel.Mode;
                var            available             = pathTypeModel.Mode != Global.Settings.Modes.ParkAndRide && pathTypeModel.Available;
                var            generalizedTimeLogsum = pathTypeModel.GeneralizedTimeLogsum;

                var alternative = choiceProbabilityCalculator.GetAlternative(mode, available, choice == mode);
                alternative.Choice = mode;

                alternative.AddNestedAlternative(_nestedAlternativeIds[pathTypeModel.Mode], _nestedAlternativeIndexes[pathTypeModel.Mode], THETA_PARAMETER);

                if (!available)
                {
                    continue;
                }


                var modeTimeCoefficient = (Global.Configuration.AV_IncludeAutoTypeChoice && household.OwnsAutomatedVehicles > 0 && mode >= Global.Settings.Modes.Sov && mode <= Global.Settings.Modes.Hov3) ?
                                          tour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) : tour.TimeCoefficient;
                alternative.AddUtilityTerm(2, generalizedTimeLogsum * modeTimeCoefficient);

                if (mode == Global.Settings.Modes.Transit)
                {
                    alternative.AddUtilityTerm(20, 1);
                    alternative.AddUtilityTerm(21, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(120, shoppingTourFlag);
                    //                        alternative.AddUtility(121, mealTourFlag);
                    //                        alternative.AddUtility(129, destinationParcel.MixedUse2Index1());
                    alternative.AddUtilityTerm(128, destinationParcel.TotalEmploymentDensity1());
                    //                        alternative.AddUtility(127, destinationParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(126, originParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(125, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(124, originParcel.MixedUse2Index1());
                    //                        alternative.AddUtility(123, Math.Log(destinationParcel.StopsTransitBuffer1+1));
                    //                        alternative.AddUtility(122, Math.Log(originParcel.StopsTransitBuffer1+1));
                    alternative.AddUtilityTerm(180, univStudentFlag);
                }
                else if (mode == Global.Settings.Modes.Hov3)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT3));
                    alternative.AddUtilityTerm(30, 1);
                    alternative.AddUtilityTerm(31, childrenUnder5);
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    alternative.AddUtilityTerm(34, nonworkingAdults + retiredAdults);
                    alternative.AddUtilityTerm(35, ((double)pathTypeModel.PathDistance).AlmostEquals(0) ? 0 : Math.Log(pathTypeModel.PathDistance));
                    alternative.AddUtilityTerm(38, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(39, twoPersonHouseholdFlag);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(43, carsLessThanWorkersFlag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                    alternative.AddUtilityTerm(136, shoppingTourFlag);
                    alternative.AddUtilityTerm(137, mealTourFlag);
                    alternative.AddUtilityTerm(138, socialOrRecreationTourFlag);
                }
                else if (mode == Global.Settings.Modes.Hov2)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT2));
                    alternative.AddUtilityTerm(31, childrenUnder5);
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    alternative.AddUtilityTerm(34, nonworkingAdults + retiredAdults);
                    alternative.AddUtilityTerm(35, ((double)pathTypeModel.PathDistance).AlmostEquals(0) ? 0 : Math.Log(pathTypeModel.PathDistance));
                    alternative.AddUtilityTerm(40, 1);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(43, carsLessThanWorkersFlag);
                    alternative.AddUtilityTerm(48, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                    alternative.AddUtilityTerm(136, shoppingTourFlag);
                    alternative.AddUtilityTerm(137, mealTourFlag);
                    alternative.AddUtilityTerm(138, socialOrRecreationTourFlag);
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient));
                    alternative.AddUtilityTerm(50, 1);
                    alternative.AddUtilityTerm(52, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(54, income0To25KFlag);
                    alternative.AddUtilityTerm(131, escortPercentage);
                    alternative.AddUtilityTerm(132, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    double class1Dist = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions ?
                                        ImpedanceRoster.GetValue("class1distance", mode, Global.Settings.PathTypes.FullNetwork,
                                                                 Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable : 0;
                    double class2Dist = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions ?
                                        ImpedanceRoster.GetValue("class2distance", mode, Global.Settings.PathTypes.FullNetwork,
                                                                 Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable : 0;
                    double worstDist = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions ?
                                       ImpedanceRoster.GetValue("worstdistance", mode, Global.Settings.PathTypes.FullNetwork,
                                                                Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable : 0;

                    alternative.AddUtilityTerm(60, 1);
                    alternative.AddUtilityTerm(61, maleFlag);
                    alternative.AddUtilityTerm(63, ageBetween51And98Flag);
                    alternative.AddUtilityTerm(160, socialOrRecreationTourFlag);
                    alternative.AddUtilityTerm(169, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(168, destinationParcel.TotalEmploymentDensity1());
                    //                        alternative.AddUtility(167, destinationParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(166, originParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(165, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(164, originParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(161, (class1Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(162, (class2Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(163, (worstDist > 0).ToFlag());
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    alternative.AddUtilityTerm(70, 1);
                    alternative.AddUtilityTerm(73, ageBetween51And98Flag);
                    alternative.AddUtilityTerm(171, mealTourFlag);
                    alternative.AddUtilityTerm(172, socialOrRecreationTourFlag);
                    //                        alternative.AddUtility(179, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(178, destinationParcel.HouseholdDensity1());
                    //                        alternative.AddUtility(177, destinationParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(176, originParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(175, originParcel.HouseholdDensity1());
                    //                        alternative.AddUtility(174, originParcel.MixedUse4Index1());
                }

                RegionSpecificCustomizations(alternative, tour, pathTypeModel.PathType, mode, destinationParcel);
            }
        }
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper tour, IEnumerable <IPathTypeModel> pathTypeModels, IParcelWrapper destinationParcel, int choice = Constants.DEFAULT_VALUE)
        {
            IHouseholdWrapper household = tour.Household;

            Framework.DomainModels.Models.IHouseholdTotals householdTotals = household.HouseholdTotals;
            IPersonWrapper    person    = tour.Person;
            IPersonDayWrapper personDay = tour.PersonDay;

            // household inputs
            int income0To25KFlag        = household.Has0To25KIncome.ToFlag();
            int incomeOver100Flag       = household.Has100KPlusIncome.ToFlag();
            int childrenUnder5          = householdTotals.ChildrenUnder5;
            int childrenAge5Through15   = householdTotals.ChildrenAge5Through15;
            int nonworkingAdults        = householdTotals.NonworkingAdults;
            int retiredAdults           = householdTotals.RetiredAdults;
            int onePersonHouseholdFlag  = household.IsOnePersonHousehold.ToFlag();
            int twoPersonHouseholdFlag  = household.IsTwoPersonHousehold.ToFlag();
            int noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(household.VehiclesAvailable);
            int carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(household.VehiclesAvailable);
            int carsLessThanWorkersFlag = household.GetFlagForCarsLessThanWorkers(household.VehiclesAvailable);

            // person inputs
            int maleFlag = person.IsMale.ToFlag();
            int ageBetween51And98Flag = person.AgeIsBetween51And98.ToFlag();
            int univStudentFlag       = person.IsUniversityStudent.ToFlag();

            // tour inputs
            int shoppingTourFlag           = (tour.DestinationPurpose == Global.Settings.Purposes.Shopping).ToFlag();
            int mealTourFlag               = (tour.DestinationPurpose == Global.Settings.Purposes.Meal).ToFlag();
            int socialOrRecreationTourFlag = (tour.DestinationPurpose == Global.Settings.Purposes.Social).ToFlag();

            // remaining inputs
            IParcelWrapper originParcel           = tour.OriginParcel;
            int            parkingDuration        = ChoiceModelUtility.GetParkingDuration(person.IsFulltimeWorker);
            double         destinationParkingCost = destinationParcel.ParkingCostBuffer1(parkingDuration);

            ChoiceModelUtility.SetEscortPercentages(personDay, out double escortPercentage, out double nonEscortPercentage, true);


            //foreach (var pathTypeModel in pathTypeModels) {
            foreach (IPathTypeModel pathTypeModel in pathTypeModels)
            {
                IPathTypeModel ipathTypeModel = pathTypeModel;
                int            mode           = pathTypeModel.Mode;
                bool           available      = (pathTypeModel.Mode != Global.Settings.Modes.ParkAndRide || Global.Configuration.IncludeParkAndRideInOtherHomeBasedTourModeModel) &&
                                                (pathTypeModel.Mode != Global.Settings.Modes.PaidRideShare || Global.Configuration.PaidRideShareModeIsAvailable) &&
                                                pathTypeModel.Available;
                double generalizedTimeLogsum = pathTypeModel.GeneralizedTimeLogsum;

                ChoiceProbabilityCalculator.Alternative alternative = choiceProbabilityCalculator.GetAlternative(mode, available, choice == mode);
                alternative.Choice = mode;

                alternative.AddNestedAlternative(_nestedAlternativeIds[pathTypeModel.Mode], _nestedAlternativeIndexes[pathTypeModel.Mode], THETA_PARAMETER);

                if (!available)
                {
                    continue;
                }


                double modeTimeCoefficient = (Global.Configuration.AV_IncludeAutoTypeChoice && household.OwnsAutomatedVehicles > 0 && mode >= Global.Settings.Modes.Sov && mode <= Global.Settings.Modes.Hov3) ?
                                             tour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) :
                                             (mode == Global.Settings.Modes.PaidRideShare && Global.Configuration.AV_PaidRideShareModeUsesAVs) ?
                                             tour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) : tour.TimeCoefficient;
                alternative.AddUtilityTerm(2, generalizedTimeLogsum * modeTimeCoefficient);

                if (mode == Global.Settings.Modes.ParkAndRide)
                {
                    alternative.AddUtilityTerm(5, 1);
                    alternative.AddUtilityTerm(6, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(7, carsLessThanWorkersFlag);
                    alternative.AddUtilityTerm(128, destinationParcel.TotalEmploymentDensity1());
                }
                else if (mode == Global.Settings.Modes.Transit)
                {
                    alternative.AddUtilityTerm(20, 1);
                    alternative.AddUtilityTerm(21, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(22, carsLessThanDriversFlag); //for calibration
                    alternative.AddUtilityTerm(120, shoppingTourFlag);
                    alternative.AddUtilityTerm(121, mealTourFlag);
                    //                        alternative.AddUtility(129, destinationParcel.MixedUse2Index1());
                    alternative.AddUtilityTerm(128, destinationParcel.TotalEmploymentDensity1());
                    //                        alternative.AddUtility(127, destinationParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(126, originParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(125, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(124, originParcel.MixedUse2Index1());
                    //                        alternative.AddUtility(123, Math.Log(destinationParcel.StopsTransitBuffer1+1));
                    //                        alternative.AddUtility(122, Math.Log(originParcel.StopsTransitBuffer1+1));
                    alternative.AddUtilityTerm(180, univStudentFlag);
                }
                else if (mode == Global.Settings.Modes.Hov3)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT3));
                    alternative.AddUtilityTerm(30, 1);
                    alternative.AddUtilityTerm(31, childrenUnder5);
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    alternative.AddUtilityTerm(34, nonworkingAdults + retiredAdults);
                    alternative.AddUtilityTerm(35, pathTypeModel.PathDistance.AlmostEquals(0) ? 0 : Math.Log(pathTypeModel.PathDistance));
                    alternative.AddUtilityTerm(36, noCarsInHouseholdFlag);   // for calibration of hov3 vs hov2
                    alternative.AddUtilityTerm(37, carsLessThanWorkersFlag); // for calibration of hov3 vs hov2
                    alternative.AddUtilityTerm(38, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(39, twoPersonHouseholdFlag);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(43, carsLessThanWorkersFlag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                    alternative.AddUtilityTerm(136, shoppingTourFlag);
                    alternative.AddUtilityTerm(137, mealTourFlag);
                    alternative.AddUtilityTerm(138, socialOrRecreationTourFlag);
                }
                else if (mode == Global.Settings.Modes.Hov2)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT2));
                    alternative.AddUtilityTerm(31, childrenUnder5);
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    alternative.AddUtilityTerm(34, nonworkingAdults + retiredAdults);
                    alternative.AddUtilityTerm(35, pathTypeModel.PathDistance.AlmostEquals(0) ? 0 : Math.Log(pathTypeModel.PathDistance));
                    alternative.AddUtilityTerm(40, 1);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(43, carsLessThanWorkersFlag);
                    alternative.AddUtilityTerm(48, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                    alternative.AddUtilityTerm(136, shoppingTourFlag);
                    alternative.AddUtilityTerm(137, mealTourFlag);
                    alternative.AddUtilityTerm(138, socialOrRecreationTourFlag);
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient));
                    alternative.AddUtilityTerm(50, 1);
                    alternative.AddUtilityTerm(52, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(54, income0To25KFlag);
                    alternative.AddUtilityTerm(131, escortPercentage);
                    alternative.AddUtilityTerm(132, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    double class1Dist = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions ?
                                        ImpedanceRoster.GetValue("class1distance", mode, Global.Settings.PathTypes.FullNetwork,
                                                                 Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable : 0;
                    double class2Dist = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions ?
                                        ImpedanceRoster.GetValue("class2distance", mode, Global.Settings.PathTypes.FullNetwork,
                                                                 Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable : 0;
                    double worstDist = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions ?
                                       ImpedanceRoster.GetValue("worstdistance", mode, Global.Settings.PathTypes.FullNetwork,
                                                                Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable : 0;

                    alternative.AddUtilityTerm(60, 1);
                    alternative.AddUtilityTerm(61, maleFlag);
                    alternative.AddUtilityTerm(63, ageBetween51And98Flag);
                    alternative.AddUtilityTerm(67, noCarsInHouseholdFlag);   //for calibration
                    alternative.AddUtilityTerm(68, carsLessThanDriversFlag); //for calibration
                    alternative.AddUtilityTerm(160, socialOrRecreationTourFlag);
                    alternative.AddUtilityTerm(169, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(168, destinationParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(167, destinationParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(166, originParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(165, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(164, originParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(161, (class1Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(162, (class2Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(163, (worstDist > 0).ToFlag());
                    alternative.AddUtilityTerm(170, 1.0 * destinationParcel.MixedUse4Index1()
                                               + 0.00002 * destinationParcel.TotalEmploymentDensity1()
                                               + 0.001 * destinationParcel.NetIntersectionDensity1()
                                               + 0.001 * originParcel.NetIntersectionDensity1()
                                               + 0.0002 * originParcel.HouseholdDensity1()
                                               + 1.0 * originParcel.MixedUse4Index1());
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    alternative.AddUtilityTerm(70, 1);
                    alternative.AddUtilityTerm(73, ageBetween51And98Flag);
                    alternative.AddUtilityTerm(77, noCarsInHouseholdFlag);   //for calibration
                    alternative.AddUtilityTerm(78, carsLessThanDriversFlag); //for calibration
                    alternative.AddUtilityTerm(171, mealTourFlag);
                    alternative.AddUtilityTerm(172, socialOrRecreationTourFlag);
                    alternative.AddUtilityTerm(179, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(178, destinationParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(177, destinationParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(176, originParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(175, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(174, originParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(181, 1.0 * destinationParcel.MixedUse4Index1()
                                               + 0.00001 * destinationParcel.TotalEmploymentDensity1()
                                               + 0.001 * destinationParcel.NetIntersectionDensity1()
                                               + 0.001 * originParcel.NetIntersectionDensity1()
                                               + 0.0001 * originParcel.HouseholdDensity1()
                                               + 1.0 * originParcel.MixedUse4Index1());
                }
                else if (mode == Global.Settings.Modes.PaidRideShare)
                {
                    if (Global.Configuration.PaidRideshare_UseEstimatedInsteadOfAssertedCoefficients)
                    {
                        alternative.AddUtilityTerm(80, 1.0);
                        //alternative.AddUtilityTerm(81, tour.Person.AgeIsBetween26And35.ToFlag());
                        //alternative.AddUtilityTerm(82, tour.Person.AgeIsBetween18And25.ToFlag());
                        alternative.AddUtilityTerm(81, noCarsInHouseholdFlag);   //for calibration
                        alternative.AddUtilityTerm(82, carsLessThanDriversFlag); //for calibration
                        alternative.AddUtilityTerm(83, (tour.Person.Age >= 65).ToFlag());
                        alternative.AddUtilityTerm(84, originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2);
                        alternative.AddUtilityTerm(85, destinationParcel.HouseholdsBuffer2 + destinationParcel.StudentsUniversityBuffer2 + destinationParcel.EmploymentTotalBuffer2);
                        alternative.AddUtilityTerm(86, income0To25KFlag);
                        alternative.AddUtilityTerm(87, incomeOver100Flag);
                        alternative.AddUtilityTerm(88, mealTourFlag);
                        alternative.AddUtilityTerm(89, shoppingTourFlag);
                    }
                    else
                    {
                        double modeConstant = Global.Configuration.AV_PaidRideShareModeUsesAVs
                        ? Global.Configuration.AV_PaidRideShare_ModeConstant
                                              + Global.Configuration.AV_PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2,
                                                                                                                    (Global.Configuration.PaidRideShare_DensityMeasureCapValue > 0) ? Global.Configuration.PaidRideShare_DensityMeasureCapValue : 6000)
                                              + Global.Configuration.AV_PaidRideShare_AVOwnerCoefficient * (household.OwnsAutomatedVehicles > 0).ToFlag()
                        : Global.Configuration.PaidRideShare_ModeConstant
                                              + Global.Configuration.PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2,
                                                                                                                 (Global.Configuration.PaidRideShare_DensityMeasureCapValue > 0) ? Global.Configuration.PaidRideShare_DensityMeasureCapValue : 6000);

                        alternative.AddUtilityTerm(90, modeConstant);
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age26to35Coefficient * tour.Person.AgeIsBetween26And35.ToFlag());
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age18to25Coefficient * tour.Person.AgeIsBetween18And25.ToFlag());
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_AgeOver65Coefficient * (tour.Person.Age >= 65).ToFlag());
                    }
                }


                RegionSpecificCustomizations(alternative, tour, pathTypeModel.PathType, mode, destinationParcel);
            }
        }
Beispiel #7
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper tour, IEnumerable <IPathTypeModel> pathTypeModels, IParcelWrapper destinationParcel, int householdCars, int choice = Constants.DEFAULT_VALUE)
        {
            var household       = tour.Household;
            var householdTotals = household.HouseholdTotals;
            var personDay       = tour.PersonDay;
            var person          = tour.Person;

            // household inputs
            var childrenUnder5        = householdTotals.ChildrenUnder5;
            var childrenAge5Through15 = householdTotals.ChildrenAge5Through15;
            //            var nonworkingAdults = householdTotals.NonworkingAdults;
            //            var retiredAdults = householdTotals.RetiredAdults;
            var onePersonHouseholdFlag  = household.IsOnePersonHousehold.ToFlag();
            var twoPersonHouseholdFlag  = household.IsTwoPersonHousehold.ToFlag();
            var noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(householdCars);
            var carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(householdCars);
            var carsLessThanWorkersFlag = household.GetFlagForCarsLessThanWorkers(householdCars);
            var income0To25KFlag        = household.Has0To25KIncome.ToFlag();
            var income75kPlusFlag       = household.Has75KPlusIncome.ToFlag();

            // person inputs
            var maleFlag = person.IsMale.ToFlag();
            var ageBetween51And98Flag = person.AgeIsBetween51And98.ToFlag();
            var univStudentFlag       = person.IsUniversityStudent.ToFlag();

            var originParcel    = tour.OriginParcel;
            var parkingDuration = ChoiceModelUtility.GetParkingDuration(person.IsFulltimeWorker);
            // parking at work is free if no paid parking at work and tour goes to usual workplace
            var destinationParkingCost = (Global.Configuration.ShouldRunPayToParkAtWorkplaceModel && tour.Person.UsualWorkParcel != null &&
                                          destinationParcel == tour.Person.UsualWorkParcel && person.PaidParkingAtWorkplace == 0) ? 0.0 : destinationParcel.ParkingCostBuffer1(parkingDuration);

            double escortPercentage;
            double nonEscortPercentage;

            ChoiceModelUtility.SetEscortPercentages(personDay, out escortPercentage, out nonEscortPercentage);

            //            var timeWindow = (originParcel == tour.Household.ResidenceParcel) ? personDay.TimeWindow : tour.ParentTour.TimeWindow;
            //            var longestWindow = timeWindow.MaxAvailableMinutesAfter(1);
            //            var totalWindow = timeWindow.TotalAvailableMinutesAfter(1);
            //            var expectedDurationCurrentTour = person.IsFulltimeWorker ? Global.Settings.Times.EightHours : Global.Settings.Times.FourHours;
            //            var expectedDurationOtherTours = (personDay.TotalTours - personDay.TotalSimulatedTours) * Global.Settings.Times.TwoHours;
            //            var expectedDurationStops = (Math.Min(personDay.TotalStops,1) - Math.Min(personDay.TotalSimulatedStops,1)) * Global.Settings.Times.OneHour;
            //            var totalExpectedDuration = expectedDurationCurrentTour + expectedDurationOtherTours + expectedDurationStops;

            // paidRideShare is a special case
            if (Global.Configuration.PaidRideShareModeIsAvailable)
            {
                var pathTypeExtra              = pathTypeModels.First(x => x.Mode == Global.Settings.Modes.PaidRideShare);
                var modeExtra                  = Global.Settings.Modes.PaidRideShare;
                var availableExtra             = pathTypeExtra.Available;
                var generalizedTimeLogsumExtra = pathTypeExtra.GeneralizedTimeLogsum;
                var distanceExtra              = pathTypeExtra.PathDistance;

                var alternative = choiceProbabilityCalculator.GetAlternative(modeExtra - 1, availableExtra, choice == modeExtra);
                alternative.Choice = modeExtra;

                alternative.AddNestedAlternative(_nestedAlternativeIds[modeExtra], _nestedAlternativeIndexes[modeExtra], THETA_PARAMETER);

                if (availableExtra)
                {
                    var extraCostPerMile = Global.Configuration.AV_PaidRideShareModeUsesAVs ?
                                           Global.Configuration.AV_PaidRideShare_ExtraCostPerDistanceUnit : Global.Configuration.PaidRideShare_ExtraCostPerDistanceUnit;
                    var fixedCostPerRide = Global.Configuration.AV_PaidRideShareModeUsesAVs ?
                                           Global.Configuration.AV_PaidRideShare_FixedCostPerRide : Global.Configuration.PaidRideShare_FixedCostPerRide;
                    var autoTimeCoefficient = Global.Configuration.AV_PaidRideShareModeUsesAVs ?
                                              tour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) : tour.TimeCoefficient;
                    alternative.AddUtilityTerm(2, generalizedTimeLogsumExtra * autoTimeCoefficient);
                    alternative.AddUtilityTerm(2, distanceExtra * extraCostPerMile * tour.CostCoefficient);
                    alternative.AddUtilityTerm(2, fixedCostPerRide * tour.CostCoefficient);

                    var modeConstant = Global.Configuration.AV_PaidRideShareModeUsesAVs
                     ? Global.Configuration.AV_PaidRideShare_ModeConstant
                                       + Global.Configuration.AV_PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2, 6000)
                                       + Global.Configuration.AV_PaidRideShare_AVOwnerCoefficient * (household.OwnsAutomatedVehicles > 0).ToFlag()
                     : Global.Configuration.PaidRideShare_ModeConstant
                                       + Global.Configuration.PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2, 6000);

                    alternative.AddUtilityTerm(90, modeConstant);
                    alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age26to35Coefficient * tour.Person.AgeIsBetween26And35.ToFlag());
                    alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age18to25Coefficient * tour.Person.AgeIsBetween18And25.ToFlag());
                    alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_AgeOver65Coefficient * (tour.Person.Age >= 65).ToFlag());
                }
            }


            foreach (var pathTypeModel in pathTypeModels)
            {
                var mode = pathTypeModel.Mode;
                var generalizedTimeLogsum = pathTypeModel.GeneralizedTimeLogsum;
                //                var travelTime = pathTypeModel.PathTime;
                //                var travelCost = pathTypeModel.PathCost;

                var available = pathTypeModel.Available; //&& (travelTime < longestWindow);

                var alternative = choiceProbabilityCalculator.GetAlternative(mode, available, choice == mode);
                alternative.Choice = mode;

                alternative.AddNestedAlternative(_nestedAlternativeIds[pathTypeModel.Mode], _nestedAlternativeIndexes[pathTypeModel.Mode], THETA_PARAMETER);

                //                if (mode == Global.Settings.Modes.ParkAndRide) {
                //                    Console.WriteLine("Park and ride logsum = {0}", generalizedTimeLogsum);
                //                }

                if (!available)
                {
                    continue;
                }

                var modeTimeCoefficient = (Global.Configuration.AV_IncludeAutoTypeChoice && household.OwnsAutomatedVehicles > 0 && mode >= Global.Settings.Modes.Sov && mode <= Global.Settings.Modes.Hov3) ?
                                          tour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) : tour.TimeCoefficient;
                alternative.AddUtilityTerm(2, generalizedTimeLogsum * modeTimeCoefficient);

                //                alternative.AddUtility(3, Math.Log(1.0 - travelTime / longestWindow));
                //                alternative.AddUtility(4, travelTime < longestWindow - expectedDurationCurrentTour ? Math.Log(1.0 - travelTime / (longestWindow - expectedDurationCurrentTour)) : 0);
                //                alternative.AddUtility(5, travelTime < longestWindow - expectedDurationCurrentTour ? 0 : 1);
                //                alternative.AddUtility(6, travelTime < totalWindow - totalExpectedDuration ? Math.Log(1.0 - travelTime / (totalWindow - totalExpectedDuration)) : 0);
                //                alternative.AddUtility(7, travelTime < totalWindow - totalExpectedDuration ? 0 : 1);
                //                var vot = tour.TimeCoefficient / tour.CostCoefficient;

                if (mode == Global.Settings.Modes.ParkAndRide)
                {
                    alternative.AddUtilityTerm(10, 1);
                    alternative.AddUtilityTerm(11, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(13, carsLessThanWorkersFlag);
                    //                        alternative.AddUtility(129, destinationParcel.MixedUse2Index1());
                    alternative.AddUtilityTerm(130, Math.Log(destinationParcel.TotalEmploymentDensity1() + 1) * 2553.0 / Math.Log(2553.0));
                    alternative.AddUtilityTerm(128, destinationParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(127, destinationParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(123, Math.Log(destinationParcel.StopsTransitBuffer1+1));
                }
                else if (mode == Global.Settings.Modes.Transit)
                {
                    alternative.AddUtilityTerm(20, 1);
                    //                        alternative.AddUtility(129, destinationParcel.MixedUse2Index1());
                    alternative.AddUtilityTerm(130, Math.Log(destinationParcel.TotalEmploymentDensity1() + 1) * 2553.0 / Math.Log(2553.0));
                    alternative.AddUtilityTerm(128, destinationParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(127, destinationParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(126, originParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(125, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(124, originParcel.MixedUse2Index1());
                    //                        alternative.AddUtility(123, Math.Log(destinationParcel.StopsTransitBuffer1+1));
                    //                        alternative.AddUtility(122, Math.Log(originParcel.StopsTransitBuffer1+1));
                    alternative.AddUtilityTerm(180, univStudentFlag);
                    alternative.AddUtilityTerm(100, income75kPlusFlag);
                }
                else if (mode == Global.Settings.Modes.Hov3)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / Global.Configuration.Coefficients_HOV3CostDivisor_Work));
                    alternative.AddUtilityTerm(30, 1);
                    alternative.AddUtilityTerm(31, childrenUnder5);
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    //                        alternative.AddUtility(34, nonworkingAdults + retiredAdults);
                    alternative.AddUtilityTerm(35, ((double)pathTypeModel.PathDistance).AlmostEquals(0) ? 0 : Math.Log(pathTypeModel.PathDistance));
                    alternative.AddUtilityTerm(38, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(39, twoPersonHouseholdFlag);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(42, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Hov2)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / Global.Configuration.Coefficients_HOV2CostDivisor_Work));
                    alternative.AddUtilityTerm(31, childrenUnder5);
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    //                        alternative.AddUtility(34, nonworkingAdults + retiredAdults);
                    alternative.AddUtilityTerm(35, ((double)pathTypeModel.PathDistance).AlmostEquals(0) ? 0 : Math.Log(pathTypeModel.PathDistance));
                    alternative.AddUtilityTerm(40, 1);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(42, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(48, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost) * tour.CostCoefficient);
                    alternative.AddUtilityTerm(50, 1);
                    alternative.AddUtilityTerm(53, carsLessThanWorkersFlag);
                    alternative.AddUtilityTerm(54, income0To25KFlag);
                    alternative.AddUtilityTerm(131, escortPercentage);
                    alternative.AddUtilityTerm(132, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    double class1Dist
                        = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                            ? ImpedanceRoster.GetValue("class1distance", mode, Global.Settings.PathTypes.FullNetwork,
                                                       Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                            : 0;

                    double class2Dist =
                        Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                            ? ImpedanceRoster.GetValue("class2distance", mode, Global.Settings.PathTypes.FullNetwork,
                                                       Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                            : 0;

                    //                  double worstDist = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions ?
                    //                         ImpedanceRoster.GetValue("worstdistance", mode, Global.Settings.PathTypes.FullNetwork,
                    //                            Global.Settings.VotGroups.Medium, tour.DestinationArrivalTime,originParcel, destinationParcel).Variable : 0;

                    alternative.AddUtilityTerm(60, 1);
                    alternative.AddUtilityTerm(61, maleFlag);
                    alternative.AddUtilityTerm(63, ageBetween51And98Flag);
                    alternative.AddUtilityTerm(169, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(168, destinationParcel.TotalEmploymentDensity1());
                    //                        alternative.AddUtility(167, destinationParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(166, originParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(165, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(164, originParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(162, (class1Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(162, (class2Dist > 0).ToFlag());
                    //                        alternative.AddUtility(163, (worstDist > 0).ToFlag());
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    alternative.AddUtilityTerm(70, 1);
                    alternative.AddUtilityTerm(71, maleFlag);
                    //                        alternative.AddUtility(73, ageBetween51And98Flag);
                    alternative.AddUtilityTerm(179, destinationParcel.MixedUse4Index1());
                    //                        alternative.AddUtility(178, destinationParcel.TotalEmploymentDensity1());
                    //                        alternative.AddUtility(177, destinationParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(176, originParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(175, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(179, originParcel.MixedUse4Index1());
                }

                RegionSpecificCustomizations(alternative, tour, pathTypeModel.PathType, mode, destinationParcel);
            }
        }
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper tour, IEnumerable <dynamic> pathTypeModels, IParcelWrapper destinationParcel, int householdCars, int choice = Constants.DEFAULT_VALUE)
        {
            var household = tour.Household;
            var person    = tour.Person;
            var personDay = tour.PersonDay;

            // household inputs
            var income0To25KFlag        = household.Has0To25KIncome.ToFlag();
            var income25To50KFlag       = household.Has25To50KIncome.ToFlag();
            var income75KPlusFlag       = household.Has75KPlusIncome.ToFlag();
            var onePersonHouseholdFlag  = household.IsOnePersonHousehold.ToFlag();
            var twoPersonHouseholdFlag  = household.IsTwoPersonHousehold.ToFlag();
            var noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(householdCars);
            var carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(householdCars);

            // person inputs
            var childUnder5Flag       = person.IsChildUnder5.ToFlag();
            var adultFlag             = person.IsAdult.ToFlag();
            var drivingAgeStudentFlag = person.IsDrivingAgeStudent.ToFlag();
            var maleFlag = person.IsMale.ToFlag();

            // remaining inputs
            var originParcel           = tour.OriginParcel;
            var destinationParkingCost = destinationParcel.ParkingCostBuffer1(6);

            double escortPercentage;
            double nonEscortPercentage;

            ChoiceModelUtility.SetEscortPercentages(personDay, out escortPercentage, out nonEscortPercentage);

            // school bus is a special case - use HOV3 impedance
            var pathTypeExtra              = pathTypeModels.First(x => x.Mode == Global.Settings.Modes.Hov3);
            var modeExtra                  = Global.Settings.Modes.SchoolBus;
            var availableExtra             = pathTypeExtra.Available;
            var generalizedTimeLogsumExtra = pathTypeExtra.GeneralizedTimeLogsum;

            var alternative = choiceProbabilityCalculator.GetAlternative(modeExtra, availableExtra, choice == modeExtra);

            alternative.Choice = modeExtra;

            alternative.AddNestedAlternative(_nestedAlternativeIds[modeExtra], _nestedAlternativeIndexes[modeExtra], THETA_PARAMETER);

            if (availableExtra)
            {
                //	case Global.Settings.Modes.SchoolBus:
                alternative.AddUtilityTerm(2, generalizedTimeLogsumExtra * tour.TimeCoefficient);
                alternative.AddUtilityTerm(10, 1);
                alternative.AddUtilityTerm(17, childUnder5Flag);
                alternative.AddUtilityTerm(18, adultFlag);
            }

            foreach (var pathTypeModel in pathTypeModels)
            {
                var mode                  = pathTypeModel.Mode;
                var available             = pathTypeModel.Available;
                var generalizedTimeLogsum = pathTypeModel.GeneralizedTimeLogsum;

                alternative        = choiceProbabilityCalculator.GetAlternative(mode, available, choice == mode);
                alternative.Choice = mode;

                alternative.AddNestedAlternative(_nestedAlternativeIds[mode], _nestedAlternativeIndexes[mode], THETA_PARAMETER);

                if (!available)
                {
                    continue;
                }

                alternative.AddUtilityTerm(2, generalizedTimeLogsum * tour.TimeCoefficient);

                if (mode == Global.Settings.Modes.Transit)
                {
                    alternative.AddUtilityTerm(20, 1);
                    alternative.AddUtilityTerm(21, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(22, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(27, childUnder5Flag);
                    alternative.AddUtilityTerm(28, adultFlag);
                    alternative.AddUtilityTerm(29, drivingAgeStudentFlag);
                    alternative.AddUtilityTerm(129, destinationParcel.MixedUse2Index1());
//						alternative.AddUtility(128, destinationParcel.TotalEmploymentDensity1());
//						alternative.AddUtility(127, destinationParcel.NetIntersectionDensity1());
//						alternative.AddUtility(126, originParcel.NetIntersectionDensity1());
//						alternative.AddUtility(125, originParcel.HouseholdDensity1());
//						alternative.AddUtility(124, originParcel.MixedUse2Index1());
//						alternative.AddUtility(123, Math.Log(destinationParcel.StopsTransitBuffer1+1));
                    alternative.AddUtilityTerm(122, Math.Log(originParcel.StopsTransitBuffer1 + 1));
                }
                else if (mode == Global.Settings.Modes.Hov3)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT3));
                    alternative.AddUtilityTerm(30, 1);
                    alternative.AddUtilityTerm(37, twoPersonHouseholdFlag);
                    alternative.AddUtilityTerm(37, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(44, income0To25KFlag);
                    alternative.AddUtilityTerm(45, income25To50KFlag);
                    alternative.AddUtilityTerm(47, childUnder5Flag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Hov2)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT2));
                    alternative.AddUtilityTerm(38, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(40, 1);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(44, income0To25KFlag);
                    alternative.AddUtilityTerm(45, income25To50KFlag);
                    alternative.AddUtilityTerm(47, childUnder5Flag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient));
                    alternative.AddUtilityTerm(50, 1);
                    alternative.AddUtilityTerm(52, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(54, income0To25KFlag);
                    alternative.AddUtilityTerm(56, income75KPlusFlag);
                    alternative.AddUtilityTerm(59, drivingAgeStudentFlag);
                    alternative.AddUtilityTerm(131, escortPercentage);
                    alternative.AddUtilityTerm(132, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    double class1Dist =
                        Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                                                        ? ImpedanceRoster.GetValue("class1distance", mode, Global.Settings.PathTypes.FullNetwork,
                                                                                   Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                                                        : 0;

                    double class2Dist =
                        Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                                                        ? ImpedanceRoster.GetValue("class2distance", mode, Global.Settings.PathTypes.FullNetwork,
                                                                                   Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                                                        : 0;

                    double worstDist =
                        Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                                                        ? ImpedanceRoster.GetValue("worstdistance", mode, Global.Settings.PathTypes.FullNetwork,
                                                                                   Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                                                        : 0;

                    alternative.AddUtilityTerm(60, 1);
                    alternative.AddUtilityTerm(61, maleFlag);
                    alternative.AddUtilityTerm(69, adultFlag);
                    alternative.AddUtilityTerm(169, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(168, destinationParcel.TotalEmploymentDensity1());
//						alternative.AddUtility(167, destinationParcel.NetIntersectionDensity1());
//						alternative.AddUtility(166, originParcel.NetIntersectionDensity1());
//						alternative.AddUtility(165, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(164, originParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(161, (class1Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(162, (class2Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(163, (worstDist > 0).ToFlag());
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    alternative.AddUtilityTerm(79, adultFlag);
                    alternative.AddUtilityTerm(179, destinationParcel.MixedUse4Index1());
//						alternative.AddUtility(178, destinationParcel.TotalEmploymentDensity1());
//						alternative.AddUtility(177, destinationParcel.NetIntersectionDensity1());
//						alternative.AddUtility(176, originParcel.NetIntersectionDensity1());
//						alternative.AddUtility(175, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(179, originParcel.MixedUse4Index1());
                }
            }
        }
Beispiel #9
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITripWrapper trip, IEnumerable <IPathTypeModel> pathTypeModels, IParcelWrapper originParcel, IParcelWrapper destinationParcel, int departureTime, int choice = Constants.DEFAULT_VALUE)
        {
            IHouseholdWrapper household = trip.Household;

            Framework.DomainModels.Models.IHouseholdTotals householdTotals = household.HouseholdTotals;
            IPersonWrapper person = trip.Person;
            ITourWrapper   tour   = trip.Tour;

            Framework.DomainModels.Models.IHalfTour halfTour = trip.HalfTour;

            // household inputs
            int onePersonHouseholdFlag  = household.IsOnePersonHousehold.ToFlag();
            int twoPersonHouseholdFlag  = household.IsTwoPersonHousehold.ToFlag();
            int income0To25KFlag        = household.Has0To25KIncome.ToFlag();
            int income25To45KFlag       = household.Has25To45KIncome.ToFlag();
            int incomeOver100Flag       = household.Has100KPlusIncome.ToFlag();
            int childrenAge5Through15   = householdTotals.ChildrenAge5Through15;
            int nonworkingAdults        = householdTotals.NonworkingAdults;
            int retiredAdults           = householdTotals.RetiredAdults;
            int noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(household.VehiclesAvailable);
            int carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(household.VehiclesAvailable);

            // person inputs
            int drivingAgeStudentFlag = person.IsDrivingAgeStudent.ToFlag();
            int maleFlag          = person.IsMale.ToFlag();
            int ageLessThan35Flag = person.AgeIsLessThan35.ToFlag();

            // tour inputs
            int parkAndRideTourFlag       = tour.IsParkAndRideMode().ToFlag();
            int transitTourFlag           = tour.IsTransitMode().ToFlag();
            int schoolBusTourFlag         = tour.IsSchoolBusMode().ToFlag();
            int hov3TourFlag              = tour.IsHov3Mode().ToFlag();
            int hov2TourFlag              = tour.IsHov2Mode().ToFlag();
            int sovTourFlag               = tour.IsSovMode().ToFlag();
            int bikeTourFlag              = tour.IsBikeMode().ToFlag();
            int walkTourFlag              = tour.IsWalkMode().ToFlag();
            int tncTourFlag               = (tour.Mode == Global.Settings.Modes.PaidRideShare).ToFlag();
            int homeBasedWorkTourFlag     = (tour.IsHomeBasedTour && tour.IsWorkPurpose()).ToFlag();
            int homeBasedSchoolTourFlag   = (tour.IsHomeBasedTour && tour.IsSchoolPurpose()).ToFlag();
            int homeBasedEscortTourFlag   = (tour.IsHomeBasedTour && tour.IsEscortPurpose()).ToFlag();
            int homeBasedShoppingTourFlag = (tour.IsHomeBasedTour && tour.IsShoppingPurpose()).ToFlag();
            int homeBasedMealTourFlag     = (tour.IsHomeBasedTour && tour.IsMealPurpose()).ToFlag();
            int homeBasedSocialTourFlag   = (tour.IsHomeBasedTour && tour.IsSocialPurpose()).ToFlag();
            int notHomeBasedTourFlag      = (!tour.IsHomeBasedTour).ToFlag();
            //            var homeBasedNotWorkSchoolEscortTourFlag = (tour.IsHomeBasedTour && tour.DestinationPurpose > Global.Settings.Purposes.Escort).ToFlag();
            //            var costco = -0.1432 * homeBasedWorkTourFlag - 0.2000 * homeBasedSchoolTourFlag - 0.3000 * homeBasedEscortTourFlag - 0.2000 * homeBasedNotWorkSchoolEscortTourFlag - 0.2000 * notHomeBasedTourFlag;

            // trip inputs
            int originHomeEscortFlag = (trip.IsNoneOrHomePurposeByOrigin() && trip.IsEscortPurposeByDestination()).ToFlag();
            int originWorkEscortFlag = (trip.IsWorkPurposeByOrigin() && trip.IsEscortPurposeByDestination()).ToFlag();

            int destinationHomeEscortFlag = (trip.IsNoneOrHomePurposeByDestination() && trip.IsEscortPurposeByOrigin()).ToFlag();
            int destinationWorkEscortFlag = (trip.IsWorkPurposeByDestination() && trip.IsEscortPurposeByOrigin()).ToFlag();

            // only trip on first half-tour
            int onlyTripOnFirstHalfFlag = (trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips == 1 && trip.IsToTourOrigin).ToFlag();

            // first trip on first half-tour, not only one
            int firstTripOnFirstHalfFlag = (trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips == 1 && !trip.IsToTourOrigin).ToFlag();

            // last trip first half-tour, not only one
            int lastTripOnFirstHalfFlag = (trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips > 1 && trip.IsToTourOrigin).ToFlag();

            // only trip on second half-tour
            int onlyTripOnSecondHalfFlag = (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips == 1 && trip.IsToTourOrigin).ToFlag();

            // first trip on second half-tour, not only one
            int firstTripOnSecondHalfFlag = (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips == 1 && !trip.IsToTourOrigin).ToFlag();

            // last trip second half-tour, not only one
            int lastTripOnSecondHalfFlag = (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips > 1 && trip.IsToTourOrigin).ToFlag();

            // remaining inputs
            double originMixedDensity        = originParcel.MixedUse4Index1();
            double originIntersectionDensity = originParcel.NetIntersectionDensity1();
            double destinationParkingCost    = destinationParcel.ParkingCostBuffer1(2);
            int    amPeriodFlag      = departureTime.IsLeftExclusiveBetween(Global.Settings.Times.SixAM, Global.Settings.Times.TenAM).ToFlag();
            int    middayPeriodFlag  = departureTime.IsLeftExclusiveBetween(Global.Settings.Times.TenAM, Global.Settings.Times.ThreePM).ToFlag();
            int    pmPeriodFlag      = departureTime.IsLeftExclusiveBetween(Global.Settings.Times.ThreePM, Global.Settings.Times.SevenPM).ToFlag();
            int    eveningPeriodFlag = (departureTime > Global.Settings.Times.SevenPM).ToFlag();

            // availability
            bool[] tripModeAvailable = new bool[Global.Settings.Modes.TotalModes + 1];

            bool isLastTripInTour = (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips == 1 && trip.IsToTourOrigin) || (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips > 1 && trip.IsToTourOrigin);
            int  frequencyPreviousTripModeIsTourMode =
                trip.IsHalfTourFromOrigin
                    ? tour.HalfTourFromOrigin.Trips.Where(x => x.Sequence < trip.Sequence).Count(x => tour.Mode == x.Mode)
                    : tour.HalfTourFromOrigin.Trips.Union(tour.HalfTourFromDestination.Trips.Where(x => x.Sequence < trip.Sequence)).Count(x => tour.Mode == x.Mode);

            // if a park and ride tour, only car is available
            int maxAvail = tour.Mode;

            if (tour.Mode == Global.Settings.Modes.ParkAndRide)
            {
                tripModeAvailable[Global.Settings.Modes.Sov]  = tour.Household.VehiclesAvailable > 0 && tour.Person.IsDrivingAge;
                tripModeAvailable[Global.Settings.Modes.Hov2] = !tripModeAvailable[Global.Settings.Modes.Sov];
            }
            // if the last trip of the tour and tour mode not yet used, only the tour mode is available
            else if (isLastTripInTour && frequencyPreviousTripModeIsTourMode == 0 && tour.Mode <= maxAvail)
            {
                tripModeAvailable[tour.Mode] = true;
            }
            else
            {
                // set availability based on tour mode
                for (int mode = Global.Settings.Modes.Walk; mode <= maxAvail; mode++)
                {
                    if (mode != Global.Settings.Modes.ParkAndRide)
                    {
                        tripModeAvailable[mode] = true;
                    }
                }
            }

            {
                // school bus is a special case - use HOV3 impedance and only available for school bus tours
                IPathTypeModel pathTypeExtra              = pathTypeModels.First(x => x.Mode == Global.Settings.Modes.Hov3);
                int            modeExtra                  = Global.Settings.Modes.SchoolBus;
                bool           availableExtra             = pathTypeExtra.Available && tour.IsSchoolBusMode() && tripModeAvailable[modeExtra];
                double         generalizedTimeLogsumExtra = pathTypeExtra.GeneralizedTimeLogsum;

                ChoiceProbabilityCalculator.Alternative alternative = choiceProbabilityCalculator.GetAlternative(modeExtra, availableExtra, choice == modeExtra);
                alternative.Choice = modeExtra;

                alternative.AddNestedAlternative(_nestedAlternativeIds[modeExtra], _nestedAlternativeIndexes[modeExtra], THETA_PARAMETER);

                if (availableExtra)
                {
                    //    case Global.Settings.Modes.SchoolBus:
                    alternative.AddUtilityTerm(2, generalizedTimeLogsumExtra * tour.TimeCoefficient);
                    alternative.AddUtilityTerm(18, 1);
                    alternative.AddUtilityTerm(100, schoolBusTourFlag);
                    alternative.AddUtilityTerm(102, (schoolBusTourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (schoolBusTourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (schoolBusTourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (schoolBusTourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (schoolBusTourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (schoolBusTourFlag * lastTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(112, parkAndRideTourFlag);
                    alternative.AddUtilityTerm(113, transitTourFlag);
                }
            }

            foreach (IPathTypeModel pathTypeModel in pathTypeModels)
            {
                int  mode      = pathTypeModel.Mode;
                bool available = (pathTypeModel.Mode != Global.Settings.Modes.PaidRideShare || Global.Configuration.PaidRideShareModeIsAvailable) &&
                                 pathTypeModel.Available && tripModeAvailable[mode];
                double generalizedTimeLogsum = pathTypeModel.GeneralizedTimeLogsum;

                ChoiceProbabilityCalculator.Alternative alternative = choiceProbabilityCalculator.GetAlternative(mode, available, choice == mode);
                alternative.Choice = mode;

                alternative.AddNestedAlternative(_nestedAlternativeIds[mode], _nestedAlternativeIndexes[mode], THETA_PARAMETER);

                if (!available)
                {
                    continue;
                }

                double modeTimeCoefficient = (Global.Configuration.AV_IncludeAutoTypeChoice && household.OwnsAutomatedVehicles > 0 && mode >= Global.Settings.Modes.Sov && mode <= Global.Settings.Modes.Hov3) ?
                                             tour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) :
                                             (mode == Global.Settings.Modes.PaidRideShare && Global.Configuration.AV_PaidRideShareModeUsesAVs) ?
                                             tour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) : tour.TimeCoefficient;
                alternative.AddUtilityTerm(2, generalizedTimeLogsum * modeTimeCoefficient);


                if (mode == Global.Settings.Modes.Transit)
                {
                    alternative.AddUtilityTerm(20, 1);
                    alternative.AddUtilityTerm(22, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(100, transitTourFlag);
                    alternative.AddUtilityTerm(102, (transitTourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (transitTourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (transitTourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (transitTourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (transitTourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (transitTourFlag * lastTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(136, tncTourFlag);
                }
                else if (mode == Global.Settings.Modes.Hov3)
                {
                    alternative.AddUtilityTerm(30, 1);
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT3));
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    alternative.AddUtilityTerm(34, (nonworkingAdults + retiredAdults));
                    alternative.AddUtilityTerm(36, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(37, twoPersonHouseholdFlag);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(100, hov3TourFlag);
                    alternative.AddUtilityTerm(102, (hov3TourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (hov3TourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (hov3TourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (hov3TourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (hov3TourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (hov3TourFlag * lastTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(114, parkAndRideTourFlag);
                    alternative.AddUtilityTerm(115, transitTourFlag);
                    alternative.AddUtilityTerm(116, schoolBusTourFlag);
                    alternative.AddUtilityTerm(135, tncTourFlag);
                    alternative.AddUtilityTerm(149, homeBasedWorkTourFlag);
                    alternative.AddUtilityTerm(150, homeBasedSchoolTourFlag);
                    alternative.AddUtilityTerm(152, homeBasedEscortTourFlag);
                    alternative.AddUtilityTerm(153, homeBasedShoppingTourFlag);
                    alternative.AddUtilityTerm(154, homeBasedMealTourFlag);
                    alternative.AddUtilityTerm(155, homeBasedSocialTourFlag);
                    alternative.AddUtilityTerm(161, (destinationWorkEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(162, (originWorkEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(163, (originHomeEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(164, (originHomeEscortFlag * middayPeriodFlag));
                    alternative.AddUtilityTerm(165, (originHomeEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(166, (originHomeEscortFlag * eveningPeriodFlag));
                    alternative.AddUtilityTerm(167, (destinationHomeEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(168, (destinationHomeEscortFlag * middayPeriodFlag));
                    alternative.AddUtilityTerm(169, (destinationHomeEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(170, (destinationHomeEscortFlag * eveningPeriodFlag));
                }
                else if (mode == Global.Settings.Modes.Hov2)
                {
                    alternative.AddUtilityTerm(40, 1);
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT2));
                    alternative.AddUtilityTerm(32, (childrenAge5Through15 * (1 - homeBasedEscortTourFlag)));
                    alternative.AddUtilityTerm(34, ((nonworkingAdults + retiredAdults) * (1 - homeBasedEscortTourFlag)));
                    alternative.AddUtilityTerm(38, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(100, hov2TourFlag);
                    alternative.AddUtilityTerm(102, (hov2TourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (hov2TourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (hov2TourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (hov2TourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (hov2TourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (hov2TourFlag * lastTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(118, transitTourFlag);
                    alternative.AddUtilityTerm(119, schoolBusTourFlag);
                    alternative.AddUtilityTerm(120, hov3TourFlag);
                    alternative.AddUtilityTerm(135, tncTourFlag);
                    alternative.AddUtilityTerm(149, homeBasedWorkTourFlag);
                    alternative.AddUtilityTerm(150, homeBasedSchoolTourFlag);
                    alternative.AddUtilityTerm(152, homeBasedEscortTourFlag);
                    alternative.AddUtilityTerm(153, homeBasedShoppingTourFlag);
                    alternative.AddUtilityTerm(154, homeBasedMealTourFlag);
                    alternative.AddUtilityTerm(155, homeBasedSocialTourFlag);
                    alternative.AddUtilityTerm(161, (destinationWorkEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(162, (originWorkEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(163, (originHomeEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(164, (originHomeEscortFlag * middayPeriodFlag));
                    alternative.AddUtilityTerm(165, (originHomeEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(166, (originHomeEscortFlag * eveningPeriodFlag));
                    alternative.AddUtilityTerm(167, (destinationHomeEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(168, (destinationHomeEscortFlag * middayPeriodFlag));
                    alternative.AddUtilityTerm(169, (destinationHomeEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(170, (destinationHomeEscortFlag * eveningPeriodFlag));
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    alternative.AddUtilityTerm(50, 1);
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient));
                    alternative.AddUtilityTerm(52, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(54, income0To25KFlag);
                    alternative.AddUtilityTerm(55, income25To45KFlag);
                    alternative.AddUtilityTerm(59, drivingAgeStudentFlag);
                    alternative.AddUtilityTerm(100, sovTourFlag);
                    alternative.AddUtilityTerm(102, (sovTourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (sovTourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (sovTourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (sovTourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (sovTourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (sovTourFlag * lastTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(121, parkAndRideTourFlag);
                    alternative.AddUtilityTerm(122, transitTourFlag);
                    alternative.AddUtilityTerm(124, hov3TourFlag);
                    alternative.AddUtilityTerm(125, hov2TourFlag);
                    alternative.AddUtilityTerm(134, tncTourFlag);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    alternative.AddUtilityTerm(60, 1);
                    alternative.AddUtilityTerm(61, maleFlag);
                    alternative.AddUtilityTerm(62, ageLessThan35Flag);
                    alternative.AddUtilityTerm(65, originIntersectionDensity);
                    alternative.AddUtilityTerm(100, bikeTourFlag);
                    alternative.AddUtilityTerm(102, (bikeTourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (bikeTourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (bikeTourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (bikeTourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (bikeTourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (bikeTourFlag * lastTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(127, transitTourFlag);
                    alternative.AddUtilityTerm(128, schoolBusTourFlag);
                    alternative.AddUtilityTerm(130, hov2TourFlag);
                    alternative.AddUtilityTerm(131, sovTourFlag);
                    alternative.AddUtilityTerm(133, tncTourFlag);
                    alternative.AddUtilityTerm(147, notHomeBasedTourFlag);
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    alternative.AddUtilityTerm(72, ageLessThan35Flag);
                    alternative.AddUtilityTerm(75, originIntersectionDensity);
                    alternative.AddUtilityTerm(78, originMixedDensity); // origin and destination mixed use measures - geometric avg. - half mile from cell, in 1000s
                    alternative.AddUtilityTerm(100, walkTourFlag);
                    alternative.AddUtilityTerm(102, (walkTourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (walkTourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (walkTourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (walkTourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (walkTourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (walkTourFlag * lastTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(141, homeBasedWorkTourFlag);
                    alternative.AddUtilityTerm(142, homeBasedSchoolTourFlag);
                }
                else if (mode == Global.Settings.Modes.PaidRideShare)
                {
                    alternative.AddUtilityTerm(100, tncTourFlag);
                    alternative.AddUtilityTerm(102, (tncTourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (tncTourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (tncTourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (tncTourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (tncTourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (tncTourFlag * lastTripOnSecondHalfFlag));
                    if (Global.Configuration.PaidRideshare_UseEstimatedInsteadOfAssertedCoefficients)
                    {
                        alternative.AddUtilityTerm(80, 1.0);
                        alternative.AddUtilityTerm(81, noCarsInHouseholdFlag);
                        alternative.AddUtilityTerm(82, carsLessThanDriversFlag);
                        alternative.AddUtilityTerm(83, tour.Person.AgeIsBetween26And35.ToFlag());
                        alternative.AddUtilityTerm(83, tour.Person.AgeIsBetween18And25.ToFlag());
                        alternative.AddUtilityTerm(84, (tour.Person.Age >= 65).ToFlag());
                        alternative.AddUtilityTerm(85, originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2);
                        alternative.AddUtilityTerm(86, destinationParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2);
                        alternative.AddUtilityTerm(87, income0To25KFlag);
                        alternative.AddUtilityTerm(88, incomeOver100Flag);
                        alternative.AddUtilityTerm(89, homeBasedWorkTourFlag);
                        alternative.AddUtilityTerm(90, homeBasedSchoolTourFlag);
                        alternative.AddUtilityTerm(91, homeBasedEscortTourFlag);
                        alternative.AddUtilityTerm(92, homeBasedShoppingTourFlag);
                    }
                    else
                    {
                        double modeConstant = Global.Configuration.AV_PaidRideShareModeUsesAVs
                        ? Global.Configuration.AV_PaidRideShare_ModeConstant
                                              + Global.Configuration.AV_PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2,
                                                                                                                    (Global.Configuration.PaidRideShare_DensityMeasureCapValue > 0) ? Global.Configuration.PaidRideShare_DensityMeasureCapValue : 6000)
                                              + Global.Configuration.AV_PaidRideShare_AVOwnerCoefficient * (household.OwnsAutomatedVehicles > 0).ToFlag()
                        : Global.Configuration.PaidRideShare_ModeConstant
                                              + Global.Configuration.PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2,
                                                                                                                 (Global.Configuration.PaidRideShare_DensityMeasureCapValue > 0) ? Global.Configuration.PaidRideShare_DensityMeasureCapValue : 6000);

                        alternative.AddUtilityTerm(90, modeConstant);
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age26to35Coefficient * tour.Person.AgeIsBetween26And35.ToFlag());
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age18to25Coefficient * tour.Person.AgeIsBetween18And25.ToFlag());
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_AgeOver65Coefficient * (tour.Person.Age >= 65).ToFlag());
                    }
                }
            }
        }
Beispiel #10
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, TripWrapper trip,
                              IEnumerable <dynamic> pathTypeModels, IParcelWrapper originParcel,
                              IParcelWrapper destinationParcel,
                              int choice = Constants.DEFAULT_VALUE)
        {
            var household       = trip.Household;
            var householdTotals = household.HouseholdTotals;
            var person          = trip.Person;
            var tour            = trip.Tour;
            var halfTour        = trip.HalfTour;

            // household inputs
            var onePersonHouseholdFlag  = household.IsOnePersonHousehold.ToFlag();
            var HHwithChildrenFlag      = household.HasChildren.ToFlag();
            var HHwithSmallChildrenFlag = household.HasChildrenUnder5.ToFlag();
            var childrenAge5Through15   = householdTotals.ChildrenAge5Through15;
            var HHwithLowIncomeFlag     = (household.Income >= 300000 && household.Income < 600000).ToFlag();
            var HHwithMidleIncomeFlag   = (household.Income >= 600000 && household.Income < 900000).ToFlag();
            var HHwithHighIncomeFlag    = (household.Income >= 900000).ToFlag();
            var nonworkingAdults        = householdTotals.NonworkingAdults;
            var retiredAdults           = householdTotals.RetiredAdults;
            var twoPersonHouseholdFlag  = household.IsTwoPersonHousehold.ToFlag();
            var noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(household.VehiclesAvailable);
            var carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(household.VehiclesAvailable);

            // person inputs
            //var drivingAgeStudentFlag = person.IsDrivingAgeStudent.ToFlag();
            var maleFlag   = person.IsMale.ToFlag();
            var femaleFlag = person.IsFemale.ToFlag();

            var PTpass = person.TransitPassOwnership;

            //var ageLessThan35Flag = person.AgeIsLessThan35.ToFlag();

            // tour inputs
            var bikeTourFlag        = tour.IsBikeMode().ToFlag();
            var walkTourFlag        = tour.IsWalkMode().ToFlag();
            var CarDrivAloneFlag    = tour.IsSovMode().ToFlag();
            var CarDrivNotAloneFlag = tour.IsHov2Mode().ToFlag();
            var CarPassengerFlag    = tour.IsHov3Mode().ToFlag();
            var transitTourFlag     = tour.IsTransitMode().ToFlag();

            var homeBasedWorkTourFlag                = (tour.IsHomeBasedTour && tour.IsWorkPurpose()).ToFlag();
            var homeBasedSchoolTourFlag              = (tour.IsHomeBasedTour && tour.IsSchoolPurpose()).ToFlag();
            var homeBasedEscortTourFlag              = (tour.IsHomeBasedTour && tour.IsEscortPurpose()).ToFlag();
            var homeBasedShoppingTourFlag            = (tour.IsHomeBasedTour && tour.IsShoppingPurpose()).ToFlag();
            var homeBasedSocialTourFlag              = (tour.IsHomeBasedTour && tour.IsSocialPurpose()).ToFlag();
            var notHomeBasedTourFlag                 = (!tour.IsHomeBasedTour).ToFlag();
            var homeBasedNotWorkSchoolEscortTourFlag =
                (tour.IsHomeBasedTour && tour.DestinationPurpose > Global.Settings.Purposes.Escort).ToFlag();

            var jointTourFlag       = (tour.JointTourSequence > 0) ? 1 : 0;
            var partialHalfTourFlag = (trip.IsHalfTourFromOrigin
                                                           ? tour.PartialHalfTour1Sequence > 0
                                                           : tour.PartialHalfTour2Sequence > 0)
                                                          ? 1
                                                          : 0;
            var fullHalfTourFlag = (trip.IsHalfTourFromOrigin ? tour.FullHalfTour1Sequence > 0 : tour.FullHalfTour2Sequence > 0)
                                                       ? 1
                                                       : 0;


            // trip inputs
            var originHomeEscortFlag = (trip.IsNoneOrHomePurposeByOrigin() && trip.IsEscortPurposeByDestination()).ToFlag();
            var originWorkEscortFlag = (trip.IsWorkPurposeByOrigin() && trip.IsEscortPurposeByDestination()).ToFlag();

            var destinationHomeEscortFlag = (trip.IsNoneOrHomePurposeByDestination() && trip.IsEscortPurposeByOrigin()).ToFlag();
            var destinationWorkEscortFlag = (trip.IsWorkPurposeByDestination() && trip.IsEscortPurposeByOrigin()).ToFlag();

            // only trip on first half-tour
            var onlyTripOnFirstHalfFlag =
                (trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips == 1 && trip.IsToTourOrigin).ToFlag();

            // first trip on first half-tour, not only one
            var firstTripOnFirstHalfFlag =
                (trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips == 1 && !trip.IsToTourOrigin).ToFlag();

            // last trip first half-tour, not only one
            var lastTripOnFirstHalfFlag =
                (trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips > 1 && trip.IsToTourOrigin).ToFlag();

            // only trip on second half-tour
            var onlyTripOnSecondHalfFlag =
                (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips == 1 && trip.IsToTourOrigin).ToFlag();

            // first trip on second half-tour, not only one
            var firstTripOnSecondHalfFlag =
                (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips == 1 && !trip.IsToTourOrigin).ToFlag();

            // last trip second half-tour, not only one
            var lastTripOnSecondHalfFlag =
                (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips > 1 && trip.IsToTourOrigin).ToFlag();

            // remaining inputs
            var departureTime = trip.IsHalfTourFromOrigin ? trip.LatestDepartureTime : trip.EarliestDepartureTime;

            var originMixedDensity        = originParcel.MixedUse4Index1();
            var originIntersectionDensity = originParcel.NetIntersectionDensity1();
            var destinationParkingCost    = destinationParcel.ParkingCostBuffer1(2);
            var amPeriodFlag = departureTime.IsLeftExclusiveBetween(Global.Settings.Times.SixAM, Global.Settings.Times.NineAM).ToFlag();
            //GV changed to 6-9 am
            var middayPeriodFlag = departureTime.IsLeftExclusiveBetween(Global.Settings.Times.NineAM, Global.Settings.Times.ThreePM).ToFlag();
            var pmPeriodFlag     = departureTime.IsLeftExclusiveBetween(Global.Settings.Times.ThreePM, Global.Settings.Times.SixPM).ToFlag();
            //GV changed to 3-6 pm
            var eveningPeriodFlag = (departureTime > Global.Settings.Times.SixPM).ToFlag();             //GV changed to 6 pm

            // availability
            var tripModeAvailable = new bool[Global.Settings.Modes.TotalModes];

            var isLastTripInTour = (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips >= 1 && trip.IsToTourOrigin);
            var frequencyPreviousTripModeIsTourMode = 0;

            if (trip.IsHalfTourFromOrigin)
            {
                frequencyPreviousTripModeIsTourMode +=
                    tour.HalfTourFromOrigin.Trips.Where(x => x.Sequence < trip.Sequence).Count(x => tour.Mode == x.Mode);
            }
            else
            {
                if (tour.HalfTourFromOrigin != null)
                {
                    frequencyPreviousTripModeIsTourMode +=
                        tour.HalfTourFromOrigin.Trips.Where(x => x.Sequence > 0).Count(x => tour.Mode == x.Mode);
                }
                frequencyPreviousTripModeIsTourMode +=
                    tour.HalfTourFromDestination.Trips.Where(x => x.Sequence < trip.Sequence).Count(x => tour.Mode == x.Mode);
            }

            // GV commented out park and ride
            //// if a park and ride tour, only car is available
            //if (tour.Mode == Global.Settings.Modes.ParkAndRide) {
            //	tripModeAvailable[Global.Settings.Modes.Sov] = tour.Household.VehiclesAvailable > 0 && tour.Person.IsDrivingAge;
            //	tripModeAvailable[Global.Settings.Modes.Hov2] = !tripModeAvailable[Global.Settings.Modes.Sov];
            //}
            //// if the last trip of the tour and tour mode not yet used, only the tour mode is available
            if (isLastTripInTour && frequencyPreviousTripModeIsTourMode == 0)
            {
                tripModeAvailable[tour.Mode] = true;
            }
            else
            {
                // set availability based on tour mode
                for (var mode = Global.Settings.Modes.Walk; mode <= tour.Mode; mode++)
                {
                    tripModeAvailable[mode] = true;
                }
            }
            if (person.Age < 18)
            {
                tripModeAvailable[Global.Settings.Modes.Sov]       = false;
                tripModeAvailable[Global.Settings.Modes.HovDriver] = false;
            }

            // GV commented out School Bus
            // school bus is a special case - use HOV3 impedance and only available for school bus tours
            //var pathTypeExtra = pathTypeModels.First(x => x.Mode == Global.Settings.Modes.Hov3);
            //const int modeExtra = Global.Settings.Modes.SchoolBus;
            //var availableExtra = pathTypeExtra.Available && tour.IsSchoolBusMode && tripModeAvailable[modeExtra]
            //	 && (trip.IsHalfTourFromOrigin
            //		  ? trip.LatestDepartureTime - pathTypeExtra.PathTime >= trip.ArrivalTimeLimit
            //		  : trip.EarliestDepartureTime + pathTypeExtra.PathTime <= trip.ArrivalTimeLimit);
            //var generalizedTimeLogsumExtra = pathTypeExtra.GeneralizedTimeLogsum;

            //var alternative = choiceProbabilityCalculator.GetAlternative(modeExtra, availableExtra, choice == modeExtra);
            //alternative.Choice = modeExtra;

            //alternative.AddNestedAlternative(_nestedAlternativeIds[modeExtra], _nestedAlternativeIndexes[modeExtra], THETA_PARAMETER);
            //
            //if (availableExtra) {
            //	//	case Global.Settings.Modes.SchoolBus:
            //	alternative.AddUtilityTerm(2, generalizedTimeLogsumExtra * tour.TimeCoefficient);
            //	alternative.AddUtilityTerm(18, 1);
            //	alternative.AddUtilityTerm(100, schoolBusTourFlag);
            //	alternative.AddUtilityTerm(102, (schoolBusTourFlag * onlyTripOnFirstHalfFlag));
            //	alternative.AddUtilityTerm(103, (schoolBusTourFlag * onlyTripOnSecondHalfFlag));
            //	alternative.AddUtilityTerm(104, (schoolBusTourFlag * firstTripOnFirstHalfFlag));
            //	alternative.AddUtilityTerm(105, (schoolBusTourFlag * firstTripOnSecondHalfFlag));
            //	alternative.AddUtilityTerm(106, (schoolBusTourFlag * lastTripOnFirstHalfFlag));
            //	alternative.AddUtilityTerm(107, (schoolBusTourFlag * lastTripOnSecondHalfFlag));
            //	alternative.AddUtilityTerm(112, parkAndRideTourFlag);
            //	alternative.AddUtilityTerm(113, transitTourFlag);
            //}

            ChoiceProbabilityCalculator.Alternative alternative;

            foreach (var pathTypeModel in pathTypeModels)
            {
                var mode      = pathTypeModel.Mode;
                var available = pathTypeModel.Available && tripModeAvailable[mode] &&
                                (trip.IsHalfTourFromOrigin
                                                            ? trip.LatestDepartureTime - pathTypeModel.PathTime >= trip.ArrivalTimeLimit
                                                            : trip.EarliestDepartureTime + pathTypeModel.PathTime <= trip.ArrivalTimeLimit);
                var generalizedTimeLogsum = pathTypeModel.GeneralizedTimeLogsum;

                alternative        = choiceProbabilityCalculator.GetAlternative(mode - 1, available, choice == mode);
                alternative.Choice = mode;

                // GV for tree Logit uncomment this
                alternative.AddNestedAlternative(_nestedAlternativeIds[mode], _nestedAlternativeIndexes[mode], THETA_PARAMETER);

                if (!available)
                {
                    continue;
                }

                alternative.AddUtilityTerm(2, generalizedTimeLogsum * tour.TimeCoefficient);

                if (mode == Global.Settings.Modes.Transit)
                {
                    alternative.AddUtilityTerm(20, 1);
                    alternative.AddUtilityTerm(22, carsLessThanDriversFlag);

                    alternative.AddUtilityTerm(54, HHwithLowIncomeFlag);
                    alternative.AddUtilityTerm(55, HHwithMidleIncomeFlag);
                    alternative.AddUtilityTerm(56, HHwithHighIncomeFlag);

                    alternative.AddUtilityTerm(57, PTpass);

                    alternative.AddUtilityTerm(100, transitTourFlag);
                    alternative.AddUtilityTerm(102, (transitTourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (transitTourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (transitTourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (transitTourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (transitTourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (transitTourFlag * lastTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(181, jointTourFlag);
                    alternative.AddUtilityTerm(182, fullHalfTourFlag + partialHalfTourFlag);
                }
                else if (mode == Global.Settings.Modes.HovPassenger)
                {
                    alternative.AddUtilityTerm(30, 1);
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT3));
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    alternative.AddUtilityTerm(33, femaleFlag);
                    alternative.AddUtilityTerm(34, (nonworkingAdults + retiredAdults));
                    alternative.AddUtilityTerm(36, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(37, twoPersonHouseholdFlag);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(100, CarPassengerFlag);
                    alternative.AddUtilityTerm(102, (CarPassengerFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (CarPassengerFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (CarPassengerFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (CarPassengerFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (CarPassengerFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (CarPassengerFlag * lastTripOnSecondHalfFlag));

                    alternative.AddUtilityTerm(115, transitTourFlag);

                    alternative.AddUtilityTerm(149, homeBasedWorkTourFlag);
                    alternative.AddUtilityTerm(150, homeBasedSchoolTourFlag);
                    alternative.AddUtilityTerm(152, homeBasedEscortTourFlag);
                    alternative.AddUtilityTerm(153, homeBasedShoppingTourFlag);
                    alternative.AddUtilityTerm(155, homeBasedSocialTourFlag);

                    alternative.AddUtilityTerm(161, (destinationWorkEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(162, (originWorkEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(163, (originHomeEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(164, (originHomeEscortFlag * middayPeriodFlag));
                    alternative.AddUtilityTerm(165, (originHomeEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(166, (originHomeEscortFlag * eveningPeriodFlag));
                    alternative.AddUtilityTerm(167, (destinationHomeEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(168, (destinationHomeEscortFlag * middayPeriodFlag));
                    alternative.AddUtilityTerm(169, (destinationHomeEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(170, (destinationHomeEscortFlag * eveningPeriodFlag));
                    alternative.AddUtilityTerm(183, jointTourFlag);
                    alternative.AddUtilityTerm(184, fullHalfTourFlag + partialHalfTourFlag);
                }
                else if (mode == Global.Settings.Modes.HovDriver)
                {
                    alternative.AddUtilityTerm(40, 1);
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT2));
                    alternative.AddUtilityTerm(42, (childrenAge5Through15 * (1 - homeBasedEscortTourFlag)));
                    alternative.AddUtilityTerm(43, (femaleFlag * (1 - homeBasedEscortTourFlag)));
                    alternative.AddUtilityTerm(44, ((nonworkingAdults + retiredAdults) * (1 - homeBasedEscortTourFlag)));
                    alternative.AddUtilityTerm(38, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(100, CarDrivNotAloneFlag);
                    alternative.AddUtilityTerm(102, (CarDrivNotAloneFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (CarDrivNotAloneFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (CarDrivNotAloneFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (CarDrivNotAloneFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (CarDrivNotAloneFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (CarDrivNotAloneFlag * lastTripOnSecondHalfFlag));

                    alternative.AddUtilityTerm(118, transitTourFlag);
                    alternative.AddUtilityTerm(120, CarPassengerFlag);

                    alternative.AddUtilityTerm(149, homeBasedWorkTourFlag);
                    alternative.AddUtilityTerm(150, homeBasedSchoolTourFlag);
                    alternative.AddUtilityTerm(152, homeBasedEscortTourFlag);
                    alternative.AddUtilityTerm(153, homeBasedShoppingTourFlag);
                    alternative.AddUtilityTerm(155, homeBasedSocialTourFlag);

                    alternative.AddUtilityTerm(161, (destinationWorkEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(162, (originWorkEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(163, (originHomeEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(164, (originHomeEscortFlag * middayPeriodFlag));
                    alternative.AddUtilityTerm(165, (originHomeEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(166, (originHomeEscortFlag * eveningPeriodFlag));
                    alternative.AddUtilityTerm(167, (destinationHomeEscortFlag * amPeriodFlag));
                    alternative.AddUtilityTerm(168, (destinationHomeEscortFlag * middayPeriodFlag));
                    alternative.AddUtilityTerm(169, (destinationHomeEscortFlag * pmPeriodFlag));
                    alternative.AddUtilityTerm(170, (destinationHomeEscortFlag * eveningPeriodFlag));
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    alternative.AddUtilityTerm(50, 1);
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient));
                    alternative.AddUtilityTerm(52, carsLessThanDriversFlag);

                    alternative.AddUtilityTerm(100, CarDrivAloneFlag);
                    alternative.AddUtilityTerm(102, (CarDrivAloneFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (CarDrivAloneFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (CarDrivAloneFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (CarDrivAloneFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (CarDrivAloneFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (CarDrivAloneFlag * lastTripOnSecondHalfFlag));

                    alternative.AddUtilityTerm(122, transitTourFlag);
                    alternative.AddUtilityTerm(124, CarPassengerFlag);
                    alternative.AddUtilityTerm(125, CarDrivNotAloneFlag);

                    alternative.AddUtilityTerm(126, maleFlag);

                    alternative.AddUtilityTerm(185, jointTourFlag);
                    alternative.AddUtilityTerm(186, fullHalfTourFlag + partialHalfTourFlag);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    alternative.AddUtilityTerm(60, 1);
                    //alternative.AddUtilityTerm(65, originIntersectionDensity);

                    alternative.AddUtilityTerm(100, bikeTourFlag);
                    alternative.AddUtilityTerm(102, (bikeTourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (bikeTourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (bikeTourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (bikeTourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (bikeTourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (bikeTourFlag * lastTripOnSecondHalfFlag));

                    alternative.AddUtilityTerm(127, transitTourFlag);
                    alternative.AddUtilityTerm(130, CarDrivNotAloneFlag);
                    alternative.AddUtilityTerm(131, CarDrivAloneFlag);

                    alternative.AddUtilityTerm(132, maleFlag);

                    alternative.AddUtilityTerm(147, notHomeBasedTourFlag);
                    alternative.AddUtilityTerm(187, jointTourFlag);
                    alternative.AddUtilityTerm(188, fullHalfTourFlag + partialHalfTourFlag);
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    alternative.AddUtilityTerm(70, 1);

                    //alternative.AddUtilityTerm(75, originIntersectionDensity);
                    alternative.AddUtilityTerm(78, originMixedDensity);
                    // origin and destination mixed use measures - geometric avg. - half mile from cell, in 1000s

                    alternative.AddUtilityTerm(100, walkTourFlag);
                    alternative.AddUtilityTerm(102, (walkTourFlag * onlyTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(103, (walkTourFlag * onlyTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(104, (walkTourFlag * firstTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(105, (walkTourFlag * firstTripOnSecondHalfFlag));
                    alternative.AddUtilityTerm(106, (walkTourFlag * lastTripOnFirstHalfFlag));
                    alternative.AddUtilityTerm(107, (walkTourFlag * lastTripOnSecondHalfFlag));

                    alternative.AddUtilityTerm(141, homeBasedWorkTourFlag);
                    alternative.AddUtilityTerm(142, homeBasedSchoolTourFlag);

                    alternative.AddUtilityTerm(187, jointTourFlag);
                    alternative.AddUtilityTerm(188, fullHalfTourFlag + partialHalfTourFlag);
                }
            }
        }
Beispiel #11
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper tour, IEnumerable <IPathTypeModel> pathTypeModels, IParcelWrapper destinationParcel, int householdCars, int choice = Constants.DEFAULT_VALUE)
        {
            var household = tour.Household;
            var person    = tour.Person;
            var personDay = tour.PersonDay;

            // household inputs
            var income0To25KFlag        = household.Has0To25KIncome.ToFlag();
            var income25To50KFlag       = household.Has25To50KIncome.ToFlag();
            var income75KPlusFlag       = household.Has75KPlusIncome.ToFlag();
            var onePersonHouseholdFlag  = household.IsOnePersonHousehold.ToFlag();
            var twoPersonHouseholdFlag  = household.IsTwoPersonHousehold.ToFlag();
            var noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(householdCars);
            var carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(householdCars);

            // person inputs
            var childUnder5Flag       = person.IsChildUnder5.ToFlag();
            var adultFlag             = person.IsAdult.ToFlag();
            var drivingAgeStudentFlag = person.IsDrivingAgeStudent.ToFlag();
            var maleFlag = person.IsMale.ToFlag();

            // remaining inputs
            var originParcel           = tour.OriginParcel;
            var destinationParkingCost = destinationParcel.ParkingCostBuffer1(6);

            double escortPercentage;
            double nonEscortPercentage;

            ChoiceModelUtility.SetEscortPercentages(personDay, out escortPercentage, out nonEscortPercentage);

            // school bus is a special case - use HOV3 impedance
            {
                var pathTypeExtra              = pathTypeModels.First(x => x.Mode == Global.Settings.Modes.Hov3);
                var modeExtra                  = Global.Settings.Modes.SchoolBus;
                var availableExtra             = pathTypeExtra.Available;
                var generalizedTimeLogsumExtra = pathTypeExtra.GeneralizedTimeLogsum;

                var alternative = choiceProbabilityCalculator.GetAlternative(modeExtra, availableExtra, choice == modeExtra);
                alternative.Choice = modeExtra;

                alternative.AddNestedAlternative(_nestedAlternativeIds[modeExtra], _nestedAlternativeIndexes[modeExtra], THETA_PARAMETER);

                if (availableExtra)
                {
                    //    case Global.Settings.Modes.SchoolBus:
                    alternative.AddUtilityTerm(2, generalizedTimeLogsumExtra * tour.TimeCoefficient);
                    alternative.AddUtilityTerm(10, 1);
                    alternative.AddUtilityTerm(17, childUnder5Flag);
                    alternative.AddUtilityTerm(18, adultFlag);
                }
            }

            // paidRideShare is another special case
            if (Global.Configuration.PaidRideShareModeIsAvailable)
            {
                var pathTypeExtra              = pathTypeModels.First(x => x.Mode == Global.Settings.Modes.PaidRideShare);
                var modeExtra                  = Global.Settings.Modes.PaidRideShare;
                var availableExtra             = pathTypeExtra.Available;
                var generalizedTimeLogsumExtra = pathTypeExtra.GeneralizedTimeLogsum;
                var distanceExtra              = pathTypeExtra.PathDistance;

                var alternative = choiceProbabilityCalculator.GetAlternative(modeExtra - 2, availableExtra, choice == modeExtra);
                alternative.Choice = modeExtra;

                alternative.AddNestedAlternative(_nestedAlternativeIds[modeExtra], _nestedAlternativeIndexes[modeExtra], THETA_PARAMETER);

                if (availableExtra)
                {
                    var extraCostPerMile = Global.Configuration.AV_PaidRideShareModeUsesAVs ?
                                           Global.Configuration.AV_PaidRideShare_ExtraCostPerDistanceUnit : Global.Configuration.PaidRideShare_ExtraCostPerDistanceUnit;
                    var fixedCostPerRide = Global.Configuration.AV_PaidRideShareModeUsesAVs ?
                                           Global.Configuration.AV_PaidRideShare_FixedCostPerRide : Global.Configuration.PaidRideShare_FixedCostPerRide;
                    var autoTimeCoefficient = Global.Configuration.AV_PaidRideShareModeUsesAVs ?
                                              tour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) : tour.TimeCoefficient;
                    alternative.AddUtilityTerm(2, generalizedTimeLogsumExtra * autoTimeCoefficient);
                    alternative.AddUtilityTerm(2, distanceExtra * extraCostPerMile * tour.CostCoefficient);
                    alternative.AddUtilityTerm(2, fixedCostPerRide * tour.CostCoefficient);

                    var modeConstant = Global.Configuration.AV_PaidRideShareModeUsesAVs
                      ? Global.Configuration.AV_PaidRideShare_ModeConstant
                                       + Global.Configuration.AV_PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2, 6000)
                                       + Global.Configuration.AV_PaidRideShare_AVOwnerCoefficient * (household.OwnsAutomatedVehicles > 0).ToFlag()
                      : Global.Configuration.PaidRideShare_ModeConstant
                                       + Global.Configuration.PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2, 6000);

                    alternative.AddUtilityTerm(90, modeConstant);
                    alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age26to35Coefficient * tour.Person.AgeIsBetween26And35.ToFlag());
                    alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age18to25Coefficient * tour.Person.AgeIsBetween18And25.ToFlag());
                    alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_AgeOver65Coefficient * (tour.Person.Age < 16).ToFlag());
                }
            }


            foreach (var pathTypeModel in pathTypeModels)
            {
                var mode                  = pathTypeModel.Mode;
                var available             = pathTypeModel.Available;
                var generalizedTimeLogsum = pathTypeModel.GeneralizedTimeLogsum;

                var alternative = choiceProbabilityCalculator.GetAlternative(mode, available, choice == mode);
                alternative.Choice = mode;

                alternative.AddNestedAlternative(_nestedAlternativeIds[mode], _nestedAlternativeIndexes[mode], THETA_PARAMETER);

                if (!available)
                {
                    continue;
                }

                var modeTimeCoefficient = (Global.Configuration.AV_IncludeAutoTypeChoice && household.OwnsAutomatedVehicles > 0 && mode >= Global.Settings.Modes.Sov && mode <= Global.Settings.Modes.Hov3) ?
                                          tour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) : tour.TimeCoefficient;
                alternative.AddUtilityTerm(2, generalizedTimeLogsum * modeTimeCoefficient);


                if (mode == Global.Settings.Modes.Transit)
                {
                    alternative.AddUtilityTerm(20, 1);
                    alternative.AddUtilityTerm(21, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(22, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(27, childUnder5Flag);
                    alternative.AddUtilityTerm(28, adultFlag);
                    alternative.AddUtilityTerm(29, drivingAgeStudentFlag);
                    alternative.AddUtilityTerm(129, destinationParcel.MixedUse2Index1());
                    //                        alternative.AddUtilityTerm(128, destinationParcel.TotalEmploymentDensity1());
                    //                        alternative.AddUtilityTerm(127, destinationParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtilityTerm(126, originParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtilityTerm(125, originParcel.HouseholdDensity1());
                    //                        alternative.AddUtilityTerm(124, originParcel.MixedUse2Index1());
                    //                        alternative.AddUtilityTerm(123, Math.Log(destinationParcel.StopsTransitBuffer1+1));
                    alternative.AddUtilityTerm(122, Math.Log(originParcel.StopsTransitBuffer1 + 1));
                }
                else if (mode == Global.Settings.Modes.Hov3)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT3));
                    alternative.AddUtilityTerm(30, 1);
                    alternative.AddUtilityTerm(37, twoPersonHouseholdFlag);
                    alternative.AddUtilityTerm(37, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(44, income0To25KFlag);
                    alternative.AddUtilityTerm(45, income25To50KFlag);
                    alternative.AddUtilityTerm(47, childUnder5Flag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Hov2)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / ChoiceModelUtility.CPFACT2));
                    alternative.AddUtilityTerm(38, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(40, 1);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(44, income0To25KFlag);
                    alternative.AddUtilityTerm(45, income25To50KFlag);
                    alternative.AddUtilityTerm(47, childUnder5Flag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient));
                    alternative.AddUtilityTerm(50, 1);
                    alternative.AddUtilityTerm(52, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(54, income0To25KFlag);
                    alternative.AddUtilityTerm(56, income75KPlusFlag);
                    alternative.AddUtilityTerm(59, drivingAgeStudentFlag);
                    alternative.AddUtilityTerm(131, escortPercentage);
                    alternative.AddUtilityTerm(132, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    double class1Dist =
                        Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                            ? ImpedanceRoster.GetValue("class1distance", mode, Global.Settings.PathTypes.FullNetwork,
                                                       Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                            : 0;

                    double class2Dist =
                        Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                            ? ImpedanceRoster.GetValue("class2distance", mode, Global.Settings.PathTypes.FullNetwork,
                                                       Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                            : 0;

                    double worstDist =
                        Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                            ? ImpedanceRoster.GetValue("worstdistance", mode, Global.Settings.PathTypes.FullNetwork,
                                                       Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                            : 0;

                    alternative.AddUtilityTerm(60, 1);
                    alternative.AddUtilityTerm(61, maleFlag);
                    alternative.AddUtilityTerm(69, adultFlag);
                    alternative.AddUtilityTerm(169, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(168, destinationParcel.TotalEmploymentDensity1());
                    //                        alternative.AddUtilityTerm(167, destinationParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtilityTerm(166, originParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtilityTerm(165, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(164, originParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(161, (class1Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(162, (class2Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(163, (worstDist > 0).ToFlag());
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    alternative.AddUtilityTerm(70, 1);
                    alternative.AddUtilityTerm(79, adultFlag);
                    alternative.AddUtilityTerm(179, destinationParcel.MixedUse4Index1());
                    //                        alternative.AddUtilityTerm(178, destinationParcel.TotalEmploymentDensity1());
                    //                        alternative.AddUtilityTerm(177, destinationParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtilityTerm(176, originParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtilityTerm(175, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(179, originParcel.MixedUse4Index1());
                }

                RegionSpecificCustomizations(alternative, tour, pathTypeModel.PathType, mode, destinationParcel);
            }
        }
Beispiel #12
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper subtour, IEnumerable <IPathTypeModel> pathTypeModels, IParcelWrapper destinationParcel, int parentTourMode, int choice = Constants.DEFAULT_VALUE)
        {
            IHouseholdWrapper household = subtour.Household;
            IPersonWrapper    person    = subtour.Person;
            IPersonDayWrapper personDay = subtour.PersonDay;

            // household inputs
            int income0To25KFlag  = household.Has0To25KIncome.ToFlag();
            int income25To50KFlag = household.Has25To50KIncome.ToFlag();
            int incomeOver100Flag = household.Has100KPlusIncome.ToFlag();

            // person inputs
            int maleFlag = person.IsMale.ToFlag();

            // tour inputs
            int sovTourFlag  = (parentTourMode == Global.Settings.Modes.Sov).ToFlag();
            int hov2TourFlag = (parentTourMode == Global.Settings.Modes.Hov2).ToFlag();
            int bikeTourFlag = (parentTourMode == Global.Settings.Modes.Bike).ToFlag();
            int walkTourFlag = (parentTourMode == Global.Settings.Modes.Walk).ToFlag();
            int tncTourFlag  = (parentTourMode == Global.Settings.Modes.PaidRideShare).ToFlag();

            // remaining inputs
            IParcelWrapper originParcel           = subtour.OriginParcel;
            int            parkingDuration        = ChoiceModelUtility.GetParkingDuration(person.IsFulltimeWorker);
            double         destinationParkingCost = destinationParcel.ParkingCostBuffer1(parkingDuration);

            ChoiceModelUtility.SetEscortPercentages(personDay, out double escortPercentage, out double nonEscortPercentage, true);


            foreach (IPathTypeModel pathTypeModel in pathTypeModels)
            {
                int  mode      = pathTypeModel.Mode;
                bool available = (pathTypeModel.Mode != Global.Settings.Modes.PaidRideShare || Global.Configuration.PaidRideShareModeIsAvailable) &&
                                 pathTypeModel.Available;
                double generalizedTimeLogsum = pathTypeModel.GeneralizedTimeLogsum;

                ChoiceProbabilityCalculator.Alternative alternative = choiceProbabilityCalculator.GetAlternative(mode, available, choice == mode);
                alternative.Choice = mode;

                alternative.AddNestedAlternative(_nestedAlternativeIds[pathTypeModel.Mode], _nestedAlternativeIndexes[pathTypeModel.Mode], THETA_PARAMETER);

                if (!available)
                {
                    continue;
                }

                double modeTimeCoefficient = (Global.Configuration.AV_IncludeAutoTypeChoice && household.OwnsAutomatedVehicles > 0 && mode >= Global.Settings.Modes.Sov && mode <= Global.Settings.Modes.Hov3) ?
                                             subtour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) :
                                             (mode == Global.Settings.Modes.PaidRideShare && Global.Configuration.AV_PaidRideShareModeUsesAVs) ?
                                             subtour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) : subtour.TimeCoefficient;
                alternative.AddUtilityTerm(2, generalizedTimeLogsum * modeTimeCoefficient);


                if (mode == Global.Settings.Modes.Transit)
                {
                    alternative.AddUtilityTerm(20, 1);
                    //                        alternative.AddUtility(129, destinationParcel.MixedUse2Index1());
                    //                        alternative.AddUtility(128, destinationParcel.TotalEmploymentDensity1());
                    //                        alternative.AddUtility(127, destinationParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(126, originParcel.NetIntersectionDensity1());
                    //                        alternative.AddUtility(125, originParcel.TotalEmploymentDensity1());
                    //                        alternative.AddUtility(124, originParcel.MixedUse2Index1());
                    //                        alternative.AddUtility(123, Math.Log(destinationParcel.StopsTransitBuffer1+1));
                    //                        alternative.AddUtility(122, Math.Log(originParcel.StopsTransitBuffer1+1));
                }
                else if (mode == Global.Settings.Modes.Hov3)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * subtour.CostCoefficient / ChoiceModelUtility.CPFACT3));
                    alternative.AddUtilityTerm(30, 1);
                    alternative.AddUtilityTerm(88, sovTourFlag);
                    alternative.AddUtilityTerm(89, hov2TourFlag);
                }
                else if (mode == Global.Settings.Modes.Hov2)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * subtour.CostCoefficient / ChoiceModelUtility.CPFACT2));
                    alternative.AddUtilityTerm(40, 1);
                    alternative.AddUtilityTerm(88, sovTourFlag);
                    alternative.AddUtilityTerm(89, hov2TourFlag);
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * subtour.CostCoefficient));
                    alternative.AddUtilityTerm(50, 1);
                    alternative.AddUtilityTerm(54, income0To25KFlag);
                    alternative.AddUtilityTerm(55, income25To50KFlag);
                    alternative.AddUtilityTerm(58, sovTourFlag);
                    alternative.AddUtilityTerm(59, hov2TourFlag);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    alternative.AddUtilityTerm(60, 1);
                    alternative.AddUtilityTerm(61, maleFlag);
                    alternative.AddUtilityTerm(69, bikeTourFlag);
                    alternative.AddUtilityTerm(169, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(168, destinationParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(167, destinationParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(166, originParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(165, originParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(164, originParcel.MixedUse4Index1());
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    alternative.AddUtilityTerm(70, 1);
                    alternative.AddUtilityTerm(79, walkTourFlag);
                    alternative.AddUtilityTerm(179, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(178, destinationParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(177, destinationParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(176, originParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(175, originParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(174, originParcel.MixedUse4Index1());
                }
                else if (mode == Global.Settings.Modes.PaidRideShare)
                {
                    if (Global.Configuration.PaidRideshare_UseEstimatedInsteadOfAssertedCoefficients)
                    {
                        alternative.AddUtilityTerm(80, 1.0);
                        alternative.AddUtilityTerm(81, sovTourFlag);
                        alternative.AddUtilityTerm(82, walkTourFlag + bikeTourFlag);
                        alternative.AddUtilityTerm(83, subtour.Person.AgeIsBetween26And35.ToFlag());
                        alternative.AddUtilityTerm(83, subtour.Person.AgeIsBetween18And25.ToFlag());
                        alternative.AddUtilityTerm(84, tncTourFlag);
                        alternative.AddUtilityTerm(85, originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2);
                        alternative.AddUtilityTerm(86, income0To25KFlag);
                        alternative.AddUtilityTerm(87, incomeOver100Flag);
                    }
                    else
                    {
                        double modeConstant = Global.Configuration.AV_PaidRideShareModeUsesAVs
                        ? Global.Configuration.AV_PaidRideShare_ModeConstant
                                              + Global.Configuration.AV_PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2,
                                                                                                                    (Global.Configuration.PaidRideShare_DensityMeasureCapValue > 0) ? Global.Configuration.PaidRideShare_DensityMeasureCapValue : 6000)
                                              + Global.Configuration.AV_PaidRideShare_AVOwnerCoefficient * (household.OwnsAutomatedVehicles > 0).ToFlag()
                        : Global.Configuration.PaidRideShare_ModeConstant
                                              + Global.Configuration.PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2,
                                                                                                                 (Global.Configuration.PaidRideShare_DensityMeasureCapValue > 0) ? Global.Configuration.PaidRideShare_DensityMeasureCapValue : 6000);

                        alternative.AddUtilityTerm(90, modeConstant);
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age26to35Coefficient * subtour.Person.AgeIsBetween26And35.ToFlag());
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age18to25Coefficient * subtour.Person.AgeIsBetween18And25.ToFlag());
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_AgeOver65Coefficient * (subtour.Person.Age >= 65).ToFlag());
                    }
                }
            }
        }
Beispiel #13
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper tour, IEnumerable <PathTypeModel> pathTypeModels, IParcelWrapper destinationParcel, int householdCars, int choice = Constants.DEFAULT_VALUE)
        {
            var household       = tour.Household;
            var householdTotals = household.HouseholdTotals;
            var personDay       = tour.PersonDay;
            var person          = tour.Person;

            // household inputs
            var childrenUnder5        = householdTotals.ChildrenUnder5;
            var childrenAge5Through15 = householdTotals.ChildrenAge5Through15;
            //			var nonworkingAdults = householdTotals.NonworkingAdults;
            //			var retiredAdults = householdTotals.RetiredAdults;
            var onePersonHouseholdFlag  = household.IsOnePersonHousehold.ToFlag();
            var twoPersonHouseholdFlag  = household.IsTwoPersonHousehold.ToFlag();
            var noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(householdCars);
            var carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(householdCars);
            var carsLessThanWorkersFlag = household.GetFlagForCarsLessThanWorkers(householdCars);
            var income0To25KFlag        = household.Has0To25KIncome.ToFlag();

            // person inputs
            var maleFlag = person.IsMale.ToFlag();
            var ageBetween51And98Flag = person.AgeIsBetween51And98.ToFlag();

            var originParcel    = tour.OriginParcel;
            var parkingDuration = ChoiceModelUtility.GetParkingDuration(person.IsFulltimeWorker);
            // parking at work is free if no paid parking at work and tour goes to usual workplace
            var destinationParkingCost = (Global.Configuration.ShouldRunPayToParkAtWorkplaceModel && tour.Person.UsualWorkParcel != null &&
                                          destinationParcel == tour.Person.UsualWorkParcel && person.PaidParkingAtWorkplace == 0) ? 0.0 : destinationParcel.ParkingCostBuffer1(parkingDuration);

            double escortPercentage;
            double nonEscortPercentage;

            ChoiceModelUtility.SetEscortPercentages(personDay, out escortPercentage, out nonEscortPercentage);

            //			var timeWindow = (originParcel == tour.Household.ResidenceParcel) ? personDay.TimeWindow : tour.ParentTour.TimeWindow;
            //			var longestWindow = timeWindow.MaxAvailableMinutesAfter(1);
            //			var totalWindow = timeWindow.TotalAvailableMinutesAfter(1);
            //			var expectedDurationCurrentTour = person.IsFulltimeWorker ? Global.Settings.Times.EightHours : Global.Settings.Times.FourHours;
            //			var expectedDurationOtherTours = (personDay.TotalTours - personDay.TotalSimulatedTours) * Global.Settings.Times.TwoHours;
            //			var expectedDurationStops = (Math.Min(personDay.TotalStops,1) - Math.Min(personDay.TotalSimulatedStops,1)) * Global.Settings.Times.OneHour;
            //			var totalExpectedDuration = expectedDurationCurrentTour + expectedDurationOtherTours + expectedDurationStops;

            foreach (var pathTypeModel in pathTypeModels)
            {
                var mode            = pathTypeModel.Mode;
                var generalizedTime = pathTypeModel.GeneralizedTimeLogsum;
                //				var travelTime = pathTypeModel.PathTime;
                //				var travelCost = pathTypeModel.PathCost;

                var available = pathTypeModel.Available;                 //&& (travelTime < longestWindow);

                var alternative = choiceProbabilityCalculator.GetAlternative(mode, available, choice == mode);
                alternative.Choice = mode;

                alternative.AddNestedAlternative(_nestedAlternativeIds[pathTypeModel.Mode], _nestedAlternativeIndexes[pathTypeModel.Mode], THETA_PARAMETER);

                //				if (mode == Global.Settings.Modes.ParkAndRide) {
                //					Console.WriteLine("Park and ride logsum = {0}", generalizedTimeLogsum);
                //				}

                if (!available)
                {
                    continue;
                }

                alternative.AddUtilityTerm(2, generalizedTime * tour.TimeCoefficient);
                //				alternative.AddUtility(3, Math.Log(1.0 - travelTime / longestWindow));
                //				alternative.AddUtility(4, travelTime < longestWindow - expectedDurationCurrentTour ? Math.Log(1.0 - travelTime / (longestWindow - expectedDurationCurrentTour)) : 0);
                //				alternative.AddUtility(5, travelTime < longestWindow - expectedDurationCurrentTour ? 0 : 1);
                //				alternative.AddUtility(6, travelTime < totalWindow - totalExpectedDuration ? Math.Log(1.0 - travelTime / (totalWindow - totalExpectedDuration)) : 0);
                //				alternative.AddUtility(7, travelTime < totalWindow - totalExpectedDuration ? 0 : 1);
                //				var vot = tour.TimeCoefficient / tour.CostCoefficient;

                if (mode == Global.Settings.Modes.ParkAndRide)
                {
                    alternative.AddUtilityTerm(10, 1);
                    alternative.AddUtilityTerm(11, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(13, carsLessThanWorkersFlag);
                    //						alternative.AddUtility(129, destinationParcel.MixedUse2Index1());
                    alternative.AddUtilityTerm(128, destinationParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(127, destinationParcel.NetIntersectionDensity1());
                    //						alternative.AddUtility(123, Math.Log(destinationParcel.StopsTransitBuffer1+1));
                }
                else if (mode == Global.Settings.Modes.Transit)
                {
                    alternative.AddUtilityTerm(20, 1);
                    //						alternative.AddUtility(129, destinationParcel.MixedUse2Index1());
                    alternative.AddUtilityTerm(128, destinationParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(127, destinationParcel.NetIntersectionDensity1());
                    //						alternative.AddUtility(126, originParcel.NetIntersectionDensity1());
                    //						alternative.AddUtility(125, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(124, originParcel.MixedUse2Index1());
                    //						alternative.AddUtility(123, Math.Log(destinationParcel.StopsTransitBuffer1+1));
                    //						alternative.AddUtility(122, Math.Log(originParcel.StopsTransitBuffer1+1));
                }
                else if (mode == Global.Settings.Modes.Hov3)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / Global.Configuration.Coefficients_HOV3CostDivisor_Work));
                    alternative.AddUtilityTerm(30, 1);
                    alternative.AddUtilityTerm(31, childrenUnder5);
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    //						alternative.AddUtility(34, nonworkingAdults + retiredAdults);
                    alternative.AddUtilityTerm(35, pathTypeModel.PathDistance.AlmostEquals(0) ? 0 : Math.Log(pathTypeModel.PathDistance));
                    alternative.AddUtilityTerm(38, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(39, twoPersonHouseholdFlag);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(42, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Hov2)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / Global.Configuration.Coefficients_HOV2CostDivisor_Work));
                    alternative.AddUtilityTerm(31, childrenUnder5);
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    //						alternative.AddUtility(34, nonworkingAdults + retiredAdults);
                    alternative.AddUtilityTerm(35, pathTypeModel.PathDistance.AlmostEquals(0) ? 0 : Math.Log(pathTypeModel.PathDistance));
                    alternative.AddUtilityTerm(40, 1);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(42, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(48, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost) * tour.CostCoefficient);
                    alternative.AddUtilityTerm(50, 1);
                    alternative.AddUtilityTerm(53, carsLessThanWorkersFlag);
                    alternative.AddUtilityTerm(54, income0To25KFlag);
                    alternative.AddUtilityTerm(131, escortPercentage);
                    alternative.AddUtilityTerm(132, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    var class1Dist
                        = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                                                        ? ImpedanceRoster.GetValue("class1distance", mode, Global.Settings.PathTypes.FullNetwork,
                                                                                   Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                                                        : 0;

                    var class2Dist =
                        Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                                                        ? ImpedanceRoster.GetValue("class2distance", mode, Global.Settings.PathTypes.FullNetwork,
                                                                                   Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                                                        : 0;

                    //						var worstDist = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions ?
                    //						 ImpedanceRoster.GetValue("worstdistance", mode, Global.Settings.PathTypes.FullNetwork,
                    //							Global.Settings.VotGroups.Medium, tour.DestinationArrivalTime,originParcel, destinationParcel).Variable : 0;

                    alternative.AddUtilityTerm(60, 1);
                    alternative.AddUtilityTerm(61, maleFlag);
                    alternative.AddUtilityTerm(63, ageBetween51And98Flag);
                    alternative.AddUtilityTerm(169, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(168, destinationParcel.TotalEmploymentDensity1());
                    //						alternative.AddUtility(167, destinationParcel.NetIntersectionDensity1());
                    //						alternative.AddUtility(166, originParcel.NetIntersectionDensity1());
                    //						alternative.AddUtility(165, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(164, originParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(162, (class1Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(162, (class2Dist > 0).ToFlag());
                    //						alternative.AddUtility(163, (worstDist > 0).ToFlag());
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    alternative.AddUtilityTerm(71, maleFlag);
                    //						alternative.AddUtility(73, ageBetween51And98Flag);
                    alternative.AddUtilityTerm(179, destinationParcel.MixedUse4Index1());
                    //						alternative.AddUtility(178, destinationParcel.TotalEmploymentDensity1());
                    //						alternative.AddUtility(177, destinationParcel.NetIntersectionDensity1());
                    //						alternative.AddUtility(176, originParcel.NetIntersectionDensity1());
                    //						alternative.AddUtility(175, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(179, originParcel.MixedUse4Index1());
                }
            }
        }
Beispiel #14
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper tour, IEnumerable <IPathTypeModel> pathTypeModels, IParcelWrapper destinationParcel, int householdCars, int choice = Constants.DEFAULT_VALUE)
        {
            IHouseholdWrapper household = tour.Household;

            Framework.DomainModels.Models.IHouseholdTotals householdTotals = household.HouseholdTotals;
            IPersonDayWrapper personDay = tour.PersonDay;
            IPersonWrapper    person    = tour.Person;

            // household inputs
            int childrenUnder5        = householdTotals.ChildrenUnder5;
            int childrenAge5Through15 = householdTotals.ChildrenAge5Through15;
            //            var nonworkingAdults = householdTotals.NonworkingAdults;
            //            var retiredAdults = householdTotals.RetiredAdults;
            int onePersonHouseholdFlag  = household.IsOnePersonHousehold.ToFlag();
            int twoPersonHouseholdFlag  = household.IsTwoPersonHousehold.ToFlag();
            int noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(householdCars);
            int carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(householdCars);
            int carsLessThanWorkersFlag = household.GetFlagForCarsLessThanWorkers(householdCars);
            int income0To25KFlag        = household.Has0To25KIncome.ToFlag();
            int income75kPlusFlag       = household.Has75KPlusIncome.ToFlag();
            int incomeOver100Flag       = household.Has100KPlusIncome.ToFlag();

            // person inputs
            int maleFlag = person.IsMale.ToFlag();
            int ageBetween51And98Flag = person.AgeIsBetween51And98.ToFlag();
            int univStudentFlag       = person.IsUniversityStudent.ToFlag();

            IParcelWrapper originParcel    = tour.OriginParcel;
            int            parkingDuration = ChoiceModelUtility.GetParkingDuration(person.IsFulltimeWorker);
            // parking at work is free if no paid parking at work and tour goes to usual workplace
            double destinationParkingCost = (Global.Configuration.ShouldRunPayToParkAtWorkplaceModel && tour.Person.UsualWorkParcel != null &&
                                             destinationParcel == tour.Person.UsualWorkParcel && person.PaidParkingAtWorkplace == 0) ? 0.0 : destinationParcel.ParkingCostBuffer1(parkingDuration);

            ChoiceModelUtility.SetEscortPercentages(personDay, out double escortPercentage, out double nonEscortPercentage);

            foreach (IPathTypeModel pathTypeModel in pathTypeModels)
            {
                int    mode = pathTypeModel.Mode;
                double generalizedTimeLogsum = pathTypeModel.GeneralizedTimeLogsum;

                bool available = (pathTypeModel.Mode != Global.Settings.Modes.PaidRideShare || Global.Configuration.PaidRideShareModeIsAvailable) &&
                                 pathTypeModel.Available;

                ChoiceProbabilityCalculator.Alternative alternative = choiceProbabilityCalculator.GetAlternative(mode, available, choice == mode);
                alternative.Choice = mode;

                alternative.AddNestedAlternative(_nestedAlternativeIds[pathTypeModel.Mode], _nestedAlternativeIndexes[pathTypeModel.Mode], THETA_PARAMETER);

                if (!available)
                {
                    continue;
                }

                double modeTimeCoefficient = (Global.Configuration.AV_IncludeAutoTypeChoice && household.OwnsAutomatedVehicles > 0 && mode >= Global.Settings.Modes.Sov && mode <= Global.Settings.Modes.Hov3) ?
                                             tour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) :
                                             (mode == Global.Settings.Modes.PaidRideShare && Global.Configuration.AV_PaidRideShareModeUsesAVs) ?
                                             tour.TimeCoefficient * (1.0 - Global.Configuration.AV_InVehicleTimeCoefficientDiscountFactor) : tour.TimeCoefficient;
                alternative.AddUtilityTerm(2, generalizedTimeLogsum * modeTimeCoefficient);

                if (mode == Global.Settings.Modes.ParkAndRide)
                {
                    alternative.AddUtilityTerm(10, 1);
                    alternative.AddUtilityTerm(11, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(13, carsLessThanWorkersFlag);
                    //                        alternative.AddUtility(129, destinationParcel.MixedUse2Index1());
                    alternative.AddUtilityTerm(130, Math.Log(destinationParcel.TotalEmploymentDensity1() + 1) * 2553.0 / Math.Log(2553.0));
                    alternative.AddUtilityTerm(128, destinationParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(127, destinationParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(123, Math.Log(destinationParcel.StopsTransitBuffer1 + 1));
                    //alternative.AddUtilityTerm(120, Math.Log(destinationParcel.TotalEmploymentDensity1() + 1) * 2553.0 / Math.Log(2553.0));
                    alternative.AddUtilityTerm(118, destinationParcel.TotalEmploymentDensity1());
                    //alternative.AddUtilityTerm(117, destinationParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(113, Math.Log(destinationParcel.StopsTransitBuffer1 + 1));
                    alternative.AddUtilityTerm(112, Math.Log(originParcel.StopsTransitBuffer1 + 1));
                    alternative.AddUtilityTerm(115, Math.Log(pathTypeModel.PathParkAndRideNodeCapacity + 1));
                }
                else if (mode == Global.Settings.Modes.Transit)
                {
                    alternative.AddUtilityTerm(20, 1);
                    alternative.AddUtilityTerm(21, noCarsInHouseholdFlag);   //for calibration
                    alternative.AddUtilityTerm(22, carsLessThanDriversFlag); //for calibration
                                                                             //                        alternative.AddUtility(129, destinationParcel.MixedUse2Index1());
                    alternative.AddUtilityTerm(130, Math.Log(destinationParcel.TotalEmploymentDensity1() + 1) * 2553.0 / Math.Log(2553.0));
                    alternative.AddUtilityTerm(128, destinationParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(127, destinationParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(126, originParcel.NetIntersectionDensity1());
                    //alternative.AddUtilityTerm(125, originParcel.HouseholdDensity1());
                    //alternative.AddUtilityTerm(124, originParcel.MixedUse2Index1());
                    alternative.AddUtilityTerm(123, Math.Log(destinationParcel.StopsTransitBuffer1 + 1));
                    alternative.AddUtilityTerm(122, Math.Log(originParcel.StopsTransitBuffer1 + 1));
                    alternative.AddUtilityTerm(180, univStudentFlag);
                    alternative.AddUtilityTerm(100, income75kPlusFlag);
                }
                else if (mode == Global.Settings.Modes.Hov3)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / Global.Configuration.Coefficients_HOV3CostDivisor_Work));
                    alternative.AddUtilityTerm(30, 1);
                    alternative.AddUtilityTerm(31, childrenUnder5);
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    //                       alternative.AddUtilityTerm(34, nonworkingAdults + retiredAdults);
                    alternative.AddUtilityTerm(35, pathTypeModel.PathDistance.AlmostEquals(0) ? 0 : Math.Log(pathTypeModel.PathDistance));
                    alternative.AddUtilityTerm(36, noCarsInHouseholdFlag);   // for calibration of hov3 vs hov2
                    alternative.AddUtilityTerm(37, carsLessThanDriversFlag); // for calibration of hov3 vs hov2
                    alternative.AddUtilityTerm(38, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(39, twoPersonHouseholdFlag);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(42, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Hov2)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost * tour.CostCoefficient / Global.Configuration.Coefficients_HOV2CostDivisor_Work));
                    alternative.AddUtilityTerm(31, childrenUnder5);
                    alternative.AddUtilityTerm(32, childrenAge5Through15);
                    //                        alternative.AddUtilityTerm(34, nonworkingAdults + retiredAdults);
                    alternative.AddUtilityTerm(35, pathTypeModel.PathDistance.AlmostEquals(0) ? 0 : Math.Log(pathTypeModel.PathDistance));
                    alternative.AddUtilityTerm(40, 1);
                    alternative.AddUtilityTerm(41, noCarsInHouseholdFlag);
                    alternative.AddUtilityTerm(42, carsLessThanDriversFlag);
                    alternative.AddUtilityTerm(48, onePersonHouseholdFlag);
                    alternative.AddUtilityTerm(133, escortPercentage);
                    alternative.AddUtilityTerm(134, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    alternative.AddUtilityTerm(1, (destinationParkingCost) * tour.CostCoefficient);
                    alternative.AddUtilityTerm(50, 1);
                    alternative.AddUtilityTerm(53, carsLessThanWorkersFlag);
                    alternative.AddUtilityTerm(54, income0To25KFlag);
                    alternative.AddUtilityTerm(131, escortPercentage);
                    alternative.AddUtilityTerm(132, nonEscortPercentage);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    double class1Dist
                        = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                  ? ImpedanceRoster.GetValue("class1distance", mode, Global.Settings.PathTypes.FullNetwork,
                                             Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                  : 0;

                    double class2Dist =
                        Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions
                  ? ImpedanceRoster.GetValue("class2distance", mode, Global.Settings.PathTypes.FullNetwork,
                                             Global.Settings.ValueOfTimes.DefaultVot, tour.DestinationArrivalTime, originParcel, destinationParcel).Variable
                  : 0;

                    //                  double worstDist = Global.Configuration.PathImpedance_BikeUseTypeSpecificDistanceFractions ?
                    //                         ImpedanceRoster.GetValue("worstdistance", mode, Global.Settings.PathTypes.FullNetwork,
                    //                            Global.Settings.VotGroups.Medium, tour.DestinationArrivalTime,originParcel, destinationParcel).Variable : 0;

                    alternative.AddUtilityTerm(60, 1);
                    alternative.AddUtilityTerm(61, maleFlag);
                    alternative.AddUtilityTerm(63, ageBetween51And98Flag);
                    alternative.AddUtilityTerm(67, noCarsInHouseholdFlag);   //for calibration
                    alternative.AddUtilityTerm(68, carsLessThanDriversFlag); //for calibration
                    alternative.AddUtilityTerm(169, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(168, destinationParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(167, destinationParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(166, originParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(165, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(164, originParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(162, (class1Dist > 0).ToFlag());
                    alternative.AddUtilityTerm(162, (class2Dist > 0).ToFlag());
                    //                        alternative.AddUtility(163, (worstDist > 0).ToFlag());
                    alternative.AddUtilityTerm(170, 1.0 * destinationParcel.MixedUse4Index1()
                                               + 0.00002 * destinationParcel.TotalEmploymentDensity1()
                                               + 0.001 * destinationParcel.NetIntersectionDensity1()
                                               + 0.001 * originParcel.NetIntersectionDensity1()
                                               + 0.0002 * originParcel.HouseholdDensity1()
                                               + 1.0 * originParcel.MixedUse4Index1());

                    //alternative.AddUtilityTerm(261, originParcel.PCA_DensityTerm_Buffer1());
                    //alternative.AddUtilityTerm(262, originParcel.PCA_WalkabilityTerm_Buffer1());
                    //alternative.AddUtilityTerm(263, originParcel.PCA_MixedUseTerm_Buffer1());
                    //alternative.AddUtilityTerm(264, originParcel.PCA_TransitAccessTerm_Buffer1());
                    //alternative.AddUtilityTerm(261, destinationParcel.PCA_DensityTerm_Buffer1());
                    //alternative.AddUtilityTerm(262, destinationParcel.PCA_WalkabilityTerm_Buffer1());
                    //alternative.AddUtilityTerm(263, destinationParcel.PCA_MixedUseTerm_Buffer1());
                    //alternative.AddUtilityTerm(264, destinationParcel.PCA_TransitAccessTerm_Buffer1());
                    //alternative.AddUtilityTerm(265, destinationParcel.PCA_DensityTerm_Buffer1());
                    //alternative.AddUtilityTerm(266, destinationParcel.PCA_WalkabilityTerm_Buffer1());
                    //alternative.AddUtilityTerm(267, destinationParcel.PCA_MixedUseTerm_Buffer1());
                    //alternative.AddUtilityTerm(268, destinationParcel.PCA_TransitAccessTerm_Buffer1());
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    alternative.AddUtilityTerm(70, 1); //for calibration
                    alternative.AddUtilityTerm(71, maleFlag);
                    alternative.AddUtilityTerm(73, ageBetween51And98Flag);
                    alternative.AddUtilityTerm(75, pathTypeModel.PathDistance * pathTypeModel.PathDistance);
                    alternative.AddUtilityTerm(77, noCarsInHouseholdFlag);   //for calibration
                    alternative.AddUtilityTerm(78, carsLessThanDriversFlag); //for calibration
                    alternative.AddUtilityTerm(179, destinationParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(178, destinationParcel.TotalEmploymentDensity1());
                    alternative.AddUtilityTerm(177, destinationParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(176, originParcel.NetIntersectionDensity1());
                    alternative.AddUtilityTerm(175, originParcel.HouseholdDensity1());
                    alternative.AddUtilityTerm(179, originParcel.MixedUse4Index1());
                    alternative.AddUtilityTerm(181, 1.0 * destinationParcel.MixedUse4Index1()
                                               + 0.00001 * destinationParcel.TotalEmploymentDensity1()
                                               + 0.001 * destinationParcel.NetIntersectionDensity1()
                                               + 0.001 * originParcel.NetIntersectionDensity1()
                                               + 0.0001 * originParcel.HouseholdDensity1()
                                               + 1.0 * originParcel.MixedUse4Index1());

                    //alternative.AddUtilityTerm(271, originParcel.PCA_DensityTerm_Buffer1());
                    //alternative.AddUtilityTerm(272, originParcel.PCA_WalkabilityTerm_Buffer1());
                    //alternative.AddUtilityTerm(273, originParcel.PCA_MixedUseTerm_Buffer1());
                    //alternative.AddUtilityTerm(274, originParcel.PCA_TransitAccessTerm_Buffer1());
                    //alternative.AddUtilityTerm(271, destinationParcel.PCA_DensityTerm_Buffer1());
                    //alternative.AddUtilityTerm(272, destinationParcel.PCA_WalkabilityTerm_Buffer1());
                    //alternative.AddUtilityTerm(273, destinationParcel.PCA_MixedUseTerm_Buffer1());
                    //alternative.AddUtilityTerm(274, destinationParcel.PCA_TransitAccessTerm_Buffer1());
                    //alternative.AddUtilityTerm(275, destinationParcel.PCA_DensityTerm_Buffer1());
                    //alternative.AddUtilityTerm(276, destinationParcel.PCA_WalkabilityTerm_Buffer1());
                    //alternative.AddUtilityTerm(277, destinationParcel.PCA_MixedUseTerm_Buffer1());
                    //alternative.AddUtilityTerm(278, destinationParcel.PCA_TransitAccessTerm_Buffer1());
                }
                else if (mode == Global.Settings.Modes.PaidRideShare)
                {
                    if (Global.Configuration.PaidRideshare_UseEstimatedInsteadOfAssertedCoefficients)
                    {
                        alternative.AddUtilityTerm(80, 1.0);
                        alternative.AddUtilityTerm(81, noCarsInHouseholdFlag);   //for calibration
                        alternative.AddUtilityTerm(82, carsLessThanDriversFlag); //for calibration
                        alternative.AddUtilityTerm(83, tour.Person.AgeIsBetween26And35.ToFlag());
                        alternative.AddUtilityTerm(83, tour.Person.AgeIsBetween18And25.ToFlag());
                        alternative.AddUtilityTerm(84, (tour.Person.Age >= 65).ToFlag());            //alternative.AddUtilityTerm(81, tour.Person.AgeIsBetween26And35.ToFlag());
                        alternative.AddUtilityTerm(85, originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2);
                        alternative.AddUtilityTerm(86, destinationParcel.HouseholdsBuffer2 + destinationParcel.StudentsUniversityBuffer2 + destinationParcel.EmploymentTotalBuffer2);
                        alternative.AddUtilityTerm(87, income0To25KFlag);
                        alternative.AddUtilityTerm(88, incomeOver100Flag);
                    }
                    else
                    {
                        double modeConstant = Global.Configuration.AV_PaidRideShareModeUsesAVs
                        ? Global.Configuration.AV_PaidRideShare_ModeConstant
                                              + Global.Configuration.AV_PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2,
                                                                                                                    (Global.Configuration.PaidRideShare_DensityMeasureCapValue > 0) ? Global.Configuration.PaidRideShare_DensityMeasureCapValue : 6000)
                                              + Global.Configuration.AV_PaidRideShare_AVOwnerCoefficient * (household.OwnsAutomatedVehicles > 0).ToFlag()
                        : Global.Configuration.PaidRideShare_ModeConstant
                                              + Global.Configuration.PaidRideShare_DensityCoefficient * Math.Min(originParcel.HouseholdsBuffer2 + originParcel.StudentsUniversityBuffer2 + originParcel.EmploymentTotalBuffer2,
                                                                                                                 (Global.Configuration.PaidRideShare_DensityMeasureCapValue > 0) ? Global.Configuration.PaidRideShare_DensityMeasureCapValue : 6000);

                        alternative.AddUtilityTerm(90, modeConstant);
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age26to35Coefficient * tour.Person.AgeIsBetween26And35.ToFlag());
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_Age18to25Coefficient * tour.Person.AgeIsBetween18And25.ToFlag());
                        alternative.AddUtilityTerm(90, Global.Configuration.PaidRideShare_AgeOver65Coefficient * (tour.Person.Age >= 65).ToFlag());
                    }
                }



                RegionSpecificCustomizations(alternative, tour, pathTypeModel.PathType, mode, destinationParcel);
            }
        }