Example #1
0
        public static int GetFrequency(ITashaPerson person, Activity activity, Random random, int maxFrequency,
            Time startTime, Time endTime, int householdPD, int workPD, GenerationAdjustment[] generationAdjustments)
        {
            bool feasibleFreq = false;
            int freq = 0;
            while (!feasibleFreq)
            {
                freq = Distribution.GetRandomFrequencyValue(
                    0, maxFrequency, random, Distribution.GetDistributionID(person, activity), householdPD, workPD, generationAdjustments);
                if (freq == 0)
                {
                    break;
                }
                Time duration;
                if (!Distribution.GetRandomStartTimeFrequency(
                Distribution.GetDistributionID(person, activity), freq,
                Distribution.TimeOfDayToDistribution(startTime), Distribution.TimeOfDayToDistribution(endTime), random, out duration))
                {
                    //a bad thing happens here
                }
                else if (duration != Time.Zero)
                {
                    feasibleFreq = true;
                }
            }

            return freq;
        }
Example #2
0
 public static int GetFrequency(ITashaPerson person, Activity activity, Random random, int maxFrequency, int householdPD, int workPD, GenerationAdjustment[] generationAdjustments)
 {
     int freq = 0;
     freq = Distribution.GetRandomFrequencyValue(
         0, maxFrequency, random, Distribution.GetDistributionID(person, activity), householdPD, workPD, generationAdjustments);
     return freq;
 }
Example #3
0
 public static int GetFrequency(ITashaPerson person, Activity activity, Random random, int householdPD, int workPD, GenerationAdjustment[] generationAdjustments)
 {
     int freq = 0;
     var distID = Distribution.GetDistributionID(person, activity);
     if (distID < 0)
     {
         throw new XTMFRuntimeException("We were unable to get the distribution ID number for a person doing a '" + activity.ToString()
             + "' episode The person's householdID was " + person.Household.HouseholdId + ", personID was " + person.Id + ", was " + person.Age +
             " years old, with employment status '" + person.EmploymentStatus.ToString() + "' occupation '" + person.Occupation.ToString() + "' Student Status '"
             + person.StudentStatus.ToString() + "'.  Their work zone is '" + (person.EmploymentZone != null ? person.EmploymentZone.ZoneNumber.ToString() : "None")
             + "' and their school zone is '"
             + (person.SchoolZone != null ? person.SchoolZone.ZoneNumber.ToString() : "None") + "'.");
     }
     freq = Distribution.GetRandomFrequencyValue(
         0, Distribution.NumberOfFrequencies - 1, random, distID, householdPD, workPD, generationAdjustments);
     return freq;
 }
Example #4
0
 private static float GetGenerationAdjustment(GenerationAdjustment[] generationAdjustments, int distributionID, int householdPD, int workPD)
 {
     for (int i = 0; i < generationAdjustments.Length; i++)
     {
         if (generationAdjustments[i].DistributionIDs.Contains(distributionID))
         {
             if (generationAdjustments[i].PlanningDistricts.Contains(householdPD))
             {
                 if (generationAdjustments[i].WorkPlanningDistrict.Contains(workPD))
                 {
                     return generationAdjustments[i].Factor;
                 }
             }
         }
     }
     return 1.0f;
 }
Example #5
0
 /// <summary>
 /// Do a frequency random assignment
 /// </summary>
 /// <param name="min">The smallest possible value (>= 0)</param>
 /// <param name="max">The largest possible value (<= 10)</param>
 /// <param name="distributionID">Which distro to look for</param>
 /// <returns>[min,max] the value selected</returns>
 internal static int GetRandomFrequencyValue(int min, int max, Random random, int distributionID, int householdPD, int workPD,
     GenerationAdjustment[] generationAdjustments)
 {
     if (min == max) return min;
     float rand = (float)random.NextDouble();
     float cdf = 0;
     float total = 0;
     DistributionInformation pdf = Distributions[distributionID];
     float adjustment = GetGenerationAdjustment(generationAdjustments, distributionID, householdPD, workPD);
     if (pdf == null || pdf.Frequency == null)
     {
         throw new XTMFRuntimeException("Unable to load PDF #" + distributionID + " from the Distribution Frequency File!");
     }
     // to start with just add
     for (int i = min; i <= max; i++)
     {
         total += i == 0 ? pdf.Frequency[i] : pdf.Frequency[i] * adjustment;
     }
     rand = rand * total;
     for (int i = min; i <= max; i++)
     {
         // we can just multiply now, faster than division
         cdf += i == 0 ? pdf.Frequency[i] : pdf.Frequency[i] * adjustment;
         if (rand < cdf)
         {
             return i;
         }
     }
     return 0;
 }
Example #6
0
 internal static bool GenerateWorkAtHomesActivity(ITashaPerson person, Random random, int householdPD, GenerationAdjustment[] generationAdjustments)
 {
     return Distribution.GetRandomFrequencyValue(0, 1, random,
     Distribution.GetDistributionID(person, Activity.WorkAtHomeBusiness), householdPD, householdPD, generationAdjustments)
         > 0;
 }
Example #7
0
        private static void ProcessWorkBusiness(ITashaPerson person, Schedule workSchedule, Random random, Episode primWorkEpisode,
            int householdPD, int workPD, GenerationAdjustment[] generationAdjustments)
        {
            Time startTimeB;
            Time durationB;
            Time startTime = primWorkEpisode.StartTime;
            Time endTime = primWorkEpisode.EndTime;

            int freq = TimeTable.GetFrequency(person, Activity.WorkBasedBusiness, random, Scheduler.MaxFrequency, startTime, endTime,
                householdPD, workPD, generationAdjustments);
            for (int i = 0; i < freq; i++)
            {
                var attempt = 0;
                while (attempt < Scheduler.EpisodeSchedulingAttempts)
                {
                    attempt++;
                    if (!TimeTable.GetStartTime(person, Activity.WorkBasedBusiness, freq,
                        startTime, endTime, random, out startTimeB))
                    {
                        continue;
                    }

                    if (!TimeTable.GetDuration(person, Activity.WorkBasedBusiness, startTimeB, endTime - startTimeB, random, out durationB))
                    {
                        continue;
                    }

                    Episode businessEpisode;
                    businessEpisode = new ActivityEpisode(0,
                        new TimeWindow(startTimeB, startTimeB + durationB), Activity.WorkBasedBusiness, person);
                    if (workSchedule.Insert(businessEpisode, random))
                    {
                        break;
                    }
                }
            }
        }
Example #8
0
        private static bool GenerateIndividualMarketEpisodes(this ITashaHousehold household, Random random, GenerationAdjustment[] generationAdjustments)
        {
            int householdPD = household.HomeZone.PlanningDistrict;
            foreach (var person in household.Persons)
            {
                if (!person.Child)
                {
                    var empZone = person.EmploymentZone;
                    int workPD = empZone == null ? 0 : empZone.PlanningDistrict;
                    int freq_I = TimeTable.GetFrequency(person, Activity.Market, random, householdPD, workPD, generationAdjustments);
                    int outerAttempts = 0;
                    for (int j = 0; j < freq_I; ++j)
                    {
                        //Update the trip generation count

                        Time startTime;
                        Time duration;
                        bool success = false;
                        for (int attempt = 0; attempt < Scheduler.EpisodeSchedulingAttempts && !success; attempt++)
                        {
                            if (!TimeTable.GetStartTime(person, Activity.Market, random, out startTime))
                            {
                                continue;
                            }

                            if (!TimeTable.GetDuration(person, Activity.Market, startTime, random, out duration))
                            {
                                continue;
                            }

                            var endTime = startTime + duration;
                            if (endTime > Time.EndOfDay + TashaRuntime.EndOfDay + Time.OneQuantum)
                            {
                                success = false;
                                continue;
                            }

                            //instantiate a temporary individual market episode on the heap space and store pointer in p_marketEpisode

                            var window = new TimeWindow(startTime, startTime + duration);
                            Episode marketEpisode = new ActivityEpisode(0, window, Activity.Market, person);
                            Project workProject = person.GetWorkProject();
                            Schedule workProjSchedule = workProject.Schedule;
                            Project schoolProject = person.GetSchoolProject();
                            Schedule schoolProjSchedule = schoolProject.Schedule;

                            Time overlap = workProjSchedule.CheckOverlap(marketEpisode) + schoolProjSchedule.CheckOverlap(marketEpisode);

                            float percentOverlap = overlap / duration;

                            if (percentOverlap < Scheduler.PercentOverlapAllowed || attempt == Scheduler.EpisodeSchedulingAttempts - 1)
                            {
                                Project marketProject = person.GetMarketProject();
                                Schedule marketSchedule = marketProject.Schedule;

                                if (marketSchedule.Insert(marketEpisode, random))
                                {
                                    //inserted ok
                                    success = true;
                                }
                                else
                                {
                                    success = false;
                                    //didn't work
                                }
                            }
                            else // i.e. too much overlap with the work and school projects
                            {
                                // attempt will be auto incremented so we don't need to worry about this
                            }
                        }
                        if ((outerAttempts++) < Scheduler.EpisodeSchedulingAttempts && !success)
                        {
                            j = -1;
                            Project marketProject = person.GetMarketProject();
                            Schedule marketSchedule = marketProject.Schedule;
                            marketSchedule.Clear();
                        }
                    }
                }
            }
            return true;
        }
Example #9
0
        private static bool GenerateWorkEpisodes(this ITashaHousehold household, Random random, GenerationAdjustment[] generationAdjustments)
        {
            var householdPD = household.HomeZone.PlanningDistrict;
            foreach (ITashaPerson person in household.Persons)
            {
                // people need to be older than "11" to be allowed to work
                if (person.Age < Scheduler.MinimumWorkingAge) continue;
                // Check to see if they are in a regular job type case
                Project workProject = person.GetWorkProject();
                Schedule workSchedule = workProject.Schedule;
                if ((person.Occupation == Occupation.NotEmployed) | (person.Occupation == Occupation.Unknown))
                {
                    continue;
                }
                if (((person.EmploymentStatus == TTSEmploymentStatus.FullTime) | (person.EmploymentStatus == TTSEmploymentStatus.PartTime)))
                {
                    var empZone = person.EmploymentZone;
                    int workPD = empZone == null ? 0 : empZone.PlanningDistrict;
                    if (person.EmploymentZone == null)
                    {
                        //continue;
                        throw new XTMFRuntimeException("There was a person whom had no employment zone however was a full-time/part-time worker!");
                    }
                    //Employment zone doesn't exist so generate our own based on distributions
                    if (person.EmploymentZone.ZoneNumber == TashaRuntime.ZoneSystem.RoamingZoneNumber)
                    {
                        GenerateWorkBusinessEpisode(person, workSchedule, random, householdPD, workPD, generationAdjustments);
                        continue;
                    }

                    int freq = TimeTable.GetFrequency(person, Activity.PrimaryWork, random, householdPD, workPD, generationAdjustments);
                    if (freq <= 0)
                    {
                        continue;
                    }
                    Time startTime = Time.Zero;
                    Time duration = Time.Zero;
                    bool success = false;
                    for (int i = 0; i < freq; i++)
                    {
                        for (int attempts = 0; attempts < Scheduler.EpisodeSchedulingAttempts; attempts++)
                        {
                            // If we use an and here the compiler can't prove that we assign to duration
                            if (!TimeTable.GetStartTime(person, Activity.PrimaryWork, freq, random, out startTime))
                            {
                                continue;
                            }

                            if (TimeTable.GetDuration(person, Activity.PrimaryWork, startTime, random, out duration))
                            {
                                // if we got the duration, success lets continue on
                                success = true;
                                break;
                            }
                        }
                        // if we were unable to get a duration
                        if (!success)
                        {
                            // try the next person
                            continue;
                        }
                        Time endTime = startTime + duration;

                        SchedulerHousehold.CheckAndUpdateLatestWorkingTime(person.Household,
                            endTime);

                        Episode primWorkEpisode;

                        primWorkEpisode = new ActivityEpisode(0, new TimeWindow(startTime, endTime),
                                    Activity.PrimaryWork, person);
                        primWorkEpisode.Zone = person.EmploymentZone;
                        if (!workSchedule.Insert(primWorkEpisode, random) && i == 0)
                        {
                            throw new XTMFRuntimeException("Failed to insert the primary work episode into the work project!");
                        }
                        //set up work business activities
                        ProcessWorkBusiness(person, workSchedule, random, primWorkEpisode, householdPD, workPD, generationAdjustments);
                        //set up secondary work activities
                        ProcessSecondaryWork(person, workSchedule, random, primWorkEpisode, householdPD, workPD, generationAdjustments);
                        //set up return home from work activities
                        ProcessReturnHomeFromWork(person, workSchedule, random, primWorkEpisode, householdPD, workPD, generationAdjustments);
                    }
                }
                // Check to see if they work from home
                else if (person.Age >= 19 &&
                    ((person.EmploymentStatus == TTSEmploymentStatus.WorkAtHome_FullTime)
                    | (person.EmploymentStatus == TTSEmploymentStatus.WorkAtHome_PartTime)))
                {
                    ProcessWorkAtHome(person, workSchedule, random, householdPD, householdPD, generationAdjustments);
                }
                // If they don't work, just continue on
            }
            return true;
        }
Example #10
0
 private static void GenerateWorkBusinessEpisode(ITashaPerson person, Schedule workSchedule, Random random, int householdPD,
     int workPD, GenerationAdjustment[] generationAdjustments)
 {
     int freq_B = TimeTable.GetFrequency(person, Activity.WorkBasedBusiness, random, householdPD, workPD, generationAdjustments);
     for (int i = 0; i < freq_B; i++)
     {
         Time startTime;
         Time duration;
         for (int attempt = 0; attempt < Scheduler.EpisodeSchedulingAttempts; attempt++)
         {
             if (!TimeTable.GetStartTime(person, Activity.WorkBasedBusiness, random, out startTime))
             {
                 continue;
             }
             if (!TimeTable.GetDuration(person, Activity.WorkBasedBusiness, startTime, random, out duration))
             {
                 continue;
             }
             var endTime = startTime + duration;
             if (endTime > Time.EndOfDay + TashaRuntime.EndOfDay + Time.OneQuantum)
             {
                 continue;
             }
             Episode workEpisode = new ActivityEpisode(System.Threading.Interlocked.Increment(ref Episode.GeneratedEpisodes), new TimeWindow(startTime, startTime + duration),
                 Activity.WorkBasedBusiness, person);
             workSchedule.Insert(workEpisode, random);
             break;
         }
     }
 }
Example #11
0
        private static bool GenerateSchoolEpisodes(this ITashaHousehold household, Random random, GenerationAdjustment[] generationAdjustments)
        {
            var householdPD = household.HomeZone.PlanningDistrict;
            foreach (ITashaPerson person in household.Persons)
            {
                if (person.StudentStatus == StudentStatus.FullTime ||
                    person.StudentStatus == StudentStatus.PartTime)
                {
                    if (person.Age >= 11)
                    {
                        var empZone = person.EmploymentZone;
                        int workPD = empZone == null ? 0 : empZone.PlanningDistrict;
                        var freq = TimeTable.GetFrequency(person, Activity.School, random, householdPD, workPD, generationAdjustments);
                        //if there is a school activity generated
                        for (int i = 0; i < freq; i++)
                        {
                            bool success = false;
                            short attempt = 0;
                            Time duration = new Time(), startTime = new Time();
                            int maxAttempts = Scheduler.EpisodeSchedulingAttempts;
                            while (!success && (attempt < maxAttempts))
                            {
                                attempt++;
                                //get start time end time and duration
                                if (!TimeTable.GetStartTime(person, Activity.School, random, out startTime))
                                {
                                    continue;
                                }
                                if (!TimeTable.GetDuration(person, Activity.School, startTime, random, out duration))
                                {
                                    continue;
                                }
                                if (duration != Time.Zero) // no valid duration
                                {
                                    var endTime = startTime + duration;
                                    if (endTime > Time.EndOfDay + TashaRuntime.EndOfDay + Time.OneQuantum)
                                    {
                                        success = false;
                                        continue;
                                    }
                                    //instantiate temporary school episode;
                                    Schedule schoolSchedule;
                                    Episode schoolEpisode;

                                    schoolEpisode =
                                       new ActivityEpisode(0, new TimeWindow(startTime, startTime + duration),
                                           Activity.School, person);
                                    schoolEpisode.Zone = person.SchoolZone != null ? person.SchoolZone : person.Household.HomeZone;
                                    Project schoolProject = person.GetSchoolProject();
                                    schoolSchedule = schoolProject.Schedule;
                                    success = schoolSchedule.Insert(schoolEpisode, random);
                                }
                            }
                        }
                    }
                    else if (person.Age >= 6)
                    {
                        //this child is in kindergarten
                        //generate random number between 0 and 1
                        Time startTime = new Time(), endTime = new Time();
                        startTime = Scheduler.SchoolMorningStart;
                        endTime = Scheduler.SchoolAfternoonEnd;
                        Schedule schoolSchedule;
                        Episode schoolEpisode;

                        schoolEpisode =
                           new ActivityEpisode(0, new TimeWindow(startTime, endTime),
                               Activity.School, person);
                        schoolEpisode.Zone = person.SchoolZone != null ? person.SchoolZone : person.Household.HomeZone;
                        Project schoolProject = person.GetSchoolProject();
                        schoolSchedule = schoolProject.Schedule;
                        schoolSchedule.Insert(schoolEpisode, random);
                    }
                    else if (person.Age == 5)
                    {
                        //this child is in kindergarten
                        //generate random number between 0 and 1
                        int randNum = random.Next(0, 1);
                        Time duration = new Time(), startTime = new Time(), endTime = new Time();
                        if (randNum <= 0.5) //morning shift
                        {
                            //morning shift
                            startTime = Scheduler.SchoolMorningStart;
                            endTime = Scheduler.SchoolMorningEnd;
                            duration = endTime - startTime;
                        }
                        else
                        {
                            //afternoon shift
                            startTime = Scheduler.SchoolAfternoonStart;
                            endTime = Scheduler.SchoolAfternoonEnd;
                        }
                        Schedule schoolSchedule;
                        Episode schoolEpisode;

                        schoolEpisode =
                           new ActivityEpisode(0, new TimeWindow(startTime, endTime),
                               Activity.School, person);
                        schoolEpisode.Zone = person.SchoolZone != null ? person.SchoolZone : person.Household.HomeZone;
                        Project schoolProject = person.GetSchoolProject();
                        schoolSchedule = schoolProject.Schedule;
                        schoolSchedule.Insert(schoolEpisode, random);
                    }
                }
            }
            return true;
        }
Example #12
0
        private static bool GenerateJointOtherEpisodes(this ITashaHousehold household, Random random, GenerationAdjustment[] generationAdjustments)
        {
            var householdPD = household.HomeZone.PlanningDistrict;
            //make sure there at least 2 people and one adult
            if ((household.Persons.Length >= 2) & (household.NumberOfAdults > 0))
            {
                int freqJ = TimeTable.GetFrequency(household, Activity.JointOther, random, householdPD, 0, generationAdjustments);

                Time duration, startTime;
                int numEpisodeAdults = Distribution.GetNumAdultsJointEpisode(household, random,
                    Activity.JointOther);

                for (int i = 0; i < freqJ; i++)
                {
                    bool success = false;
                    int attempt = 0;
                    while (!success && attempt < Scheduler.EpisodeSchedulingAttempts)
                    {
                        if (!TimeTable.GetStartTime(household, Activity.JointOther, freqJ, random, out startTime))
                        {
                            success = false;
                            attempt++;
                            continue;
                        }
                        if (!TimeTable.GetDuration(household, Activity.JointOther, startTime, random, out duration))
                        {
                            success = false;
                            attempt++;
                            continue;
                        }

                        if (duration == Time.Zero || startTime == Time.Zero)
                        {
                            success = false;
                            attempt++;
                        }
                        else
                        {
                            Time endTime = startTime + duration;
                            List<ITashaPerson> availableAdults = new List<ITashaPerson>();
                            foreach (ITashaPerson person in household.Persons)
                            {
                                Time workSchoolStartTime = SchedulerPerson.GetWorkSchoolStartTime(person);
                                Time workSchoolEndTime = SchedulerPerson.GetWorkSchoolEndTime(person);
                                bool available = false;
                                if (workSchoolStartTime > endTime ||
                                    workSchoolEndTime < startTime ||
                                    workSchoolStartTime == Time.Zero)
                                    available = true;
                                if (person.Age >= 16 && available)
                                {
                                    availableAdults.Add(person);
                                }
                            }

                            if (availableAdults.Count >= numEpisodeAdults && availableAdults.Count > 0)
                            {
                                Episode jointOtherEpisode;
                                var owner = availableAdults[0];
                                jointOtherEpisode = new ActivityEpisode(0,
                                    new TimeWindow(startTime, endTime), Activity.JointOther, owner);

                                for (int j = 0; j < numEpisodeAdults; j++)
                                {
                                    jointOtherEpisode.AddPerson(availableAdults[j]);
                                }

                                Project jointOtherProject = household.GetJointOtherProject();
                                Schedule jointOtherSchedule = jointOtherProject.Schedule;
                                bool inserted = jointOtherSchedule.Insert(jointOtherEpisode, random);
                                success = true;

                                if (!inserted)
                                {
                                    success = false;
                                    attempt++;
                                }
                            }
                            else
                            {
                                success = false;
                                attempt++;
                            }
                        }
                    }
                }
            }

            return true;
        }
Example #13
0
        private static bool GenerateJointMarketEpisodes(this ITashaHousehold household, Random random, GenerationAdjustment[] generationAdjustment)
        {
            var householdPD = household.HomeZone.PlanningDistrict;
            // initialize available adults
            var availableAdults = new List<ITashaPerson>(household.Persons.Length);
            // We can only do this with households with more than one person
            if (household.Persons.Length >= 2 && household.NumberOfAdults > 0)
            {
                // Figure out how many times this home is going to go on a joint market trip
                int howManyTimes = TimeTable.GetFrequency(household, Activity.JointMarket, random, householdPD, 0, generationAdjustment);
                // Processes each of those trips
                for (int i = 0; i < howManyTimes; i++)
                {
                    Time startTime, duration;
                    int numEpisodeAdults = Distribution.GetNumAdultsJointEpisode(household, random,
                                            Activity.JointMarket);
                    bool success = false;
                    // continue to try until either we get it to work or we fail to schedule this episode
                    for (int attempt = 0; !success && attempt < Scheduler.EpisodeSchedulingAttempts; attempt++)
                    {
                        if (!TimeTable.GetStartTime(household, Activity.JointMarket, random, out startTime))
                        {
                            continue;
                        }
                        if (!TimeTable.GetDuration(household, Activity.JointMarket, startTime, random, out duration))
                        {
                            continue;
                        }
                        // Now that we have our start time and duration, compute our end time
                        Time endTime = startTime + duration;
                        if (availableAdults.Count > 0) availableAdults.Clear();
                        Time workSchoolStartTime, workSchoolEndTime;
                        bool available = false;
                        foreach (var person in household.Persons)
                        {
                            workSchoolStartTime = SchedulerPerson.GetWorkSchoolStartTime(person);
                            workSchoolEndTime = SchedulerPerson.GetWorkSchoolEndTime(person);

                            // this person is available if
                            available = (workSchoolStartTime > endTime) | (workSchoolEndTime < startTime) | (workSchoolStartTime == Time.Zero);

                            if (person.Age >= 16 && available)
                            {
                                availableAdults.Add(person);
                            }
                        }

                        if ((availableAdults.Count > 0) & (availableAdults.Count >= numEpisodeAdults))
                        {
                            Episode jointMarketEpisode;
                            jointMarketEpisode = new ActivityEpisode(0,
                                new TimeWindow(startTime, endTime), Activity.JointMarket,
                                availableAdults[0]);

                            foreach (ITashaPerson adult in availableAdults)
                            {
                                jointMarketEpisode.AddPerson(adult);
                            }

                            Project jointMarketProject = household.GetJointMarketProject();
                            Schedule jointMarketSchedule = jointMarketProject.Schedule;
                            bool insert = jointMarketSchedule.Insert(jointMarketEpisode, random);

                            success = insert;
                        }
                    }
                }
            }
            return true;
        }
Example #14
0
        private static bool GenerateIndividualOtherEpisodes(this ITashaHousehold household, Random random, GenerationAdjustment[] generationAdjustments)
        {
            int householdPD = household.HomeZone.PlanningDistrict;
            foreach (var person in household.Persons)
            {
                if (!person.Child)
                {
                    var empZone = person.EmploymentZone;
                    int workPD = empZone == null ? 0 : empZone.PlanningDistrict;
                    int freqO = TimeTable.GetFrequency(person, Activity.IndividualOther, random, householdPD, workPD, generationAdjustments);
                    int outerAttempts = 0;

                    Time durationO, startTimeO = Time.Zero;

                    for (int i = 0; i < freqO; i++)
                    {
                        bool success = false;

                        for (int attempt = 0; !success && (attempt < Scheduler.EpisodeSchedulingAttempts); attempt++)
                        {
                            if (!TimeTable.GetStartTime(person, Activity.IndividualOther, freqO, random, out startTimeO))
                            {
                                success = false;
                                continue;
                            }
                            if (!TimeTable.GetDuration(person, Activity.IndividualOther, startTimeO, random, out durationO))
                            {
                                success = false;
                                continue;
                            }

                            var endTime = startTimeO + durationO;
                            if (endTime > Time.EndOfDay + TashaRuntime.EndOfDay + Time.OneQuantum)
                            {
                                success = false;
                                continue;
                            }

                            Episode otherEpisode;
                            otherEpisode = new ActivityEpisode(0,
                                new TimeWindow(startTimeO, endTime),
                                Activity.IndividualOther, person);
                            Project workProject = person.GetWorkProject();
                            Schedule workProjSchedule = workProject.Schedule;
                            Project schoolProject = person.GetSchoolProject();
                            Schedule schoolProjSchedule = schoolProject.Schedule;

                            Time overlap = workProjSchedule.CheckOverlap(otherEpisode) + schoolProjSchedule.CheckOverlap(otherEpisode);

                            float percentOverlap = overlap / durationO;

                            if (percentOverlap < Scheduler.PercentOverlapAllowed || attempt == Scheduler.EpisodeSchedulingAttempts - 1)
                            {
                                Project otherProject = person.GetOtherProject();
                                Schedule otherSchedule = otherProject.Schedule;

                                if (otherSchedule.Insert(otherEpisode, random))
                                {
                                    //inserted ok
                                    success = true;
                                }
                            }
                        }
                        if ((outerAttempts++) < Scheduler.EpisodeSchedulingAttempts && !success)
                        {
                            i = -1;
                            Project otherProject = person.GetOtherProject();
                            Schedule otherSchedule = otherProject.Schedule;
                            otherSchedule.Clear();
                        }
                    }
                }
            }
            return true;
        }
Example #15
0
 internal static bool GenerateIndividualOtherActivity(ITashaPerson person, Random random, int householdPD, int workPD, GenerationAdjustment[] generationAdjustments)
 {
     return Distribution.GetRandomFrequencyValue(0, 1, random,
       Distribution.GetDistributionID(person, Activity.IndividualOther), householdPD, workPD, generationAdjustments) > 0;
 }
Example #16
0
 internal static bool GenerateSecondaryWorkTrip(ITashaPerson person, Random random, int householdPD, int workPD, GenerationAdjustment[] generationAdjustments)
 {
     return Distribution.GetRandomFrequencyValue(0, 1, random,
         Distribution.GetDistributionID(person, Activity.SecondaryWork), householdPD, workPD, generationAdjustments)
         > 0;
 }
Example #17
0
        /// <summary>
        /// Return home from work creates a return home from work activity (return home from work may involve
        /// going to lunch at home or going to check up on something in the house during work).
        /// </summary>
        /// <param name="person"></param>
        /// <param name="schedule"></param>
        /// <param name="episode"></param>
        /// <returns></returns>
        private static bool ProcessReturnHomeFromWork(ITashaPerson person, Schedule schedule, Random random, Episode episode, int householdPD, int workPD, GenerationAdjustment[] generationAdjustments)
        {
            int freq = 0;

            //the current work schedule doesn't allow for a return from work activity
            if (episode.StartTime > Scheduler.MaxPrimeWorkStartTimeForReturnHomeFromWork
                || episode.Duration < Scheduler.MinPrimaryWorkDurationForReturnHomeFromWork)
            {
                return false;
            }

            //End time of work to home activity
            Time endTime = episode.EndTime + new Time(0.3f) < Scheduler.ReturnHomeFromWorkMaxEndTime ?
                episode.EndTime + new Time(0.3f) : Scheduler.ReturnHomeFromWorkMaxEndTime;

            freq = TimeTable.GetFrequency(person, Activity.ReturnFromWork, random, 1, episode.StartTime + new Time(0.3f), endTime,
                householdPD, workPD, generationAdjustments);

            if (freq == 1)
            {
                IZone homeZone = person.Household.HomeZone;

                Time HalfAnHour = new Time() { Minutes = 30 };

                Time MaxEndTime = ((episode.EndTime - HalfAnHour) < Scheduler.ReturnHomeFromWorkMaxEndTime) ? (episode.EndTime - HalfAnHour) : Scheduler.ReturnHomeFromWorkMaxEndTime;

                Time startTime;
                if (!TimeTable.GetStartTime(person, Activity.ReturnFromWork
                     , freq
                     , episode.StartTime + HalfAnHour
                     , MaxEndTime, random, out startTime))
                {
                    return false;
                }

                Time maxDuration = new Time(Math.Min((Scheduler.ReturnHomeFromWorkMaxEndTime - Time.OneHour).ToFloat(),
                    (episode.EndTime - HalfAnHour - startTime).ToFloat()));

                Time duration;
                if (!TimeTable.GetDuration(person, Activity.ReturnFromWork, startTime, maxDuration, random, out duration))
                {
                    // reject
                    return false;
                }

                Episode returnFromWorkEpisode;
                returnFromWorkEpisode = new ActivityEpisode(0,
                    new TimeWindow(startTime, startTime + duration), Activity.ReturnFromWork,
                    person);
                returnFromWorkEpisode.Zone = homeZone;
                schedule.Insert(returnFromWorkEpisode, random);
            }
            return true;
        }
Example #18
0
        /// <summary>
        /// Secondary work creates a secondary work activity for person
        /// </summary>
        /// <param name="person"></param>
        /// <param name="schedule"></param>
        /// <param name="primaryWorkEpisode"></param>
        private static void ProcessSecondaryWork(ITashaPerson person, Schedule schedule, Random random, Episode primaryWorkEpisode, int householdPD,
            int workPD, GenerationAdjustment[] generationAdjustments)
        {
            //can only work if finish primary work by 7:00PM
            if (primaryWorkEpisode.EndTime < Scheduler.SecondaryWorkThreshold)
            {
                int freq_R = 0;

                //getting earliest possible startTime
                Time HourAfterWork = primaryWorkEpisode.EndTime + Time.OneHour;
                Time MinStartTime = HourAfterWork > Scheduler.SecondaryWorkMinStartTime ? HourAfterWork : Scheduler.SecondaryWorkMinStartTime;

                freq_R = TimeTable.GetFrequency(person, Activity.SecondaryWork, random, 10, MinStartTime, Time.EndOfDay,
                    householdPD, workPD, generationAdjustments);

                for (int i = 0; i < freq_R; i++)
                {
                    //zone same as work zone
                    IZone zone = primaryWorkEpisode.Zone.ZoneNumber == Scheduler.Tasha.ZoneSystem.RoamingZoneNumber
                        ? Scheduler.LocationChoiceModel.GetLocationHomeBased(Activity.SecondaryWork, person.Household.HomeZone, random)
                        : primaryWorkEpisode.Zone;

                    //getting start time and duration of secondary work
                    Time startTimeR;

                    Time durationR;

                    if (!TimeTable.GetStartTime(person, primaryWorkEpisode.ActivityType, freq_R, MinStartTime, Time.EndOfDay, random, out startTimeR))
                    {
                        //TODO: We might want to reconsider this, skipping instead of just throwing an exception
                        //throw new XTMFRuntimeException("Unable to find a start time for a primary work episode");
                        return;
                    }

                    if (!TimeTable.GetDuration(person, Activity.SecondaryWork, startTimeR, Time.EndOfDay - startTimeR, random, out durationR))
                    {
                        //throw new XTMFRuntimeException("Unable to find a duration for a primary work episode");
                        return;
                    }

                    //inserting secondary work into schedule
                    Episode secondaryWorkEpisode;
                    secondaryWorkEpisode = new ActivityEpisode(0,
                        new TimeWindow(startTimeR, startTimeR + durationR),
                        Activity.SecondaryWork, person);
                    secondaryWorkEpisode.Zone = zone;
                    schedule.Insert(secondaryWorkEpisode, random);
                }
            }
        }
Example #19
0
        private static void ProcessWorkAtHome(ITashaPerson person, Schedule workSchedule, Random random, int householdPD,
            int workPD, GenerationAdjustment[] generationAdjustments)
        {
            int freq_A = TimeTable.GetFrequency(person, Activity.WorkAtHomeBusiness, random, householdPD,
                workPD, generationAdjustments);
            Time duration, startTime;
            for (int i = 0; i < freq_A; i++)
            {
                bool success = false;
                short attempt = 0;
                while (!success && (attempt < Scheduler.EpisodeSchedulingAttempts))
                {
                    if (!TimeTable.GetStartTime(person, Activity.WorkAtHomeBusiness, freq_A, random, out startTime))
                    {
                        success = false;
                        attempt++;
                        continue;
                    }
                    if (!TimeTable.GetDuration(person, Activity.WorkAtHomeBusiness,
                        startTime, Time.EndOfDay - startTime, random, out duration))
                    {
                        success = false;
                        attempt++;
                        continue;
                    }

                    Time endTime = startTime + duration;
                    SchedulerHousehold.CheckAndUpdateLatestWorkingTime(person.Household,
                        endTime);
                    Episode wahBusinessEpisode;
                    wahBusinessEpisode = new ActivityEpisode(0,
                        new TimeWindow(startTime, endTime), Activity.WorkAtHomeBusiness, person);
                    if (!workSchedule.Insert(wahBusinessEpisode, random))
                    {
                        success = false;
                        attempt++;
                    }
                    else
                    {
                        success = true;
                    }
                }
            }
        }
Example #20
0
        public static int GetFrequency(ITashaHousehold household, Activity activity, Random random, int householdPD, int workPD, GenerationAdjustment[] generationAdjustments)
        {
            bool feasibleFreq = false;
            int freq = 0;
            while (!feasibleFreq)
            {
                freq = Distribution.GetRandomFrequencyValue(
                    0, Distribution.NumberOfFrequencies - 1, random, Distribution.GetDistributionID(household, activity),
                    householdPD, workPD, generationAdjustments);
                if (freq == 0)
                {
                    feasibleFreq = true;
                }
                Time startTime;
                if (!Distribution.GetRandomStartTimeFrequency(
                    Distribution.GetDistributionID(household, activity), freq,
                    0, Scheduler.StartTimeQuanta - 1, random, out startTime))
                {
                    //a bad thing happens here
                }
                else if (startTime != Time.StartOfDay)
                {
                    feasibleFreq = true;
                }
            }

            return freq;
        }
Example #21
0
 /// <summary>
 /// Generate all of the project schedules for the household
 /// </summary>
 /// <param name="household"></param>
 /// <returns></returns>
 internal static bool GenerateProjectSchedules(this ITashaHousehold household, Random random, GenerationAdjustment[] generationRateAdjustments)
 {
     return household.GenerateWorkEpisodes(random, generationRateAdjustments)
         && household.GenerateSchoolEpisodes(random, generationRateAdjustments)
         && household.GenerateJointOtherEpisodes(random, generationRateAdjustments)
         && household.GenerateJointMarketEpisodes(random, generationRateAdjustments)
         && household.GenerateIndividualOtherEpisodes(random, generationRateAdjustments)
         && household.GenerateIndividualMarketEpisodes(random, generationRateAdjustments);
 }