public void Run(TourWrapper subtour)
        {
            if (subtour == null)
            {
                throw new ArgumentNullException("subtour");
            }

            TourTime.InitializeTourTimes();

            subtour.PersonDay.ResetRandom(50 + subtour.Sequence - 1);

            if (subtour == null)
            {
                throw new ArgumentNullException("subtour");
            }

            if (Global.Configuration.IsInEstimationMode)
            {
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME)
                {
                    return;
                }
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator(subtour.Id);

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                if (subtour.DestinationParcel == null || subtour.OriginParcel == null || subtour.Mode <= Global.Settings.Modes.None || subtour.Mode >= Global.Settings.Modes.ParkAndRide)
                {
                    return;
                }

                RunModel(choiceProbabilityCalculator, subtour, new TourTime(subtour.DestinationArrivalTime, subtour.DestinationDepartureTime));

                choiceProbabilityCalculator.WriteObservation();
            }
            else
            {
                RunModel(choiceProbabilityCalculator, subtour);

                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(subtour.Household.RandomUtility);

                if (chosenAlternative == null)
                {
                    Global.PrintFile.WriteNoAlternativesAvailableWarning(CHOICE_MODEL_NAME, "Run", subtour.PersonDay.Id);
                    if (!Global.Configuration.IsInEstimationMode)
                    {
                        subtour.PersonDay.IsValid = false;
                    }
                    return;
                }

                var choice           = (TourTime)chosenAlternative.Choice;
                var destinationTimes = choice.GetDestinationTimes(subtour);

                subtour.DestinationArrivalTime   = destinationTimes.Start;
                subtour.DestinationDepartureTime = destinationTimes.End;
            }
        }
Esempio n. 2
0
        public int Run(HouseholdDayWrapper householdDay, int jHTSimulated, int genChoice, int[] participants, int[] jHTChauffeurSequence)
        {
            if (householdDay == null)
            {
                throw new ArgumentNullException("householdDay");
            }

            householdDay.ResetRandom(930 + jHTSimulated);              // JLB TODO:  what index to use? need to enable ResetRandom for HouseholdDay

            IEnumerable <PersonDayWrapper> orderedPersonDays = householdDay.PersonDays.OrderBy(p => p.GetJointHalfTourParticipationPriority()).ToList().Cast <PersonDayWrapper>();

            int chauffeurSequence = 0;
            int choice            = 0;

            if (Global.Configuration.IsInEstimationMode)
            {
                chauffeurSequence = jHTChauffeurSequence[jHTSimulated];
                int i = 0;
                foreach (PersonDayWrapper personDay in orderedPersonDays)
                {
                    i++;
                    if (personDay.Person.Sequence == chauffeurSequence && i <= 5)
                    {
                        choice = i;
                    }
                }
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME || choice == 0)
                {
                    return(chauffeurSequence);
                }
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator(((householdDay.Household.Id * 10 + householdDay.Day) * 397) ^ jHTSimulated);

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                RunModel(choiceProbabilityCalculator, householdDay, jHTSimulated, genChoice, participants, choice);

                choiceProbabilityCalculator.WriteObservation();

                return(chauffeurSequence);
            }
            else
            {
                RunModel(choiceProbabilityCalculator, householdDay, genChoice, jHTSimulated, participants);

                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(householdDay.Household.RandomUtility);

                int i = 0;
                foreach (PersonDayWrapper personDay in orderedPersonDays)
                {
                    i++;
                    if ((int)chosenAlternative.Choice == i)
                    {
                        chauffeurSequence = personDay.Person.Sequence;
                    }
                }
                return(chauffeurSequence);
            }
        }
Esempio n. 3
0
        public void Run(PersonDayWrapper personDay, HouseholdDayWrapper householdDay)
        {
            if (personDay == null)
            {
                throw new ArgumentNullException("personDay");
            }

            personDay.Person.ResetRandom(961);

            int choice = 0;

            if (Global.Configuration.IsInEstimationMode)
            {
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME)
                {
                    return;
                }
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator(personDay.Person.Id * 10 + personDay.Day);

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                choice = Math.Min(personDay.BusinessStops, 1) + 2 * Math.Min(personDay.SchoolStops, 1);

                RunModel(choiceProbabilityCalculator, personDay, householdDay, choice);

                choiceProbabilityCalculator.WriteObservation();
            }

            else if (Global.Configuration.TestEstimationModelInApplicationMode)
            {
                Global.Configuration.IsInEstimationMode = false;

                choice = Math.Min(personDay.BusinessStops, 1) + 2 * Math.Min(personDay.SchoolStops, 1);

                RunModel(choiceProbabilityCalculator, personDay, householdDay);

                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(householdDay.Household.RandomUtility, personDay.Id, choice);

                Global.Configuration.IsInEstimationMode = true;
            }

            else
            {
                RunModel(choiceProbabilityCalculator, personDay, householdDay);

                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(householdDay.Household.RandomUtility);
                choice = (int)chosenAlternative.Choice;

                if (choice == 1 || choice == 3)
                {
                    personDay.BusinessStops = 1;
                }
                if (choice == 2 || choice == 3)
                {
                    personDay.SchoolStops = 1;
                }
            }
        }
Esempio n. 4
0
        private void Save()
        {
            while (true)
            {
                lock (_queue) {
#if DEBUG
                    ParallelUtility.countLocks("_queue");
#endif

                    if (_queue.Count == 0 && !_shutdown)
                    {
                        Monitor.Wait(_queue);
                    }

                    if (_queue.Count > 0)
                    {
                        ISavable item = _queue.Dequeue();

                        item.Save();
                    }
                    else if (_shutdown)
                    {
                        break;
                    }
                }
            }
        }
Esempio n. 5
0
        public static void BeginProgram()
        {
            var timer = new Timer("Starting DaySim Controller...");

            Console.WriteLine("Run initial DaySim steps locally");

            var settingsFactory = new SettingsFactory(Global.Configuration);

            Global.Settings = settingsFactory.Create();

            ParallelUtility.Init(Global.Configuration);

            Engine.InitializeDaySim();
            Engine.BeginInitialize();
            Engine.BeginRunRawConversion();
            Engine.BeginImportData();
            Engine.BeginBuildIndexes();

            BeginRunRemoteProcesses();
            BeginMerge();
            BeginLoadData();

            Engine.BeginUpdateShadowPricing();
            if (Global.Configuration.RemoteCopySPFilesToRemoteMachines)
            {
                BeginCopyFilesToRemoteMachines();
            }

            timer.Stop("Total running time");
        }
Esempio n. 6
0
 public static void SetTimeImpedances(ITripWrapper trip)
 {
     foreach (var time in Times[ParallelUtility.GetBatchFromThreadId()])
     {
         SetTimeImpedanceAndWindow(trip, time);
     }
 }
Esempio n. 7
0
        public ChoiceProbabilityCalculator.Alternative RunNested(ITourWrapper tour, IParcelWrapper destinationParcel)
        {
            if (tour == null)
            {
                throw new ArgumentNullException("tour");
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetNestedChoiceProbabilityCalculator();

            IEnumerable <dynamic> pathTypeModels =
                PathTypeModelFactory.Model.RunAll(
                    tour.Household.RandomUtility,
                    tour.OriginParcel,
                    destinationParcel,
                    tour.DestinationArrivalTime,
                    tour.DestinationDepartureTime,
                    tour.DestinationPurpose,
                    tour.CostCoefficient,
                    tour.TimeCoefficient,
                    tour.Person.IsDrivingAge,
                    tour.Household.VehiclesAvailable,
                    tour.Person.GetTransitFareDiscountFraction(),
                    false);

            RunModel(choiceProbabilityCalculator, tour, pathTypeModels, destinationParcel);

            return(choiceProbabilityCalculator.SimulateChoice(tour.Household.RandomUtility));
        }
        public int Run(IPersonDayWrapper personDay, IHouseholdDayWrapper householdDay, int maxPurpose, int choice)
        {
            if (householdDay == null)
            {
                throw new ArgumentNullException("householdDay");
            }

            householdDay.ResetRandom(949 + personDay.GetTotalCreatedTours());

            if (Global.Configuration.IsInEstimationMode)
            {
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME)
                {
                    return(choice);
                }
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator(((personDay.Person.Id * 10 + personDay.Day) * 397) ^ personDay.GetTotalCreatedTours());

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                RunModel(choiceProbabilityCalculator, personDay, householdDay, maxPurpose, choice);

                choiceProbabilityCalculator.WriteObservation();
            }
            else
            {
                RunModel(choiceProbabilityCalculator, personDay, householdDay, maxPurpose);

                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(householdDay.Household.RandomUtility);
                choice = (int)chosenAlternative.Choice;
            }

            return(choice);
        }
Esempio n. 9
0
        public static void SetModeTimeImpedances(IHouseholdDayWrapper householdDay, ITourWrapper tour,
                                                 int constrainedMode, int constrainedArrivalTime, int constrainedDepartureTime, int constrainedHouseholdCars, double constrainedTransitDiscountFraction, IParcelWrapper alternativeDestination = null)
        {
            /*			if (householdDay.Household.Id == 80059 && tour.Person.Sequence == 2 && tour.Sequence == 2
             *                              && constrainedMode == 5 && constrainedArrivalTime == 354 && constrainedDepartureTime == 361) {
             *                              bool testBreak = true;
             *                      }
             */

            var timeWindow = (householdDay != null && tour != null) ? tour.GetRelevantTimeWindow(householdDay) : new TimeWindow();

            {
                foreach (var modeTimes in ModeTimes[ParallelUtility.GetBatchFromThreadId()])
                {
                    modeTimes.LongestFeasibleWindow = null;
                    if ((constrainedMode <= 0 || constrainedMode == modeTimes.Mode)
                        &&
                        (constrainedArrivalTime <= 0 ||
                         constrainedArrivalTime.IsBetween(modeTimes.ArrivalPeriod.Start, modeTimes.ArrivalPeriod.End))
                        &&
                        (constrainedDepartureTime <= 0 ||
                         constrainedDepartureTime.IsBetween(modeTimes.DeparturePeriod.Start, modeTimes.DeparturePeriod.End)))
                    {
                        SetImpedanceAndWindow(timeWindow, tour, modeTimes, constrainedHouseholdCars, constrainedTransitDiscountFraction, alternativeDestination);
                    }
                }
            }
        }
Esempio n. 10
0
        public void Run(IPersonWrapper person, int sampleSize)
        {
            if (person == null)
            {
                throw new ArgumentNullException("person");
            }

            person.ResetRandom(0);

            if (Global.Configuration.IsInEstimationMode)
            {
                //	if (!_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode) {
                //		return;
                //	}
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME)
                {
                    return;
                }
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator(person.Id);

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                if (person.UsualWorkParcel == null)
                {
                    return;
                }

                var choseHome    = person.UsualWorkParcelId == person.Household.ResidenceParcelId;              // JLB 20120329 added these two lines
                var chosenParcel = choseHome ? null : person.UsualWorkParcel;

                //RunModel(choiceProbabilityCalculator, person, sampleSize, person.UsualWorkParcel);
                RunModel(choiceProbabilityCalculator, person, sampleSize, chosenParcel, choseHome);                 // JLB 20120329 replaced above line
                // when chosenParcel is null:
                // DestinationSampler doesn't try to assign one of the sampled destinations as chosen
                // choseHome is NOT null, and RunModel sets the oddball location as chosen

                choiceProbabilityCalculator.WriteObservation();
            }
            else
            {
                RunModel(choiceProbabilityCalculator, person, sampleSize);

                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(person.Household.RandomUtility);
                var choice            = (ParcelWrapper)chosenAlternative.Choice;

                person.UsualWorkParcelId = choice.Id;
                person.UsualWorkParcel   = choice;
                person.UsualWorkZoneKey  = ChoiceModelFactory.ZoneKeys[choice.ZoneId];

                var skimValue = ImpedanceRoster.GetValue("ivtime", Global.Settings.Modes.Sov, Global.Settings.PathTypes.FullNetwork, Global.Settings.ValueOfTimes.DefaultVot, 1, person.Household.ResidenceParcel, choice);

                person.AutoTimeToUsualWork     = skimValue.Variable;
                person.AutoDistanceToUsualWork = skimValue.BlendVariable;

                person.SetWorkParcelPredictions();
            }
        }
        public void Run(PersonDayWrapper personDay, HouseholdDayWrapper householdDay)
        {
            if (personDay == null)
            {
                throw new ArgumentNullException("personDay");
            }

            personDay.Person.ResetRandom(903);

            int choice = 0;

            if (Global.Configuration.IsInEstimationMode)
            {
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME)
                {
                    return;
                }
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator(personDay.Person.Id * 10 + personDay.Day);

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                choice = personDay.PatternType;

                RunModel(choiceProbabilityCalculator, personDay, householdDay, choice);

                choiceProbabilityCalculator.WriteObservation();
            }

            else if (Global.Configuration.TestEstimationModelInApplicationMode)
            {
                Global.Configuration.IsInEstimationMode = false;

                //choice = personDay.PatternType;

                RunModel(choiceProbabilityCalculator, personDay, householdDay);

                //var observedChoice = new HTourModeTime(tour.Mode, tour.DestinationArrivalTime, tour.DestinationDepartureTime);

                //var simulatedChoice = choiceProbabilityCalculator.SimulateChoice(householdDay.Household.RandomUtility, householdDay.Household.Id, altPTypes.);

                //var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(householdDay.Household.RandomUtility, householdDay.Household.Id, choice);

                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(householdDay.Household.RandomUtility, personDay.Id, personDay.PatternType - 1);

                Global.Configuration.IsInEstimationMode = true;
            }

            else
            {
                RunModel(choiceProbabilityCalculator, personDay, householdDay);

                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(householdDay.Household.RandomUtility);
                choice = (int)chosenAlternative.Choice;

                personDay.PatternType = choice;
            }
        }
        public int Run(PersonDayWrapper personDay, HouseholdDayWrapper householdDay, int nCallsForTour, int[] simulatedMandatoryTours, int choice)
        {
            if (personDay == null)
            {
                throw new ArgumentNullException("personDay");
            }

            personDay.Person.ResetRandom(904 + nCallsForTour);

            if (Global.Configuration.IsInEstimationMode)
            {
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME)
                {
                    return(choice);
                }
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator(((personDay.Person.Id * 10 + personDay.Day) * 397) ^ nCallsForTour);

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                RunModel(choiceProbabilityCalculator, personDay, householdDay, nCallsForTour, simulatedMandatoryTours, choice);
                choiceProbabilityCalculator.WriteObservation();
            }

            else if (Global.Configuration.TestEstimationModelInApplicationMode)
            {
                Global.Configuration.IsInEstimationMode = false;

                RunModel(choiceProbabilityCalculator, personDay, householdDay, nCallsForTour, simulatedMandatoryTours);

                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(householdDay.Household.RandomUtility, personDay.Id, choice);

                Global.Configuration.IsInEstimationMode = true;
            }

            else
            {
                RunModel(choiceProbabilityCalculator, personDay, householdDay, nCallsForTour, simulatedMandatoryTours);
                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(personDay.Household.RandomUtility);
                choice = (int)chosenAlternative.Choice;
                if (choice == 1)
                {
                    personDay.UsualWorkplaceTours++;
                    personDay.WorkTours++;
                }
                else if (choice == 2)
                {
                    personDay.BusinessTours++;
                }
                else if (choice == 3)
                {
                    personDay.SchoolTours++;
                }
            }

            return(choice);
        }
Esempio n. 13
0
        public void Run(IPersonDayWrapper personDay, IHouseholdDayWrapper householdDay)
        {
            if (personDay == null)
            {
                throw new ArgumentNullException("personDay");
            }

            personDay.Person.ResetRandom(904);

            if (Global.Configuration.IsInEstimationMode)
            {
                if (personDay.WorkAtHomeDuration >= 120 && personDay.Person.IsFullOrPartTimeWorker)
                {
                    personDay.WorksAtHomeFlag = 1;
                }
                else
                {
                    personDay.WorksAtHomeFlag = 0;
                }
                if (!_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode || !personDay.Person.IsFullOrPartTimeWorker)
                {
                    return;
                }
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator(personDay.Person.Id * 10 + personDay.Day);

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME)
                {
                    return;
                }

                RunModel(choiceProbabilityCalculator, personDay, householdDay, personDay.WorksAtHomeFlag);

                choiceProbabilityCalculator.WriteObservation();
            }
            else
            {
                int choice;

                if (!personDay.Person.IsFullOrPartTimeWorker)
                {
                    choice = 0;
                }
                else
                {
                    RunModel(choiceProbabilityCalculator, personDay, householdDay);

                    var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(personDay.Household.RandomUtility);
                    choice = (int)chosenAlternative.Choice;
                }
                personDay.WorksAtHomeFlag    = choice;
                personDay.WorkAtHomeDuration = choice * 120; //default predicted duration for output
            }
        }
Esempio n. 14
0
        public void Run(ITourWrapper tour, int sampleSize)
        {
            if (tour == null)
            {
                throw new ArgumentNullException("tour");
            }

            tour.PersonDay.ResetRandom(20 + tour.Sequence - 1);

            if (Global.Configuration.IsInEstimationMode)
            {
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME)
                {
                    return;
                }
                if (!TourDestinationUtilities.ShouldRunInEstimationModeForModel(tour))
                {
                    return;
                }
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator(tour.Id);

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                RunModel(choiceProbabilityCalculator, tour, sampleSize, tour.DestinationParcel);

                choiceProbabilityCalculator.WriteObservation();
            }
            else
            {
                RunModel(choiceProbabilityCalculator, tour, sampleSize);

                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(tour.Household.RandomUtility);

                if (chosenAlternative == null)
                {
                    Global.PrintFile.WriteNoAlternativesAvailableWarning(CHOICE_MODEL_NAME, "Run", tour.PersonDay.Id);
                    tour.PersonDay.IsValid = false;

                    return;
                }

                var choice = (ParcelWrapper)chosenAlternative.Choice;

                tour.DestinationParcelId    = choice.Id;
                tour.DestinationParcel      = choice;
                tour.DestinationZoneKey     = ChoiceModelFactory.ZoneKeys[choice.ZoneId];
                tour.DestinationAddressType = choice.Id == tour.Person.UsualWorkParcelId ? Global.Settings.AddressTypes.UsualWorkplace : Global.Settings.AddressTypes.Other;

                if (choice.Id == tour.Person.UsualWorkParcelId)
                {
                    tour.PersonDay.UsualWorkplaceTours++;
                }
            }
        }
Esempio n. 15
0
        public RandomUniform01(int randseed = 1)
        {
            lock (_randomUniform01ResetLock) {
#if DEBUG
                ParallelUtility.countLocks("_randomUniform01ResetLock");
#endif

                ResetUniform01(randseed);
            }
        }
Esempio n. 16
0
        public TModel Seek(int id)
        {
            lock (typeof(TModel)) {
#if DEBUG
                ParallelUtility.countLocks("typeof (TModel)");
#endif


                return(!_index.TryGetValue(id, out long location) ? null : Seek(location));
            }
        }
Esempio n. 17
0
        public void Add(ISavable obj)
        {
            lock (_queue) {
#if DEBUG
                ParallelUtility.countLocks("_queue");
#endif

                _queue.Enqueue(obj);

                Monitor.Pulse(_queue);
            }
        }
        public void Run(IPersonDayWrapper personDay)
        {
            if (personDay == null)
            {
                throw new ArgumentNullException("personDay");
            }

            personDay.ResetRandom(5);

            if (Global.Configuration.IsInEstimationMode)
            {
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME)
                {
                    return;
                }
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator(personDay.Id);

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                RunModel(choiceProbabilityCalculator, personDay, new DayPattern(personDay));

                choiceProbabilityCalculator.WriteObservation();
            }
            else
            {
                if (personDay.Person.UsualWorkParcelId != Global.Settings.OutOfRegionParcelId && personDay.Person.UsualSchoolParcelId != Global.Settings.OutOfRegionParcelId)
                {
                    RunModel(choiceProbabilityCalculator, personDay);

                    var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(personDay.Household.RandomUtility);
                    var choice            = (DayPattern)chosenAlternative.Choice;

                    personDay.WorkTours             = choice.WorkTours;
                    personDay.SchoolTours           = choice.SchoolTours;
                    personDay.EscortTours           = choice.EscortTours;
                    personDay.PersonalBusinessTours = choice.PersonalBusinessTours;
                    personDay.ShoppingTours         = choice.ShoppingTours;
                    personDay.MealTours             = choice.MealTours;
                    personDay.SocialTours           = choice.SocialTours;

                    personDay.WorkStops             = choice.WorkStops;
                    personDay.SchoolStops           = choice.SchoolStops;
                    personDay.EscortStops           = choice.EscortStops;
                    personDay.PersonalBusinessStops = choice.PersonalBusinessStops;
                    personDay.ShoppingStops         = choice.ShoppingStops;
                    personDay.MealStops             = choice.MealStops;
                    personDay.SocialStops           = choice.SocialStops;
                }
            }
        }
Esempio n. 19
0
        public IList <TModel> Seek(int parentId, string indexName)
        {
            lock (typeof(TModel)) {
#if DEBUG
                ParallelUtility.countLocks("typeof (TModel)");
#endif

                Dictionary <int, int[]> index = _indexes[indexName];


                return(index.TryGetValue(parentId, out int[] elements) ? elements.Select(Seek).ToList() : new List <TModel>());
            }
        }
Esempio n. 20
0
        /// <summary>
        /// Refreshes the cache for the given set of files if expired
        /// </summary>
        public async Task RefreshCacheAsync(IEnumerable <CacheFileMetadata> librariesCacheMetadata, ILogger logger, CancellationToken cancellationToken)
        {
            await ParallelUtility.ForEachAsync(DownloadFileIfNecessaryAsync, MaxConcurrentDownloads, librariesCacheMetadata, cancellationToken).ConfigureAwait(false);

            async Task DownloadFileIfNecessaryAsync(CacheFileMetadata metadata)
            {
                if (!File.Exists(metadata.DestinationPath))
                {
                    logger.Log(string.Format(Resources.Text.DownloadingFile, metadata.Source), LogLevel.Operation);
                    await DownloadToFileAsync(metadata.Source, metadata.DestinationPath, attempts : 5, cancellationToken : cancellationToken).ConfigureAwait(false);
                }
            }
        }
Esempio n. 21
0
File: ap.cs Progetto: sfcta/DaySim
        public static int randominteger(int N)
        {
            int r = 0;

            lock (rndobject) {
#if DEBUG
                ParallelUtility.countLocks("rndobject");
#endif

                r = rndobject.Next(N);
            }
            return(r);
        }
Esempio n. 22
0
File: ap.cs Progetto: sfcta/DaySim
        public static double randomreal()
        {
            double r = 0;

            lock (rndobject) {
#if DEBUG
                ParallelUtility.countLocks("rndobject");
#endif

                r = rndobject.NextDouble();
            }
            return(r);
        }
Esempio n. 23
0
        public void Run(IHouseholdDayWrapper householdDay, ITripWrapper trip)
        {
            if (trip == null)
            {
                throw new ArgumentNullException("trip");
            }

            trip.PersonDay.ResetRandom(40 * (2 * trip.Tour.Sequence - 1 + trip.Direction - 1) + 50 + trip.Sequence - 1);

            if (Global.Configuration.IsInEstimationMode)
            {
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME)
                {
                    return;
                }
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator(trip.Id);

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                if (trip.DestinationParcel == null || trip.OriginParcel == null || trip.Mode <= Global.Settings.Modes.None || trip.Mode == Global.Settings.Modes.Other)
                {
                    return;
                }

                RunModel(choiceProbabilityCalculator, householdDay, trip, new HTripTime(trip.DepartureTime));

                choiceProbabilityCalculator.WriteObservation();
            }
            else
            {
                RunModel(choiceProbabilityCalculator, householdDay, trip);

                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(trip.Household.RandomUtility);

                if (chosenAlternative == null)
                {
                    Global.PrintFile.WriteNoAlternativesAvailableWarning(CHOICE_MODEL_NAME, "Run", trip.PersonDay.Id);
                    if (!Global.Configuration.IsInEstimationMode)
                    {
                        trip.PersonDay.IsValid = false;
                    }
                    return;
                }

                var choice = (HTripTime)chosenAlternative.Choice;

                trip.DepartureTime = choice.GetRandomFeasibleMinute(trip, choice);
            }
        }
        public int Run(HouseholdDayWrapper householdDay, int nCallsForTour, int choice)
        {
            if (householdDay == null)
            {
                throw new ArgumentNullException("householdDay");
            }

            householdDay.ResetRandom(935 + nCallsForTour);             // TODO:  fix the ResetRandom call parameter

            if (Global.Configuration.IsInEstimationMode)
            {
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME)
                {
                    return(choice);
                }
            }

            var choiceProbabilityCalculator =
                _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator(((householdDay.Household.Id * 10 + householdDay.Day) * 397) ^ nCallsForTour);

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                RunModel(choiceProbabilityCalculator, householdDay, nCallsForTour, choice);

                choiceProbabilityCalculator.WriteObservation();
            }

            else if (Global.Configuration.TestEstimationModelInApplicationMode)
            {
                Global.Configuration.IsInEstimationMode = false;

                //choice = Math.Min(personDay.BusinessStops, 1) + 2 * Math.Min(personDay.SchoolStops, 1);

                RunModel(choiceProbabilityCalculator, householdDay, nCallsForTour);

                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(householdDay.Household.RandomUtility, householdDay.Household.Id, choice);

                Global.Configuration.IsInEstimationMode = true;
            }

            else
            {
                RunModel(choiceProbabilityCalculator, householdDay, nCallsForTour);

                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(householdDay.Household.RandomUtility);
                choice = (int)chosenAlternative.Choice;
            }

            return(choice);
        }
Esempio n. 25
0
        public int Run(TourWrapper tour, HouseholdDayWrapper householdDay, int nCallsForTour, int choice)
        {
            if (tour == null)
            {
                throw new ArgumentNullException("tour");
            }

            tour.PersonDay.Person.ResetRandom(908 + (tour.Sequence - 1) * 3 + nCallsForTour);

            if (Global.Configuration.IsInEstimationMode)
            {
                if (!(choice == Global.Settings.Purposes.NoneOrHome))                    // simplifying choice.  TODO:  will need to deal with personal business subtours distinctly from home based in tour models
                {
                    choice = Global.Settings.Purposes.PersonalBusiness;
                }
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME)
                {
                    return(choice);
                }
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator((tour.Id * 397) ^ nCallsForTour);

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                if (tour.PersonDay.GetTotalStops() > 0)
                {
                    RunModel(choiceProbabilityCalculator, tour, householdDay, nCallsForTour, choice);

                    choiceProbabilityCalculator.WriteObservation();
                }
            }
            else
            {
                if (tour.PersonDay.GetTotalStops() > 0)
                {
                    RunModel(choiceProbabilityCalculator, tour, householdDay, nCallsForTour);

                    var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(tour.Household.RandomUtility);
                    choice = (int)chosenAlternative.Choice;
                }
                else
                {
                    choice = Global.Settings.Purposes.NoneOrHome;
                }
            }

            return(choice);
        }
Esempio n. 26
0
        private void FindPeriod(int departureTime)
        {
            foreach (var period in DayPeriod.HSmallDayPeriods.Where(period => departureTime.IsBetween(period.Start, period.End)))
            {
                DeparturePeriod = period;
            }

            foreach (
                var time in Times[ParallelUtility.GetBatchFromThreadId()].Where(time => time.DeparturePeriod == DeparturePeriod))
            {
                Index = time.Index;

                break;
            }
        }
Esempio n. 27
0
        public void Run(HouseholdWrapper household)
        {
            if (household == null)
            {
                throw new ArgumentNullException("household");
            }

            household.ResetRandom(4);

            if (Global.Configuration.IsInEstimationMode)
            {
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME)
                {
                    return;
                }
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator(household.Id);

            if (household.VehiclesAvailable > 4)
            {
                household.VehiclesAvailable = 4;
            }

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                RunModel(choiceProbabilityCalculator, household, household.VehiclesAvailable);

                choiceProbabilityCalculator.WriteObservation();
            }
            else if (Global.Configuration.TestEstimationModelInApplicationMode)
            {
                Global.Configuration.IsInEstimationMode = false;
                RunModel(choiceProbabilityCalculator, household);
                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(household.RandomUtility, household.Id, household.VehiclesAvailable);
                Global.Configuration.IsInEstimationMode = true;
            }
            else
            {
                RunModel(choiceProbabilityCalculator, household);

                var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(household.RandomUtility);
                var choice            = (int)chosenAlternative.Choice;

                household.VehiclesAvailable = choice;
            }
        }
Esempio n. 28
0
        public int Run(ITourWrapper tour, int nCallsForTour, int choice)
        {
            if (tour == null)
            {
                throw new ArgumentNullException("tour");
            }

            tour.PersonDay.ResetRandom(30 + tour.Sequence + nCallsForTour - 1);

            if (Global.Configuration.IsInEstimationMode)
            {
                if (Global.Configuration.EstimationModel != CHOICE_MODEL_NAME)
                {
                    return(choice);
                }
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator((tour.Id * 397) ^ nCallsForTour);

            if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode)
            {
                if (tour.PersonDay.GetTotalStops() > 0)
                {
                    RunModel(choiceProbabilityCalculator, tour, nCallsForTour, choice);

                    choiceProbabilityCalculator.WriteObservation();
                }
            }
            else
            {
                if (tour.PersonDay.GetTotalStops() > 0)
                {
                    RunModel(choiceProbabilityCalculator, tour, nCallsForTour);

                    var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(tour.Household.RandomUtility);
                    choice = (int)chosenAlternative.Choice;
                }
                else
                {
                    choice = Global.Settings.Purposes.NoneOrHome;
                }
            }

            return(choice);
        }
        public void Run(HouseholdDayWrapper householdDay)
        {
            if (householdDay == null)
            {
                throw new ArgumentNullException("householdDay");
            }

            householdDay.ResetRandom(962);

            if (Global.Configuration.IsInEstimationMode)
            {
                return;
            }

            var choiceProbabilityCalculator = _helpers[ParallelUtility.GetBatchFromThreadId()].GetChoiceProbabilityCalculator(householdDay.Household.Id);



            //if (_helpers[ParallelUtility.GetBatchFromThreadId()].ModelIsInEstimationMode) {

            //				// set choice variable here  (derive from available household properties)
            //				if (householdDay.SharedActivityHomeStays >= 1
            //					//&& householdDay.DurationMinutesSharedHomeStay >=60
            //					&& householdDay.AdultsInSharedHomeStay >= 1
            //					&& householdDay.NumberInLargestSharedHomeStay >= (householdDay.Household.Size)
            //                   )
            //				{
            //					householdDay.PrimaryPriorityTimeFlag = 1;
            //				}
            //				else    householdDay.PrimaryPriorityTimeFlag = 0;

            //RunModel(choiceProbabilityCalculator, householdDay, householdDay.PrimaryPriorityTimeFlag);

            //choiceProbabilityCalculator.WriteObservation();
            //}
            //else {
            RunModel(choiceProbabilityCalculator, householdDay, _pfptSchedule);

            var chosenAlternative = choiceProbabilityCalculator.SimulateChoice(householdDay.Household.RandomUtility);
            var choice            = (int[])chosenAlternative.Choice;

            householdDay.StartingMinuteSharedHomeStay  = choice[1];
            householdDay.DurationMinutesSharedHomeStay = choice[2];
            //}
        }
Esempio n. 30
0
        public double Uniform01()
        {
            lock (_randomUniform01Lock) {
#if DEBUG
                ParallelUtility.countLocks("_randomUniform01Lock");
#endif

                int r = _randseed / 177;
                int s = _randseed - 177 * r;

                _randseed = 171 * s - 2 * r;

                if (_randseed < 0)
                {
                    _randseed += 30269;
                }

                r = _randSy / 176;
                s = _randSy - 176 * r;

                _randSy = 172 * s - 35 * r;

                if (_randSy < 0)
                {
                    _randSy += 30307;
                }

                r = _randSz / 178;
                s = _randSz - 178 * r;

                _randSz = 170 * s - 63 * r;

                if (_randSz < 0)
                {
                    _randSz += 30323;
                }

                double f = _randseed / 30269D + _randSy / 30307D + _randSz / 30323D;

                f = f - (int)f;

                return(f);
            }
        }