예제 #1
0
        private void RunModel(ChoiceProbabilityCalculator choiceProbabilityCalculator, IPersonWrapper person, int choice = Constants.DEFAULT_VALUE)
        {
            IParcelWrapper homeParcel   = person.Household.ResidenceParcel;
            IParcelWrapper workParcel   = person.IsUniversityStudent ? person.UsualSchoolParcel : person.UsualWorkParcel;
            IParcelWrapper schoolParcel = person.IsUniversityStudent ? null : person.UsualSchoolParcel;

            bool workParcelMissing   = workParcel == null;
            bool schoolParcelMissing = schoolParcel == null;

            const double maxTranDist = 1.5;

            double homeTranDist = 99.0;

            if (homeParcel.GetDistanceToTransit() >= 0.0001 && homeParcel.GetDistanceToTransit() <= maxTranDist)
            {
                homeTranDist = homeParcel.GetDistanceToTransit();
            }

            double workTranDist = 99.0;

            if (!workParcelMissing && workParcel.GetDistanceToTransit() >= 0.0001 && workParcel.GetDistanceToTransit() <= maxTranDist)
            {
                workTranDist = workParcel.GetDistanceToTransit();
            }

            double schoolTranDist = 99.0;

            if (!schoolParcelMissing && schoolParcel.GetDistanceToTransit() >= 0.0001 && schoolParcel.GetDistanceToTransit() <= maxTranDist)
            {
                schoolTranDist = schoolParcel.GetDistanceToTransit();
            }

            double workGenTimeNoPass   = -99.0;
            double workGenTimeWithPass = -99.0;

            if (!workParcelMissing && workTranDist < maxTranDist && homeTranDist < maxTranDist)
            {
                IEnumerable <IPathTypeModel> pathTypeModels = PathTypeModelFactory.Singleton.Run(
                    person.Household.RandomUtility,
                    homeParcel,
                    workParcel,
                    Global.Settings.Times.EightAM,
                    Global.Settings.Times.FivePM,
                    Global.Settings.Purposes.Work,
                    Global.Coefficients_BaseCostCoefficientPerMonetaryUnit,
                    Global.Configuration.Coefficients_MeanTimeCoefficient_Work,
                    /* isDrivingAge */ true,
                    /* householdCars */ 1,
                    /* transitPassOwnership */ 0,
                    /* carsAreAvs */ false,
                    0.0,
                    false,
                    Global.Settings.Modes.Transit);

                IPathTypeModel path = pathTypeModels.First();

                workGenTimeNoPass = path.GeneralizedTimeLogsum;

                pathTypeModels = PathTypeModelFactory.Singleton.Run(
                    person.Household.RandomUtility,
                    homeParcel,
                    workParcel,
                    Global.Settings.Times.EightAM,
                    Global.Settings.Times.FivePM,
                    Global.Settings.Purposes.Work,
                    Global.Coefficients_BaseCostCoefficientPerMonetaryUnit,
                    Global.Configuration.Coefficients_MeanTimeCoefficient_Work,
                    /* isDrivingAge */ true,
                    /* householdCars */ 1,
                    /* transitPassOwnership */ 0,
                    /* carsAreAvs */ false,
                    1.0,
                    false,
                    Global.Settings.Modes.Transit);

                path = pathTypeModels.First();

                workGenTimeWithPass = path.GeneralizedTimeLogsum;
            }

            //            double schoolGenTimeNoPass = -99.0;
            double schoolGenTimeWithPass = -99.0;

            if (!schoolParcelMissing && schoolTranDist < maxTranDist && homeTranDist < maxTranDist)
            {
                //                schoolGenTimeNoPass = path.GeneralizedTimeLogsum;
                IEnumerable <IPathTypeModel> pathTypeModels =
                    PathTypeModelFactory.Singleton.Run(
                        person.Household.RandomUtility,
                        homeParcel,
                        schoolParcel,
                        Global.Settings.Times.EightAM,
                        Global.Settings.Times.ThreePM,
                        Global.Settings.Purposes.School,
                        Global.Coefficients_BaseCostCoefficientPerMonetaryUnit,
                        Global.Configuration.Coefficients_MeanTimeCoefficient_Other,
                        /* isDrivingAge */ true,
                        /* householdCars */ 1,
                        /* transitPassOwnership */ 0,
                        /* carsAreAvs */ false,
                        1.0,
                        false,
                        Global.Settings.Modes.Transit);

                IPathTypeModel path = pathTypeModels.First();

                schoolGenTimeWithPass = path.GeneralizedTimeLogsum;
            }

            const double inflection = 0.50;

            double homeTranDist1 = Math.Pow(Math.Min(inflection, homeTranDist), 2.0);
            double homeTranDist2 = Math.Pow(Math.Max(homeTranDist - inflection, 0), 0.5);

            //            var workTranDist1 = Math.Pow(Math.Min(inflection, workTranDist),2.0);
            //            var workTranDist2 = Math.Pow(Math.Max(workTranDist - inflection, 0),0.5);

            const double minimumAggLogsum = -15.0;
            int          votSegment       = person.Household.GetVotALSegment();

            int    homeTaSegment            = homeParcel.TransitAccessSegment();
            double homeAggregateLogsumNoCar = Math.Max(minimumAggLogsum, Global.AggregateLogsums[homeParcel.ZoneId][Global.Settings.Purposes.HomeBasedComposite][Global.Settings.CarOwnerships.NoCars][votSegment][homeTaSegment]);

            int    workTaSegment            = workParcelMissing ? 0 : workParcel.TransitAccessSegment();
            double workAggregateLogsumNoCar =
                workParcelMissing
                    ? 0
                    : Math.Max(minimumAggLogsum, Global.AggregateLogsums[workParcel.ZoneId][Global.Settings.Purposes.WorkBased][Global.Settings.CarOwnerships.NoCars][votSegment][workTaSegment]);

            int    schoolTaSegment            = schoolParcelMissing ? 0 : schoolParcel.TransitAccessSegment();
            double schoolAggregateLogsumNoCar =
                schoolParcelMissing
                    ? 0
                    : Math.Max(minimumAggLogsum, Global.AggregateLogsums[schoolParcel.ZoneId][Global.Settings.Purposes.WorkBased][Global.Settings.CarOwnerships.NoCars][votSegment][schoolTaSegment]);

            double transitPassCostChange = !Global.Configuration.IsInEstimationMode ? Global.Configuration.PathImpedance_TransitPassCostPercentChangeVersusBase : 0;

            // 0 No transit pass
            ChoiceProbabilityCalculator.Alternative alternative = choiceProbabilityCalculator.GetAlternative(0, true, choice == 0);
            alternative.Choice = 0;

            alternative.AddUtilityTerm(1, 0.0);

            // 1 Transit pass
            alternative        = choiceProbabilityCalculator.GetAlternative(1, true, choice == 1);
            alternative.Choice = 1;

            alternative.AddUtilityTerm(1, 1.0);
            alternative.AddUtilityTerm(2, person.IsPartTimeWorker.ToFlag());
            alternative.AddUtilityTerm(3, (person.IsWorker && person.IsNotFullOrPartTimeWorker).ToFlag());
            alternative.AddUtilityTerm(4, person.IsUniversityStudent.ToFlag());
            alternative.AddUtilityTerm(5, person.IsRetiredAdult.ToFlag());
            alternative.AddUtilityTerm(6, person.IsNonworkingAdult.ToFlag());
            alternative.AddUtilityTerm(7, person.IsDrivingAgeStudent.ToFlag());
            alternative.AddUtilityTerm(8, person.IsChildUnder16.ToFlag());
            alternative.AddUtilityTerm(9, Math.Log(Math.Max(1, person.Household.Income)));
            alternative.AddUtilityTerm(10, person.Household.HasMissingIncome.ToFlag());
            alternative.AddUtilityTerm(11, workParcelMissing.ToFlag());
            alternative.AddUtilityTerm(12, schoolParcelMissing.ToFlag());
            alternative.AddUtilityTerm(13, (homeTranDist < 90.0) ? homeTranDist1 : 0);
            alternative.AddUtilityTerm(14, (homeTranDist < 90.0) ? homeTranDist2 : 0);
            alternative.AddUtilityTerm(15, (homeTranDist > 90.0) ? 1 : 0);
            //            alternative.AddUtility(16, (workTranDist < 90.0) ? workTranDist : 0);
            //            alternative.AddUtility(17, (workTranDist < 90.0) ? workTranDist2 : 0);
            //            alternative.AddUtility(18, (workTranDist > 90.0) ? 1 : 0);
            //            alternative.AddUtility(19, (schoolTranDist < 90.0) ? schoolTranDist : 0);
            //            alternative.AddUtility(20, (schoolTranDist > 90.0) ? 1 : 0);
            //            alternative.AddUtility(21, (!workParcelMissing && workGenTimeWithPass > -90 ) ? workGenTimeWithPass : 0);
            alternative.AddUtilityTerm(22, (!workParcelMissing && workGenTimeWithPass <= -90) ? 1 : 0);
            alternative.AddUtilityTerm(23, (!workParcelMissing && workGenTimeWithPass > -90 && workGenTimeNoPass > -90) ? workGenTimeNoPass - workGenTimeWithPass : 0);
            //            alternative.AddUtility(24, (!schoolParcelMissing && schoolGenTimeWithPass > -90 ) ? schoolGenTimeWithPass : 0);
            alternative.AddUtilityTerm(25, (!schoolParcelMissing && schoolGenTimeWithPass <= -90) ? 1 : 0);
            alternative.AddUtilityTerm(26, homeAggregateLogsumNoCar * (person.IsFullOrPartTimeWorker || person.IsUniversityStudent).ToFlag());
            alternative.AddUtilityTerm(27, homeAggregateLogsumNoCar * (person.IsDrivingAgeStudent || person.IsChildUnder16).ToFlag());
            alternative.AddUtilityTerm(28, homeAggregateLogsumNoCar * (person.IsNonworkingAdult).ToFlag());
            alternative.AddUtilityTerm(29, homeAggregateLogsumNoCar * (person.IsRetiredAdult).ToFlag());
            alternative.AddUtilityTerm(30, workParcelMissing ? 0 : workAggregateLogsumNoCar);
            alternative.AddUtilityTerm(31, schoolParcelMissing ? 0 : schoolAggregateLogsumNoCar);
            alternative.AddUtilityTerm(32, transitPassCostChange);
        }
        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);
        }