Exemplo n.º 1
0
        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);
                }
            }
        }
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITripWrapper trip, int choice = Constants.DEFAULT_VALUE)
        {
            IHouseholdWrapper household = trip.Household;
            IPersonWrapper    person    = trip.Person;
            IPersonDayWrapper personDay = trip.PersonDay;
            ITourWrapper      tour      = trip.Tour;

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

            // household inputs
            int childrenFlag           = household.HasChildren.ToFlag();
            int onePersonHouseholdFlag = household.IsOnePersonHousehold.ToFlag();

            // person inputs
            int adultMaleFlag          = person.IsAdultMale.ToFlag();
            int adultFemaleFlag        = person.IsAdultFemale.ToFlag();
            int partTimeWorkerFlag     = person.IsPartTimeWorker.ToFlag();
            int retiredAdultFlag       = person.IsRetiredAdult.ToFlag();
            int drivingAgeStudentFlag  = person.IsDrivingAgeStudent.ToFlag();
            int nonworkingAdultFlag    = person.IsNonworkingAdult.ToFlag();
            int childAge5Through15Flag = person.IsChildAge5Through15.ToFlag();
            int childUnder5Flag        = person.IsChildUnder5.ToFlag();

            // person-day inputs
            int homeBasedTours     = personDay.HomeBasedTours;
            int simulatedToursFlag = personDay.SimulatedToursExist().ToFlag();

            int simulatedWorkStops     = personDay.SimulatedWorkStops;
            int simulatedWorkStopsFlag = personDay.SimulatedWorkStopsExist().ToFlag();

            int simulatedSchoolStops = personDay.SimulatedSchoolStops;
            //            var simulatedSchoolStopsFlag = personDay.HasSimulatedSchoolStops.ToFlag();

            int simulatedEscortStops = personDay.SimulatedEscortStops;
            //            var simulatedEscortStopsFlag = personDay.HasSimulatedEscortStops.ToFlag();

            int simulatedPersonalBusinessStops = personDay.SimulatedPersonalBusinessStops;
            //            var simulatedPersonalBusinessStopsFlag = personDay.HasSimulatedPersonalBusinessStops.ToFlag();

            int simulatedShoppingStops = personDay.SimulatedShoppingStops;
            //            var simulatedShoppingStopsFlag = personDay.HasSimulatedShoppingStops.ToFlag();

            int simulatedMealStops = personDay.SimulatedMealStops;
            //            var simulatedMealStopsFlag = personDay.HasSimulatedMealStops.ToFlag();

            int simulatedSocialStops = personDay.SimulatedSocialStops;
            //            var simulatedSocialStopsFlag = personDay.HasSimulatedSocialStops.ToFlag();

            // tour inputs
            int hov2TourFlag         = tour.IsHov2Mode().ToFlag();
            int hov3TourFlag         = tour.IsHov3Mode().ToFlag();
            int transitTourFlag      = tour.IsTransitMode().ToFlag();
            int bikeTourFlag         = tour.IsBikeMode().ToFlag();
            int walkTourFlag         = tour.IsWalkMode().ToFlag();
            int notHomeBasedTourFlag = (!tour.IsHomeBasedTour).ToFlag();
            int workTourFlag         = tour.IsWorkPurpose().ToFlag();
            int personalBusinessOrMedicalTourFlag = tour.IsPersonalBusinessOrMedicalPurpose().ToFlag();
            int socialOrRecreationTourFlag        = tour.IsSocialOrRecreationPurpose().ToFlag();
            int schoolTourFlag   = tour.IsSchoolPurpose().ToFlag();
            int escortTourFlag   = tour.IsEscortPurpose().ToFlag();
            int shoppingTourFlag = tour.IsShoppingPurpose().ToFlag();
            int mealTourFlag     = tour.IsMealPurpose().ToFlag();

            // trip inputs
            int oneSimulatedTripFlag           = halfTour.OneSimulatedTripFlag;
            int twoSimulatedTripsFlag          = halfTour.TwoSimulatedTripsFlag;
            int threeSimulatedTripsFlag        = halfTour.ThreeSimulatedTripsFlag;
            int fourSimulatedTripsFlag         = halfTour.FourSimulatedTripsFlag;
            int fiveSimulatedTripsFlag         = halfTour.FiveSimulatedTripsFlag;
            int halfTourFromOriginFlag         = trip.IsHalfTourFromOrigin.ToFlag();
            int halfTourFromDestinationFlag    = (!trip.IsHalfTourFromOrigin).ToFlag();
            int beforeMandatoryDestinationFlag = trip.IsBeforeMandatoryDestination().ToFlag();

            // remaining inputs
            int time = trip.IsHalfTourFromOrigin ? tour.DestinationArrivalTime : tour.DestinationDepartureTime;

            int from7AMto9AMFlag  = (time >= Global.Settings.Times.SevenAM && time < Global.Settings.Times.NineAM).ToFlag();
            int from9AMto11AMFlag = (time >= Global.Settings.Times.NineAM && time < Global.Settings.Times.ElevenAM).ToFlag();
            int from11AMto1PMFlag = (time >= Global.Settings.Times.ElevenAM && time < Global.Settings.Times.OnePM).ToFlag();
            int from1PMto3PMFlag  = (time >= Global.Settings.Times.OnePM && time < Global.Settings.Times.ThreePM).ToFlag();
            int from3PMto5PMFlag  = (time >= Global.Settings.Times.ThreePM && time < Global.Settings.Times.FivePM).ToFlag();
            int from7PMto9PMFlag  = (time >= Global.Settings.Times.SevenPM && time < Global.Settings.Times.NinePM).ToFlag();
            int from9PMto11PMFlag = (time >= Global.Settings.Times.NinePM && time < Global.Settings.Times.ElevenPM).ToFlag();
            int from11PMto7AMFlag = (time >= Global.Settings.Times.ElevenPM).ToFlag();

            double foodRetailServiceMedicalQtrMileLog = household.ResidenceParcel.FoodRetailServiceMedicalLogBuffer1();
            int    remainingToursCount = personDay.HomeBasedTours - personDay.GetTotalSimulatedTours();

            // time-of-day for trip window calculations
            int startTime = trip.GetStartTime();

            // time window in minutes and in hours for yet unmodeled portion of half-tour, only consider persons on this trip
            int timeWindow =
                tour.IsHomeBasedTour
                    ? personDay.TimeWindow.AvailableWindow(startTime, Global.Settings.TimeDirections.Both)
                    : tour.ParentTour.TimeWindow.AvailableWindow(startTime, Global.Settings.TimeDirections.Both);

            double duration = timeWindow / 60D;

            // connectivity attributes
            double c34Ratio = trip.OriginParcel.C34RatioBuffer1();

            // 0 - NO MORE STOPS

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

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

            alternative.AddUtilityTerm(1, twoSimulatedTripsFlag * halfTourFromOriginFlag);
            alternative.AddUtilityTerm(2, threeSimulatedTripsFlag * halfTourFromOriginFlag);
            alternative.AddUtilityTerm(3, fourSimulatedTripsFlag * halfTourFromOriginFlag);
            alternative.AddUtilityTerm(4, fiveSimulatedTripsFlag * halfTourFromOriginFlag);
            alternative.AddUtilityTerm(5, twoSimulatedTripsFlag * halfTourFromDestinationFlag);
            alternative.AddUtilityTerm(6, threeSimulatedTripsFlag * halfTourFromDestinationFlag);
            alternative.AddUtilityTerm(7, fourSimulatedTripsFlag * halfTourFromDestinationFlag);
            alternative.AddUtilityTerm(8, fiveSimulatedTripsFlag * halfTourFromDestinationFlag);
            alternative.AddUtilityTerm(9, homeBasedTours);
            alternative.AddUtilityTerm(10, simulatedToursFlag);
            alternative.AddUtilityTerm(11, notHomeBasedTourFlag);
            alternative.AddUtilityTerm(12, beforeMandatoryDestinationFlag);
            alternative.AddUtilityTerm(16, (transitTourFlag + walkTourFlag + bikeTourFlag) * c34Ratio * foodRetailServiceMedicalQtrMileLog);
            alternative.AddUtilityTerm(17, transitTourFlag);
            alternative.AddUtilityTerm(40, schoolTourFlag);

            // 1 - WORK STOP

            if (personDay.WorkStops > 0 && tour.DestinationPurpose <= Global.Settings.Purposes.School)
            {
                alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Work, true, choice == Global.Settings.Purposes.Work);

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

                alternative.AddUtilityTerm(33, workTourFlag + schoolTourFlag);
                //                alternative.AddUtility(47, escortTourFlag);
                //                alternative.AddUtility(54, personalBusinessOrMedicalTourFlag);
                //                alternative.AddUtility(61, shoppingTourFlag);
                //                alternative.AddUtility(68, mealTourFlag);
                //                alternative.AddUtility(75, socialOrRecreationTourFlag);
                alternative.AddUtilityTerm(82, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(89, simulatedWorkStops);
                alternative.AddUtilityTerm(96, simulatedWorkStopsFlag);
                alternative.AddUtilityTerm(103, remainingToursCount);
                alternative.AddUtilityTerm(110, duration);
                alternative.AddUtilityTerm(131, from9AMto11AMFlag + from11AMto1PMFlag + from1PMto3PMFlag + from3PMto5PMFlag);
                alternative.AddUtilityTerm(175, adultMaleFlag);
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Work, false, choice == Global.Settings.Purposes.Work);
            }

            // 2 - SCHOOL STOP

            if (personDay.SchoolStops > 0 && tour.DestinationPurpose <= Global.Settings.Purposes.School)
            {
                alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.School, true, choice == Global.Settings.Purposes.School);

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

                alternative.AddUtilityTerm(34, workTourFlag + schoolTourFlag);
                //                alternative.AddUtility(48, escortTourFlag);
                //                alternative.AddUtility(55, personalBusinessOrMedicalTourFlag);
                //                alternative.AddUtility(62, shoppingTourFlag);
                //                alternative.AddUtility(69, mealTourFlag);
                //                alternative.AddUtility(76, socialOrRecreationTourFlag);
                alternative.AddUtilityTerm(83, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(90, simulatedSchoolStops);
                //                alternative.AddUtility(97, simulatedSchoolStopsFlag);
                alternative.AddUtilityTerm(104, remainingToursCount);
                alternative.AddUtilityTerm(111, duration);
                alternative.AddUtilityTerm(138, from7AMto9AMFlag + from7PMto9PMFlag + from9PMto11PMFlag + from11PMto7AMFlag);
                alternative.AddUtilityTerm(237, oneSimulatedTripFlag);
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.School, false, choice == Global.Settings.Purposes.School);
            }

            // 3 - ESCORT STOP

            if (personDay.EscortStops > 0 && tour.DestinationPurpose <= Global.Settings.Purposes.Escort)
            {
                alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Escort, true, choice == Global.Settings.Purposes.Escort);

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

                alternative.AddUtilityTerm(35, workTourFlag + schoolTourFlag);
                alternative.AddUtilityTerm(49, escortTourFlag);
                //                alternative.AddUtility(56, personalBusinessOrMedicalTourFlag);
                //                alternative.AddUtility(63, shoppingTourFlag);
                //                alternative.AddUtility(70, mealTourFlag);
                //                alternative.AddUtility(77, socialOrRecreationTourFlag);
                alternative.AddUtilityTerm(84, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(91, simulatedEscortStops);
                //                alternative.AddUtility(98, simulatedEscortStopsFlag);
                alternative.AddUtilityTerm(105, remainingToursCount);
                alternative.AddUtilityTerm(112, duration);
                alternative.AddUtilityTerm(146, from7AMto9AMFlag);
                alternative.AddUtilityTerm(147, from9AMto11AMFlag + from11AMto1PMFlag + from1PMto3PMFlag + from3PMto5PMFlag);
                alternative.AddUtilityTerm(181, childrenFlag * adultFemaleFlag);
                alternative.AddUtilityTerm(183, hov2TourFlag);
                alternative.AddUtilityTerm(184, hov3TourFlag);
                alternative.AddUtilityTerm(238, oneSimulatedTripFlag);
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Escort, false, choice == Global.Settings.Purposes.Escort);
            }

            // 4 - PERSONAL BUSINESS STOP

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

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

                alternative.AddUtilityTerm(36, workTourFlag + schoolTourFlag);
                alternative.AddUtilityTerm(50, escortTourFlag);
                alternative.AddUtilityTerm(57, personalBusinessOrMedicalTourFlag);
                alternative.AddUtilityTerm(64, shoppingTourFlag);
                alternative.AddUtilityTerm(71, mealTourFlag);
                alternative.AddUtilityTerm(78, socialOrRecreationTourFlag);
                alternative.AddUtilityTerm(85, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(92, simulatedPersonalBusinessStops);
                //                alternative.AddUtility(99, simulatedPersonalBusinessStopsFlag);
                alternative.AddUtilityTerm(106, remainingToursCount);
                alternative.AddUtilityTerm(113, duration);
                alternative.AddUtilityTerm(154, from7AMto9AMFlag + from7PMto9PMFlag + from9PMto11PMFlag + from11PMto7AMFlag);
                alternative.AddUtilityTerm(155, from9AMto11AMFlag + from11AMto1PMFlag + from1PMto3PMFlag + from3PMto5PMFlag);
                alternative.AddUtilityTerm(195, onePersonHouseholdFlag);
                alternative.AddUtilityTerm(196, hov2TourFlag);
                alternative.AddUtilityTerm(197, hov3TourFlag);
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.PersonalBusiness, false, choice == Global.Settings.Purposes.PersonalBusiness);
            }

            // 5 - SHOPPING STOP

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

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

                alternative.AddUtilityTerm(37, workTourFlag + schoolTourFlag);
                alternative.AddUtilityTerm(51, escortTourFlag);
                alternative.AddUtilityTerm(58, personalBusinessOrMedicalTourFlag);
                alternative.AddUtilityTerm(65, shoppingTourFlag);
                alternative.AddUtilityTerm(72, mealTourFlag);
                alternative.AddUtilityTerm(79, socialOrRecreationTourFlag);
                alternative.AddUtilityTerm(86, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(93, simulatedShoppingStops);
                //                alternative.AddUtility(100, simulatedShoppingStopsFlag);
                alternative.AddUtilityTerm(107, remainingToursCount);
                alternative.AddUtilityTerm(114, duration);
                alternative.AddUtilityTerm(162, from7AMto9AMFlag + from9PMto11PMFlag + from11PMto7AMFlag);
                alternative.AddUtilityTerm(164, from11AMto1PMFlag + from1PMto3PMFlag + from3PMto5PMFlag);
                alternative.AddUtilityTerm(207, childrenFlag * adultFemaleFlag);
                alternative.AddUtilityTerm(209, hov2TourFlag);
                alternative.AddUtilityTerm(210, hov3TourFlag);
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Shopping, false, choice == Global.Settings.Purposes.Shopping);
            }

            // 6 - MEAL STOP

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

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

                alternative.AddUtilityTerm(38, workTourFlag);
                alternative.AddUtilityTerm(46, schoolTourFlag);
                alternative.AddUtilityTerm(52, escortTourFlag);
                alternative.AddUtilityTerm(59, personalBusinessOrMedicalTourFlag);
                alternative.AddUtilityTerm(66, shoppingTourFlag);
                alternative.AddUtilityTerm(73, mealTourFlag);
                alternative.AddUtilityTerm(80, socialOrRecreationTourFlag);
                alternative.AddUtilityTerm(87, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(94, simulatedMealStops);
                //                alternative.AddUtility(101, simulatedMealStopsFlag);
                alternative.AddUtilityTerm(108, remainingToursCount);
                alternative.AddUtilityTerm(115, duration);
                alternative.AddUtilityTerm(170, from7AMto9AMFlag + from11PMto7AMFlag);
                alternative.AddUtilityTerm(171, from11AMto1PMFlag + from1PMto3PMFlag);
                alternative.AddUtilityTerm(172, from7PMto9PMFlag);
                alternative.AddUtilityTerm(221, onePersonHouseholdFlag);
                alternative.AddUtilityTerm(222, hov2TourFlag);
                alternative.AddUtilityTerm(223, hov3TourFlag);
                alternative.AddUtilityTerm(226, partTimeWorkerFlag + retiredAdultFlag + drivingAgeStudentFlag);
                alternative.AddUtilityTerm(228, nonworkingAdultFlag + childAge5Through15Flag + childUnder5Flag);
                alternative.AddUtilityTerm(239, oneSimulatedTripFlag);
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Meal, false, choice == Global.Settings.Purposes.Meal);
            }

            // 7 - SOCIAL (OR RECREATION) STOP

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

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

                alternative.AddUtilityTerm(39, workTourFlag + schoolTourFlag);
                alternative.AddUtilityTerm(53, escortTourFlag);
                alternative.AddUtilityTerm(60, personalBusinessOrMedicalTourFlag);
                alternative.AddUtilityTerm(67, shoppingTourFlag);
                alternative.AddUtilityTerm(74, mealTourFlag);
                alternative.AddUtilityTerm(81, socialOrRecreationTourFlag);
                alternative.AddUtilityTerm(88, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(95, simulatedSocialStops);
                //                alternative.AddUtility(102, simulatedSocialStopsFlag);
                alternative.AddUtilityTerm(109, remainingToursCount);
                alternative.AddUtilityTerm(116, duration);
                alternative.AddUtilityTerm(173, from7AMto9AMFlag + from11PMto7AMFlag);
                alternative.AddUtilityTerm(174, from11AMto1PMFlag + from1PMto3PMFlag + from3PMto5PMFlag);
                alternative.AddUtilityTerm(235, hov2TourFlag);
                alternative.AddUtilityTerm(236, hov3TourFlag);
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Social, false, choice == Global.Settings.Purposes.Social);
            }
        }
Exemplo n.º 3
0
        protected override void RegionSpecificOtherTourDistrictCoefficients(ChoiceProbabilityCalculator.Alternative alternative, ITourWrapper _tour, IParcelWrapper destinationParcel, IPersonWrapper person)
        {
            //add any region-specific new terms in region-specific class, using coefficient numbers 114-120, or other unused variable #
            //Global.PrintFile.WriteLine("PSRC_OtherTourDestinationModel.RegionSpecificOtherTourDistrictCoefficients called");
            int origdist          = _tour.OriginParcel.District;
            int destdist          = destinationParcel.District;
            int origKitDestTRP    = (origdist == 9 || origdist == 11) && (destdist == 8 || destdist == 10 || destdist == 7) ? 1 : 0;
            int origEastDestCBD   = origdist >= 60 && origdist <= 65 && destdist == 4 ? 1 : 0;
            int origTacDestKit    = origdist == 8 && destdist == 9 || destdist == 11 ? 1 : 0;
            int origKitDestNotKit = (origdist == 9 || origdist == 11) && (destdist != 9 && destdist != 11) ? 1 : 0;
            int origSTacWorkCBD   = (origdist == 11 && destdist == 4) ? 1 : 0;

            alternative.AddUtilityTerm(115, origEastDestCBD);
            alternative.AddUtilityTerm(116, origKitDestTRP);
            alternative.AddUtilityTerm(117, origTacDestKit);
            alternative.AddUtilityTerm(118, origKitDestNotKit);
            alternative.AddUtilityTerm(119, origSTacWorkCBD);


            // Below is BKR customization
            //Global.PrintFile.WriteLine("Default BKR_OtherHomeBasedTourModeModel.RegionSpecificCustomizations2 called");
            if (_tour.IsShoppingPurpose() && (_tour.Mode == Global.Settings.Modes.Sov || _tour.Mode == Global.Settings.Modes.Hov2 || _tour.Mode == Global.Settings.Modes.Hov3))
            {
                if (destinationParcel.District == 62 && _tour.OriginParcel.District != 62)
                {
                    alternative.AddUtilityTerm(120, 1);
                }
            }
            if (_tour.IsMealPurpose() && (_tour.Mode == Global.Settings.Modes.Sov || _tour.Mode == Global.Settings.Modes.Hov2 || _tour.Mode == Global.Settings.Modes.Hov3))
            {
                if (destinationParcel.District == 62 && _tour.OriginParcel.District != 62)
                {
                    alternative.AddUtilityTerm(121, 1);
                }
            }
            if (_tour.IsPersonalBusinessPurpose() && (_tour.Mode == Global.Settings.Modes.Sov || _tour.Mode == Global.Settings.Modes.Hov2 || _tour.Mode == Global.Settings.Modes.Hov3))
            {
                if (destinationParcel.District == 62 && _tour.OriginParcel.District != 62)
                {
                    alternative.AddUtilityTerm(122, 1);
                }
            }
            if (_tour.IsSocialPurpose() && (_tour.Mode == Global.Settings.Modes.Sov || _tour.Mode == Global.Settings.Modes.Hov2 || _tour.Mode == Global.Settings.Modes.Hov3))
            {
                if (destinationParcel.District == 62 && _tour.OriginParcel.District != 62)
                {
                    alternative.AddUtilityTerm(123, 1);
                }
            }
            if (_tour.IsEscortPurpose() && (_tour.Mode == Global.Settings.Modes.Sov || _tour.Mode == Global.Settings.Modes.Hov2 || _tour.Mode == Global.Settings.Modes.Hov3))
            {
                if (destinationParcel.District == 62 && _tour.OriginParcel.District != 62)
                {
                    alternative.AddUtilityTerm(124, 1);
                }
            }

            // to Belleuve Square from outside of Bel CBD
            // even thoug the zoneid is clearly labeled, it is zero-based. that means if you want to reference a TAZ nunber, you need to use TAZ number - 1 in ZoneId.
            // in this case, Bellevue Square is in TAZ 11. its ZoneId in destinationParcel is 10.
            if (destinationParcel.ZoneId == 10 && origdist != 62)
            {
                alternative.AddUtilityTerm(125, 1);
            }
        }
Exemplo n.º 4
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper tour, TourTime choice = null)
        {
            var household = tour.Household;
            var person    = tour.Person;
            var personDay = tour.PersonDay;

            // household inputs
            var income0To25KFlag   = household.Has0To25KIncome.ToFlag();
            var income100KPlusFlag = household.Has100KPlusIncome.ToFlag();

            // person inputs
            var partTimeWorkerFlag     = person.IsPartTimeWorker.ToFlag();
            var nonworkingAdultFlag    = person.IsNonworkingAdult.ToFlag();
            var universityStudentFlag  = person.IsUniversityStudent.ToFlag();
            var retiredAdultFlag       = person.IsRetiredAdult.ToFlag();
            var drivingAgeStudentFlag  = person.IsDrivingAgeStudent.ToFlag();
            var fulltimeWorkerFlag     = person.IsFulltimeWorker.ToFlag();
            var childAge5Through15Flag = person.IsChildAge5Through15.ToFlag();
            var childUnder5Flag        = person.IsChildUnder5.ToFlag();

            // person-day inputs
            var homeBasedToursOnlyFlag          = personDay.OnlyHomeBasedToursExist().ToFlag();
            var firstSimulatedHomeBasedTourFlag = personDay.IsFirstSimulatedHomeBasedTour().ToFlag();
            var laterSimulatedHomeBasedTourFlag = personDay.IsLaterSimulatedHomeBasedTour().ToFlag();
            var totalStopPurposes       = personDay.GetTotalStopPurposes();       // JLB 20150401 change from Stops to StopPurposes to make definition consistent in apply and estimate
            var totalSimulatedStops     = personDay.GetTotalSimulatedStops();
            var escortStops             = personDay.EscortStops;
            var homeBasedTours          = personDay.HomeBasedTours;
            var simulatedHomeBasedTours = personDay.SimulatedHomeBasedTours;

            // tour inputs
            var escortTourFlag           = tour.IsEscortPurpose().ToFlag();
            var shoppingTourFlag         = tour.IsShoppingPurpose().ToFlag();
            var mealTourFlag             = tour.IsMealPurpose().ToFlag();
            var socialTourFlag           = tour.IsSocialPurpose().ToFlag();
            var personalBusinessTourFlag = tour.IsPersonalBusinessPurpose().ToFlag();

            var sovOrHovTourFlag = tour.IsAnAutoMode().ToFlag();
            var transitTourFlag  = tour.UsesTransitModes().ToFlag();

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

            // Lower priority tour(s) of 2+ tours for the same purpose
            var lowPrioritySameFlag = (tour.GetTotalSimulatedToursByPurpose() > 1).ToFlag();

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

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

            var timeWindow       = tour.ParentTour == null ? personDay.TimeWindow : tour.ParentTour.TimeWindow;
            var impedances       = tour.GetTourModeImpedances();
            var period1Middle    = 15;
            var smallPeriodCount = DayPeriod.SmallDayPeriods.Count();

            for (var periodIndex = 0; periodIndex < smallPeriodCount; periodIndex++)
            {
                // set arrival period component
                var arrivalPeriodIndex            = periodIndex;
                var arrivalPeriodShift            = arrivalPeriodIndex * (48.0 / DayPeriod.SMALL_DAY_PERIOD_TOTAL_TRIP_TIMES);      //adjust shift amount if period lengths change
                var arrivalPeriod                 = DayPeriod.SmallDayPeriods[arrivalPeriodIndex];
                var earlyArriveFlag               = arrivalPeriod.Middle.IsBetween(Global.Settings.Times.ThreeAM, Global.Settings.Times.SevenAM).ToFlag();
                var arrivalImpedance              = impedances[arrivalPeriodIndex];
                var arrivalPeriodAvailableMinutes = timeWindow.TotalAvailableMinutes(arrivalPeriod.Start, arrivalPeriod.End);

                choiceProbabilityCalculator.CreateUtilityComponent(3 * periodIndex + 0);
                var arrivalComponent = choiceProbabilityCalculator.GetUtilityComponent(3 * periodIndex + 0);

                if (arrivalPeriodAvailableMinutes > 0 || Global.Configuration.IsInEstimationMode)
                {
                    arrivalComponent.AddUtilityTerm(11, arrivalPeriod.Middle.IsBetween(Global.Settings.Times.ThreeAM, Global.Settings.Times.SixAM).ToFlag());
                    arrivalComponent.AddUtilityTerm(12, arrivalPeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.SixAM, Global.Settings.Times.SevenAM).ToFlag());
                    arrivalComponent.AddUtilityTerm(13, arrivalPeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.SevenAM, Global.Settings.Times.EightAM).ToFlag());
                    arrivalComponent.AddUtilityTerm(14, arrivalPeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.EightAM, Global.Settings.Times.NineAM).ToFlag());
                    arrivalComponent.AddUtilityTerm(15, arrivalPeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.NineAM, Global.Settings.Times.TenAM).ToFlag());
                    arrivalComponent.AddUtilityTerm(16, arrivalPeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.TenAM, Global.Settings.Times.OnePM).ToFlag());
                    arrivalComponent.AddUtilityTerm(17, arrivalPeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.OnePM, Global.Settings.Times.FourPM).ToFlag());
                    arrivalComponent.AddUtilityTerm(18, arrivalPeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.FourPM, Global.Settings.Times.SevenPM).ToFlag());
                    arrivalComponent.AddUtilityTerm(19, arrivalPeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.SevenPM, Global.Settings.Times.TenPM).ToFlag());
                    arrivalComponent.AddUtilityTerm(20, arrivalPeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.TenPM, Global.Settings.Times.MinutesInADay).ToFlag());
                    arrivalComponent.AddUtilityTerm(41, partTimeWorkerFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(43, nonworkingAdultFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(45, universityStudentFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(47, retiredAdultFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(49, drivingAgeStudentFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(139, fulltimeWorkerFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(141, childAge5Through15Flag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(143, childUnder5Flag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(145, escortTourFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(147, shoppingTourFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(149, mealTourFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(151, socialTourFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(153, personalBusinessTourFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(51, income0To25KFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(53, income100KPlusFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(55, highPrioritySameFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(57, lowPrioritySameFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(155, highPriorityDifferentFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(157, lowPriorityDifferentFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(59, homeBasedToursOnlyFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(61, totalStopPurposes * homeBasedToursOnlyFlag * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(63, totalStopPurposes * (1 - homeBasedToursOnlyFlag) * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(65, (totalStopPurposes - totalSimulatedStops) * (1 - homeBasedToursOnlyFlag) * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(67, escortStops * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(69, tour.TotalSubtours * arrivalPeriodShift);
                    arrivalComponent.AddUtilityTerm(72, income100KPlusFlag * earlyArriveFlag);
                    arrivalComponent.AddUtilityTerm(175, escortTourFlag * earlyArriveFlag);
                    arrivalComponent.AddUtilityTerm(176, shoppingTourFlag * earlyArriveFlag);
                    arrivalComponent.AddUtilityTerm(177, mealTourFlag * earlyArriveFlag);
                    arrivalComponent.AddUtilityTerm(85, sovOrHovTourFlag * arrivalImpedance.GeneralizedTimeFromOrigin * tour.TimeCoefficient);
                    arrivalComponent.AddUtilityTerm(87, transitTourFlag * Math.Max(0, arrivalImpedance.GeneralizedTimeFromOrigin) * tour.TimeCoefficient);
                    arrivalComponent.AddUtilityTerm(89, transitTourFlag * (arrivalImpedance.GeneralizedTimeFromOrigin < 0).ToFlag());
                    arrivalComponent.AddUtilityTerm(91, Math.Log(Math.Max(1, arrivalPeriodAvailableMinutes)));
                    arrivalComponent.AddUtilityTerm(93, arrivalImpedance.AdjacentMinutesBefore * firstSimulatedHomeBasedTourFlag);
                    arrivalComponent.AddUtilityTerm(95, arrivalImpedance.AdjacentMinutesBefore * laterSimulatedHomeBasedTourFlag);
                    arrivalComponent.AddUtilityTerm(99, (totalStopPurposes - totalSimulatedStops) / (1D + arrivalImpedance.AdjacentMinutesBefore));
                }

                // set departure period component
                var departurePeriodIndex            = periodIndex;
                var departurePeriod                 = DayPeriod.SmallDayPeriods[departurePeriodIndex];
                var lateDepartFlag                  = departurePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.NinePM, Global.Settings.Times.MinutesInADay).ToFlag();
                var departureImpedance              = impedances[departurePeriodIndex];
                var departurePeriodAvailableMinutes = timeWindow.TotalAvailableMinutes(departurePeriod.Start, departurePeriod.End);

                choiceProbabilityCalculator.CreateUtilityComponent(3 * periodIndex + 1);
                var departureComponent = choiceProbabilityCalculator.GetUtilityComponent(3 * periodIndex + 1);

                if (departurePeriodAvailableMinutes > 0 || Global.Configuration.IsInEstimationMode)
                {
                    departureComponent.AddUtilityTerm(21, departurePeriod.Middle.IsBetween(Global.Settings.Times.ThreeAM, Global.Settings.Times.SevenAM).ToFlag());
                    departureComponent.AddUtilityTerm(22, departurePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.SevenAM, Global.Settings.Times.TenAM).ToFlag());
                    departureComponent.AddUtilityTerm(23, departurePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.TenAM, Global.Settings.Times.OnePM).ToFlag());
                    departureComponent.AddUtilityTerm(24, departurePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.OnePM, Global.Settings.Times.ThreePM).ToFlag());
                    departureComponent.AddUtilityTerm(124, departurePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.ThreePM, Global.Settings.Times.FourPM).ToFlag());
                    departureComponent.AddUtilityTerm(25, departurePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.FourPM, Global.Settings.Times.FivePM).ToFlag());
                    departureComponent.AddUtilityTerm(26, departurePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.FivePM, Global.Settings.Times.SixPM).ToFlag());
                    departureComponent.AddUtilityTerm(27, departurePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.SixPM, Global.Settings.Times.SevenPM).ToFlag());
                    departureComponent.AddUtilityTerm(28, departurePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.SevenPM, Global.Settings.Times.NinePM).ToFlag());
                    departureComponent.AddUtilityTerm(29, departurePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.NinePM, Global.Settings.Times.Midnight).ToFlag());
                    departureComponent.AddUtilityTerm(30, departurePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.Midnight, Global.Settings.Times.MinutesInADay).ToFlag());
                    departureComponent.AddUtilityTerm(73, income100KPlusFlag * lateDepartFlag);
                    departureComponent.AddUtilityTerm(178, escortTourFlag * lateDepartFlag);
                    departureComponent.AddUtilityTerm(179, shoppingTourFlag * lateDepartFlag);
                    departureComponent.AddUtilityTerm(180, mealTourFlag * lateDepartFlag);
                    departureComponent.AddUtilityTerm(86, sovOrHovTourFlag * departureImpedance.GeneralizedTimeFromDestination * tour.TimeCoefficient);
                    departureComponent.AddUtilityTerm(88, transitTourFlag * Math.Max(0, departureImpedance.GeneralizedTimeFromDestination) * tour.TimeCoefficient);
                    departureComponent.AddUtilityTerm(89, transitTourFlag * (departureImpedance.GeneralizedTimeFromDestination < 0).ToFlag());
                    departureComponent.AddUtilityTerm(92, Math.Log(Math.Max(1, departurePeriodAvailableMinutes)));
                    departureComponent.AddUtilityTerm(94, departureImpedance.AdjacentMinutesAfter * firstSimulatedHomeBasedTourFlag);
                    departureComponent.AddUtilityTerm(96, departureImpedance.AdjacentMinutesAfter * laterSimulatedHomeBasedTourFlag);
                    departureComponent.AddUtilityTerm(100, (totalStopPurposes - totalSimulatedStops) / (1D + departureImpedance.AdjacentMinutesAfter));
                }

                // set duration component (relative to period 1)
                var periodSpan = periodIndex * (48.0 / DayPeriod.SMALL_DAY_PERIOD_TOTAL_TRIP_TIMES);                 //adjust shift amount if period lengths change;
                if (arrivalPeriodIndex == 0)
                {
                    period1Middle = arrivalPeriod.Middle;
                }
                var duration = departurePeriod.Middle - period1Middle;
                var durationUnder1HourFlag  = ChoiceModelUtility.GetDurationUnder1HourFlag(duration);
                var duration1To2HoursFlag   = ChoiceModelUtility.GetDuration1To2HoursFlag(duration);
                var durationUnder4HoursFlag = ChoiceModelUtility.GetDurationUnder4HoursFlag(duration);
                var durationUnder8HoursFlag = ChoiceModelUtility.GetDurationUnder8HoursFlag(duration);
                var durationUnder9HoursFlag = ChoiceModelUtility.GetDurationUnder9HoursFlag(duration);

                choiceProbabilityCalculator.CreateUtilityComponent(3 * periodIndex + 2);
                var durationComponent = choiceProbabilityCalculator.GetUtilityComponent(3 * periodIndex + 2);

                if (tour.IsWorkPurpose() || tour.IsSchoolPurpose())
                {
                    durationComponent.AddUtilityTerm(31, duration.IsRightExclusiveBetween(Global.Settings.Times.ZeroHours, Global.Settings.Times.ThreeHours).ToFlag());
                    durationComponent.AddUtilityTerm(32, duration.IsRightExclusiveBetween(Global.Settings.Times.ThreeHours, Global.Settings.Times.FiveHours).ToFlag());
                    durationComponent.AddUtilityTerm(33, duration.IsRightExclusiveBetween(Global.Settings.Times.FiveHours, Global.Settings.Times.SevenHours).ToFlag());
                    durationComponent.AddUtilityTerm(34, duration.IsRightExclusiveBetween(Global.Settings.Times.SevenHours, Global.Settings.Times.NineHours).ToFlag());
                    durationComponent.AddUtilityTerm(35, duration.IsRightExclusiveBetween(Global.Settings.Times.NineHours, Global.Settings.Times.TenHours).ToFlag());
                    durationComponent.AddUtilityTerm(36, duration.IsRightExclusiveBetween(Global.Settings.Times.TenHours, Global.Settings.Times.ElevenHours).ToFlag());
                    durationComponent.AddUtilityTerm(37, duration.IsRightExclusiveBetween(Global.Settings.Times.ElevenHours, Global.Settings.Times.TwelveHours).ToFlag());
                    durationComponent.AddUtilityTerm(38, duration.IsRightExclusiveBetween(Global.Settings.Times.TwelveHours, Global.Settings.Times.FourteenHours).ToFlag());
                    durationComponent.AddUtilityTerm(39, duration.IsRightExclusiveBetween(Global.Settings.Times.FourteenHours, Global.Settings.Times.EighteenHours).ToFlag());
                    durationComponent.AddUtilityTerm(40, (duration >= Global.Settings.Times.EighteenHours).ToFlag());
                }
                else
                {
                    durationComponent.AddUtilityTerm(31, duration.IsRightExclusiveBetween(Global.Settings.Times.ZeroHours, Global.Settings.Times.OneHour).ToFlag());
                    durationComponent.AddUtilityTerm(32, duration.IsRightExclusiveBetween(Global.Settings.Times.OneHour, Global.Settings.Times.TwoHours).ToFlag());
                    durationComponent.AddUtilityTerm(33, duration.IsRightExclusiveBetween(Global.Settings.Times.TwoHours, Global.Settings.Times.ThreeHours).ToFlag());
                    durationComponent.AddUtilityTerm(34, duration.IsRightExclusiveBetween(Global.Settings.Times.ThreeHours, Global.Settings.Times.FiveHours).ToFlag());
                    durationComponent.AddUtilityTerm(35, duration.IsRightExclusiveBetween(Global.Settings.Times.FiveHours, Global.Settings.Times.SevenHours).ToFlag());
                    durationComponent.AddUtilityTerm(36, duration.IsRightExclusiveBetween(Global.Settings.Times.SevenHours, Global.Settings.Times.NineHours).ToFlag());
                    durationComponent.AddUtilityTerm(37, duration.IsRightExclusiveBetween(Global.Settings.Times.NineHours, Global.Settings.Times.TwelveHours).ToFlag());
                    durationComponent.AddUtilityTerm(38, duration.IsRightExclusiveBetween(Global.Settings.Times.TwelveHours, Global.Settings.Times.FourteenHours).ToFlag());
                    durationComponent.AddUtilityTerm(39, duration.IsRightExclusiveBetween(Global.Settings.Times.FourteenHours, Global.Settings.Times.EighteenHours).ToFlag());
                    durationComponent.AddUtilityTerm(40, (duration >= Global.Settings.Times.EighteenHours).ToFlag());
                }

                durationComponent.AddUtilityTerm(42, partTimeWorkerFlag * periodSpan);
                durationComponent.AddUtilityTerm(44, nonworkingAdultFlag * periodSpan);
                durationComponent.AddUtilityTerm(46, universityStudentFlag * periodSpan);
                durationComponent.AddUtilityTerm(48, retiredAdultFlag * periodSpan);
                durationComponent.AddUtilityTerm(50, drivingAgeStudentFlag * periodSpan);
                durationComponent.AddUtilityTerm(140, fulltimeWorkerFlag * periodSpan);
                durationComponent.AddUtilityTerm(142, childAge5Through15Flag * periodSpan);
                durationComponent.AddUtilityTerm(144, childUnder5Flag * periodSpan);
                durationComponent.AddUtilityTerm(146, escortTourFlag * periodSpan);
                durationComponent.AddUtilityTerm(148, shoppingTourFlag * periodSpan);
                durationComponent.AddUtilityTerm(150, mealTourFlag * periodSpan);
                durationComponent.AddUtilityTerm(152, socialTourFlag * periodSpan);
                durationComponent.AddUtilityTerm(154, personalBusinessTourFlag * periodSpan);
                durationComponent.AddUtilityTerm(52, income0To25KFlag * periodSpan);
                durationComponent.AddUtilityTerm(54, income100KPlusFlag * periodSpan);
                durationComponent.AddUtilityTerm(56, highPrioritySameFlag * periodSpan);
                durationComponent.AddUtilityTerm(58, lowPrioritySameFlag * periodSpan);
                durationComponent.AddUtilityTerm(156, highPriorityDifferentFlag * periodSpan);
                durationComponent.AddUtilityTerm(158, lowPriorityDifferentFlag * periodSpan);
                durationComponent.AddUtilityTerm(60, homeBasedToursOnlyFlag * periodSpan);
                durationComponent.AddUtilityTerm(62, totalStopPurposes * homeBasedToursOnlyFlag * periodSpan);
                durationComponent.AddUtilityTerm(64, totalStopPurposes * (1 - homeBasedToursOnlyFlag) * periodSpan);
                durationComponent.AddUtilityTerm(66, (totalStopPurposes - totalSimulatedStops) * (1 - homeBasedToursOnlyFlag) * periodSpan);
                durationComponent.AddUtilityTerm(68, escortStops * periodSpan);
                durationComponent.AddUtilityTerm(70, tour.TotalSubtours * periodSpan);
                durationComponent.AddUtilityTerm(71, fulltimeWorkerFlag * durationUnder9HoursFlag);
                durationComponent.AddUtilityTerm(169, escortTourFlag * durationUnder1HourFlag);
                durationComponent.AddUtilityTerm(170, shoppingTourFlag * durationUnder1HourFlag);
                durationComponent.AddUtilityTerm(171, mealTourFlag * durationUnder1HourFlag);
                durationComponent.AddUtilityTerm(172, escortTourFlag * duration1To2HoursFlag);
                durationComponent.AddUtilityTerm(173, shoppingTourFlag * duration1To2HoursFlag);
                durationComponent.AddUtilityTerm(174, mealTourFlag * duration1To2HoursFlag);
                durationComponent.AddUtilityTerm(81, highPrioritySameFlag * durationUnder8HoursFlag);
                durationComponent.AddUtilityTerm(82, lowPrioritySameFlag * durationUnder8HoursFlag);
                durationComponent.AddUtilityTerm(83, highPriorityDifferentFlag * durationUnder8HoursFlag);
                durationComponent.AddUtilityTerm(84, lowPriorityDifferentFlag * durationUnder4HoursFlag);
            }

            foreach (var time in TourTime.Times)
            {
                var available =
                    (time.ArrivalPeriod.Index < time.DeparturePeriod.Index &&
                     timeWindow.EntireSpanIsAvailable(time.ArrivalPeriod.End, time.DeparturePeriod.Start)) ||
                    (time.ArrivalPeriod.Index == time.DeparturePeriod.Index &&
                     timeWindow.TotalAvailableMinutes(time.ArrivalPeriod.Start, time.ArrivalPeriod.End) > 1);

                var alternative = choiceProbabilityCalculator.GetAlternative(time.Index, available, choice != null && choice.Equals(time));

                if (!alternative.Available && !Global.Configuration.IsInEstimationMode)
                {
                    continue;
                }

                var arrivalPeriodIndex   = time.ArrivalPeriod.Index;
                var arrivalImpedance     = impedances[arrivalPeriodIndex];
                var departurePeriodIndex = time.DeparturePeriod.Index;
                var departureImpedance   = impedances[departurePeriodIndex];

                alternative.Choice = time;

                // arrival period utility component
                alternative.AddUtilityComponent(choiceProbabilityCalculator.GetUtilityComponent(3 * time.ArrivalPeriod.Index + 0));

                // departure period utility component
                alternative.AddUtilityComponent(choiceProbabilityCalculator.GetUtilityComponent(3 * time.DeparturePeriod.Index + 1));

                // duration utility components
                alternative.AddUtilityComponent(choiceProbabilityCalculator.GetUtilityComponent(3 * (time.DeparturePeriod.Index - time.ArrivalPeriod.Index) + 2));

                alternative.AddUtilityTerm(97, Math.Min(0.3, (homeBasedTours - simulatedHomeBasedTours) / (1.0 + arrivalImpedance.TotalMinutesBefore + departureImpedance.TotalMinutesAfter)));
                alternative.AddUtilityTerm(98, Math.Min(0.3, (homeBasedTours - simulatedHomeBasedTours) / (1.0 + Math.Max(arrivalImpedance.MaxMinutesBefore, departureImpedance.MaxMinutesAfter))));
            }
        }
Exemplo n.º 5
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());
                    }
                }
            }
        }
Exemplo n.º 6
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITripWrapper trip, IEnumerable <IPathTypeModel> pathTypeModels, IParcelWrapper originParcel, IParcelWrapper destinationParcel,
                              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 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 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();
            int homeBasedNotWorkSchoolEscortTourFlag = (tour.IsHomeBasedTour && tour.DestinationPurpose > Global.Settings.Purposes.Escort).ToFlag();
            int jointTourFlag                        = (tour.JointTourSequence > 0) ? 1 : 0;
            int partialHalfTourFlag                  = (trip.IsHalfTourFromOrigin ? tour.PartialHalfTour1Sequence > 0 : tour.PartialHalfTour2Sequence > 0) ? 1 : 0;
            int fullHalfTourFlag                     = (trip.IsHalfTourFromOrigin ? tour.FullHalfTour1Sequence > 0 : tour.FullHalfTour2Sequence > 0) ? 1 : 0;

            // 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
            int departureTime = trip.IsHalfTourFromOrigin ? trip.LatestDepartureTime : trip.EarliestDepartureTime;

            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];

            bool isLastTripInTour = (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips >= 1 && trip.IsToTourOrigin);
            int  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);
            }

            // 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 (int 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
            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] &&
                                            (trip.IsHalfTourFromOrigin
                      ? trip.LatestDepartureTime - pathTypeExtra.PathTime >= trip.ArrivalTimeLimit
                      : trip.EarliestDepartureTime + pathTypeExtra.PathTime <= trip.ArrivalTimeLimit);
            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.Available && tripModeAvailable[mode] &&
                                 (trip.IsHalfTourFromOrigin
                          ? trip.LatestDepartureTime - pathTypeModel.PathTime >= trip.ArrivalTimeLimit
                          : trip.EarliestDepartureTime + pathTypeModel.PathTime <= trip.ArrivalTimeLimit);
                double 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));
                    alternative.AddUtilityTerm(181, jointTourFlag);
                    alternative.AddUtilityTerm(182, fullHalfTourFlag + partialHalfTourFlag);
                }
                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));
                    alternative.AddUtilityTerm(183, jointTourFlag);
                    alternative.AddUtilityTerm(184, fullHalfTourFlag + partialHalfTourFlag);
                }
                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);
                    alternative.AddUtilityTerm(185, jointTourFlag);
                    alternative.AddUtilityTerm(186, fullHalfTourFlag + partialHalfTourFlag);
                }
                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);
                    alternative.AddUtilityTerm(187, jointTourFlag);
                    alternative.AddUtilityTerm(188, fullHalfTourFlag + partialHalfTourFlag);
                }
                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);
                    alternative.AddUtilityTerm(187, jointTourFlag);
                    alternative.AddUtilityTerm(188, fullHalfTourFlag + partialHalfTourFlag);
                }
            }
        }
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITripWrapper trip, IHouseholdDayWrapper householdDay, int choice = Constants.DEFAULT_VALUE)
        {
            IHouseholdWrapper household = trip.Household;
            IPersonWrapper    person    = trip.Person;
            IPersonDayWrapper personDay = trip.PersonDay;
            ITourWrapper      tour      = trip.Tour;

            Framework.DomainModels.Models.IHalfTour halfTour = trip.HalfTour;
            List <IPersonDayWrapper> personDays = householdDay.PersonDays;

            int            isJointTour       = tour.JointTourSequence > 0 ? 1 : 0;
            IParcelWrapper destinationParcel = tour.DestinationParcel;
            int            fullJointHalfTour = ((trip.Direction == Global.Settings.TourDirections.OriginToDestination && tour.FullHalfTour1Sequence > 0) ||
                                                (trip.Direction == Global.Settings.TourDirections.DestinationToOrigin && tour.FullHalfTour2Sequence > 0)).ToFlag();
            int partialJointHalfTour = ((trip.Direction == Global.Settings.TourDirections.OriginToDestination && tour.PartialHalfTour1Sequence > 0) ||
                                        (trip.Direction == Global.Settings.TourDirections.DestinationToOrigin && tour.PartialHalfTour2Sequence > 0)).ToFlag();
            int isJoint      = (isJointTour + fullJointHalfTour + partialJointHalfTour > 0) ? 1 : 0;
            int isIndividual = 1 - isJoint;

            //destination parcel variables
            double foodBuffer2   = 0.0;
            double totEmpBuffer2 = 0.0;
            double retailBuffer2 = 0.0;

            if (destinationParcel != null)
            {
                foodBuffer2   = Math.Log(1 + destinationParcel.EmploymentFoodBuffer2);
                totEmpBuffer2 = Math.Log(1 + destinationParcel.EmploymentTotalBuffer2);
                retailBuffer2 = Math.Log(1 + destinationParcel.EmploymentRetailBuffer2);
            }

            int carOwnership = person.GetCarOwnershipSegment();

            // household inputs
            int onePersonHouseholdFlag = household.IsOnePersonHousehold.ToFlag();
            //var householdInc75KP = household.Has75KPlusIncome;

            int votALSegment         = tour.GetVotALSegment();
            int transitAccessSegment = household.ResidenceParcel.TransitAccessSegment();

            double totalAggregateLogsum = Global.AggregateLogsums[household.ResidenceParcel.ZoneId]
                                          [Global.Settings.Purposes.HomeBasedComposite][carOwnership][votALSegment][transitAccessSegment];

            double homeFoodBuffer2   = Math.Log(1 + household.ResidenceParcel.EmploymentFoodBuffer2);
            double homeTotEmpBuffer2 = Math.Log(1 + household.ResidenceParcel.EmploymentTotalBuffer2);
            double homeRetailBuffer2 = Math.Log(1 + household.ResidenceParcel.EmploymentRetailBuffer2);

            // person-day inputs
            int homeBasedTours                 = personDay.HomeBasedTours;
            int simulatedToursFlag             = personDay.SimulatedToursExist().ToFlag();
            int simulatedWorkStops             = personDay.SimulatedWorkStops;
            int simulatedWorkStopsFlag         = personDay.SimulatedWorkStopsExist().ToFlag();
            int simulatedSchoolStops           = personDay.SimulatedSchoolStops;
            int simulatedEscortStops           = personDay.SimulatedEscortStops;
            int simulatedPersonalBusinessStops = personDay.SimulatedPersonalBusinessStops;
            int simulatedShoppingStops         = personDay.SimulatedShoppingStops;
            int simulatedMealStops             = personDay.SimulatedMealStops;
            int simulatedSocialStops           = personDay.SimulatedSocialStops;
            int simulatedRecreationStops       = personDay.SimulatedRecreationStops;
            int simulatedMedicalStops          = personDay.SimulatedMedicalStops;

            // tour inputs
            int hov2TourFlag         = tour.IsHov2Mode().ToFlag();
            int hov3TourFlag         = tour.IsHov3Mode().ToFlag();
            int transitTourFlag      = tour.IsTransitMode().ToFlag();
            int notHomeBasedTourFlag = (!tour.IsHomeBasedTour).ToFlag();
            int workTourFlag         = tour.IsWorkPurpose().ToFlag();
            int personalBusinessOrMedicalTourFlag = tour.IsPersonalBusinessOrMedicalPurpose().ToFlag();
            int socialTourFlag             = tour.IsSocialPurpose().ToFlag();
            int socialOrRecreationTourFlag = tour.IsSocialOrRecreationPurpose().ToFlag();
            int schoolTourFlag             = tour.IsSchoolPurpose().ToFlag();
            int escortTourFlag             = tour.IsEscortPurpose().ToFlag();
            int shoppingTourFlag           = tour.IsShoppingPurpose().ToFlag();
            int mealTourFlag = tour.IsMealPurpose().ToFlag();

            // trip inputs
            int oneSimulatedTripFlag           = halfTour.OneSimulatedTripFlag;
            int twoSimulatedTripsFlag          = halfTour.TwoSimulatedTripsFlag;
            int threeSimulatedTripsFlag        = halfTour.ThreeSimulatedTripsFlag;
            int fourSimulatedTripsFlag         = halfTour.FourSimulatedTripsFlag;
            int fivePlusSimulatedTripsFlag     = halfTour.FivePlusSimulatedTripsFlag; //JLB changed 5 to 5Plus
            int halfTourFromOriginFlag         = trip.IsHalfTourFromOrigin.ToFlag();
            int halfTourFromDestinationFlag    = (!trip.IsHalfTourFromOrigin).ToFlag();
            int beforeMandatoryDestinationFlag = trip.IsBeforeMandatoryDestination().ToFlag();

            // remaining inputs, including joint tour variables
            int time = trip.IsHalfTourFromOrigin ? tour.DestinationArrivalTime : tour.DestinationDepartureTime;

            int from7AMto9AMFlag  = (time >= Global.Settings.Times.SevenAM && time < Global.Settings.Times.NineAM).ToFlag();
            int from9AMto11AMFlag = (time >= Global.Settings.Times.NineAM && time < Global.Settings.Times.ElevenAM).ToFlag();
            int from11AMto1PMFlag = (time >= Global.Settings.Times.ElevenAM && time < Global.Settings.Times.OnePM).ToFlag();
            int from1PMto3PMFlag  = (time >= Global.Settings.Times.OnePM && time < Global.Settings.Times.ThreePM).ToFlag();
            int from3PMto5PMFlag  = (time >= Global.Settings.Times.ThreePM && time < Global.Settings.Times.FivePM).ToFlag();
            int from7PMto9PMFlag  = (time >= Global.Settings.Times.SevenPM && time < Global.Settings.Times.NinePM).ToFlag();
            int from9PMto11PMFlag = (time >= Global.Settings.Times.NinePM && time < Global.Settings.Times.ElevenPM).ToFlag();
            int from11PMto7AMFlag = (time >= Global.Settings.Times.ElevenPM).ToFlag();

            int remainingToursCount = personDay.HomeBasedTours - personDay.GetTotalSimulatedTours();

            // connectivity attributes
            double c34Ratio = trip.OriginParcel.C34RatioBuffer1();

            double adis    = 0.0;
            double logDist = 0.0;
            int    minute  = DayPeriod.BigDayPeriods[DayPeriod.MIDDAY].Start;

            //distance from origin to destination
            if (tour.OriginParcel != null && tour.DestinationParcel != null)
            {
                if (trip.Direction == Global.Settings.TourDirections.OriginToDestination)
                {
                    adis = ImpedanceRoster.GetValue("distance", Global.Settings.Modes.Sov, Global.Settings.PathTypes.FullNetwork, Global.Settings.ValueOfTimes.DefaultVot, minute, tour.OriginParcel, tour.DestinationParcel).Variable;
                }
                else
                {
                    adis = ImpedanceRoster.GetValue("distance", Global.Settings.Modes.Sov, Global.Settings.PathTypes.FullNetwork, Global.Settings.ValueOfTimes.DefaultVot, minute, destinationParcel, tour.OriginParcel).Variable;
                }
                logDist = Math.Log(1 + adis);
            }

            double shortestTravelTime = 0D;

            if (trip.Sequence > 1 && tour.OriginParcel != null && trip.OriginParcel != null)
            {
                if (trip.Direction == Global.Settings.TourDirections.OriginToDestination)
                {
                    shortestTravelTime = ImpedanceRoster.GetValue("ivtime", Global.Settings.Modes.Hov2, Global.Settings.PathTypes.FullNetwork, Global.Settings.ValueOfTimes.DefaultVot, minute, tour.OriginParcel, trip.OriginParcel).Variable;
                }
                else
                {
                    shortestTravelTime = ImpedanceRoster.GetValue("ivtime", Global.Settings.Modes.Hov2, Global.Settings.PathTypes.FullNetwork, Global.Settings.ValueOfTimes.DefaultVot, minute, trip.OriginParcel, tour.OriginParcel).Variable;
                }
            }

            double windowDuration = Math.Max(1.0,
                                             trip.IsHalfTourFromOrigin // first trip in half tour, use tour destination time
                    ? trip.Sequence == 1
                          ? tour.DestinationArrivalTime - tour.EarliestOriginDepartureTime - tour.IndicatedTravelTimeToDestination
                          : trip.GetPreviousTrip().ArrivalTime - tour.EarliestOriginDepartureTime - shortestTravelTime
                    : trip.Sequence == 1
                          ? tour.LatestOriginArrivalTime - tour.DestinationDepartureTime - tour.IndicatedTravelTimeFromDestination
                          : tour.LatestOriginArrivalTime - trip.GetPreviousTrip().ArrivalTime - shortestTravelTime);

            double duration = windowDuration / 60D;

            IEnumerable <PersonDayWrapper> orderedPersonDays = householdDay.PersonDays.OrderBy(p => p.GetJointTourParticipationPriority()).ToList().Cast <PersonDayWrapper>();
            int numChildrenOnJointTour = 0;
            int numAdultsOnJointTour   = 0;
            int totHHToursJT           = 0;

            //int totHHStopsJT=0;

            //MB use calculated variables at tour level rather than redoing time window
            //TimeWindow timeWindow = new TimeWindow();
            if (tour.JointTourSequence > 0)
            {
                foreach (PersonDayWrapper pDay in householdDay.PersonDays)
                {
                    ITourWrapper tInJoint = pDay.Tours.Find(t => t.JointTourSequence == tour.JointTourSequence);
                    if (!(tInJoint == null))
                    {
                        // set jointTour time window
                        //timeWindow.IncorporateAnotherTimeWindow(tInJoint.PersonDay.TimeWindow);
                        totHHToursJT = personDay.HomeBasedTours + totHHToursJT;

                        if (pDay.Person.Age < 18)
                        {
                            numChildrenOnJointTour++;
                        }

                        if (pDay.Person.Age >= 18)
                        {
                            numAdultsOnJointTour++;
                        }
                    }
                }
            }
            else if (trip.Direction == Global.Settings.TourDirections.OriginToDestination && tour.FullHalfTour1Sequence > 0)
            {
                foreach (PersonDayWrapper pDay in householdDay.PersonDays)
                {
                    ITourWrapper tInJoint = pDay.Tours.Find(t => t.FullHalfTour1Sequence == tour.FullHalfTour1Sequence);
                    if (!(tInJoint == null))
                    {
                        // set jointTour time window
                        //timeWindow.IncorporateAnotherTimeWindow(tInJoint.PersonDay.TimeWindow);
                        totHHToursJT = personDay.HomeBasedTours + totHHToursJT;

                        if (pDay.Person.Age < 18)
                        {
                            numChildrenOnJointTour++;
                        }

                        if (pDay.Person.Age >= 18)
                        {
                            numAdultsOnJointTour++;
                        }
                    }
                }
            }
            else if (trip.Direction == Global.Settings.TourDirections.DestinationToOrigin && tour.FullHalfTour2Sequence > 0)
            {
                foreach (PersonDayWrapper pDay in householdDay.PersonDays)
                {
                    ITourWrapper tInJoint = pDay.Tours.Find(t => t.FullHalfTour2Sequence == tour.FullHalfTour2Sequence);
                    if (!(tInJoint == null))
                    {
                        // set jointTour time window
                        //timeWindow.IncorporateAnotherTimeWindow(tInJoint.PersonDay.TimeWindow);
                        totHHToursJT = personDay.HomeBasedTours + totHHToursJT;

                        if (pDay.Person.Age < 18)
                        {
                            numChildrenOnJointTour++;
                        }

                        if (pDay.Person.Age >= 18)
                        {
                            numAdultsOnJointTour++;
                        }
                    }
                }
            }
            else if (tour.ParentTour == null)
            {
                //timeWindow.IncorporateAnotherTimeWindow(personDay.TimeWindow);
            }
            else
            {
                //timeWindow.IncorporateAnotherTimeWindow(tour.ParentTour.TimeWindow);
            }

            //timeWindow.SetBusyMinutes(Global.Settings.Times.EndOfRelevantWindow, Global.Settings.Times.MinutesInADay + 1);

            // time window in minutes for yet unmodeled portion of halftour, only consider persons on this trip
            //var availableWindow = timeWindow.AvailableWindow(destinationDepartureTime, Global.Settings.TimeDirections.Both);

            //var duration = availableWindow/ 60D;


            // 0 - NO MORE STOPS

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

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

            alternative.AddUtilityTerm(1, twoSimulatedTripsFlag * halfTourFromOriginFlag * isIndividual);
            alternative.AddUtilityTerm(2, threeSimulatedTripsFlag * halfTourFromOriginFlag * isIndividual);
            alternative.AddUtilityTerm(3, fourSimulatedTripsFlag * halfTourFromOriginFlag * isIndividual);
            alternative.AddUtilityTerm(4, fivePlusSimulatedTripsFlag * halfTourFromOriginFlag * isIndividual);
            alternative.AddUtilityTerm(5, twoSimulatedTripsFlag * halfTourFromDestinationFlag * isIndividual);
            alternative.AddUtilityTerm(6, threeSimulatedTripsFlag * halfTourFromDestinationFlag * isIndividual);
            alternative.AddUtilityTerm(7, fourSimulatedTripsFlag * halfTourFromDestinationFlag * isIndividual);
            alternative.AddUtilityTerm(8, fivePlusSimulatedTripsFlag * halfTourFromDestinationFlag * isIndividual);
            alternative.AddUtilityTerm(9, homeBasedTours * isIndividual);
            alternative.AddUtilityTerm(10, homeBasedTours * isJointTour);
            alternative.AddUtilityTerm(11, notHomeBasedTourFlag);
            //alternative.AddUtilityTerm(12, beforeMandatoryDestinationFlag*isJointTour);
            alternative.AddUtilityTerm(13, beforeMandatoryDestinationFlag);
            alternative.AddUtilityTerm(14, numAdultsOnJointTour);
            alternative.AddUtilityTerm(15, numChildrenOnJointTour);
            alternative.AddUtilityTerm(16, totHHToursJT);
            //alternative.AddUtilityTerm(17, totHHStopsJT);
            alternative.AddUtilityTerm(22, (threeSimulatedTripsFlag + fourSimulatedTripsFlag + fivePlusSimulatedTripsFlag) * halfTourFromOriginFlag * isJoint);
            alternative.AddUtilityTerm(26, threeSimulatedTripsFlag * halfTourFromDestinationFlag * isJoint);
            alternative.AddUtilityTerm(27, fourSimulatedTripsFlag * halfTourFromDestinationFlag * isJoint);
            alternative.AddUtilityTerm(28, fivePlusSimulatedTripsFlag * halfTourFromDestinationFlag * isJoint);
            alternative.AddUtilityTerm(29, totalAggregateLogsum);

            // 1 - WORK STOP

            if ((personDay.WorkStops > 0 && tour.DestinationPurpose <= Global.Settings.Purposes.School) || isJoint > 0)
            {
                alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Work, true, choice == Global.Settings.Purposes.Work);

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

                //alternative.AddUtilityTerm(32, isIndividualTour);
                alternative.AddUtilityTerm(33, workTourFlag);
                alternative.AddUtilityTerm(34, schoolTourFlag);
                alternative.AddUtilityTerm(35, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(36, Math.Log(1 + simulatedWorkStops));
                alternative.AddUtilityTerm(37, simulatedWorkStopsFlag);
                alternative.AddUtilityTerm(38, (5 - simulatedWorkStops));
                alternative.AddUtilityTerm(39, duration);
                alternative.AddUtilityTerm(40, from9AMto11AMFlag + from11AMto1PMFlag + from1PMto3PMFlag + from3PMto5PMFlag);
                //alternative.AddUtilityTerm(42, logDist);
                //alternative.AddUtilityTerm(43, transitTourFlag);
                //alternative.AddUtilityTerm(44, (person.IsPartTimeWorker).ToFlag());
                alternative.AddUtilityTerm(46, totalAggregateLogsum);
                //alternative.AddUtilityTerm(47,totEmpBuffer2);
                alternative.AddUtilityTerm(48, hov2TourFlag + hov3TourFlag);
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Work, false, choice == Global.Settings.Purposes.Work);
            }

            // 2 - SCHOOL STOP

            if ((personDay.SchoolStops > 0 && tour.DestinationPurpose <= Global.Settings.Purposes.School) || isJoint > 0)
            {
                alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.School, true, choice == Global.Settings.Purposes.School);

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

                alternative.AddUtilityTerm(51, workTourFlag);
                alternative.AddUtilityTerm(52, schoolTourFlag);
                //alternative.AddUtilityTerm(53, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(54, Math.Log(1 + simulatedSchoolStops));
                alternative.AddUtilityTerm(55, (3 - simulatedSchoolStops));
                //alternative.AddUtilityTerm(55, remainingToursCount);
                alternative.AddUtilityTerm(56, duration);
                alternative.AddUtilityTerm(57, from7AMto9AMFlag + from7PMto9PMFlag + from9PMto11PMFlag + from11PMto7AMFlag);
                alternative.AddUtilityTerm(58, oneSimulatedTripFlag);
                //alternative.AddUtilityTerm(59, logDist);
                alternative.AddUtilityTerm(61, fullJointHalfTour * numChildrenOnJointTour);
                alternative.AddUtilityTerm(65, (person.Age < 12).ToFlag());
                alternative.AddUtilityTerm(66, totalAggregateLogsum);
                //alternative.AddUtilityTerm(66,  (person.IsUniversityStudent).ToFlag());
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.School, false, choice == Global.Settings.Purposes.School);
            }

            // 3 - ESCORT STOP

            if ((personDay.EscortStops > 0 && tour.DestinationPurpose <= Global.Settings.Purposes.Escort) || isJoint > 0)
            {
                alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Escort, true, choice == Global.Settings.Purposes.Escort);

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

                alternative.AddUtilityTerm(71, workTourFlag + schoolTourFlag);
                alternative.AddUtilityTerm(72, isJointTour);
                alternative.AddUtilityTerm(74, escortTourFlag);
                //alternative.AddUtilityTerm(75, socialOrRecreationTourFlag);
                //alternative.AddUtilityTerm(76, remainingToursCount);
                alternative.AddUtilityTerm(77, duration);
                alternative.AddUtilityTerm(78, from7AMto9AMFlag);
                //alternative.AddUtilityTerm(79, from9AMto11AMFlag + from11AMto1PMFlag + from1PMto3PMFlag + from3PMto5PMFlag);
                alternative.AddUtilityTerm(81, hov2TourFlag);
                alternative.AddUtilityTerm(82, hov3TourFlag);
                alternative.AddUtilityTerm(83, Math.Log(1 + simulatedEscortStops * isJointTour));
                alternative.AddUtilityTerm(84, Math.Log(1 + simulatedEscortStops * isIndividual));
                //alternative.AddUtilityTerm(85, (3 - simulatedEscortStops));

                alternative.AddUtilityTerm(85, totalAggregateLogsum);
                alternative.AddUtilityTerm(86, fullJointHalfTour);
                //alternative.AddUtilityTerm(88, enrollmentK8Buffer2);
                alternative.AddUtilityTerm(89, numChildrenOnJointTour);
                alternative.AddUtilityTerm(90, halfTourFromOriginFlag);
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Escort, false, choice == Global.Settings.Purposes.Escort);
            }

            // 4 - PERSONAL BUSINESS STOP

            if (personDay.PersonalBusinessStops > 0 || isJoint > 0)
            {
                alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.PersonalBusiness, true, choice == Global.Settings.Purposes.PersonalBusiness);

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

                alternative.AddUtilityTerm(91, (workTourFlag + schoolTourFlag));
                alternative.AddUtilityTerm(92, isJointTour);
                alternative.AddUtilityTerm(93, escortTourFlag);
                alternative.AddUtilityTerm(94, personalBusinessOrMedicalTourFlag * isIndividual);
                alternative.AddUtilityTerm(95, shoppingTourFlag);
                alternative.AddUtilityTerm(96, mealTourFlag);
                alternative.AddUtilityTerm(97, socialOrRecreationTourFlag);
                alternative.AddUtilityTerm(98, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(99, Math.Log(1 + simulatedPersonalBusinessStops * isIndividual));
                alternative.AddUtilityTerm(100, Math.Log(1 + simulatedPersonalBusinessStops * isJointTour));
                alternative.AddUtilityTerm(104, (3 - simulatedPersonalBusinessStops));
                alternative.AddUtilityTerm(101, duration);
                alternative.AddUtilityTerm(102, (from7AMto9AMFlag + from7PMto9PMFlag + from9PMto11PMFlag + from11PMto7AMFlag));
                alternative.AddUtilityTerm(103, from9AMto11AMFlag + from11AMto1PMFlag + from1PMto3PMFlag + from3PMto5PMFlag);
                alternative.AddUtilityTerm(105, hov2TourFlag);
                alternative.AddUtilityTerm(106, hov3TourFlag);
                alternative.AddUtilityTerm(109, fullJointHalfTour);
                alternative.AddUtilityTerm(110, totEmpBuffer2);
                alternative.AddUtilityTerm(111, totalAggregateLogsum);
                alternative.AddUtilityTerm(112, personalBusinessOrMedicalTourFlag * isJointTour);
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.PersonalBusiness, false, choice == Global.Settings.Purposes.PersonalBusiness);
            }

            // 5 - SHOPPING STOP

            if (personDay.ShoppingStops > 0 || isJoint > 0)
            {
                alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Shopping, true, choice == Global.Settings.Purposes.Shopping);

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

                alternative.AddUtilityTerm(121, workTourFlag + schoolTourFlag);
                alternative.AddUtilityTerm(122, isJointTour);
                alternative.AddUtilityTerm(123, escortTourFlag);
                alternative.AddUtilityTerm(124, personalBusinessOrMedicalTourFlag);
                alternative.AddUtilityTerm(125, shoppingTourFlag * isIndividual);
                alternative.AddUtilityTerm(126, mealTourFlag);
                alternative.AddUtilityTerm(127, socialOrRecreationTourFlag);
                alternative.AddUtilityTerm(128, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(129, Math.Log(1 + simulatedShoppingStops * isIndividual));
                alternative.AddUtilityTerm(130, Math.Log(1 + simulatedShoppingStops * isJointTour));
                alternative.AddUtilityTerm(131, duration);
                alternative.AddUtilityTerm(132, from7AMto9AMFlag + from9PMto11PMFlag + from11PMto7AMFlag);
                alternative.AddUtilityTerm(133, (from11AMto1PMFlag + from1PMto3PMFlag + from3PMto5PMFlag));
                //alternative.AddUtilityTerm(134, adultFemaleOnJointTour);
                alternative.AddUtilityTerm(135, hov2TourFlag);
                alternative.AddUtilityTerm(136, hov3TourFlag);
                alternative.AddUtilityTerm(137, Math.Log(1 + adis));
                alternative.AddUtilityTerm(138, shoppingTourFlag * isJointTour);
                alternative.AddUtilityTerm(140, totalAggregateLogsum);
                //alternative.AddUtilityTerm(141, retailBuffer2);
                //alternative.AddUtilityTerm(142, numChildrenOnJointTour);
                //alternative.AddUtilityTerm(143, (household.Has100KPlusIncome).ToFlag());
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Shopping, false, choice == Global.Settings.Purposes.Shopping);
            }

            // 6 - MEAL STOP

            if (personDay.MealStops > 0 || isJoint > 0)
            {
                alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Meal, true, choice == Global.Settings.Purposes.Meal);

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

                alternative.AddUtilityTerm(151, workTourFlag);
                //alternative.AddUtilityTerm(152, isJointTour);
                alternative.AddUtilityTerm(153, schoolTourFlag);
                alternative.AddUtilityTerm(154, escortTourFlag);
                alternative.AddUtilityTerm(155, personalBusinessOrMedicalTourFlag);
                alternative.AddUtilityTerm(156, shoppingTourFlag);
                alternative.AddUtilityTerm(157, mealTourFlag);
                alternative.AddUtilityTerm(158, socialOrRecreationTourFlag);
                alternative.AddUtilityTerm(159, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(160, Math.Log(1 + simulatedMealStops * isIndividual));
                alternative.AddUtilityTerm(161, Math.Log(1 + simulatedMealStops * isJointTour));
                alternative.AddUtilityTerm(162, duration);
                alternative.AddUtilityTerm(164, from11AMto1PMFlag + from1PMto3PMFlag);
                alternative.AddUtilityTerm(166, onePersonHouseholdFlag);
                alternative.AddUtilityTerm(167, hov2TourFlag);
                alternative.AddUtilityTerm(168, hov3TourFlag);
                alternative.AddUtilityTerm(170, numChildrenOnJointTour);
                alternative.AddUtilityTerm(171, oneSimulatedTripFlag);
                alternative.AddUtilityTerm(172, Math.Log(1 + adis));
                alternative.AddUtilityTerm(174, fullJointHalfTour);
                alternative.AddUtilityTerm(175, foodBuffer2);
                //alternative.AddUtilityTerm(176, homeFoodBuffer2);
                //alternative.AddUtilityTerm(177, (household.Has100KPlusIncome).ToFlag());
                alternative.AddUtilityTerm(178, numAdultsOnJointTour);
                alternative.AddUtilityTerm(179, totalAggregateLogsum);
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Meal, false, choice == Global.Settings.Purposes.Meal);
            }

            // 7 - SOCIAL STOP

            if (personDay.SocialStops > 0 || isJoint > 0)
            {
                alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Social, true, choice == Global.Settings.Purposes.Social);

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

                alternative.AddUtilityTerm(181, workTourFlag + schoolTourFlag);
                alternative.AddUtilityTerm(182, isJointTour);
                alternative.AddUtilityTerm(183, escortTourFlag);
                alternative.AddUtilityTerm(184, personalBusinessOrMedicalTourFlag);
                alternative.AddUtilityTerm(185, shoppingTourFlag);
                alternative.AddUtilityTerm(186, mealTourFlag);
                alternative.AddUtilityTerm(187, socialOrRecreationTourFlag);
                alternative.AddUtilityTerm(188, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(189, Math.Log(1 + simulatedSocialStops * isIndividual));
                alternative.AddUtilityTerm(197, Math.Log(1 + simulatedSocialStops * isJointTour));
                alternative.AddUtilityTerm(190, remainingToursCount);
                alternative.AddUtilityTerm(191, duration);
                alternative.AddUtilityTerm(192, from7AMto9AMFlag + from11PMto7AMFlag);
                alternative.AddUtilityTerm(194, hov2TourFlag);
                alternative.AddUtilityTerm(195, hov3TourFlag);
                alternative.AddUtilityTerm(196, logDist);
                alternative.AddUtilityTerm(198, totalAggregateLogsum);
                alternative.AddUtilityTerm(200, numAdultsOnJointTour);
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Social, false, choice == Global.Settings.Purposes.Social);
            }


            // 8 - RECREATION STOP

            if (personDay.RecreationStops > 0 || isJoint > 0)
            {
                alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Recreation, true, choice == Global.Settings.Purposes.Recreation);

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

                alternative.AddUtilityTerm(211, workTourFlag + schoolTourFlag);
                alternative.AddUtilityTerm(212, isJointTour);
                alternative.AddUtilityTerm(213, escortTourFlag);
                alternative.AddUtilityTerm(214, personalBusinessOrMedicalTourFlag);
                alternative.AddUtilityTerm(215, shoppingTourFlag);
                alternative.AddUtilityTerm(216, mealTourFlag);
                alternative.AddUtilityTerm(217, socialOrRecreationTourFlag);
                alternative.AddUtilityTerm(218, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(219, Math.Log(1 + simulatedRecreationStops * isIndividual));
                alternative.AddUtilityTerm(229, Math.Log(1 + simulatedRecreationStops * isJointTour));
                alternative.AddUtilityTerm(220, remainingToursCount);
                alternative.AddUtilityTerm(221, duration);
                alternative.AddUtilityTerm(222, from7AMto9AMFlag + from11PMto7AMFlag);
                alternative.AddUtilityTerm(223, from11AMto1PMFlag + from1PMto3PMFlag + from3PMto5PMFlag);
                alternative.AddUtilityTerm(225, hov3TourFlag);
                alternative.AddUtilityTerm(226, numChildrenOnJointTour);
                alternative.AddUtilityTerm(227, numAdultsOnJointTour);
                //alternative.AddUtilityTerm(228, fullJointHalfTour);
                //alternative.AddUtilityTerm(229, openSpaceBuffer2);
                alternative.AddUtilityTerm(228, totalAggregateLogsum);
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Recreation, false, choice == Global.Settings.Purposes.Recreation);
            }

            // 9 - MEDICAL STOP

            if (personDay.MedicalStops > 0 || isJoint > 0)
            {
                alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Medical, true, choice == Global.Settings.Purposes.Medical);

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

                alternative.AddUtilityTerm(231, workTourFlag + schoolTourFlag + escortTourFlag);
                alternative.AddUtilityTerm(232, isJointTour);
                alternative.AddUtilityTerm(233, personalBusinessOrMedicalTourFlag * isIndividual);
                alternative.AddUtilityTerm(234, personalBusinessOrMedicalTourFlag * isJointTour);
                alternative.AddUtilityTerm(235, shoppingTourFlag);
                alternative.AddUtilityTerm(236, mealTourFlag);
                alternative.AddUtilityTerm(237, socialOrRecreationTourFlag);
                alternative.AddUtilityTerm(238, halfTourFromOriginFlag);
                alternative.AddUtilityTerm(239, Math.Log(1 + simulatedMedicalStops * isJointTour));
                alternative.AddUtilityTerm(240, Math.Log(1 + simulatedMedicalStops * isIndividual));
                //alternative.AddUtilityTerm(240, fullJointHalfTour);
                alternative.AddUtilityTerm(241, duration);
                alternative.AddUtilityTerm(242, from7AMto9AMFlag + from11PMto7AMFlag);
                alternative.AddUtilityTerm(243, from11AMto1PMFlag + from1PMto3PMFlag + from3PMto5PMFlag);
                //alternative.AddUtilityTerm(248, numChildrenOnJointTour);
                //alternative.AddUtilityTerm(249, adultFemaleOnJointTour);
                alternative.AddUtilityTerm(244, totalAggregateLogsum);
            }
            else
            {
                choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Medical, false, choice == Global.Settings.Purposes.Medical);
            }
        }
Exemplo n.º 8
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, TripWrapper trip,
                              IEnumerable <IPathTypeModel> pathTypeModels, IParcelWrapper originParcel,
                              IParcelWrapper destinationParcel,
                              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 HHwithChildrenFlag      = household.HasChildren.ToFlag();
            int HHwithSmallChildrenFlag = household.HasChildrenUnder5.ToFlag();
            int childrenAge5Through15   = householdTotals.ChildrenAge5Through15;
            int HHwithLowIncomeFlag     = (household.Income >= 300000 && household.Income < 600000).ToFlag();
            int HHwithMidleIncomeFlag   = (household.Income >= 600000 && household.Income < 900000).ToFlag();
            int HHwithHighIncomeFlag    = (household.Income >= 900000).ToFlag();
            int nonworkingAdults        = householdTotals.NonworkingAdults;
            int retiredAdults           = householdTotals.RetiredAdults;
            int twoPersonHouseholdFlag  = household.IsTwoPersonHousehold.ToFlag();
            int noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(household.VehiclesAvailable);
            int carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(household.VehiclesAvailable);

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

            int PTpass = person.TransitPassOwnership;

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

            // tour inputs
            int bikeTourFlag          = tour.IsBikeMode().ToFlag();
            int walkTourFlag          = tour.IsWalkMode().ToFlag();
            int CarDrivAloneFlag      = tour.IsSovMode().ToFlag();
            int CarDrivNotAloneFlag   = tour.IsHov2Mode().ToFlag();
            int CarPassengerFlag      = tour.IsHov3Mode().ToFlag();
            int transitTourFlag       = tour.IsTransitMode().ToFlag();
            int paidRideShareTourFlag = ((IActumTourWrapper)tour).IsPaidRideShareMode().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 homeBasedSocialTourFlag              = (tour.IsHomeBasedTour && tour.IsSocialPurpose()).ToFlag();
            int notHomeBasedTourFlag                 = (!tour.IsHomeBasedTour).ToFlag();
            int homeBasedNotWorkSchoolEscortTourFlag =
                (tour.IsHomeBasedTour && tour.DestinationPurpose > Global.Settings.Purposes.Escort).ToFlag();

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


            // 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
            int departureTime = trip.IsHalfTourFromOrigin ? trip.LatestDepartureTime : trip.EarliestDepartureTime;

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

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

            bool isLastTripInTour = (!trip.IsHalfTourFromOrigin && halfTour.SimulatedTrips >= 1 && trip.IsToTourOrigin);
            int  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 for COMPAS1; JOHN restored it for COMPAS2
            //// 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 && person.Age >= 18;
                tripModeAvailable[Global.Settings.Modes.HovDriver]    = tour.Household.VehiclesAvailable > 0 && person.Age >= 18;
                tripModeAvailable[Global.Settings.Modes.HovPassenger] = !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 (int 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 (IPathTypeModel pathTypeModel in pathTypeModels)
            {
                int  mode      = pathTypeModel.Mode;
                bool available = pathTypeModel.Available && tripModeAvailable[mode] &&
                                 (trip.IsHalfTourFromOrigin
                                      ? trip.LatestDepartureTime - pathTypeModel.PathTime >= trip.ArrivalTimeLimit
                                      : trip.EarliestDepartureTime + pathTypeModel.PathTime <= trip.ArrivalTimeLimit);
                double 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);

                    //GV: income is not sign. 22. june 2016
                    //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);
                }
                else if (mode == Global.Settings.Modes.PaidRideShare)
                {
                    double 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(80, modeConstant);
                    alternative.AddUtilityTerm(80, Global.Configuration.PaidRideShare_Age26to35Coefficient * tour.Person.AgeIsBetween26And35.ToFlag());
                    alternative.AddUtilityTerm(80, Global.Configuration.PaidRideShare_Age18to25Coefficient * tour.Person.AgeIsBetween18And25.ToFlag());
                    alternative.AddUtilityTerm(80, Global.Configuration.PaidRideShare_AgeOver65Coefficient * (tour.Person.Age >= 65).ToFlag());

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