/// <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); }
/// <summary> /// Needs to be called by every thread that wants to use distributions /// </summary> internal static void InitializeDistributions() { int numberOfDistributions = Scheduler.NumberOfDistributions; NumberOfFrequencies = Scheduler.MaxFrequency + 1; int startTimeQuantums = Scheduler.StartTimeQuanta; NumAdultDistributions = Scheduler.NumberOfAdultDistributions; NumAdultFrequencies = Scheduler.NumberOfAdultFrequencies; AdultDistributions = new ZoneCache <AdultDistributionInformation>(Scheduler.AdultDistributionsFile, LoadAdultDistributionData).StoreAll(); Distributions = new ZoneCache <DistributionInformation>(Scheduler.FrequencyDistributionsFile, delegate(int number, float[] data) { DistributionInformation f = new DistributionInformation(); int current = 0; f.Frequency = new float[NumberOfFrequencies]; Array.Copy(data, 0, f.Frequency, 0, NumberOfFrequencies); current += NumberOfFrequencies; f.Durations = new float[startTimeQuantums][]; for (int i = 0; i < startTimeQuantums; i++) { f.Durations[i] = new float[startTimeQuantums + 1]; } for (int time = 0; time < startTimeQuantums; time++) { for (int dur = 0; dur < startTimeQuantums + 1; dur++) { f.Durations[time][dur] = data[current++]; } } f.StartTimeFrequency = new float[startTimeQuantums][]; for (int i = 0; i < startTimeQuantums; i++) { f.StartTimeFrequency[i] = new float[NumberOfFrequencies]; } for (int fre = 0; fre < NumberOfFrequencies; fre++) { for (int time = 0; time < startTimeQuantums; time++) { f.StartTimeFrequency[time][fre] = data[current++]; } } return(f); }).StoreAll(); }
/// <summary> /// Needs to be called by every thread that wants to use distributions /// </summary> internal static void InitializeDistributions() { int numberOfDistributions = Scheduler.NumberOfDistributions; NumberOfFrequencies = Scheduler.MaxFrequency + 1; int startTimeQuantums = Scheduler.StartTimeQuanta; NumAdultDistributions = Scheduler.NumberOfAdultDistributions; NumAdultFrequencies = Scheduler.NumberOfAdultFrequencies; AdultDistributions = new ZoneCache<AdultDistributionInformation>(Scheduler.AdultDistributionsFile, LoadAdultDistributionData).StoreAll(); Distributions = new ZoneCache<DistributionInformation>(Scheduler.FrequencyDistributionsFile, delegate (int number, float[] data) { DistributionInformation f = new DistributionInformation(); int current = 0; f.Frequency = new float[NumberOfFrequencies]; Array.Copy(data, 0, f.Frequency, 0, NumberOfFrequencies); current += NumberOfFrequencies; f.Durations = new float[startTimeQuantums][]; for (int i = 0; i < startTimeQuantums; i++) { f.Durations[i] = new float[startTimeQuantums + 1]; } for (int time = 0; time < startTimeQuantums; time++) { for (int dur = 0; dur < startTimeQuantums + 1; dur++) { f.Durations[time][dur] = data[current++]; } } f.StartTimeFrequency = new float[startTimeQuantums][]; for (int i = 0; i < startTimeQuantums; i++) { f.StartTimeFrequency[i] = new float[NumberOfFrequencies]; } for (int fre = 0; fre < NumberOfFrequencies; fre++) { for (int time = 0; time < startTimeQuantums; time++) { f.StartTimeFrequency[time][fre] = data[current++]; } } return f; }).StoreAll(); }