Пример #1
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, HouseholdDayWrapper householdDay, TourWrapper tour,
                              IParcelWrapper destinationParcel, int householdCars,
                              int constrainedMode, int constrainedArrivalTime, int constrainedDepartureTime, HTourModeTime choice = null)
        {
            //private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, HouseholdDayWrapper householdDay, TourWrapper tour,
            //			int constrainedMode, int constrainedArrivalTime, int constrainedDepartureTime, HTourModeTime choice = null) {

            IHouseholdWrapper household       = tour.Household;
            IPersonWrapper    person          = tour.Person;
            IPersonDayWrapper personDay       = tour.PersonDay;
            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;
            int noCarsInHouseholdFlag   = household.GetFlagForNoCarsInHousehold(householdCars);
            int carsLessThanDriversFlag = household.GetFlagForCarsLessThanDrivers(householdCars);
            int carsLessThanWorkersFlag = household.GetFlagForCarsLessThanWorkers(householdCars);

            int HHwithChildrenFlag      = household.HasChildren.ToFlag();
            int HHwithSmallChildrenFlag = household.HasChildrenUnder5.ToFlag();
            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 primaryFamilyTimeFlag = (householdDay == null) ? 0 : householdDay.PrimaryPriorityTimeFlag;

            // person inputs
            int partTimeWorkerFlag     = person.IsPartTimeWorker.ToFlag();
            int nonworkingAdultFlag    = person.IsNonworkingAdult.ToFlag();
            int universityStudentFlag  = person.IsUniversityStudent.ToFlag();
            int retiredAdultFlag       = person.IsRetiredAdult.ToFlag();
            int fullTimeWorkerFlag     = person.IsFulltimeWorker.ToFlag();
            int childAge5Through15Flag = person.IsChildAge5Through15.ToFlag();
            int childUnder5Flag        = person.IsChildUnder5.ToFlag();
            int adultFlag = person.IsAdult.ToFlag();

            int maleFlag   = person.IsMale.ToFlag();
            int femaleFlag = person.IsFemale.ToFlag();

            int PTpass = person.TransitPassOwnership;

            // person-day inputs
            int homeBasedToursOnlyFlag          = 1;
            int firstSimulatedHomeBasedTourFlag = 1;
            int laterSimulatedHomeBasedTourFlag = 0;
            int totalStops              = 0;
            int totalSimulatedStops     = 0;
            int escortStops             = 0;
            int homeBasedTours          = 1;
            int simulatedHomeBasedTours = 0;

            if (!(personDay == null))
            {
                homeBasedToursOnlyFlag          = personDay.OnlyHomeBasedToursExist().ToFlag();
                firstSimulatedHomeBasedTourFlag = personDay.IsFirstSimulatedHomeBasedTour().ToFlag();
                laterSimulatedHomeBasedTourFlag = personDay.IsLaterSimulatedHomeBasedTour().ToFlag();
                totalStops              = personDay.GetTotalStops();
                totalSimulatedStops     = personDay.GetTotalSimulatedStops();
                escortStops             = personDay.EscortStops;
                homeBasedTours          = personDay.HomeBasedTours;
                simulatedHomeBasedTours = personDay.SimulatedHomeBasedTours;
            }

            // tour inputs
            int escortTourFlag           = tour.IsEscortPurpose().ToFlag();
            int shoppingTourFlag         = tour.IsShoppingPurpose().ToFlag();
            int socialTourFlag           = tour.IsSocialPurpose().ToFlag();
            int personalBusinessTourFlag = tour.IsPersonalBusinessPurpose().ToFlag();
            int workTourFlag             = tour.IsWorkPurpose().ToFlag();
            int educationTourFlag        = tour.IsSchoolPurpose().ToFlag();
            int businessTourFlag         = tour.IsBusinessPurpose().ToFlag();

            IParcelWrapper originParcel = tour.OriginParcel;
            //var destinationParcel = tour.DestinationParcel;
            int  jointTourFlag        = (tour.JointTourSequence > 0) ? 1 : 0;
            int  partialHalfTour1Flag = (tour.PartialHalfTour1Sequence > 0) ? 1 : 0;
            int  partialHalfTour2Flag = (tour.PartialHalfTour2Sequence > 0) ? 1 : 0;
            bool partialHalfTour      = (tour.PartialHalfTour1Sequence > 0 || tour.PartialHalfTour2Sequence > 0);
            int  fullHalfTour1Flag    = (tour.FullHalfTour1Sequence > 0) ? 1 : 0;
            int  fullHalfTour2Flag    = (tour.FullHalfTour2Sequence > 0) ? 1 : 0;


            // remaining inputs

            //Initialize a few variables in case personDay is null
            // Higher priority tour of 2+ tours for the same purpose
            int highPrioritySameFlag = 0;
            // Lower priority tour(s) of 2+ tours for the same purpose
            int lowPrioritySameFlag = 0;
            // Higher priority tour of 2+ tours for different purposes
            int highPriorityDifferentFlag = 0;
            // Lower priority tour of 2+ tours for different purposes
            int lowPriorityDifferentFlag = 0;

            if (!(personDay == null))
            {
                // Higher priority tour of 2+ tours for the same purpose
                highPrioritySameFlag = (tour.GetTotalToursByPurpose() > tour.GetTotalSimulatedToursByPurpose() && tour.GetTotalSimulatedToursByPurpose() == 1).ToFlag();
                // Lower priority tour(s) of 2+ tours for the same purpose
                lowPrioritySameFlag = (tour.GetTotalSimulatedToursByPurpose() > 1).ToFlag();
                // Higher priority tour of 2+ tours for different purposes
                highPriorityDifferentFlag = (personDay.IsFirstSimulatedHomeBasedTour() && personDay.HomeBasedToursExist()).ToFlag() * (1 - highPrioritySameFlag);
                // Lower priority tour of 2+ tours for different purposes
                lowPriorityDifferentFlag = (personDay.IsLaterSimulatedHomeBasedTour() && personDay.HomeBasedToursExist()).ToFlag() * (1 - lowPrioritySameFlag);
            }

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

            if (totalMinutesAvailableInDay < 0)
            {
                if (!Global.Configuration.IsInEstimationMode)
                {
                    householdDay.IsValid = false;
                }
                totalMinutesAvailableInDay = 0;
            }

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

            bool useTimeComponents = Global.Configuration.IsInEstimationMode || constrainedArrivalTime == 0 || constrainedDepartureTime == 0;
            int  componentIndex    = 0;
            int  periodComb        = -1;

            //set components
            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, childAge5Through15Flag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 7, childUnder5Flag * hoursArrival);

                                arrivalComponent.AddUtilityTerm(firstCoef + 8, educationTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 9, escortTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 10, shoppingTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 11, businessTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 12, personalBusinessTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 13, socialTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 14, workTourFlag * hoursArrival);

                                arrivalComponent.AddUtilityTerm(firstCoef + 15, primaryFamilyTimeFlag * hoursArrival);

                                arrivalComponent.AddUtilityTerm(firstCoef + 16, HHwithLowIncomeFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 17, HHwithMidleIncomeFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 18, HHwithHighIncomeFlag * hoursArrival);

                                arrivalComponent.AddUtilityTerm(firstCoef + 19, highPrioritySameFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 20, lowPrioritySameFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 21, highPriorityDifferentFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 22, lowPriorityDifferentFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 23, jointTourFlag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 24, partialHalfTour1Flag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 25, fullHalfTour1Flag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 26, partialHalfTour2Flag * hoursArrival);
                                arrivalComponent.AddUtilityTerm(firstCoef + 27, 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 + 30, primaryFamilyTimeFlag * hoursDuration);

                            combinationComponent.AddUtilityTerm(firstCoef + 31, escortTourFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 32, shoppingTourFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 33, educationTourFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 34, socialTourFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 35, personalBusinessTourFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 36, businessTourFlag * hoursDuration);
                            combinationComponent.AddUtilityTerm(firstCoef + 37, workTourFlag * 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, partTimeWorkerFlag);
                                combinationComponent.AddUtilityTerm(firstCoef + 50, maleFlag);
                            }
                        }
                    }
                }
            }
            //for (var mode = Global.Settings.Modes.Walk; mode <= Global.Settings.Modes.WalkRideBike; mode++) {  replaced 20171120 JLB
            for (int mode = Global.Settings.Modes.Walk; mode <= Global.Settings.Modes.PaidRideShare; 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);
                //}
                if (mode == Global.Settings.Modes.Transit)
                {
                    modeComponent.AddUtilityTerm(20, 1);
                    modeComponent.AddUtilityTerm(21, femaleFlag);
                    //modeComponent.AddUtilityTerm(22, retiredAdultFlag);

                    modeComponent.AddUtilityTerm(22, PTpass);

                    //GV: one person HH; 8. juni 2016
                    //modeComponent.AddUtilityTerm(23, onePersonHouseholdFlag);

                    //GV: income effect is not with a correct sign
                    //modeComponent.AddUtilityTerm(23, HHwithLowIncomeFlag);
                    //modeComponent.AddUtilityTerm(24, HHwithMidleIncomeFlag);
                    //modeComponent.AddUtilityTerm(25, HHwithHighIncomeFlag);

                    //GV: not significant
                    //modeComponent.AddUtilityTerm(26, childrenUnder5);
                    //modeComponent.AddUtilityTerm(27, childrenAge5Through15);

                    modeComponent.AddUtilityTerm(28, nonworkingAdults + retiredAdults);

                    //modeComponent.AddUtilityTerm(26, noCarsInHouseholdFlag);
                    modeComponent.AddUtilityTerm(29, carsLessThanDriversFlag);
                }
                else if (mode == Global.Settings.Modes.Hov3)
                {
                    modeComponent.AddUtilityTerm(30, 1);
                    modeComponent.AddUtilityTerm(31, childrenUnder5);
                    modeComponent.AddUtilityTerm(32, childrenAge5Through15);
                    modeComponent.AddUtilityTerm(33, nonworkingAdults + retiredAdults);
                    modeComponent.AddUtilityTerm(34, femaleFlag);

                    //modeComponent.AddUtilityTerm(38, onePersonHouseholdFlag);
                    modeComponent.AddUtilityTerm(36, twoPersonHouseholdFlag);

                    //GV: commented out 7. june 2016
                    //modeComponent.AddUtilityTerm(37, noCarsInHouseholdFlag);
                    modeComponent.AddUtilityTerm(38, carsLessThanDriversFlag);
                }
                else if (mode == Global.Settings.Modes.Hov2)
                {
                    modeComponent.AddUtilityTerm(40, 1);
                    modeComponent.AddUtilityTerm(41, maleFlag);
                    //modeComponent.AddUtilityTerm(41, onePersonHouseholdFlag);

                    //GV: Testing coeff. 42-44 again, 26. may 2016, coeff. numbering changed
                    modeComponent.AddUtilityTerm(42, childrenUnder5);
                    modeComponent.AddUtilityTerm(43, childrenAge5Through15);
                    modeComponent.AddUtilityTerm(44, nonworkingAdults + retiredAdults);

                    //GV: these are significant and plus; 8. juni 2016
                    //modeComponent.AddUtilityTerm(45, HHwithLowIncomeFlag);
                    //modeComponent.AddUtilityTerm(46, HHwithMidleIncomeFlag);
                    //modeComponent.AddUtilityTerm(47, HHwithHighIncomeFlag);

                    //GV coeff. numbering changed; 8. june 2016 - not significant
                    //modeComponent.AddUtilityTerm(48, noCarsInHouseholdFlag);
                    //modeComponent.AddUtilityTerm(49, carsLessThanDriversFlag);
                }
                else if (mode == Global.Settings.Modes.Sov)
                {
                    modeComponent.AddUtilityTerm(50, 1);
                    //not significant: modeComponent.AddUtilityTerm(51, maleFlag);
                    modeComponent.AddUtilityTerm(52, fullTimeWorkerFlag);
                    modeComponent.AddUtilityTerm(53, partTimeWorkerFlag);
                    modeComponent.AddUtilityTerm(54, onePersonHouseholdFlag);

                    //GV: these are NOT significant
                    //modeComponent.AddUtilityTerm(55, HHwithLowIncomeFlag);
                    //modeComponent.AddUtilityTerm(56, HHwithMidleIncomeFlag);
                    //modeComponent.AddUtilityTerm(57, HHwithHighIncomeFlag);

                    //GV: coeff. numbering changed, 26. may 2016
                    modeComponent.AddUtilityTerm(58, carsLessThanWorkersFlag);
                }
                else if (mode == Global.Settings.Modes.Bike)
                {
                    modeComponent.AddUtilityTerm(60, 1);
                    modeComponent.AddUtilityTerm(61, femaleFlag);
                    //modeComponent.AddUtilityTerm(62, childrenUnder5);
                    //GV: changed small kids to retired; 3. june 2016
                    modeComponent.AddUtilityTerm(62, retiredAdults);
                    modeComponent.AddUtilityTerm(63, childAge5Through15Flag);

                    //GV: 3. june 2016 - added "no cars in HH" for full/part_time workers
                    modeComponent.AddUtilityTerm(64, fullTimeWorkerFlag + noCarsInHouseholdFlag);
                    modeComponent.AddUtilityTerm(65, partTimeWorkerFlag + noCarsInHouseholdFlag);

                    //modeComponent.AddUtilityTerm(66, noCarsInHouseholdFlag);
                    modeComponent.AddUtilityTerm(67, carsLessThanDriversFlag);

                    //GV: university students; 3. june 2016 - not significant
                    //modeComponent.AddUtilityTerm(68, universityStudentFlag);

                    //GV: one person HH; 8. juni 2016
                    modeComponent.AddUtilityTerm(69, onePersonHouseholdFlag);
                }
                else if (mode == Global.Settings.Modes.Walk)
                {
                    modeComponent.AddUtilityTerm(70, 1.0);
                    //GV: Testing female variable again; 26. may 2016 - not sign.
                    //modeComponent.AddUtilityTerm(71, femaleFlag);
                    modeComponent.AddUtilityTerm(72, nonworkingAdults);
                    //not sign.: modeComponent.AddUtilityTerm(73, retiredAdults);

                    //GV: one person HH
                    modeComponent.AddUtilityTerm(74, onePersonHouseholdFlag);

                    //GV: not significant
                    //modeComponent.AddUtilityTerm(76, noCarsInHouseholdFlag);
                    //modeComponent.AddUtilityTerm(77, carsLessThanDriversFlag);
                }
                else if (mode == Global.Settings.Modes.ParkAndRide)
                {
                    modeComponent.AddUtilityTerm(80, 1.0);
                }
                else if (mode == Global.Settings.Modes.CarKissRideWalk)
                {
                    modeComponent.AddUtilityTerm(90, 1.0);
                }
                else if (mode == Global.Settings.Modes.BikeParkRideWalk)
                {
                    modeComponent.AddUtilityTerm(100, 1.0);
                }
                else if (mode == Global.Settings.Modes.BikeParkRideBike)
                {
                    modeComponent.AddUtilityTerm(110, 1.0);
                }
                else if (mode == Global.Settings.Modes.BikeOnTransit)
                {
                    modeComponent.AddUtilityTerm(120, 1.0);
                }
                else if (mode == Global.Settings.Modes.CarParkRideBike)
                {
                    modeComponent.AddUtilityTerm(130, 1.0);
                }
                else if (mode == Global.Settings.Modes.WalkRideBike)
                {
                    modeComponent.AddUtilityTerm(140, 1.0);
                }
                else if (mode == Global.Settings.Modes.PaidRideShare)
                {
                    //modeComponent.AddUtilityTerm(150, 1.0);

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

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


                //GV: Estimation of importance of "purpose" per mode - SOV is zero-alt and Work is zero-alt
                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, educationTourFlag);
                    modeComponent.AddUtilityTerm(firstCoef + 3, socialTourFlag);
                    modeComponent.AddUtilityTerm(firstCoef + 4, personalBusinessTourFlag);
                    modeComponent.AddUtilityTerm(firstCoef + 5, businessTourFlag);
                    //modeComponent.AddUtilityTerm(firstCoef + 6, workTourFlag); //GV: "work" is zero alternative

                    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));
                }
            }



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



                    //limit availability based on mode and tour purpose
                    bool available = (mode == Global.Settings.Modes.BikeParkRideBike || mode == Global.Settings.Modes.CarParkRideBike || mode == Global.Settings.Modes.WalkRideBike) &&
                                     (!(tour.IsWorkPurpose() || tour.IsSchoolPurpose())) ? false : true;

                    //further limit availability on mode
                    if (mode == Global.Settings.Modes.PaidRideShare && !Global.Configuration.PaidRideShareModeIsAvailable)
                    {
                        available = false;
                    }

                    //further limit availability if tour includes joint travel
                    if ((tour.JointTourSequence > 0 ||
                         tour.FullHalfTour1Sequence > 0 || tour.FullHalfTour2Sequence > 0 ||
                         tour.PartialHalfTour1Sequence > 0 || tour.PartialHalfTour2Sequence > 0) &&
                        (mode > Global.Settings.Modes.Transit))
                    {
                        available = false;
                    }

                    //further limit availability:  kissAndRide and carParkRideBike are not supported
                    available = (available == true) && (mode != Global.Settings.Modes.CarKissRideWalk) && (mode != Global.Settings.Modes.CarParkRideBike);

                    //further limit availabillity based on time window variables and any constrained choices
                    available = (available == true) &&
                                (modeTimes.LongestFeasibleWindow != null) && (mode > 0)
                                //&& (mode <= Global.Settings.Modes.Transit)
                                && (person.Age >= 18 || (modeTimes.Mode != Global.Settings.Modes.Sov && modeTimes.Mode != Global.Settings.Modes.HovDriver)) &&
                                (constrainedMode > 0 || mode == Global.Settings.Modes.Walk || mode == Global.Settings.Modes.Bike || mode == Global.Settings.Modes.HovDriver || mode == Global.Settings.Modes.Transit || !partialHalfTour) &&
                                (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);
                    alternative.AddNestedAlternative(HTourModeTime.TotalTourModeTimes + mode, mode - 1, THETA_PARAMETER);

                    //if (Global.Configuration.IsInEstimationMode && 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);

                    //}
                    //Following code was used to test handling of partially joint half tours (JLB 20140603)
                    //if (partialHalfTour) {
                    //	Global.PrintFile.WriteLine("HH pers {0} {1} avail {2} Aper Dper Mode {3} {4} {5} Travel Times {6} {7} Window {8} {9}",
                    //	   household.Id, person.Sequence,
                    //    available,
                    //		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 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;
                    }

                    //GV and JB: the parking cost are handled as part of genaralised time

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

                    alternative.AddUtilityTerm(1, modeTimes.GeneralizedTimeToDestination + modeTimes.GeneralizedTimeFromDestination);

                    //alternative.AddUtilityTerm(3,
                    //                           Math.Log(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(840, modeTimes.LongestFeasibleWindow.End - modeTimes.LongestFeasibleWindow.Start)), 0.8)
                                                                 )));



                    alternative.AddUtilityTerm(4, Math.Log((totalMinutesAvailableInDay + 1.0) / (minimumTimeNeeded + 1.0)));

                    alternative.AddUtilityTerm(5,
                                               (maleFlag == 0 && mode == Global.Settings.Modes.Walk &&
                                                arrivalPeriod.Index >= DayPeriod.EVENING)
                                                  ? 1
                                                  : 0);

                    if (altIndex == 0)
                    {
                        alternative.AddUtilityTerm(998, tour.DestinationPurpose);
                        alternative.AddUtilityTerm(999, (tour.ParentTour == null) ? 0 : 1);
                    }
                }
            }
        }
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, TripWrapper trip, HouseholdDayWrapper householdDay, int choice = Constants.DEFAULT_VALUE)
        {
            HouseholdWrapper household = (HouseholdWrapper)trip.Household;
            PersonWrapper    person    = (PersonWrapper)trip.Person;
            PersonDayWrapper personDay = (PersonDayWrapper)trip.PersonDay;
            TourWrapper      tour      = (TourWrapper)trip.Tour;

            TourWrapper.HalfTour halfTour = (TourWrapper.HalfTour)trip.HalfTour;
            List <Framework.DomainModels.Wrappers.IPersonDayWrapper> personDays = householdDay.PersonDays;

            int isJointTour      = tour.JointTourSequence > 0 ? 1 : 0;
            int isIndividualTour = isJointTour == 1 ? 0 : 1;

            Framework.DomainModels.Wrappers.IParcelWrapper destinationParcel = tour.DestinationParcel;
            int jointHalfOfFullJointHalfTour = ((trip.Direction == Global.Settings.TourDirections.OriginToDestination && tour.FullHalfTour1Sequence > 0) ||
                                                (trip.Direction == Global.Settings.TourDirections.DestinationToOrigin && tour.FullHalfTour2Sequence > 0)).ToFlag();
            int individualHalfOfFullJointHalfTour =
                ((trip.Direction == Global.Settings.TourDirections.OriginToDestination &&
                  tour.FullHalfTour1Sequence == 0 &&
                  tour.FullHalfTour2Sequence > 0) ||
                 (trip.Direction == Global.Settings.TourDirections.DestinationToOrigin &&
                  tour.FullHalfTour2Sequence == 0 &&
                  tour.FullHalfTour1Sequence > 0)).ToFlag();
            int individualHalfTour = (isIndividualTour == 1 || individualHalfOfFullJointHalfTour == 1) ? 1 : 0;
            int jointHalfTour      = 1 - individualHalfTour;

            //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 simulatedBusinessStops         = personDay.SimulatedBusinessStops;
            int simulatedBusinessStopsFlag     = simulatedBusinessStops > 0 ? 1 : 0;
            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;
            int primaryFamilyTimeFlag          = householdDay.PrimaryPriorityTimeFlag;

            // tour inputs
            int hovDriverTourFlag          = tour.IsHovDriverMode().ToFlag();
            int hovPassengerTourFlag       = tour.IsHovPassengerMode().ToFlag();
            int transitTourFlag            = tour.IsTransitMode().ToFlag();
            int walkTourFlag               = tour.IsWalkMode().ToFlag();
            int bikeTourFlag               = tour.IsBikeMode().ToFlag();
            int autoTourFlag               = tour.IsAnAutoMode().ToFlag();
            int notHomeBasedTourFlag       = (!tour.IsHomeBasedTour).ToFlag();
            int workTourFlag               = tour.IsWorkPurpose().ToFlag();
            int businessTourFlag           = tour.IsBusinessPurpose().ToFlag();
            int personalBusinessTourFlag   = tour.IsPersonalBusinessPurpose().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();

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

            // remaining inputs, including joint tour variables

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

            int destinationDepartureTime =
                trip.IsHalfTourFromOrigin // first trip in half tour, use tour destination time
                ? trip.Sequence == 1
                      ? tour.DestinationArrivalTime
                      : trip.GetPreviousTrip().ArrivalTime
                : trip.Sequence == 1
                      ? tour.DestinationDepartureTime
                      : trip.GetPreviousTrip().ArrivalTime;

            //var time = trip.IsHalfTourFromOrigin ? tour.DestinationArrivalTime : tour.DestinationDepartureTime;
            int time = destinationDepartureTime;

            bool timeIsAvailableForAnotherTrip = true;

            if ((trip.IsHalfTourFromOrigin && time < Global.Settings.Times.FourAM) ||
                (!trip.IsHalfTourFromOrigin && time > Global.Settings.Times.TwoAM))
            {
                timeIsAvailableForAnotherTrip = false;
            }

            bool stopsNeeded = false;

            //if ((halfTour.SimulatedTrips <= 5)
            //	&& (timeIsAvailableForAnotherTrip)
            //	&& (trip.Direction == 2)
            //	&&((trip.Tour.Sequence == trip.PersonDay.TotalCreatedTours)
            //	&& ((simulatedSchoolStops == 0 && personDay.SchoolStops > 0)
            //	||(simulatedBusinessStops == 0 && personDay.BusinessStops > 0)
            //	||(simulatedEscortStops == 0 && personDay.EscortStops > 0)
            //	||(simulatedPersonalBusinessStops == 0 && personDay.PersonalBusinessStops > 0)
            //	||(simulatedShoppingStops == 0 && personDay.ShoppingStops > 0)
            //	||(simulatedSocialStops == 0 && personDay.SocialStops > 0)))) {
            //		stopsNeeded = true;
            //}


            int from7AMto9AMFlag  = (time >= Global.Settings.Times.SevenAM && time < Global.Settings.Times.NineAM).ToFlag();
            int from9AMto3PMFlag  = (time >= Global.Settings.Times.NineAM && time < Global.Settings.Times.ThreePM).ToFlag();
            int from3PMto6PMFlag  = (time >= Global.Settings.Times.ThreePM && time < Global.Settings.Times.SixPM).ToFlag();
            int from6PMto10PMFlag = (time >= Global.Settings.Times.SixPM && time < Global.Settings.Times.TenPM).ToFlag();
            int from10PMto7AMFlag = (time >= Global.Settings.Times.TenPM).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();



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

            TimeWindow timeWindow = new TimeWindow();

            if (tour.JointTourSequence > 0)
            {
                foreach (PersonDayWrapper pDay in householdDay.PersonDays)
                {
                    TourWrapper tInJoint = (TourWrapper)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)
                {
                    TourWrapper tInJoint = (TourWrapper)pDay.Tours.Find(t => t.FullHalfTour1Sequence == tour.FullHalfTour1Sequence);
                    if (!(tInJoint == null))
                    {
                        // set jointTour time window
                        timeWindow.IncorporateAnotherTimeWindow(tInJoint.PersonDay.TimeWindow);

                        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)
                {
                    TourWrapper tInJoint = (TourWrapper)pDay.Tours.Find(t => t.FullHalfTour2Sequence == tour.FullHalfTour2Sequence);
                    if (!(tInJoint == null))
                    {
                        // set jointTour time window
                        timeWindow.IncorporateAnotherTimeWindow(tInJoint.PersonDay.TimeWindow);
                        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
            int    availableWindow = timeWindow.AvailableWindow(destinationDepartureTime, Global.Settings.TimeDirections.Both);
            double timePressure    = 1000 * remainingToursCount / (Math.Max(1D, availableWindow));
            //alternative.AddUtilityTerm(98, 1000 * remainingToursCount / (Math.Max(1D, maxWindowRemaining)));


            //var duration = availableWindow / 60D;

            // connectivity attributes
            //var 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, 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);
            }

            // 0 - NO MORE STOPS

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

            alternative.Choice = Global.Settings.Purposes.NoneOrHome;
            //alternative.AddNestedAlternative(_nestedAlternativeIds[0], _nestedAlternativeIndexes[0], THETA_PARAMETER);

            alternative.AddUtilityTerm(1, oneSimulatedTripFlag);
            alternative.AddUtilityTerm(2, twoSimulatedTripsFlag);
            alternative.AddUtilityTerm(2, threeSimulatedTripsFlag);
            alternative.AddUtilityTerm(2, fourSimulatedTripsFlag);
            alternative.AddUtilityTerm(2, fivePlusSimulatedTripsFlag);

            //GV: 21. june 2016, not sign.
            //alternative.AddUtilityTerm(6, transitTourFlag);

            alternative.AddUtilityTerm(7, bikeTourFlag);
            alternative.AddUtilityTerm(8, walkTourFlag);
            alternative.AddUtilityTerm(9, jointHalfTour);
            alternative.AddUtilityTerm(10, halfTourFromOriginFlag);

            //GV: june 2016 - not sign
            //alternative.AddUtilityTerm(11, totalAggregateLogsum);

            alternative.AddUtilityTerm(12, businessTourFlag);
            alternative.AddUtilityTerm(13, personalBusinessTourFlag);
            alternative.AddUtilityTerm(14, socialTourFlag);
            //alternative.AddUtilityTerm(15, schoolTourFlag);
            alternative.AddUtilityTerm(16, escortTourFlag);
            alternative.AddUtilityTerm(17, shoppingTourFlag);
            alternative.AddUtilityTerm(18, timePressure);

            //GV: 21. june 2016, try to estimate again
            alternative.AddUtilityTerm(19, primaryFamilyTimeFlag);

            alternative.AddUtilityTerm(20, person.IsChildUnder5.ToFlag());

            //alternative.AddUtilityTerm(15, from11PMto7AMFlag);

            //alternative.AddUtilityTerm(1, twoSimulatedTripsFlag * halfTourFromOriginFlag * isIndividualTour);
            //alternative.AddUtilityTerm(2, threeSimulatedTripsFlag * halfTourFromOriginFlag * isIndividualTour);
            //alternative.AddUtilityTerm(3, fourSimulatedTripsFlag * halfTourFromOriginFlag * isIndividualTour);
            //alternative.AddUtilityTerm(4, fivePlusSimulatedTripsFlag * halfTourFromOriginFlag * isIndividualTour);
            //alternative.AddUtilityTerm(5, twoSimulatedTripsFlag * halfTourFromDestinationFlag * isIndividualTour);
            //alternative.AddUtilityTerm(6, threeSimulatedTripsFlag * halfTourFromDestinationFlag * isIndividualTour);
            //alternative.AddUtilityTerm(7, fourSimulatedTripsFlag * halfTourFromDestinationFlag * isIndividualTour);
            //alternative.AddUtilityTerm(8, fivePlusSimulatedTripsFlag * halfTourFromDestinationFlag * isIndividualTour);
            //alternative.AddUtilityTerm(9, homeBasedTours * isIndividualTour);
            //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 * isJointTour);
            //alternative.AddUtilityTerm(26, threeSimulatedTripsFlag * halfTourFromDestinationFlag * isJointTour);
            //alternative.AddUtilityTerm(27, fourSimulatedTripsFlag * halfTourFromDestinationFlag * isJointTour);
            //alternative.AddUtilityTerm(28, fivePlusSimulatedTripsFlag * halfTourFromDestinationFlag * isJointTour);

            // 1 - BUSINESS STOP

            //if (personDay.BusinessStops > 0 && (tour.DestinationPurpose <= Global.Settings.Purposes.School || tour.DestinationPurpose == Global.Settings.Purposes.Business)) {
            // JLB 20130704 business stops are allowed on escort tours per data prep
            alternative = choiceProbabilityCalculator.GetAlternative(1,
                                                                     (personDay.BusinessStops > 0 &&
                                                                      (tour.DestinationPurpose <= Global.Settings.Purposes.Escort || tour.DestinationPurpose == Global.Settings.Purposes.Business) &&
                                                                      (halfTour.SimulatedTrips <= 5) &&
                                                                      timeIsAvailableForAnotherTrip),
                                                                     choice == Global.Settings.Purposes.Business);

            alternative.Choice = Global.Settings.Purposes.Business;
            //alternative.AddNestedAlternative(_nestedAlternativeIds[1], _nestedAlternativeIndexes[1], THETA_PARAMETER);

            //alternative.AddUtilityTerm(32, isIndividualTour);
            alternative.AddUtilityTerm(32, 1.0);

            //GV: june 2016 - not sign
            //alternative.AddUtilityTerm(33, businessTourFlag);


            //alternative.AddUtilityTerm(34, schoolTourFlag);
            //alternative.AddUtilityTerm(35, halfTourFromOriginFlag);
            //alternative.AddUtilityTerm(36, simulatedBusinessStops);
            //alternative.AddUtilityTerm(37, simulatedBusinessStopsFlag);
            //alternative.AddUtilityTerm(39, duration);

            //alternative.AddUtilityTerm(40, from9AMto11AMFlag + from11AMto1PMFlag + from1PMto3PMFlag + from3PMto5PMFlag);
            alternative.AddUtilityTerm(40, from9AMto3PMFlag + from3PMto6PMFlag);


            //alternative.AddUtilityTerm(42, logDist);
            //alternative.AddUtilityTerm(43, transitTourFlag);
            //alternative.AddUtilityTerm(44, (person.IsPartTimeWorker).ToFlag());

            //GV: 21. aug - I commented out as it is the only logsum in the model
            //alternative.AddUtilityTerm(46, totalAggregateLogsum);

            //alternative.AddUtilityTerm(47,totEmpBuffer2);
            //alternative.AddUtilityTerm(48, hovDriverTourFlag + hovPassengerTourFlag);


            // 2 - SCHOOL STOP

            alternative = choiceProbabilityCalculator.GetAlternative(2,
                                                                     (((personDay.SchoolStops > 0 && tour.DestinationPurpose <= Global.Settings.Purposes.School) || (isJointTour == 1)) &&
                                                                      halfTour.SimulatedTrips <= 5 &&
                                                                      timeIsAvailableForAnotherTrip),
                                                                     choice == Global.Settings.Purposes.School);

            alternative.Choice = Global.Settings.Purposes.School;
            //alternative.AddNestedAlternative(_nestedAlternativeIds[2], _nestedAlternativeIndexes[2], THETA_PARAMETER);

            //alternative.AddUtilityTerm(51, workTourFlag);
            alternative.AddUtilityTerm(51, 1.0);
            //alternative.AddUtilityTerm(52, schoolTourFlag);
            //alternative.AddUtilityTerm(53, halfTourFromOriginFlag);
            //alternative.AddUtilityTerm(54, simulatedSchoolStops);
            //alternative.AddUtilityTerm(55, remainingToursCount);
            //alternative.AddUtilityTerm(56, duration);

            //alternative.AddUtilityTerm(57, from7AMto9AMFlag + from7PMto9PMFlag + from9PMto11PMFlag + from11PMto7AMFlag);
            alternative.AddUtilityTerm(57, from7AMto9AMFlag + from9AMto3PMFlag + from10PMto7AMFlag);

            //alternative.AddUtilityTerm(58, oneSimulatedTripFlag);
            //alternative.AddUtilityTerm(59, logDist);
            alternative.AddUtilityTerm(61, jointHalfOfFullJointHalfTour * numChildrenOnJointTour);
            //alternative.AddUtilityTerm(65, (person.Age < 12).ToFlag());
            //alternative.AddUtilityTerm(66,  (person.IsUniversityStudent).ToFlag());



            // 3 - ESCORT STOP

            //if ((personDay.EscortStops > 0 && (tour.DestinationPurpose <= Global.Settings.Purposes.Escort || tour.DestinationPurpose == Global.Settings.Purposes.Business)) || (isJointTour==1)) {
            // JLB 20130704 no escort stops allowed on business tours per data prep
            alternative = choiceProbabilityCalculator.GetAlternative(3,
                                                                     (((personDay.EscortStops > 0 && tour.DestinationPurpose <= Global.Settings.Purposes.Escort) || (isJointTour == 1)) &&
                                                                      halfTour.SimulatedTrips <= 5 &&
                                                                      timeIsAvailableForAnotherTrip),
                                                                     choice == Global.Settings.Purposes.Escort);

            alternative.Choice = Global.Settings.Purposes.Escort;
            //alternative.AddNestedAlternative(_nestedAlternativeIds[3], _nestedAlternativeIndexes[3], THETA_PARAMETER);

            alternative.AddUtilityTerm(71, 1.0);
            //alternative.AddUtilityTerm(72, 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, hovDriverTourFlag);
            //alternative.AddUtilityTerm(82, hovPassengerTourFlag);
            //alternative.AddUtilityTerm(83, simulatedEscortStops * isJointTour);
            //alternative.AddUtilityTerm(84, simulatedEscortStops * isIndividualTour);
            //alternative.AddUtilityTerm(85, totalAggregateLogsum);
            //alternative.AddUtilityTerm(86, jointHalfOfFullJointHalfTour);
            //alternative.AddUtilityTerm(88, enrollmentK8Buffer2);
            //alternative.AddUtilityTerm(89, numChildrenOnJointTour);
            //alternative.AddUtilityTerm(90, halfTourFromOriginFlag);



            // 4 - PERSONAL BUSINESS STOP


            alternative = choiceProbabilityCalculator.GetAlternative(4,
                                                                     ((personDay.PersonalBusinessStops > 0 || isJointTour == 1) &&
                                                                      halfTour.SimulatedTrips <= 5 &&
                                                                      timeIsAvailableForAnotherTrip),
                                                                     choice == Global.Settings.Purposes.PersonalBusiness);

            alternative.Choice = Global.Settings.Purposes.PersonalBusiness;
            //alternative.AddNestedAlternative(_nestedAlternativeIds[4], _nestedAlternativeIndexes[4], THETA_PARAMETER);

            alternative.AddUtilityTerm(91, 1.0);
            //alternative.AddUtilityTerm(92, (workTourFlag + schoolTourFlag + businessTourFlag));
            //alternative.AddUtilityTerm(92, isJointTour);
            //alternative.AddUtilityTerm(93, escortTourFlag);
            //alternative.AddUtilityTerm(94, personalBusinessOrMedicalTourFlag * isIndividualTour);
            //alternative.AddUtilityTerm(95, shoppingTourFlag);
            //alternative.AddUtilityTerm(96, mealTourFlag);
            //alternative.AddUtilityTerm(97, socialOrRecreationTourFlag);
            //alternative.AddUtilityTerm(98, halfTourFromOriginFlag);
            //alternative.AddUtilityTerm(99, simulatedPersonalBusinessStops * isIndividualTour);
            //alternative.AddUtilityTerm(100, simulatedPersonalBusinessStops * isJointTour);
            //alternative.AddUtilityTerm(101, duration);
            //alternative.AddUtilityTerm(102, (from7AMto9AMFlag + from7PMto9PMFlag + from9PMto11PMFlag + from11PMto7AMFlag));

            //alternative.AddUtilityTerm(103, from9AMto11AMFlag + from11AMto1PMFlag + from1PMto3PMFlag + from3PMto5PMFlag);
            alternative.AddUtilityTerm(103, from9AMto3PMFlag + from3PMto6PMFlag);

            //alternative.AddUtilityTerm(105, hovDriverTourFlag);
            //alternative.AddUtilityTerm(106, hovPassengerTourFlag);
            //alternative.AddUtilityTerm(109, jointHalfOfFullJointHalfTour);
            //alternative.AddUtilityTerm(110, totEmpBuffer2);
            //alternative.AddUtilityTerm(111, totalAggregateLogsum);
            //alternative.AddUtilityTerm(112, personalBusinessOrMedicalTourFlag * isJointTour);



            // 5 - SHOPPING STOP

            alternative = choiceProbabilityCalculator.GetAlternative(5,
                                                                     ((personDay.ShoppingStops > 0 || isJointTour == 1) &&
                                                                      halfTour.SimulatedTrips <= 5 &&
                                                                      timeIsAvailableForAnotherTrip),
                                                                     choice == Global.Settings.Purposes.Shopping);

            alternative.Choice = Global.Settings.Purposes.Shopping;
            //alternative.AddNestedAlternative(_nestedAlternativeIds[5], _nestedAlternativeIndexes[5], THETA_PARAMETER);

            alternative.AddUtilityTerm(121, 1.0);
            //alternative.AddUtilityTerm(122, workTourFlag + schoolTourFlag + businessTourFlag);
            //alternative.AddUtilityTerm(122, isJointTour);
            //alternative.AddUtilityTerm(123, escortTourFlag);
            //alternative.AddUtilityTerm(124, personalBusinessOrMedicalTourFlag);
            //alternative.AddUtilityTerm(125, shoppingTourFlag * isIndividualTour);
            //alternative.AddUtilityTerm(125, shoppingTourFlag);
            //alternative.AddUtilityTerm(126, mealTourFlag);
            //alternative.AddUtilityTerm(127, socialOrRecreationTourFlag);
            //alternative.AddUtilityTerm(128, halfTourFromOriginFlag);
            //alternative.AddUtilityTerm(129, simulatedShoppingStops * isIndividualTour);
            //alternative.AddUtilityTerm(130, simulatedShoppingStops * isJointTour);
            //alternative.AddUtilityTerm(131, duration);

            //alternative.AddUtilityTerm(132, from7AMto9AMFlag + from9PMto11PMFlag + from11PMto7AMFlag);
            //alternative.AddUtilityTerm(133, (from11AMto1PMFlag + from1PMto3PMFlag + from3PMto5PMFlag));

            //alternative.AddUtilityTerm(132, from7AMto9AMFlag + from6PMto10PMFlag);
            alternative.AddUtilityTerm(133, (from9AMto3PMFlag + from3PMto6PMFlag + from6PMto10PMFlag));

            //alternative.AddUtilityTerm(134, adultFemaleOnJointTour);
            //alternative.AddUtilityTerm(135, hovDriverTourFlag);
            //alternative.AddUtilityTerm(136, hovPassengerTourFlag);
            //alternative.AddUtilityTerm(137, Math.Log(1 + adis));
            //alternative.AddUtilityTerm(138, shoppingTourFlag * isJointTour);
            //alternative.AddUtilityTerm(140, shoppingAggregateLogsum);
            //alternative.AddUtilityTerm(141, retailBuffer2);
            //alternative.AddUtilityTerm(142, numChildrenOnJointTour);
            //alternative.AddUtilityTerm(143, (household.Has100KPlusIncome).ToFlag());

            //GV: 21. june 2016, not sign.
            //alternative.AddUtilityTerm(134, primaryFamilyTimeFlag);



            // 6 - MEAL STOP

            //alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Meal, false, choice == Global.Settings.Purposes.Meal);
            //alternative.Choice = Global.Settings.Purposes.Meal;
            //alternative.AddNestedAlternative(12, 2, THETA_PARAMETER);


            // 6 - SOCIAL (OR RECREATION) STOP

            alternative = choiceProbabilityCalculator.GetAlternative(6,
                                                                     ((personDay.SocialStops > 0 || isJointTour == 1) &&
                                                                      halfTour.SimulatedTrips <= 5 &&
                                                                      timeIsAvailableForAnotherTrip),
                                                                     choice == Global.Settings.Purposes.Social);

            alternative.Choice = Global.Settings.Purposes.Social;
            //alternative.AddNestedAlternative(_nestedAlternativeIds[6], _nestedAlternativeIndexes[6], THETA_PARAMETER);

            alternative.AddUtilityTerm(181, 1.0);
            //alternative.AddUtilityTerm(182, workTourFlag + schoolTourFlag + businessTourFlag);
            //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, simulatedSocialStops * isIndividualTour);
            //alternative.AddUtilityTerm(197, simulatedSocialStops * isJointTour);
            //alternative.AddUtilityTerm(190, remainingToursCount);
            //alternative.AddUtilityTerm(191, duration);

            //alternative.AddUtilityTerm(192, from7AMto9AMFlag + from11PMto7AMFlag);
            //alternative.AddUtilityTerm(192, from7AMto9AMFlag);
            alternative.AddUtilityTerm(192, from9AMto3PMFlag + from3PMto6PMFlag + from6PMto10PMFlag);

            //alternative.AddUtilityTerm(194, hovDriverTourFlag);
            //alternative.AddUtilityTerm(195, hovPassengerTourFlag);
            //alternative.AddUtilityTerm(196, logDist);
            //alternative.AddUtilityTerm(200, numAdultsOnJointTour);


            // 8 - RECREATION STOP

            //alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Recreation, false, choice == Global.Settings.Purposes.Recreation);
            //alternative.Choice = Global.Settings.Purposes.Recreation;
            //alternative.AddNestedAlternative(12, 2, THETA_PARAMETER);

            // 9 - MEDICAL STOP

            //alternative = choiceProbabilityCalculator.GetAlternative(Global.Settings.Purposes.Medical, false, choice == Global.Settings.Purposes.Medical);
            //alternative.Choice = Global.Settings.Purposes.Medical;
            //alternative.AddNestedAlternative(12, 2, THETA_PARAMETER);
        }
Пример #3
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, HouseholdDayWrapper householdDay, TripWrapper trip, HTripTime choice = null)
        {
            if (householdDay.Household.Id == 80066 && trip.Person.Sequence == 1 && trip.Tour.Sequence == 2 &&
                trip.Direction == 2 && trip.Sequence == 1)
            {
            }

            PersonWrapper    person    = (PersonWrapper)trip.Person;
            PersonDayWrapper personDay = (PersonDayWrapper)trip.PersonDay;
            TourWrapper      tour      = (TourWrapper)trip.Tour;

            // person inputs + househol_PFPT
            int partTimeWorkerFlag    = person.IsPartTimeWorker.ToFlag();
            int nonworkingAdultFlag   = person.IsNonworkingAdult.ToFlag();
            int universityStudentFlag = person.IsUniversityStudent.ToFlag();
            int retiredAdultFlag      = person.IsRetiredAdult.ToFlag();
            //var drivingAgeStudentFlag = person.IsDrivingAgeStudent.ToFlag(); // excluded by GV
            int childAge5Through15Flag = person.IsChildAge5Through15.ToFlag();
            int childUnder5Flag        = person.IsChildUnder5.ToFlag();
            int femaleFlag             = person.IsFemale.ToFlag();
            int fullTimeWorkerFlag     = person.IsFulltimeWorker.ToFlag();
            int primaryFamilyTimeFlag  = householdDay.PrimaryPriorityTimeFlag;

            // set tour inputs
            int workTourFlag             = tour.IsWorkPurpose().ToFlag();
            int schoolTourFlag           = tour.IsSchoolPurpose().ToFlag();
            int businessTourFlag         = tour.IsBusinessPurpose().ToFlag();
            int escortTourFlag           = tour.IsEscortPurpose().ToFlag();
            int personalBusinessTourFlag = tour.IsPersonalBusinessPurpose().ToFlag();
            int shoppingTourFlag         = tour.IsShoppingPurpose().ToFlag();
            int socialTourFlag           = tour.IsSocialPurpose().ToFlag();
            int notWorkSchoolTourFlag    = 1 - workTourFlag - schoolTourFlag;
            int notWorkTourFlag          = (!tour.IsWorkPurpose()).ToFlag();
            int notHomeBasedTourFlag     = (!tour.IsHomeBasedTour).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;

            // set trip inputs - travel purpose
            bool originChangeMode           = trip.Sequence > 1 && trip.GetPreviousTrip().DestinationPurpose == Global.Settings.Purposes.ChangeMode;
            int  originSchoolFlag           = trip.IsSchoolOriginPurpose().ToFlag();
            int  originEscortFlag           = trip.IsEscortOriginPurpose().ToFlag();
            int  originShoppingFlag         = trip.IsShoppingOriginPurpose().ToFlag();
            int  originPersonalBusinessFlag = trip.IsPersonalBusinessOriginPurpose().ToFlag();
            int  originMealFlag             = trip.IsMealOriginPurpose().ToFlag();
            int  originSocialFlag           = trip.IsSocialOriginPurpose().ToFlag();
            int  originBusinessFlag         = trip.IsBusinessOriginPurpose().ToFlag();

            // set trip inputs - travel modes
            int sovOrHovTripFlag      = trip.UsesSovOrHovModes().ToFlag();
            int bikeTripFlag          = trip.IsBikeMode().ToFlag();
            int walkTripFlag          = trip.IsWalkMode().ToFlag();
            int transitTripFlag       = trip.IsTransitMode().ToFlag();
            int carDriverAloneFlag    = trip.IsSovMode().ToFlag();
            int carDriverNotAloneFlag = trip.IsHov2Mode().ToFlag();
            int carPassengerFlag      = trip.IsHov3Mode().ToFlag();

            int halfTourFromOriginFlag      = trip.IsHalfTourFromOrigin.ToFlag();
            int halfTourFromDestinationFlag = (!trip.IsHalfTourFromOrigin).ToFlag();

            // set remaining inputs
            // set remaining inputs
            TimeWindow timeWindow = new TimeWindow();

            if (tour.JointTourSequence > 0)
            {
                foreach (PersonDayWrapper pDay in householdDay.PersonDays)
                {
                    TourWrapper tInJoint = (TourWrapper)pDay.Tours.Find(t => t.JointTourSequence == tour.JointTourSequence);
                    if (!(tInJoint == null))
                    {
                        // set jointTour time window
                        timeWindow.IncorporateAnotherTimeWindow(tInJoint.PersonDay.TimeWindow);
                    }
                }
            }
            else if (trip.Direction == Global.Settings.TourDirections.OriginToDestination && tour.FullHalfTour1Sequence > 0)
            {
                foreach (PersonDayWrapper pDay in householdDay.PersonDays)
                {
                    TourWrapper tInJoint = (TourWrapper)pDay.Tours.Find(t => t.FullHalfTour1Sequence == tour.FullHalfTour1Sequence);
                    if (!(tInJoint == null))
                    {
                        // set jointTour time window
                        timeWindow.IncorporateAnotherTimeWindow(tInJoint.PersonDay.TimeWindow);
                    }
                }
            }
            else if (trip.Direction == Global.Settings.TourDirections.DestinationToOrigin && tour.FullHalfTour2Sequence > 0)
            {
                foreach (PersonDayWrapper pDay in householdDay.PersonDays)
                {
                    TourWrapper tInJoint = (TourWrapper)pDay.Tours.Find(t => t.FullHalfTour2Sequence == tour.FullHalfTour2Sequence);
                    if (!(tInJoint == null))
                    {
                        // set jointTour time window
                        timeWindow.IncorporateAnotherTimeWindow(tInJoint.PersonDay.TimeWindow);
                    }
                }
            }
            else if (tour.ParentTour == null)
            {
                timeWindow.IncorporateAnotherTimeWindow(personDay.TimeWindow);
            }
            else
            {
                timeWindow.IncorporateAnotherTimeWindow(tour.ParentTour.TimeWindow);
            }

            //set the availability and impedances for the periods
            HTripTime.SetTimeImpedances(trip);

            int remainingToursCount     = personDay.HomeBasedTours - personDay.GetTotalSimulatedTours();
            int tripRemainingInHalfTour = (trip.DestinationParcel != null && trip.DestinationParcel != tour.OriginParcel).ToFlag(); // we don't know exact #

            int previousArrivalTime = trip.IsHalfTourFromOrigin
                     ? (trip.Sequence == 1 ? tour.DestinationDepartureTime : trip.GetPreviousTrip().ArrivalTime)
                     : (trip.Sequence == 1 ? tour.DestinationArrivalTime : trip.GetPreviousTrip().ArrivalTime);

            MinuteSpan previousArrivalPeriod = new HTripTime(previousArrivalTime).DeparturePeriod;

            foreach (HTripTime time in HTripTime.Times[ParallelUtility.threadLocalAssignedIndex.Value])
            {
                MinuteSpan period = time.DeparturePeriod;

                double departurePeriodFraction = timeWindow.TotalAvailableMinutes(period.Start, period.End) / (period.End - period.Start + 1D);

                double departureShiftHours  = period.Middle / 60.0;
                int    durationShiftMinutes = Math.Abs(period.Middle - previousArrivalPeriod.Middle);
                double durationShiftHours   = durationShiftMinutes / 60.0;



                bool available = time.Available && departurePeriodFraction > 0;

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


                if (!alternative.Available)
                {
                    continue;
                }

                alternative.Choice = time;

                int indicatedTravelTime  = (int)time.ModeLOS.PathTime;
                int indicatedArrivalTime = trip.IsHalfTourFromOrigin
                     ? Math.Max(1, period.Middle - indicatedTravelTime)
                     : Math.Min(1440, period.Middle + indicatedTravelTime);

                int totalWindowRemaining = trip.IsHalfTourFromOrigin
                     ? timeWindow.TotalAvailableMinutesBefore(indicatedArrivalTime) + timeWindow.TotalAvailableMinutesAfter(previousArrivalTime)
                     : timeWindow.TotalAvailableMinutesAfter(indicatedArrivalTime) + timeWindow.TotalAvailableMinutesBefore(previousArrivalTime);

                int maxWindowRemaining = trip.IsHalfTourFromOrigin
                     ? timeWindow.MaxAvailableMinutesBefore(indicatedArrivalTime) + timeWindow.MaxAvailableMinutesAfter(previousArrivalTime)
                     : timeWindow.MaxAvailableMinutesAfter(indicatedArrivalTime) + timeWindow.MaxAvailableMinutesBefore(previousArrivalTime);

                if (trip.IsHalfTourFromOrigin)
                {
                    // outbound "departure" (arrival) period constants
                    alternative.AddUtilityTerm(11, time.DeparturePeriod.Middle.IsBetween(Global.Settings.Times.ThreeAM, Global.Settings.Times.SixAM).ToFlag());
                    alternative.AddUtilityTerm(12, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.SixAM, Global.Settings.Times.SevenAM).ToFlag());
                    alternative.AddUtilityTerm(13, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.SevenAM, Global.Settings.Times.EightAM).ToFlag());
                    alternative.AddUtilityTerm(14, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.EightAM, Global.Settings.Times.NineAM).ToFlag());
                    alternative.AddUtilityTerm(15, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.NineAM, Global.Settings.Times.TenAM).ToFlag());
                    alternative.AddUtilityTerm(16, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.TenAM, Global.Settings.Times.OnePM).ToFlag());

                    //alternative.AddUtilityTerm(17, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.OnePM, Global.Settings.Times.FourPM).ToFlag());
                    //alternative.AddUtilityTerm(18, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.FourPM, Global.Settings.Times.SevenPM).ToFlag());
                    //GV changed to 3pm
                    alternative.AddUtilityTerm(17, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.OnePM, Global.Settings.Times.ThreePM).ToFlag());
                    alternative.AddUtilityTerm(18, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.ThreePM, Global.Settings.Times.SixPM).ToFlag());
                    alternative.AddUtilityTerm(19, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.SixPM, Global.Settings.Times.TenPM).ToFlag());
                    alternative.AddUtilityTerm(20, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.TenPM, Global.Settings.Times.MinutesInADay).ToFlag());
                }
                else
                {
                    // return departure period constants
                    alternative.AddUtilityTerm(21, time.DeparturePeriod.Middle.IsBetween(Global.Settings.Times.ThreeAM, Global.Settings.Times.SevenAM).ToFlag());
                    alternative.AddUtilityTerm(22, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.SevenAM, Global.Settings.Times.TenAM).ToFlag());
                    alternative.AddUtilityTerm(23, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.TenAM, Global.Settings.Times.OnePM).ToFlag());
                    alternative.AddUtilityTerm(24, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.OnePM, Global.Settings.Times.ThreePM).ToFlag());
                    alternative.AddUtilityTerm(124, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.ThreePM, Global.Settings.Times.FourPM).ToFlag());
                    alternative.AddUtilityTerm(25, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.FourPM, Global.Settings.Times.FivePM).ToFlag());
                    alternative.AddUtilityTerm(26, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.FivePM, Global.Settings.Times.SixPM).ToFlag());
                    alternative.AddUtilityTerm(27, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.SixPM, Global.Settings.Times.SevenPM).ToFlag());
                    alternative.AddUtilityTerm(28, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.SevenPM, Global.Settings.Times.NinePM).ToFlag());
                    alternative.AddUtilityTerm(29, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.NinePM, Global.Settings.Times.Midnight).ToFlag());
                    alternative.AddUtilityTerm(30, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.Midnight, Global.Settings.Times.MinutesInADay).ToFlag());
                }

                alternative.AddUtilityTerm(31, durationShiftMinutes.IsRightExclusiveBetween(Global.Settings.Times.ZeroHours, Global.Settings.Times.OneHour).ToFlag());           // 0 - 1
                alternative.AddUtilityTerm(32, durationShiftMinutes.IsRightExclusiveBetween(Global.Settings.Times.OneHour, Global.Settings.Times.TwoHours).ToFlag());            // 1 - 2
                alternative.AddUtilityTerm(33, durationShiftMinutes.IsRightExclusiveBetween(Global.Settings.Times.TwoHours, Global.Settings.Times.ThreeHours).ToFlag());         // 2 - 3
                alternative.AddUtilityTerm(34, durationShiftMinutes.IsRightExclusiveBetween(Global.Settings.Times.ThreeHours, Global.Settings.Times.FiveHours).ToFlag());        // 3 - 5
                alternative.AddUtilityTerm(35, durationShiftMinutes.IsRightExclusiveBetween(Global.Settings.Times.FiveHours, Global.Settings.Times.SevenHours).ToFlag());        // 5 - 7
                alternative.AddUtilityTerm(36, durationShiftMinutes.IsRightExclusiveBetween(Global.Settings.Times.SevenHours, Global.Settings.Times.NineHours).ToFlag());        // 7 - 9
                alternative.AddUtilityTerm(37, durationShiftMinutes.IsRightExclusiveBetween(Global.Settings.Times.NineHours, Global.Settings.Times.TwelveHours).ToFlag());       // 9 - 12
                alternative.AddUtilityTerm(38, durationShiftMinutes.IsRightExclusiveBetween(Global.Settings.Times.TwelveHours, Global.Settings.Times.FourteenHours).ToFlag());   // 12 - 14
                alternative.AddUtilityTerm(39, durationShiftMinutes.IsRightExclusiveBetween(Global.Settings.Times.FourteenHours, Global.Settings.Times.EighteenHours).ToFlag()); // 14 - 18
                alternative.AddUtilityTerm(40, (durationShiftMinutes >= Global.Settings.Times.EighteenHours).ToFlag());                                                          // 18 - 24

                alternative.AddUtilityTerm(41, partTimeWorkerFlag * departureShiftHours);
                alternative.AddUtilityTerm(43, nonworkingAdultFlag * departureShiftHours);
                alternative.AddUtilityTerm(45, universityStudentFlag * departureShiftHours);
                alternative.AddUtilityTerm(47, retiredAdultFlag * departureShiftHours);
                alternative.AddUtilityTerm(49, femaleFlag * departureShiftHours);
                alternative.AddUtilityTerm(51, childAge5Through15Flag * departureShiftHours);
                alternative.AddUtilityTerm(53, childUnder5Flag * departureShiftHours);
                alternative.AddUtilityTerm(61, jointTourFlag * departureShiftHours);
                //alternative.AddUtilityTerm(63, partialHalfTourFlag * departureShiftHours);
                //alternative.AddUtilityTerm(65, fullHalfTourFlag * departureShiftHours);
                alternative.AddUtilityTerm(67, primaryFamilyTimeFlag * departureShiftHours);

                //alternative.AddUtilityTerm(131, workTourFlag * halfTourFromOriginFlag * departureShiftHours);
                //alternative.AddUtilityTerm(133, workTourFlag * halfTourFromDestinationFlag * departureShiftHours);
                //alternative.AddUtilityTerm(135, notWorkTourFlag * halfTourFromDestinationFlag * departureShiftHours);
                //alternative.AddUtilityTerm(137, notHomeBasedTourFlag * departureShiftHours);
                alternative.AddUtilityTerm(145, originEscortFlag * departureShiftHours);
                alternative.AddUtilityTerm(147, originShoppingFlag * departureShiftHours);
                alternative.AddUtilityTerm(149, originBusinessFlag * departureShiftHours);
                alternative.AddUtilityTerm(151, originSocialFlag * departureShiftHours);
                alternative.AddUtilityTerm(153, originPersonalBusinessFlag * departureShiftHours);
                alternative.AddUtilityTerm(155, originSchoolFlag * departureShiftHours);

                alternative.AddUtilityTerm(42, partTimeWorkerFlag * durationShiftHours);
                alternative.AddUtilityTerm(44, nonworkingAdultFlag * durationShiftHours);
                alternative.AddUtilityTerm(46, universityStudentFlag * durationShiftHours);
                alternative.AddUtilityTerm(48, retiredAdultFlag * durationShiftHours);
                alternative.AddUtilityTerm(50, femaleFlag * durationShiftHours);
                alternative.AddUtilityTerm(52, childAge5Through15Flag * durationShiftHours);
                alternative.AddUtilityTerm(54, childUnder5Flag * durationShiftHours);
                //alternative.AddUtilityTerm(62, jointTourFlag * durationShiftHours);
                //alternative.AddUtilityTerm(64, partialHalfTourFlag * durationShiftHours);
                //alternative.AddUtilityTerm(66, fullHalfTourFlag * durationShiftHours);
                alternative.AddUtilityTerm(68, primaryFamilyTimeFlag * durationShiftHours);

                alternative.AddUtilityTerm(132, workTourFlag * halfTourFromOriginFlag * durationShiftHours);
                alternative.AddUtilityTerm(134, workTourFlag * halfTourFromDestinationFlag * durationShiftHours);
                alternative.AddUtilityTerm(136, notWorkTourFlag * halfTourFromDestinationFlag * durationShiftHours);
                alternative.AddUtilityTerm(138, notHomeBasedTourFlag * durationShiftHours);
                alternative.AddUtilityTerm(146, originEscortFlag * durationShiftHours);
                alternative.AddUtilityTerm(148, originShoppingFlag * durationShiftHours);
                alternative.AddUtilityTerm(150, originBusinessFlag * durationShiftHours);
                alternative.AddUtilityTerm(152, originSocialFlag * durationShiftHours);
                alternative.AddUtilityTerm(154, originPersonalBusinessFlag * durationShiftHours);
                alternative.AddUtilityTerm(156, originSchoolFlag * durationShiftHours);

                alternative.AddUtilityTerm(158, workTourFlag * halfTourFromOriginFlag * (trip.Sequence == 1).ToFlag() * durationShiftHours);
                alternative.AddUtilityTerm(159, workTourFlag * halfTourFromDestinationFlag * (trip.Sequence == 1).ToFlag() * durationShiftHours);
                alternative.AddUtilityTerm(160, schoolTourFlag * halfTourFromOriginFlag * (trip.Sequence == 1).ToFlag() * durationShiftHours);
                alternative.AddUtilityTerm(161, schoolTourFlag * halfTourFromDestinationFlag * (trip.Sequence == 1).ToFlag() * durationShiftHours);
                alternative.AddUtilityTerm(162, businessTourFlag * halfTourFromOriginFlag * (trip.Sequence == 1).ToFlag() * durationShiftHours);
                alternative.AddUtilityTerm(163, businessTourFlag * halfTourFromDestinationFlag * (trip.Sequence == 1).ToFlag() * durationShiftHours);
                alternative.AddUtilityTerm(164, escortTourFlag * halfTourFromOriginFlag * (trip.Sequence == 1).ToFlag() * durationShiftHours);
                alternative.AddUtilityTerm(165, escortTourFlag * halfTourFromDestinationFlag * (trip.Sequence == 1).ToFlag() * durationShiftHours);
                alternative.AddUtilityTerm(166, personalBusinessTourFlag * halfTourFromOriginFlag * (trip.Sequence == 1).ToFlag() * durationShiftHours);
                alternative.AddUtilityTerm(167, personalBusinessTourFlag * halfTourFromDestinationFlag * (trip.Sequence == 1).ToFlag() * durationShiftHours);
                alternative.AddUtilityTerm(168, shoppingTourFlag * halfTourFromOriginFlag * (trip.Sequence == 1).ToFlag() * durationShiftHours);
                alternative.AddUtilityTerm(169, shoppingTourFlag * halfTourFromDestinationFlag * (trip.Sequence == 1).ToFlag() * durationShiftHours);
                alternative.AddUtilityTerm(170, socialTourFlag * halfTourFromOriginFlag * (trip.Sequence == 1).ToFlag() * durationShiftHours);
                alternative.AddUtilityTerm(171, socialTourFlag * halfTourFromDestinationFlag * (trip.Sequence == 1).ToFlag() * durationShiftHours);

                //alternative.AddUtilityTerm(172, workTourFlag * halfTourFromOriginFlag * (trip.Sequence==1).ToFlag() * departureShiftHours);


                alternative.AddUtilityTerm(86, sovOrHovTripFlag * Math.Max(time.ModeLOS.GeneralizedTimeLogsum, 0) * tour.TimeCoefficient);
                //alternative.AddUtilityTerm(87, sovOrHovTripFlag * notWorkSchoolTourFlag * Math.Max(time.ModeLOS.GeneralizedTimeLogsum, 0) * tour.TimeCoefficient);
                //alternative.AddUtilityTerm(88, transitTripFlag * Math.Max(time.ModeLOS.GeneralizedTimeLogsum, 0) * tour.TimeCoefficient);
                //alternative.AddUtilityTerm(89, sovOrHovTripFlag * notWorkSchoolTourFlag * (trip.Sequence==1).ToFlag() * Math.Max(time.ModeLOS.GeneralizedTimeLogsum, 0) * tour.TimeCoefficient);

                alternative.AddUtilityTerm(92, Math.Log(departurePeriodFraction));
                //alternative.AddUtilityTerm(92, halfTourFromDestinationFlag * Math.Log(departurePeriodFraction));
                alternative.AddUtilityTerm(99, tripRemainingInHalfTour / (Math.Max(1D, Math.Abs(trip.ArrivalTimeLimit - period.Middle))));
                //alternative.AddUtilityTerm(97, remainingToursCount / (Math.Max(1D, totalWindowRemaining)));
                alternative.AddUtilityTerm(98, 1000 * remainingToursCount / (Math.Max(1D, maxWindowRemaining)));
            }
        }