Example #1
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, IHouseholdDayWrapper householdDay, ITripWrapper trip, HTripTime choice = null)
        {
            var person    = trip.Person;
            var personDay = trip.PersonDay;
            var tour      = trip.Tour;

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

            // set tour inputs
            var workTourFlag         = tour.IsWorkPurpose().ToFlag();
            var notWorkTourFlag      = (!tour.IsWorkPurpose()).ToFlag();
            var notHomeBasedTourFlag = (!tour.IsHomeBasedTour).ToFlag();
            var jointTourFlag        = (tour.JointTourSequence > 0) ? 1 : 0;
            var partialHalfTourFlag  = (trip.IsHalfTourFromOrigin ? tour.PartialHalfTour1Sequence > 0 : tour.PartialHalfTour2Sequence > 0) ? 1 : 0;
            var fullHalfTourFlag     = (trip.IsHalfTourFromOrigin ? tour.FullHalfTour1Sequence > 0 : tour.FullHalfTour2Sequence > 0) ? 1 : 0;

            // set trip inputs
            var originChangeMode            = trip.Sequence > 1 && trip.GetPreviousTrip().DestinationPurpose == Global.Settings.Purposes.ChangeMode;
            var originSchoolFlag            = trip.IsSchoolOriginPurpose().ToFlag();
            var originEscortFlag            = trip.IsEscortOriginPurpose().ToFlag();
            var originShoppingFlag          = trip.IsShoppingOriginPurpose().ToFlag();
            var originPersonalBusinessFlag  = trip.IsPersonalBusinessOriginPurpose().ToFlag();
            var originMealFlag              = trip.IsMealOriginPurpose().ToFlag();
            var originSocialFlag            = trip.IsSocialOriginPurpose().ToFlag();
            var sovOrHovTripFlag            = trip.UsesSovOrHovModes().ToFlag();
            var transitTripFlag             = trip.IsTransitMode().ToFlag();
            var halfTourFromOriginFlag      = trip.IsHalfTourFromOrigin.ToFlag();
            var halfTourFromDestinationFlag = (!trip.IsHalfTourFromOrigin).ToFlag();

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

            if (tour.JointTourSequence > 0)
            {
                foreach (PersonDayWrapper pDay in householdDay.PersonDays)
                {
                    var tInJoint = 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)
                {
                    var tInJoint = 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)
                {
                    var tInJoint = 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);

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

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

            var previousArrivalPeriod = new HTripTime(previousArrivalTime).DeparturePeriod;

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

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

                var departureShift = period.Middle / 60.0;
                var durationShift  = Math.Abs(period.Middle - previousArrivalPeriod.Middle) / 60.0;

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

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


                if (!alternative.Available)
                {
                    continue;
                }

                alternative.Choice = time;

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

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

                var 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());
                    alternative.AddUtilityTerm(19, time.DeparturePeriod.Middle.IsLeftExclusiveBetween(Global.Settings.Times.SevenPM, 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, durationShift.IsRightExclusiveBetween(Global.Settings.Times.ZeroHours, Global.Settings.Times.OneHour).ToFlag());           // 0 - 1
                alternative.AddUtilityTerm(32, durationShift.IsRightExclusiveBetween(Global.Settings.Times.OneHour, Global.Settings.Times.TwoHours).ToFlag());            // 1 - 2
                alternative.AddUtilityTerm(33, durationShift.IsRightExclusiveBetween(Global.Settings.Times.TwoHours, Global.Settings.Times.ThreeHours).ToFlag());         // 2 - 3
                alternative.AddUtilityTerm(34, durationShift.IsRightExclusiveBetween(Global.Settings.Times.ThreeHours, Global.Settings.Times.FiveHours).ToFlag());        // 3 - 5
                alternative.AddUtilityTerm(35, durationShift.IsRightExclusiveBetween(Global.Settings.Times.FiveHours, Global.Settings.Times.SevenHours).ToFlag());        // 5 - 7
                alternative.AddUtilityTerm(36, durationShift.IsRightExclusiveBetween(Global.Settings.Times.SevenHours, Global.Settings.Times.NineHours).ToFlag());        // 7 - 9
                alternative.AddUtilityTerm(37, durationShift.IsRightExclusiveBetween(Global.Settings.Times.NineHours, Global.Settings.Times.TwelveHours).ToFlag());       // 9 - 12
                alternative.AddUtilityTerm(38, durationShift.IsRightExclusiveBetween(Global.Settings.Times.TwelveHours, Global.Settings.Times.FourteenHours).ToFlag());   // 12 - 14
                alternative.AddUtilityTerm(39, durationShift.IsRightExclusiveBetween(Global.Settings.Times.FourteenHours, Global.Settings.Times.EighteenHours).ToFlag()); // 14 - 18
                alternative.AddUtilityTerm(40, (durationShift >= Global.Settings.Times.EighteenHours).ToFlag());                                                          // 18 - 24

                alternative.AddUtilityTerm(41, partTimeWorkerFlag * departureShift);
                alternative.AddUtilityTerm(43, nonworkingAdultFlag * departureShift);
                alternative.AddUtilityTerm(45, universityStudentFlag * departureShift);
                alternative.AddUtilityTerm(47, retiredAdultFlag * departureShift);
                alternative.AddUtilityTerm(49, drivingAgeStudentFlag * departureShift);
                alternative.AddUtilityTerm(51, childAge5Through15Flag * departureShift);
                alternative.AddUtilityTerm(53, childUnder5Flag * departureShift);
                alternative.AddUtilityTerm(61, jointTourFlag * departureShift);
                alternative.AddUtilityTerm(63, partialHalfTourFlag * departureShift);
                alternative.AddUtilityTerm(65, fullHalfTourFlag * departureShift);

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

                alternative.AddUtilityTerm(42, partTimeWorkerFlag * durationShift);
                alternative.AddUtilityTerm(44, nonworkingAdultFlag * durationShift);
                alternative.AddUtilityTerm(46, universityStudentFlag * durationShift);
                alternative.AddUtilityTerm(48, retiredAdultFlag * durationShift);
                alternative.AddUtilityTerm(50, drivingAgeStudentFlag * durationShift);
                alternative.AddUtilityTerm(52, childAge5Through15Flag * durationShift);
                alternative.AddUtilityTerm(54, childUnder5Flag * durationShift);
                alternative.AddUtilityTerm(62, jointTourFlag * durationShift);
                alternative.AddUtilityTerm(64, partialHalfTourFlag * durationShift);
                alternative.AddUtilityTerm(66, fullHalfTourFlag * durationShift);

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

                alternative.AddUtilityTerm(86, sovOrHovTripFlag * Math.Max(time.ModeLOS.GeneralizedTimeLogsum, 0) * tour.TimeCoefficient);
                alternative.AddUtilityTerm(88, transitTripFlag * Math.Max(time.ModeLOS.GeneralizedTimeLogsum, 0) * tour.TimeCoefficient);
                alternative.AddUtilityTerm(92, halfTourFromOriginFlag * 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, remainingToursCount / (Math.Max(1D, maxWindowRemaining)));
            }
        }
Example #2
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper tour, IHouseholdDayWrapper householdDay, int sampleSize, IParcelWrapper choice = null)
        {
            //            var household = tour.Household;
            IPersonWrapper    person    = tour.Person;
            IPersonDayWrapper personDay = tour.PersonDay;

            //            var totalAvailableMinutes =
            //                tour.ParentTour == null
            //                    ? personDay.TimeWindow.TotalAvailableMinutes(1, Global.Settings.Times.MinutesInADay)
            //                    : tour.ParentTour.TimeWindow.TotalAvailableMinutes(1, Global.Settings.Times.MinutesInADay);


            TimeWindow timeWindow = new TimeWindow();

            if (tour.JointTourSequence > 0)
            {
                foreach (PersonDayWrapper pDay in householdDay.PersonDays)
                {
                    ITourWrapper tInJoint = pDay.Tours.Find(t => t.JointTourSequence == tour.JointTourSequence);
                    if (!(tInJoint == null))
                    {
                        // set jointTour time window
                        timeWindow.IncorporateAnotherTimeWindow(tInJoint.PersonDay.TimeWindow);
                    }
                }
            }
            else if (tour.ParentTour == null)
            {
                timeWindow.IncorporateAnotherTimeWindow(personDay.TimeWindow);
            }

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

            int maxAvailableMinutes =
                (tour.JointTourSequence > 0 || tour.ParentTour == null)
                ? timeWindow.MaxAvailableMinutesAfter(Global.Settings.Times.FiveAM)
                    : tour.ParentTour.DestinationDepartureTime - tour.ParentTour.DestinationArrivalTime;


            //            var hoursAvailableInverse =
            //                tour.IsHomeBasedTour
            //                    ? (personDay.HomeBasedTours - personDay.SimulatedHomeBasedTours + 1) / (Math.Max(totalAvailableMinutes - 360, 30) / 60D)
            //                    : 1 / (Math.Max(totalAvailableMinutes, 1) / 60D);

            int fastestAvailableTimeOfDay =
                tour.IsHomeBasedTour || tour.ParentTour == null
                    ? 1
                    : tour.ParentTour.DestinationArrivalTime + (tour.ParentTour.DestinationDepartureTime - tour.ParentTour.DestinationArrivalTime) / 2;

            int tourCategory = tour.GetTourCategory();
            //            var primaryFlag = ChoiceModelUtility.GetPrimaryFlag(tourCategory);
            int secondaryFlag = ChoiceModelUtility.GetSecondaryFlag(tourCategory);

            ChoiceModelUtility.DrawRandomTourTimePeriodsActum(tour, tourCategory);

            int segment = Global.ContainerDaySim.GetInstance <SamplingWeightsSettingsFactory>().SamplingWeightsSettings.GetTourDestinationSegment(tour.DestinationPurpose, tour.IsHomeBasedTour ? Global.Settings.TourPriorities.HomeBasedTour : Global.Settings.TourPriorities.WorkBasedTour, Global.Settings.Modes.Sov, person.PersonType);
            DestinationSampler       destinationSampler       = new DestinationSampler(choiceProbabilityCalculator, segment, sampleSize, choice, tour.OriginParcel);
            TourDestinationUtilities tourDestinationUtilities = new TourDestinationUtilities(tour, sampleSize, secondaryFlag, personDay.GetIsWorkOrSchoolPattern().ToFlag(), personDay.GetIsOtherPattern().ToFlag(), fastestAvailableTimeOfDay, maxAvailableMinutes);

            destinationSampler.SampleTourDestinations(tourDestinationUtilities);
        }
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper tour, IHouseholdDayWrapper householdDay, int sampleSize, IParcelWrapper choice = null)
        {
            timesStartedRunModel++;
            //Console.WriteLine("Started {0}  Finished {1}", timesStarted,timesStartedRunModel);

            IHouseholdWrapper household = tour.Household;
            IPersonWrapper    person    = tour.Person;
            IPersonDayWrapper personDay = tour.PersonDay;

            Framework.DomainModels.Models.IHouseholdTotals householdTotals = household.HouseholdTotals;
            IParcelWrapper originParcel = tour.OriginParcel;

            int childUnder16Flag         = (person.Age < 16).ToFlag();
            int noCarsInHouseholdFlag    = (household.VehiclesAvailable == 0).ToFlag();
            int fewerCarsThanDriversFlag = household.GetFlagForCarsLessThanDrivers(household.VehiclesAvailable);

            int lowIncomeFlag     = (household.Income >= 0 && household.Income <= 40000).ToFlag();
            int highIncomeFlag    = (household.Income >= 80000).ToFlag();
            int missingIncomeFlag = (household.Income < 0).ToFlag();

            int originTransitBand1 = (originParcel.GetDistanceToTransit() >= 0 && originParcel.GetDistanceToTransit() <= 0.25).ToFlag();
            int originTransitBand3 = (originParcel.GetDistanceToTransit() > 0.5).ToFlag();


            TimeWindow timeWindow = new TimeWindow();

            if (tour.JointTourSequence > 0)
            {
                foreach (IPersonDayWrapper pDay in householdDay.PersonDays)
                {
                    ITourWrapper tInJoint = pDay.Tours.Find(t => t.JointTourSequence == tour.JointTourSequence);
                    if (!(tInJoint == null))
                    {
                        // set jointTour time window
                        timeWindow.IncorporateAnotherTimeWindow(tInJoint.PersonDay.TimeWindow);
                    }
                }
            }
            else if (tour.ParentTour == null)
            {
                timeWindow.IncorporateAnotherTimeWindow(personDay.TimeWindow);
            }

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

            int maxAvailableMinutes =
                (tour.JointTourSequence > 0 || tour.ParentTour == null)
                 ? timeWindow.MaxAvailableMinutesAfter(Global.Settings.Times.FiveAM)
                      : tour.ParentTour.DestinationDepartureTime - tour.ParentTour.DestinationArrivalTime;


            bool householdHasChildren   = household.HasChildren;
            bool householdHasNoChildren = householdHasChildren ? false : true;

            int fastestAvailableTimeOfDay =
                tour.IsHomeBasedTour || tour.ParentTour == null
                      ? 1
                      : tour.ParentTour.DestinationArrivalTime + (tour.ParentTour.DestinationDepartureTime - tour.ParentTour.DestinationArrivalTime) / 2;

            int tourCategory            = tour.GetTourCategory();
            int secondaryFlag           = ChoiceModelUtility.GetSecondaryFlag(tourCategory);
            int workOrSchoolPatternFlag = personDay.GetIsWorkOrSchoolPattern().ToFlag();
            int otherPatternFlag        = personDay.GetIsOtherPattern().ToFlag();
            int jointTourFlag           = (tour.JointTourSequence > 0).ToFlag();

            ChoiceModelUtility.DrawRandomTourTimePeriodsActum(tour, tourCategory);

            int segment = Global.ContainerDaySim.GetInstance <SamplingWeightsSettingsFactory>().SamplingWeightsSettings.GetTourDestinationSegment(tour.DestinationPurpose, tour.IsHomeBasedTour ? Global.Settings.TourPriorities.HomeBasedTour : Global.Settings.TourPriorities.WorkBasedTour, Global.Settings.Modes.Sov, person.PersonType);

            DestinationSampler       destinationSampler       = new DestinationSampler(choiceProbabilityCalculator, segment, sampleSize, choice, tour.OriginParcel);
            TourDestinationUtilities tourDestinationUtilities = new TourDestinationUtilities(tour, sampleSize, secondaryFlag, personDay.GetIsWorkOrSchoolPattern().ToFlag(), personDay.GetIsOtherPattern().ToFlag(), fastestAvailableTimeOfDay, maxAvailableMinutes);

            // get destination sample and perform code that used to be in SetUtilities below
            Dictionary <DestinationSampler.TourSampleItem, int> sampleItems = destinationSampler.SampleAndReturnTourDestinations(tourDestinationUtilities);

            int observedMode = 0;

            if (tour.Mode == Global.Settings.Modes.Bike || tour.Mode == Global.Settings.Modes.Walk)
            {
                observedMode = 0;
            }
            else if (tour.Mode == Global.Settings.Modes.Sov)
            {
                observedMode = 1;
            }
            else if (tour.Mode == Global.Settings.Modes.Hov2 || tour.Mode == Global.Settings.Modes.Hov3)
            {
                observedMode = 2;
            }
            else if (tour.Mode == Global.Settings.Modes.Transit)
            {
                observedMode = 3;
            }

            int purpose = tour.DestinationPurpose;

            if (tour.ParentTour != null)
            {
                purpose = 0;
            }
            int prefix = 0;

            int index     = 0;
            int destindex = 0;
            int indChosen = -1;

            foreach (KeyValuePair <DestinationSampler.TourSampleItem, int> sampleItem in sampleItems)
            {
                bool           parcelAvailable   = sampleItem.Key.Available;
                bool           isChosen          = sampleItem.Key.IsChosen;
                double         adjustmentFactor  = sampleItem.Key.AdjustmentFactor;
                IParcelWrapper destinationParcel = ChoiceModelFactory.Parcels[sampleItem.Key.ParcelId];

                destindex++;
                if (isChosen)
                {
                    indChosen = destindex;
                }

                if (!parcelAvailable)
                {
                    continue;
                }


                int destinationTransitBand1 = (destinationParcel.GetDistanceToTransit() >= 0 && destinationParcel.GetDistanceToTransit() <= 0.25).ToFlag();
                int destinationTransitBand3 = (destinationParcel.GetDistanceToTransit() > 0.5).ToFlag();

                //                var destinationSubzone = _zoneSubzones[destinationParcel.ZoneId][destinationTransitBand3];

                IEnumerable <IPathTypeModel> pathTypeModels =
                    PathTypeModelFactory.Singleton.Run(
                        tour.Household.RandomUtility,
                        originParcel.ZoneId,
                        destinationParcel.ZoneId,
                        tour.DestinationArrivalTime,
                        tour.DestinationDepartureTime,
                        tour.DestinationPurpose,
                        tour.CostCoefficient,
                        tour.TimeCoefficient,
                        tour.Person.IsDrivingAge,
                        tour.Household.VehiclesAvailable,
                        tour.Person.TransitPassOwnership,
                        (tour.Household.OwnsAutomatedVehicles > 0),
                        tour.Person.GetTransitFareDiscountFraction(),
                        false,
                        Global.Settings.Modes.Walk, Global.Settings.Modes.Sov, Global.Settings.Modes.Hov2, Global.Settings.Modes.Transit);

                for (int modeIndex = 0; modeIndex <= 3; modeIndex++)
                {
                    IPathTypeModel pathTypeModel = modeIndex == 0 ? pathTypeModels.First(x => x.Mode == Global.Settings.Modes.Walk)
                                       : modeIndex == 1 ? pathTypeModels.First(x => x.Mode == Global.Settings.Modes.Sov)
                                        : modeIndex == 2 ? pathTypeModels.First(x => x.Mode == Global.Settings.Modes.Hov2)
                                                          : pathTypeModels.First(x => x.Mode == Global.Settings.Modes.Transit);
                    bool modeAvailable = pathTypeModel.Available;
                    bool chosen        = modeIndex == observedMode && destinationParcel == tour.DestinationParcel;

                    //if (chosen) Global.PrintFile.WriteLine("Sequence {0}: Chosen parcel {1} Mode {2} Available {3}",timesStartedRunModel,destinationParcel.Id,observedMode,modeAvailable);
                    ChoiceProbabilityCalculator.Alternative alternative = choiceProbabilityCalculator.GetAlternative(index++, modeAvailable, chosen);
                    alternative.Choice = destinationParcel;

                    if (!modeAvailable)
                    {
                        continue;
                    }

                    alternative.AddUtilityTerm(1, tour.Id);
                    alternative.AddUtilityTerm(9, purpose);

                    if (modeIndex == 0)
                    {
                        // WALK
                        alternative.AddUtilityTerm(prefix + 3, pathTypeModel.GeneralizedTimeLogsum);
                        alternative.AddUtilityTerm(prefix + 4, destinationParcel.NetIntersectionDensity2());
                        alternative.AddUtilityTerm(prefix + 5, destinationParcel.MixedUse4Index2());
                    }
                    else if (modeIndex == 1)
                    {
                        // SOV
                        alternative.AddUtilityTerm(prefix + 3, pathTypeModel.GeneralizedTimeLogsum);
                        alternative.AddUtilityTerm(prefix + 11, 1.0);
                        alternative.AddUtilityTerm(prefix + 12, destinationParcel.ParkingOffStreetPaidHourlyPriceBuffer2);
                        alternative.AddUtilityTerm(prefix + 14, fewerCarsThanDriversFlag);
                        alternative.AddUtilityTerm(prefix + 15, lowIncomeFlag);
                        alternative.AddUtilityTerm(prefix + 16, highIncomeFlag);
                        alternative.AddUtilityTerm(prefix + 17, missingIncomeFlag);
                    }
                    else if (modeIndex == 2)
                    {
                        // HOV
                        alternative.AddUtilityTerm(prefix + 3, pathTypeModel.GeneralizedTimeLogsum);
                        alternative.AddUtilityTerm(prefix + 21, 1.0);
                        alternative.AddUtilityTerm(prefix + 22, childUnder16Flag);
                        alternative.AddUtilityTerm(prefix + 23, noCarsInHouseholdFlag);
                        alternative.AddUtilityTerm(prefix + 24, fewerCarsThanDriversFlag);
                        alternative.AddUtilityTerm(prefix + 25, lowIncomeFlag);
                        alternative.AddUtilityTerm(prefix + 26, highIncomeFlag);
                        alternative.AddUtilityTerm(prefix + 27, missingIncomeFlag);
                    }
                    else
                    {
                        // TRANSIT
                        alternative.AddUtilityTerm(prefix + 3, pathTypeModel.GeneralizedTimeLogsum);
                        alternative.AddUtilityTerm(prefix + 31, 1.0);
                        alternative.AddUtilityTerm(prefix + 32, childUnder16Flag);
                        alternative.AddUtilityTerm(prefix + 33, noCarsInHouseholdFlag);
                        alternative.AddUtilityTerm(prefix + 34, fewerCarsThanDriversFlag);
                        alternative.AddUtilityTerm(prefix + 35, lowIncomeFlag);
                        alternative.AddUtilityTerm(prefix + 36, highIncomeFlag);
                        alternative.AddUtilityTerm(prefix + 37, missingIncomeFlag);
                        alternative.AddUtilityTerm(prefix + 38, originTransitBand1);
                        alternative.AddUtilityTerm(prefix + 39, originTransitBand3);
                        //alternative.AddUtilityTerm(prefix + 40, destinationTransitBand3);
                    }



                    alternative.AddUtilityTerm(2, adjustmentFactor);

                    // Size terms
                    alternative.AddUtilityTerm(prefix + 50, destinationParcel.EmploymentService);
                    alternative.AddUtilityTerm(prefix + 51, destinationParcel.EmploymentEducation);
                    alternative.AddUtilityTerm(prefix + 52, destinationParcel.EmploymentGovernment);
                    //alternative.AddUtilityTerm(prefix + 53, destinationParcel.EmploymentIndustrial);
                    alternative.AddUtilityTerm(prefix + 54, destinationParcel.EmploymentOffice);
                    alternative.AddUtilityTerm(prefix + 55, destinationParcel.EmploymentRetail);
                    alternative.AddUtilityTerm(prefix + 56, destinationParcel.EmploymentMedical);
                    alternative.AddUtilityTerm(prefix + 57, destinationParcel.EmploymentFood);
                    //alternative.AddUtilityTerm(prefix + 58, destinationParcel.EmploymentAgricultureConstruction);
                    alternative.AddUtilityTerm(prefix + 59, destinationParcel.Households);
                    //alternative.AddUtilityTerm(prefix + 60, destinationParcel.StudentsK8 + destinationParcel.StudentsHighSchool);
                    //alternative.AddUtilityTerm(prefix + 61, destinationParcel.StudentsUniversity);
                }
            }
            //Global.PrintFile.WriteLine("Sequence {0}: Chosen parcel {1} is sample item {2} of {3}",timesStartedRunModel,tour.DestinationParcelId,indChosen,sampleItems.Count);
        }
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, TourWrapper tour, HouseholdDayWrapper householdDay, int sampleSize, IParcelWrapper choice = null)
        {
            timesStartedRunModel++;
            HouseholdWrapper household = (HouseholdWrapper)tour.Household;
            PersonWrapper    person    = (PersonWrapper)tour.Person;
            PersonDayWrapper personDay = (PersonDayWrapper)tour.PersonDay;

            //			var totalAvailableMinutes =
            //				tour.ParentTour == null
            //					? personDay.TimeWindow.TotalAvailableMinutes(1, Global.Settings.Times.MinutesInADay)
            //					: tour.ParentTour.TimeWindow.TotalAvailableMinutes(1, Global.Settings.Times.MinutesInADay);


            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 (tour.ParentTour == null)
            {
                timeWindow.IncorporateAnotherTimeWindow(personDay.TimeWindow);
            }

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

            var maxAvailableMinutes =
                (tour.JointTourSequence > 0 || tour.ParentTour == null)
                                 ? timeWindow.MaxAvailableMinutesAfter(Global.Settings.Times.FiveAM)
                                          : tour.ParentTour.DestinationDepartureTime - tour.ParentTour.DestinationArrivalTime;


            //			var hoursAvailableInverse =
            //				tour.IsHomeBasedTour
            //					? (personDay.HomeBasedTours - personDay.SimulatedHomeBasedTours + 1) / (Math.Max(totalAvailableMinutes - 360, 30) / 60D)
            //					: 1 / (Math.Max(totalAvailableMinutes, 1) / 60D);

            var householdHasChildren   = household.HasChildren;
            var householdHasNoChildren = householdHasChildren ? false : true;

            var fastestAvailableTimeOfDay =
                tour.IsHomeBasedTour || tour.ParentTour == null
                                          ? 1
                                          : tour.ParentTour.DestinationArrivalTime + (tour.ParentTour.DestinationDepartureTime - tour.ParentTour.DestinationArrivalTime) / 2;

            var tourCategory = tour.GetTourCategory();
            //			var primaryFlag = ChoiceModelUtility.GetPrimaryFlag(tourCategory);
            var secondaryFlag           = ChoiceModelUtility.GetSecondaryFlag(tourCategory);
            var workOrSchoolPatternFlag = personDay.GetIsWorkOrSchoolPattern().ToFlag();
            var otherPatternFlag        = personDay.GetIsOtherPattern().ToFlag();
            int jointTourFlag           = (tour.JointTourSequence > 0).ToFlag();



            ChoiceModelUtility.DrawRandomTourTimePeriods(tour, tourCategory);

            if (tour.Household.Id == 80049 && tour.PersonDay.Day == 1 && tour.Person.Sequence == 2 && tour.Sequence == 4)
            {
                bool testbreak = true;
            }

            var segment = Global.Kernel.Get <SamplingWeightsSettingsFactory>().SamplingWeightsSettings.GetTourDestinationSegment(tour.DestinationPurpose, tour.IsHomeBasedTour ? Global.Settings.TourPriorities.HomeBasedTour : Global.Settings.TourPriorities.WorkBasedTour, Global.Settings.Modes.Sov, person.PersonType);


            var destinationSampler       = new DestinationSampler(choiceProbabilityCalculator, segment, sampleSize, tour.OriginParcel, choice);
            var tourDestinationUtilities = new TourDestinationUtilities(tour, sampleSize, secondaryFlag, personDay.GetIsWorkOrSchoolPattern().ToFlag(), personDay.GetIsOtherPattern().ToFlag(), fastestAvailableTimeOfDay, maxAvailableMinutes);

            // get destination sample and perform code that used to be in SetUtilities below
            var sampleItems = destinationSampler.SampleAndReturnTourDestinations(tourDestinationUtilities);

            int index = 0;

            foreach (var sampleItem in sampleItems)
            {
                bool   available         = sampleItem.Key.Available;
                bool   isChosen          = sampleItem.Key.IsChosen;
                double adjustmentFactor  = sampleItem.Key.AdjustmentFactor;
                var    destinationParcel = ChoiceModelFactory.Parcels[sampleItem.Key.ParcelId];

                if (isChosen)
                {
                    Global.PrintFile.WriteLine("Sequence {0}: Chosen parcel {1} Available {2} Sample item {3} of {4}", timesStartedRunModel, destinationParcel.Id, available, index, sampleItems.Count);
                }

                var alternative = choiceProbabilityCalculator.GetAlternative(index++, available, isChosen);


                if (!available)
                {
                    continue;
                }

                var fastestTravelTime =
                    ImpedanceRoster.GetValue("ivtime", Global.Settings.Modes.Hov3, Global.Settings.PathTypes.FullNetwork, Global.Settings.ValueOfTimes.DefaultVot, fastestAvailableTimeOfDay, tour.OriginParcel, destinationParcel).Variable +
                    ImpedanceRoster.GetValue("ivtime", Global.Settings.Modes.Hov3, Global.Settings.PathTypes.FullNetwork, Global.Settings.ValueOfTimes.DefaultVot, fastestAvailableTimeOfDay, destinationParcel, tour.OriginParcel).Variable;

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

                    continue;
                }

                alternative.Choice = destinationParcel;

                double tourLogsum;

                if (tour.IsHomeBasedTour)
                {
                    if (tour.DestinationPurpose == Global.Settings.Purposes.Work)
                    {
                        //JLB 201406
                        //var nestedAlternative = Global.ChoiceModelSession.Get<WorkTourModeModel>().RunNested(tour, destinationParcel);
                        // JLB 201602
                        //var nestedAlternative = Global.ChoiceModelSession.Get<WorkTourModeTimeModel>().RunNested(tour, destinationParcel, tour.Household.VehiclesAvailable, tour.Person.GetTransitFareDiscountFraction());
                        var nestedAlternative = Global.ChoiceModelSession.Get <TourModeTimeModel>().RunNested(tour, destinationParcel, tour.Household.VehiclesAvailable, tour.Person.GetTransitFareDiscountFraction());
                        tourLogsum = nestedAlternative == null ? 0 : nestedAlternative.ComputeLogsum();
                    }

                    // JLB201406
                    //else if (tour.DestinationPurpose == Global.Settings.Purposes.Escort) {
                    //	var nestedAlternative = Global.ChoiceModelSession.Get<EscortTourModeModel>().RunNested(tour, destinationParcel);
                    //	tourLogsum = nestedAlternative == null ? 0 : nestedAlternative.ComputeLogsum();
                    //}
                    else
                    {
                        // JLB201406
                        //var nestedAlternative = Global.ChoiceModelSession.Get<OtherHomeBasedTourModeModel>().RunNested(tour, destinationParcel);
                        // JLB 201602
                        //var nestedAlternative = Global.ChoiceModelSession.Get<OtherHomeBasedTourModeTimeModel>().RunNested(tour, destinationParcel, tour.Household.VehiclesAvailable, tour.Person.GetTransitFareDiscountFraction());
                        var nestedAlternative = Global.ChoiceModelSession.Get <TourModeTimeModel>().RunNested(tour, destinationParcel, tour.Household.VehiclesAvailable, tour.Person.GetTransitFareDiscountFraction());
                        tourLogsum = nestedAlternative == null ? 0 : nestedAlternative.ComputeLogsum();
                    }
                }
                else
                {
                    // JLB201406
                    //var nestedAlternative = Global.ChoiceModelSession.Get<WorkBasedSubtourModeModel>().RunNested(tour, destinationParcel);
                    // JLB 201602
                    //var nestedAlternative = Global.ChoiceModelSession.Get<WorkBasedSubtourModeTimeModel>().RunNested(tour, destinationParcel, tour.Household.VehiclesAvailable, tour.Person.GetTransitFareDiscountFraction());
                    var nestedAlternative = Global.ChoiceModelSession.Get <TourModeTimeModel>().RunNested(tour, destinationParcel, tour.Household.VehiclesAvailable, tour.Person.GetTransitFareDiscountFraction());
                    tourLogsum = nestedAlternative == null ? 0 : nestedAlternative.ComputeLogsum();
                }

                //var purpose = tour.TourPurposeSegment;
                var carOwnership  = person.GetCarOwnershipSegment();
                var votSegment    = tour.GetVotALSegment();
                var transitAccess = destinationParcel.TransitAccessSegment();
                //var aggregateLogsum = Global.AggregateLogsums[destinationParcel.ZoneId][purpose][carOwnership][votSegment][transitAccess];
                var aggregateLogsumHomeBased = Global.AggregateLogsums[destinationParcel.ZoneId][Global.Settings.Purposes.HomeBasedComposite][carOwnership][votSegment][transitAccess];
                var aggregateLogsumWorkBased = Global.AggregateLogsums[destinationParcel.ZoneId][Global.Settings.Purposes.WorkBased][carOwnership][votSegment][transitAccess];

                var distanceFromOrigin = tour.OriginParcel.DistanceFromOrigin(destinationParcel, tour.DestinationArrivalTime);


                // 1. new from GV: Cph KM-distances
                var piecewiseDistanceFrom0To1Km = Math.Min(distanceFromOrigin, .10);

                var piecewiseDistanceFrom0To2Km = Math.Min(distanceFromOrigin, .20);                 //GV: added July 7th
                var piecewiseDistanceFrom0To5Km = Math.Min(distanceFromOrigin, .50);                 //GV: added July 7th

                var piecewiseDistanceFrom1To2Km         = Math.Max(0, Math.Min(distanceFromOrigin - .1, .2 - .1));
                var piecewiseDistanceFrom2To5Km         = Math.Max(0, Math.Min(distanceFromOrigin - .2, .5 - .2));
                var piecewiseDistanceFrom5To10Km        = Math.Max(0, Math.Min(distanceFromOrigin - .5, 1 - .5));
                var piecewiseDistanceFrom10To20Km       = Math.Max(0, Math.Min(distanceFromOrigin - 1, 2 - 1));
                var piecewiseDistanceFrom20KmToInfinity = Math.Max(0, distanceFromOrigin - 2);

                var piecewiseDistanceFrom10KmToInfinity = Math.Max(0, distanceFromOrigin - 1);
                // 1. finished

                var distanceFromOriginLog = Math.Log(1 + distanceFromOrigin);
                var distanceFromWorkLog   = person.UsualWorkParcel.DistanceFromWorkLog(destinationParcel, 1);
                var distanceFromSchoolLog = person.UsualSchoolParcel.DistanceFromSchoolLog(destinationParcel, 1);

                var timePressure = Math.Log(1 - fastestTravelTime / maxAvailableMinutes);


                // 2. new from GV: Cph buffers for neighborhood effects
                // log transforms of buffers for Neighborhood effects
                var logOfOnePlusEducationK8Buffer2          = Math.Log(destinationParcel.StudentsK8Buffer2 + 1.0);
                var logOfOnePlusEducationUniStuBuffer2      = Math.Log(destinationParcel.StudentsUniversityBuffer2 + 1.0);
                var logOfOnePlusEmploymentEducationBuffer2  = Math.Log(destinationParcel.EmploymentEducationBuffer2 + 1.0);
                var logOfOnePlusEmploymentGovernmentBuffer2 = Math.Log(destinationParcel.EmploymentGovernmentBuffer2 + 1.0);
                var logOfOnePlusEmploymentIndustrialBuffer2 = Math.Log(destinationParcel.EmploymentIndustrialBuffer2 + 1.0);
                var logOfOnePlusEmploymentOfficeBuffer2     = Math.Log(destinationParcel.EmploymentOfficeBuffer2 + 1.0);
                var logOfOnePlusEmploymentRetailBuffer2     = Math.Log(destinationParcel.EmploymentRetailBuffer2 + 1.0);
                var logOfOnePlusEmploymentServiceBuffer2    = Math.Log(destinationParcel.EmploymentServiceBuffer2 + 1.0);
                var logOfOnePlusEmploymentAgrConstrBuffer2  = Math.Log(destinationParcel.EmploymentAgricultureConstructionBuffer2 + 1.0);
                var logOfOnePlusEmploymentJobsBuffer2       = Math.Log(destinationParcel.EmploymentTotalBuffer2 + 1.0);
                var logOfOnePlusHouseholdsBuffer2           = Math.Log(destinationParcel.HouseholdsBuffer2 + 1.0);
                // 2. finished


                var logOfOnePlusParkingOffStreetDailySpacesBuffer1 = Math.Log(1 + destinationParcel.ParkingOffStreetPaidDailySpacesBuffer1);
                // connectivity attributes
                var c34Ratio = destinationParcel.C34RatioBuffer1();

                var carCompetitionFlag   = FlagUtility.GetCarCompetitionFlag(carOwnership);               // exludes no cars
                var noCarCompetitionFlag = FlagUtility.GetNoCarCompetitionFlag(carOwnership);
                var noCarsFlag           = FlagUtility.GetNoCarsFlag(carOwnership);

                alternative.AddUtilityTerm(2, household.Id);
                alternative.AddUtilityTerm(3, personDay.Day);
                alternative.AddUtilityTerm(4, person.Sequence);
                alternative.AddUtilityTerm(5, tour.Sequence);

                alternative.AddUtilityTerm(8, adjustmentFactor);
                alternative.AddUtilityTerm(9, tourLogsum);

                // 3. new from GV: definition of Cph variables

                //alternative.AddUtilityTerm(260, secondaryFlag * workOrSchoolPatternFlag * piecewiseDistanceFrom0To1Km);
                alternative.AddUtilityTerm(260, secondaryFlag * workOrSchoolPatternFlag * piecewiseDistanceFrom0To2Km);                 //GV: added July 7th
                //alternative.AddUtilityTerm(261, secondaryFlag * workOrSchoolPatternFlag * piecewiseDistanceFrom1To2Km);
                alternative.AddUtilityTerm(262, secondaryFlag * workOrSchoolPatternFlag * piecewiseDistanceFrom2To5Km);
                alternative.AddUtilityTerm(263, secondaryFlag * workOrSchoolPatternFlag * piecewiseDistanceFrom5To10Km);
                alternative.AddUtilityTerm(264, secondaryFlag * workOrSchoolPatternFlag * piecewiseDistanceFrom10To20Km);
                alternative.AddUtilityTerm(265, secondaryFlag * workOrSchoolPatternFlag * piecewiseDistanceFrom20KmToInfinity);

                //alternative.AddUtilityTerm(266, secondaryFlag * otherPatternFlag * piecewiseDistanceFrom0To1Km);
                alternative.AddUtilityTerm(266, secondaryFlag * otherPatternFlag * piecewiseDistanceFrom0To2Km);                 //GV: added July 7th
                //alternative.AddUtilityTerm(267, secondaryFlag * otherPatternFlag * piecewiseDistanceFrom1To2Km);
                alternative.AddUtilityTerm(268, secondaryFlag * otherPatternFlag * piecewiseDistanceFrom2To5Km);
                alternative.AddUtilityTerm(269, secondaryFlag * otherPatternFlag * piecewiseDistanceFrom5To10Km);
                alternative.AddUtilityTerm(270, secondaryFlag * otherPatternFlag * piecewiseDistanceFrom10To20Km);
                alternative.AddUtilityTerm(271, secondaryFlag * otherPatternFlag * piecewiseDistanceFrom20KmToInfinity);

                //alternative.AddUtilityTerm(268, (!_tour.IsHomeBasedTour).ToFlag() * distanceFromOriginLog);
                //alternative.AddUtilityTerm(269, household.Has0To15KIncome.ToFlag() * distanceFromOriginLog);
                //alternative.AddUtilityTerm(270, household.HasMissingIncome.ToFlag() * distanceFromOriginLog);
                //alternative.AddUtilityTerm(271, person.IsRetiredAdult.ToFlag() * distanceFromOriginLog);
                //alternative.AddUtilityTerm(272, person.IsUniversityStudent.ToFlag() * distanceFromOriginLog);
                //alternative.AddUtilityTerm(273, person.IsChildAge5Through15.ToFlag() * distanceFromOriginLog);
                //alternative.AddUtilityTerm(274, person.IsChildUnder5.ToFlag() * distanceFromOriginLog);

                alternative.AddUtilityTerm(272, (!tour.IsHomeBasedTour).ToFlag() * distanceFromOriginLog);
                alternative.AddUtilityTerm(273, (household.Income >= 300000 && household.Income < 600000).ToFlag() * distanceFromOriginLog);
                alternative.AddUtilityTerm(274, (household.Income >= 600000 && household.Income < 900000).ToFlag() * distanceFromOriginLog);
                alternative.AddUtilityTerm(275, (household.Income >= 900000).ToFlag() * distanceFromOriginLog);

                //alternative.AddUtilityTerm(276, person.IsChildUnder5.ToFlag() * distanceFromOriginLog); // commented out by GV, July 7th
                //alternative.AddUtilityTerm(277, person.IsChildAge5Through15.ToFlag() * distanceFromOriginLog); // commented out by GV, July 7th
                //alternative.AddUtilityTerm(278, person.IsChildUnder16.ToFlag() * distanceFromOriginLog); // commented out by GV, July 7th
                alternative.AddUtilityTerm(279, person.IsUniversityStudent.ToFlag() * distanceFromOriginLog);

                //GV: 17. june 2015 male commented out
                //alternative.AddUtilityTerm(280, person.IsAdultMale.ToFlag() * distanceFromOriginLog);
                alternative.AddUtilityTerm(281, person.IsAdultFemale.ToFlag() * distanceFromOriginLog);

                alternative.AddUtilityTerm(282, person.IsRetiredAdult.ToFlag() * distanceFromOriginLog);

                //alternative.AddUtilityTerm(283, (tour.IsHomeBasedTour).ToFlag() * timePressure); //commented out by GV: 7th July 2013
                alternative.AddUtilityTerm(284, (tour.IsHomeBasedTour).ToFlag() * distanceFromSchoolLog);
                //alternative.AddUtilityTerm(14, distanceFromWorkLog);

                // GV commented out this - on TO DO list
                //alternative.AddUtilityTerm(277, (carCompetitionFlag + noCarCompetitionFlag) * destinationParcel.ParkingHourlyEmploymentCommercialMixInParcel());
                //alternative.AddUtilityTerm(278, noCarCompetitionFlag * destinationParcel.ParkingHourlyEmploymentCommercialMixInParcel());
                //alternative.AddUtilityTerm(279, carCompetitionFlag * destinationParcel.ParkingHourlyEmploymentCommercialMixBuffer1());
                //alternative.AddUtilityTerm(280, noCarCompetitionFlag * destinationParcel.ParkingHourlyEmploymentCommercialMixBuffer1());
                //alternative.AddUtilityTerm(281, noCarsFlag * c34Ratio);
                //alternative.AddUtilityTerm(282, noCarCompetitionFlag * c34Ratio);
                //alternative.AddUtilityTerm(283, (carCompetitionFlag + noCarCompetitionFlag) * logOfOnePlusParkingOffStreetDailySpacesBuffer1);


                //alternative.AddUtilityTerm(285, jointTourFlag * piecewiseDistanceFrom0To1Km);
                //alternative.AddUtilityTerm(286, jointTourFlag * piecewiseDistanceFrom1To2Km);
                alternative.AddUtilityTerm(286, jointTourFlag * piecewiseDistanceFrom0To2Km);
                alternative.AddUtilityTerm(287, jointTourFlag * piecewiseDistanceFrom2To5Km);
                alternative.AddUtilityTerm(288, jointTourFlag * piecewiseDistanceFrom5To10Km);
                alternative.AddUtilityTerm(289, jointTourFlag * piecewiseDistanceFrom10To20Km);
                alternative.AddUtilityTerm(290, jointTourFlag * piecewiseDistanceFrom20KmToInfinity);
                // 3. finished


                //4. new from GV: purpose utilities
                // COMPAS puposes are: Work, Education, Escort, Shopping, Leisure, Personal business, business
                // You need NO "Work" and "Education", their destinations are known in the synthetic population
                if (tour.DestinationPurpose == Global.Settings.Purposes.Business)
                {
                    //alternative.AddUtilityTerm(10, piecewiseDistanceFrom0To1Km);
                    //alternative.AddUtilityTerm(11, piecewiseDistanceFrom1To2Km);
                    //alternative.AddUtilityTerm(12, piecewiseDistanceFrom2To5Km);
                    alternative.AddUtilityTerm(12, piecewiseDistanceFrom0To5Km);
                    alternative.AddUtilityTerm(13, piecewiseDistanceFrom5To10Km);
                    alternative.AddUtilityTerm(14, piecewiseDistanceFrom10To20Km);
                    alternative.AddUtilityTerm(15, piecewiseDistanceFrom20KmToInfinity);

                    //GV: june 2016 - not sign
                    //alternative.AddUtilityTerm(16, aggregateLogsumWorkBased);

                    // Neighborhood
                    //GV: commented out just temp.
                    //alternative.AddUtilityTerm(20, logOfOnePlusEducationK8Buffer2);
                    //alternative.AddUtilityTerm(21, logOfOnePlusEducationUniStuBuffer2);
                    //alternative.AddUtilityTerm(22, logOfOnePlusEmploymentEducationBuffer2);
                    alternative.AddUtilityTerm(23, logOfOnePlusEmploymentGovernmentBuffer2);
                    //alternative.AddUtilityTerm(24, logOfOnePlusEmploymentIndustrialBuffer2);
                    //alternative.AddUtilityTerm(25, logOfOnePlusEmploymentOfficeBuffer2);
                    //alternative.AddUtilityTerm(26, logOfOnePlusEmploymentRetailBuffer2);
                    alternative.AddUtilityTerm(27, logOfOnePlusEmploymentServiceBuffer2);
                    //alternative.AddUtilityTerm(28, logOfOnePlusEmploymentAgrConstrBuffer2);
                    //alternative.AddUtilityTerm(29, logOfOnePlusEmploymentJobsBuffer2);

                    // Size terms
                    alternative.AddUtilityTerm(30, destinationParcel.EmploymentEducation);
                    alternative.AddUtilityTerm(31, destinationParcel.EmploymentGovernment);
                    alternative.AddUtilityTerm(32, destinationParcel.EmploymentIndustrial);
                    alternative.AddUtilityTerm(33, destinationParcel.EmploymentOffice);
                    alternative.AddUtilityTerm(34, destinationParcel.EmploymentRetail);
                    // GV: 35 is fixed to zero
                    alternative.AddUtilityTerm(35, destinationParcel.EmploymentService);
                    alternative.AddUtilityTerm(36, destinationParcel.EmploymentAgricultureConstruction);
                    alternative.AddUtilityTerm(37, destinationParcel.EmploymentTotal);
                    alternative.AddUtilityTerm(38, destinationParcel.Households);
                }
                else if (tour.DestinationPurpose == Global.Settings.Purposes.Escort)
                {
                    //alternative.AddUtilityTerm(50, piecewiseDistanceFrom0To1Km);
                    alternative.AddUtilityTerm(51, piecewiseDistanceFrom0To2Km);
                    //alternative.AddUtilityTerm(52, piecewiseDistanceFrom2To5Km);
                    alternative.AddUtilityTerm(52, piecewiseDistanceFrom2To5Km);
                    alternative.AddUtilityTerm(53, piecewiseDistanceFrom5To10Km);
                    //alternative.AddUtilityTerm(54, piecewiseDistanceFrom10To20Km);
                    alternative.AddUtilityTerm(55, piecewiseDistanceFrom10KmToInfinity);
                    //alternative.AddUtilityTerm(55, piecewiseDistanceFrom20KmToInfinity);

                    //GV: june 2016 - not sign
                    //alternative.AddUtilityTerm(56, aggregateLogsumHomeBased);

                    // Neighborhood
                    //GV: commented out just temp.
                    alternative.AddUtilityTerm(60, householdHasNoChildren.ToFlag() * logOfOnePlusEmploymentJobsBuffer2);
                    //alternative.AddUtilityTerm(61, householdHasNoChildren.ToFlag() * logOfOnePlusHouseholdsBuffer2);
                    //alternative.AddUtilityTerm(62, householdHasChildren.ToFlag() * logOfOnePlusHouseholdsBuffer2);
                    //alternative.AddUtilityTerm(64, logOfOnePlusEmploymentJobsBuffer2);

                    // Size terms
                    // GV: no observations
                    alternative.AddUtilityTerm(70, (!householdHasChildren).ToFlag() * destinationParcel.EmploymentEducation);
                    alternative.AddUtilityTerm(71, (!householdHasChildren).ToFlag() * destinationParcel.EmploymentGovernment);
                    alternative.AddUtilityTerm(72, (!householdHasChildren).ToFlag() * destinationParcel.EmploymentIndustrial);
                    alternative.AddUtilityTerm(73, (!householdHasChildren).ToFlag() * destinationParcel.EmploymentOffice);
                    alternative.AddUtilityTerm(74, (!householdHasChildren).ToFlag() * destinationParcel.EmploymentRetail);
                    // GV: 75 is fixed to zero
                    alternative.AddUtilityTerm(75, (!householdHasChildren).ToFlag() * destinationParcel.EmploymentService);
                    alternative.AddUtilityTerm(76, (!householdHasChildren).ToFlag() * destinationParcel.EmploymentAgricultureConstruction);
                    alternative.AddUtilityTerm(77, (!householdHasChildren).ToFlag() * destinationParcel.EmploymentTotal);
                    alternative.AddUtilityTerm(78, (!householdHasChildren).ToFlag() * destinationParcel.Households);

                    alternative.AddUtilityTerm(80, (householdHasChildren).ToFlag() * destinationParcel.EmploymentEducation);
                    alternative.AddUtilityTerm(81, (householdHasChildren).ToFlag() * destinationParcel.EmploymentGovernment);
                    alternative.AddUtilityTerm(82, (householdHasChildren).ToFlag() * destinationParcel.EmploymentIndustrial);
                    alternative.AddUtilityTerm(83, (householdHasChildren).ToFlag() * destinationParcel.EmploymentOffice);
                    alternative.AddUtilityTerm(84, (householdHasChildren).ToFlag() * destinationParcel.EmploymentRetail);
                    // GV 85 is fixed to zero at the moment
                    alternative.AddUtilityTerm(85, (householdHasChildren).ToFlag() * destinationParcel.EmploymentService);
                    alternative.AddUtilityTerm(86, (householdHasChildren).ToFlag() * destinationParcel.EmploymentAgricultureConstruction);
                    alternative.AddUtilityTerm(87, (householdHasChildren).ToFlag() * destinationParcel.EmploymentTotal);
                    alternative.AddUtilityTerm(88, (householdHasChildren).ToFlag() * destinationParcel.Households);
                }
                else if (tour.DestinationPurpose == Global.Settings.Purposes.PersonalBusiness)
                {
                    alternative.AddUtilityTerm(90, piecewiseDistanceFrom0To2Km);
                    //alternative.AddUtilityTerm(91, piecewiseDistanceFrom1To2Km);
                    alternative.AddUtilityTerm(92, piecewiseDistanceFrom2To5Km);
                    alternative.AddUtilityTerm(93, piecewiseDistanceFrom5To10Km);
                    alternative.AddUtilityTerm(95, piecewiseDistanceFrom10KmToInfinity);

                    //GV: june 2016 - not sign
                    //alternative.AddUtilityTerm(96, aggregateLogsumHomeBased);

                    // Neighborhood
                    //GV: commented out just temp.
                    //alternative.AddUtilityTerm(100, logOfOnePlusEmploymentEducationBuffer2);
                    //alternative.AddUtilityTerm(101, logOfOnePlusEmploymentGovernmentBuffer2);
                    //alternative.AddUtilityTerm(102, logOfOnePlusEmploymentIndustrialBuffer2);
                    alternative.AddUtilityTerm(103, logOfOnePlusEmploymentOfficeBuffer2);
                    alternative.AddUtilityTerm(104, logOfOnePlusEmploymentRetailBuffer2);
                    alternative.AddUtilityTerm(105, logOfOnePlusEmploymentServiceBuffer2);
                    //alternative.AddUtilityTerm(106, logOfOnePlusEmploymentAgrConstrBuffer2);
                    //alternative.AddUtilityTerm(107, logOfOnePlusEmploymentJobsBuffer2);

                    // Size terms
                    alternative.AddUtilityTerm(110, destinationParcel.EmploymentEducation);
                    alternative.AddUtilityTerm(111, destinationParcel.EmploymentGovernment);
                    alternative.AddUtilityTerm(112, destinationParcel.EmploymentIndustrial);
                    alternative.AddUtilityTerm(113, destinationParcel.EmploymentOffice);
                    alternative.AddUtilityTerm(114, destinationParcel.EmploymentRetail);
                    // GV 115 is fixed to zero
                    alternative.AddUtilityTerm(115, destinationParcel.EmploymentService);
                    alternative.AddUtilityTerm(116, destinationParcel.EmploymentAgricultureConstruction);
                    alternative.AddUtilityTerm(117, destinationParcel.EmploymentTotal);
                    alternative.AddUtilityTerm(118, destinationParcel.Households);
                }
                else if (tour.DestinationPurpose == Global.Settings.Purposes.Shopping)
                {
                    //alternative.AddUtilityTerm(120, piecewiseDistanceFrom0To1Km);
                    alternative.AddUtilityTerm(121, piecewiseDistanceFrom0To2Km);
                    alternative.AddUtilityTerm(122, piecewiseDistanceFrom2To5Km);
                    alternative.AddUtilityTerm(123, piecewiseDistanceFrom5To10Km);
                    alternative.AddUtilityTerm(124, piecewiseDistanceFrom10To20Km);
                    alternative.AddUtilityTerm(125, piecewiseDistanceFrom20KmToInfinity);

                    //GV: june 2016 - not sign
                    //alternative.AddUtilityTerm(126, aggregateLogsumHomeBased);

                    // Neighborhood
                    //GV: commented out just temp.
                    //alternative.AddUtilityTerm(130, logOfOnePlusEmploymentEducationBuffer2);
                    alternative.AddUtilityTerm(131, logOfOnePlusEmploymentRetailBuffer2);
                    //alternative.AddUtilityTerm(132, logOfOnePlusEmploymentJobsBuffer2);

                    // Size terms
                    alternative.AddUtilityTerm(140, destinationParcel.EmploymentOffice);
                    alternative.AddUtilityTerm(141, destinationParcel.EmploymentRetail);
                    // GV 142 is fixed to zero
                    alternative.AddUtilityTerm(142, destinationParcel.EmploymentService);
                    alternative.AddUtilityTerm(143, destinationParcel.EmploymentTotal);
                }
                else if (tour.DestinationPurpose == Global.Settings.Purposes.Social)
                {
                    //alternative.AddUtilityTerm(170, piecewiseDistanceFrom0To1Km);
                    //alternative.AddUtilityTerm(171, piecewiseDistanceFrom1To2Km);
                    alternative.AddUtilityTerm(170, piecewiseDistanceFrom0To2Km);
                    alternative.AddUtilityTerm(172, piecewiseDistanceFrom2To5Km);
                    alternative.AddUtilityTerm(173, piecewiseDistanceFrom5To10Km);
                    alternative.AddUtilityTerm(174, piecewiseDistanceFrom10To20Km);
                    alternative.AddUtilityTerm(175, piecewiseDistanceFrom20KmToInfinity);

                    //GV: june 2016 - not sign
                    //alternative.AddUtilityTerm(176, aggregateLogsumHomeBased);

                    // Neighborhood
                    //GV: commented out just temp.
                    //alternative.AddUtilityTerm(180, logOfOnePlusEmploymentOfficeBuffer2);
                    alternative.AddUtilityTerm(181, logOfOnePlusEmploymentRetailBuffer2);
                    alternative.AddUtilityTerm(182, logOfOnePlusEmploymentServiceBuffer2);
                    //alternative.AddUtilityTerm(183, logOfOnePlusEmploymentJobsBuffer2);

                    // Size terms
                    alternative.AddUtilityTerm(190, destinationParcel.EmploymentEducation);
                    alternative.AddUtilityTerm(191, destinationParcel.EmploymentGovernment);
                    alternative.AddUtilityTerm(192, destinationParcel.EmploymentIndustrial);
                    alternative.AddUtilityTerm(193, destinationParcel.EmploymentOffice);
                    alternative.AddUtilityTerm(194, destinationParcel.EmploymentRetail);
                    // GV 195 is fixed to zero
                    alternative.AddUtilityTerm(195, destinationParcel.EmploymentService);
                    alternative.AddUtilityTerm(196, destinationParcel.EmploymentAgricultureConstruction);
                    alternative.AddUtilityTerm(197, destinationParcel.EmploymentTotal);
                    alternative.AddUtilityTerm(198, destinationParcel.Households);
                }
            }
        }
Example #5
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)));
            }
        }