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);
        }
Esempio n. 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);
        }
Esempio n. 3
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, ITourWrapper tour, 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);

            int maxAvailableMinutes =
                tour.ParentTour == null
                    ? personDay.TimeWindow.MaxAvailableMinutesAfter(121)
                    : 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.DrawRandomTourTimePeriods(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(this, tour, sampleSize, secondaryFlag, personDay.GetIsWorkOrSchoolPattern().ToFlag(), personDay.GetIsOtherPattern().ToFlag(), fastestAvailableTimeOfDay, maxAvailableMinutes);

            destinationSampler.SampleTourDestinations(tourDestinationUtilities);
        }