Exemple #1
0
 internal static void AddHouseholdProjects(this ITashaHousehold household, Schedule schedule, Random random)
 {
     for ( int i = 0; i < schedule.EpisodeCount; i++ )
     {
         var episode = (Episode)schedule.Episodes[i];
         episode.ContainingSchedule = schedule;
         var people = episode.People;
         if ( people != null )
         {
             bool first = true;
             IZone zone = null;
             foreach ( var person in people )
             {
                 var pData = person["SData"] as SchedulerPersonData;
                 //var check = pData.Schedule.EpisodeCount > 0;
                 var ep = new ActivityEpisode( 0, new TimeWindow( episode.StartTime, episode.EndTime ) /*window*/,
                     schedule.Episodes[i].ActivityType, person );
                 if ( first )
                 {
                     pData.Schedule.Insert( ep, random );
                     zone = ep.Zone;
                     first = false;
                 }
                 else
                 {
                     pData.Schedule.Insert( ep, zone );
                     pData.Schedule.CheckEpisodeIntegrity();
                 }
             }
         }
     }
 }
Exemple #2
0
        internal void InsertWorkSchedule(Schedule schedule, Random random)
        {
            bool taken = false;

            //Second pass is to add primary work trips
            for(int i = 0; i < schedule.EpisodeCount; i++)
            {
                if(schedule.Episodes[i].ActivityType == Activity.PrimaryWork)
                {
                    if(!Insert((Episode)schedule.Episodes[i], random))
                    {
                        var expFactor = Owner.ExpansionFactor;
                        taken = false;
                        SkippedWorkLock.Enter(ref taken);
                        SkippedWorkEpisodes += expFactor;
                        if(taken) SkippedWorkLock.Exit(true);
                    }
                }
            }

            //Third pass is to add everything else
            for(int i = 0; i < schedule.EpisodeCount; i++)
            {
                if(schedule.Episodes[i].ActivityType != Activity.PrimaryWork
                    && schedule.Episodes[i].ActivityType != Activity.WorkBasedBusiness)
                {
                    if(!Insert((Episode)schedule.Episodes[i], random))
                    {
                        var expFactor = Owner.ExpansionFactor;
                        taken = false;
                        SkippedWorkLock.Enter(ref taken);
                        SkippedWorkEpisodes += expFactor;
                        if(taken) SkippedWorkLock.Exit(true);
                    }
                }
            }

            // First pass is to add works based business trips
            for(int i = 0; i < schedule.EpisodeCount; i++)
            {
                if(schedule.Episodes[i].ActivityType == Activity.WorkBasedBusiness)
                {
                    if(!Insert((Episode)schedule.Episodes[i], random))
                    {
                        var expFactor = Owner.ExpansionFactor;
                        taken = false;
                        SkippedWorkLock.Enter(ref taken);
                        SkippedWorkEpisodes += expFactor;
                        if(taken) SkippedWorkLock.Exit(true);
                    }
                }
            }
        }
 private void GatherDuration(Schedule sched, float[] data, bool original, float expFactor)
 {
     var episodes = sched.Episodes;
     for(int i = 0; i < sched.EpisodeCount; i++)
     {
         if(episodes[i] != null)
         {
             int index = GetBucketIndex(original ? episodes[i].OriginalDuration : episodes[i].Duration);
             if(index >= 0 && index < WorkStartTime.Length)
             {
                 data[index] += expFactor;
             }
         }
     }
 }
 private void GatherData(Schedule sched, float[] data, bool startTime, float expFactor)
 {
     var episodes = sched.Episodes;
     for(int i = 0; i < sched.EpisodeCount; i++)
     {
         if(episodes[i] != null)
         {
             int index = GetBucketIndex(startTime ? episodes[i].StartTime : episodes[i].EndTime);
             if(index >= 0 && index < WorkStartTime.Length)
             {
                 data[index] += expFactor;
             }
         }
     }
 }
 private void CalculateWorkingPersons(Schedule sched, float expFactor)
 {
     var episodes = sched.Episodes;
     for(int i = 0; i < sched.EpisodeCount; i++)
     {
         if(episodes[i] != null)
         {
             int start = GetBucketIndex(episodes[i].StartTime);
             int end = GetBucketIndex(episodes[i].EndTime);
             for(int j = start; j < end; j++)
             {
                 if(j >= 0 && j < WorkStartTime.Length)
                 {
                     WorkingPersons[j] += expFactor;
                 }
             }
         }
     }
 }
Exemple #6
0
 internal PersonalProject(Schedule schedule, ITashaPerson person)
     : base(schedule)
 {
     this.Person = person;
 }
Exemple #7
0
 protected static string Dump(Schedule sched)
 {
     StringBuilder builder = new StringBuilder();
     bool first = true;
     for ( int i = 0; i < sched.Episodes.Length; i++ )
     {
         if ( sched.Episodes[i] == null ) continue;
         if ( !first )
         {
             builder.AppendLine();
         }
         StoreEpisode( (Episode)sched.Episodes[i], builder );
         first = false;
     }
     return builder.ToString();
 }
 private void CalculateWorkingPersons(Schedule sched, int[] data, Activity[] filter, bool include)
 {
     var episodes = sched.Episodes;
     for ( int i = 0; i < sched.EpisodeCount; i++ )
     {
         if ( episodes[i] != null && ( ( ( !include ) ^ filter.Contains( episodes[i].ActivityType ) ) ) )
         {
             int start = this.GetBucketIndex( episodes[i].StartTime );
             int end = this.GetBucketIndex( episodes[i].EndTime );
             for ( int j = start; j < end; j++ )
             {
                 if ( j >= 0 && j < data.Length )
                 {
                     System.Threading.Interlocked.Increment( ref data[j] );
                 }
             }
         }
     }
 }
Exemple #9
0
        private static void ProcessWorkBusiness(ITashaPerson person, Schedule workSchedule, Random random, Episode primWorkEpisode)
        {
            Time startTimeB;
            Time durationB;
            Time startTime = primWorkEpisode.StartTime;
            Time endTime = primWorkEpisode.EndTime;

            int freq = TimeTable.GetFrequency(person, Activity.WorkBasedBusiness, random, Scheduler.MaxFrequency, startTime, endTime);
            for(int i = 0; i < freq; i++)
            {
                var attempt = 0;
                bool success = false;
                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))
                    {
                        success = true;
                        break;
                    }
                }
            }
        }
Exemple #10
0
        private static void ProcessWorkAtHome(ITashaPerson person, Schedule workSchedule, Random random)
        {
            int freq_A = TimeTable.GetFrequency(person, Activity.WorkAtHomeBusiness, random);
            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;
                    }
                }
            }
        }
Exemple #11
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)
        {
            //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);

                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);
                }
            }
        }
Exemple #12
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 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);

            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;
        }
Exemple #13
0
 private static void GenerateWorkBusinessEpisode(ITashaPerson person, Schedule workSchedule, Random random)
 {
     int freq_B = TimeTable.GetFrequency(person, Activity.WorkBasedBusiness, random);
     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;
         }
     }
 }
 private void GatherDuration(Schedule sched, int[] data, bool original)
 {
     var episodes = sched.Episodes;
     for ( int i = 0; i < sched.EpisodeCount; i++ )
     {
         if ( episodes[i] != null )
         {
             int index = this.GetBucketIndex( original ? episodes[i].OriginalDuration : episodes[i].Duration );
             if ( index >= 0 && index < this.WorkingPersons.Length )
             {
                 System.Threading.Interlocked.Increment( ref data[index] );
             }
         }
     }
 }
 private void GatherData(Schedule sched, int[] data, bool startTime)
 {
     var episodes = sched.Episodes;
     for ( int i = 0; i < sched.EpisodeCount; i++ )
     {
         if ( episodes[i] != null )
         {
             int index = this.GetBucketIndex( startTime ? episodes[i].StartTime : episodes[i].EndTime );
             if ( index >= 0 && index < this.WorkingPersons.Length )
             {
                 System.Threading.Interlocked.Increment( ref data[index] );
             }
         }
     }
 }
Exemple #16
0
 internal static void AddPersonProjects(Schedule sched, PersonalProject project, Random random)
 {
     sched.Insert( project.Schedule, random );
 }
Exemple #17
0
 /// <summary>
 /// Add a whole Schedule to this schedule
 /// </summary>
 /// <param name="schedule">The schedule you wish to add</param>
 public void Insert(Schedule schedule, Random random)
 {
     for ( int i = 0; i < schedule.EpisodeCount; i++ )
     {
         if ( schedule.Episodes[i].ActivityType != Activity.NullActivity )
         {
             var episode = (Episode)schedule.Episodes[i];
             // take ownership of this episode
             episode.ContainingSchedule = this;
             this.Insert( episode, random );
         }
     }
 }
Exemple #18
0
 public HouseholdProject(ITashaHousehold household, Schedule schedule)
     : base(schedule)
 {
     this.Household = household;
 }