private static void SetupFrictionData(List <SparseArray <float> > productions, List <SparseArray <float> > attractions,
                                              List <PoRPoWGeneration> cats, MultiBlendSet multiset, float[][][] productionSet, float[][][] attractionSet,
                                              PoRPoWGeneration[][] multiCatSet)
        {
            int subsetIndex = -1;

            foreach (var blendSet in multiset.Subsets)
            {
                subsetIndex++;
                var set           = blendSet.Set;
                var length        = set.Count;
                int place         = 0;
                int blendSetCount = 0;
                for (int i = 0; i < length; i++)
                {
                    for (int pos = set[i].Start; pos <= set[i].Stop; pos++)
                    {
                        blendSetCount++;
                    }
                }
                productionSet[subsetIndex] = new float[blendSetCount][];
                attractionSet[subsetIndex] = new float[blendSetCount][];
                multiCatSet[subsetIndex]   = new PoRPoWGeneration[blendSetCount];
                for (int i = 0; i < length; i++)
                {
                    for (int pos = set[i].Start; pos <= set[i].Stop; pos++)
                    {
                        productionSet[subsetIndex][place] = productions[pos].GetFlatData();
                        attractionSet[subsetIndex][place] = attractions[pos].GetFlatData();
                        multiCatSet[subsetIndex][place]   = cats[pos];
                        place++;
                    }
                }
            }
        }
Example #2
0
 private void AddNewGeneration(List<IDemographicCategoryGeneration> list, int occ, Range age, int employmentStatus)
 {
     PoRPoWGeneration gen = new PoRPoWGeneration();
     gen.Root = this.Root;
     gen.LoadData = false;
     gen.OccupationCategory = CreateRangeSet( occ );
     gen.AgeCategoryRange = CreateRangeSet( age );
     gen.EmploymentStatusCategory = CreateRangeSet( employmentStatus );
     gen.ModeChoiceParameterSetIndex = this.ModeChoiceParameterSetIndex;
     gen.Mobility = new RangeSet( new List<Range>() { new Range() { Start = 0, Stop = 0 } } );
     gen.ExternalJobs = this.ExternalJobs;
     gen.ExternalRates = this.WorkExternal;
     gen.WorkIntrazonal = this.WorkIntraZonal;
     gen.WorkAtHomeRates = this.WorkAtHomeRates;
     gen.AllAges = new RangeSet( new List<Range>() { new Range() { Start = 2, Stop = 5 } } );
     gen.GenerationOutputFileName = this.GenerationOutputFileName;
     gen.Name = this.Name + " - " + gen.ToString();
     gen.AttractionFileName = AttractionFileName;
     list.Add( gen );
 }
        private IEnumerable <SparseTwinIndex <float> > CpuDoublyConstrained(IZone[] zones, IEnumerator <SparseArray <float> > ep, IEnumerator <SparseArray <float> > ea, IEnumerator <IDemographicCategory> ec)
        {
            float completed = 0f;

            Correlation.Load();
            var frictionSparse = Root.ZoneSystem.ZoneArray.CreateSquareTwinArray <float>();
            var productions    = new List <SparseArray <float> >();
            var attractions    = new List <SparseArray <float> >();
            var cats           = new List <PoRPoWGeneration>();

            WCatParameters.LoadData();
            // We need to pre load all of our generations in order to handel blending properly
            while (ep.MoveNext() && ea.MoveNext() && ec.MoveNext())
            {
                productions.Add(ep.Current);
                attractions.Add(ea.Current);
                cats.Add(ec.Current as PoRPoWGeneration);
            }
            SparseArray <float> production = Root.ZoneSystem.ZoneArray.CreateSimilarArray <float>();
            SparseArray <float> attraction = Root.ZoneSystem.ZoneArray.CreateSimilarArray <float>();

            CurrentMultiSetIndex = -1;
            foreach (var multiset in MultiBlendSets)
            {
                CurrentMultiSetIndex++;
                var numberOfSubsets = multiset.Subsets.Count;
                var productionSet   = new float[numberOfSubsets][][];
                var attractionSet   = new float[numberOfSubsets][][];
                var multiCatSet     = new PoRPoWGeneration[numberOfSubsets][];
                SetupFrictionData(productions, attractions, cats, multiset, productionSet, attractionSet, multiCatSet);
                ComputeFriction(zones, multiCatSet, productionSet, attractionSet,
                                frictionSparse.GetFlatData(), production.GetFlatData(), attraction.GetFlatData());
                // ReSharper disable once AccessToModifiedClosure
                yield return(new GravityModel(frictionSparse, (p => Progress = (p * (1f / (MultiBlendSets.Count)) + (completed / (MultiBlendSets.Count)))), Epsilon, MaxIterations)
                             .ProcessFlow(production, attraction, production.ValidIndexArray()));

                completed += 1f;
            }
            WCatParameters.UnloadData();
            Correlation.Unload();
        }
Example #4
0
 private void SetupModeChoice(PoRPoWGeneration[][] cats, IModeParameterDatabase mpd, int subset, int mobilityCategory)
 {
     // this.CurrentMultiSet == Occupation [0,3] * NumberOfMobilityCategories + mobility Category
     this.WCatParameters.SetDemographicCategory(this.CurrentMultiSetIndex * 5 + mobilityCategory);
     cats[subset][0].Mobility = new RangeSet(new List<Range>() { new Range() { Start = mobilityCategory, Stop = mobilityCategory } });
     cats[subset][0].InitializeDemographicCategory();
 }
Example #5
0
 private IEnumerable<SparseTwinIndex<float>> CPUDoublyConstrained(IZone[] zones, IEnumerator<SparseArray<float>> ep, IEnumerator<SparseArray<float>> ea, IEnumerator<IDemographicCategory> ec)
 {
     float completed = 0f;
     this.Correlation.Load();
     var frictionSparse = this.Root.ZoneSystem.ZoneArray.CreateSquareTwinArray<float>();
     var productions = new List<SparseArray<float>>();
     var attractions = new List<SparseArray<float>>();
     var cats = new List<PoRPoWGeneration>();
     this.WCatParameters.LoadData();
     // We need to pre load all of our generations in order to handel blending properly
     while(ep.MoveNext() && ea.MoveNext() && ec.MoveNext())
     {
         productions.Add(ep.Current);
         attractions.Add(ea.Current);
         cats.Add(ec.Current as PoRPoWGeneration);
     }
     var ret = this.Root.ZoneSystem.ZoneArray.CreateSquareTwinArray<float>();
     SparseArray<float> production = this.Root.ZoneSystem.ZoneArray.CreateSimilarArray<float>();
     SparseArray<float> attraction = this.Root.ZoneSystem.ZoneArray.CreateSimilarArray<float>();
     this.CurrentMultiSetIndex = -1;
     foreach(var multiset in this.MultiBlendSets)
     {
         this.CurrentMultiSetIndex++;
         var numberOfSubsets = multiset.Subsets.Count;
         var productionSet = new float[numberOfSubsets][][];
         var attractionSet = new float[numberOfSubsets][][];
         var multiCatSet = new PoRPoWGeneration[numberOfSubsets][];
         SetupFrictionData(productions, attractions, cats, multiset, productionSet, attractionSet, multiCatSet);
         this.ComputeFriction(zones, multiCatSet, productionSet, attractionSet,
             frictionSparse.GetFlatData(), production.GetFlatData(), attraction.GetFlatData());
         yield return new GravityModel(frictionSparse, (p => this.Progress = (p * (1f / (this.MultiBlendSets.Count)) + (completed / (this.MultiBlendSets.Count)))), this.Epsilon, this.MaxIterations)
             .ProcessFlow(production, attraction, production.ValidIndexArray());
         completed += 1f;
     }
     this.WCatParameters.UnloadData();
     this.Correlation.Unload();
 }
Example #6
0
 private void ComputeFriction(IZone[] zones, PoRPoWGeneration[][] cats, float[][][] productions, float[][][] attractions, float[][] friction, float[] production, float[] attraction)
 {
     ClearFriction(friction, zones.Length);
     this.InteractiveModeSplit.StartNewInteractiveModeSplit(this.MultiBlendSets.Count);
     using (var mobilityStream = File.OpenWrite(this.MobilityCacheFileName.GetFileName() + this.CurrentMultiSetIndex.ToString() + ".bin"))
     {
         var mpd = this.Root.ModeParameterDatabase;
         float[] subsetRatios = new float[productions.Length];
         // 1 temp friction per mobility category
         float[][] tempMobility = new float[5][];
         for(int i = 0; i < tempMobility.Length; i++)
         {
             tempMobility[i] = new float[zones.Length];
         }
         SumProductionAndAttraction(production, attraction, productions, attractions);
         BlockingCollection<MemoryStream[]> toWrite = new BlockingCollection<MemoryStream[]>(1);
         var writingTask = Task.Factory.StartNew(new Action(() => WriteMemoryStream(toWrite, mobilityStream)), TaskCreationOptions.LongRunning);
         for(int subset = 0; subset < cats.Length; subset++)
         {
             for(int i = 0; i < zones.Length; i++)
             {
                 // if there is no production for this origin we can just skip ahead for the next zone
                 if(production[i] == 0)
                 {
                     for(int mobilityCategory = 0; mobilityCategory < tempMobility.Length; mobilityCategory++)
                     {
                         Array.Clear(tempMobility[mobilityCategory], 0, tempMobility[mobilityCategory].Length);
                     }
                 }
                 else
                 {
                     ComputeSubsetRatios(i, subsetRatios, productions);
                     for(int mobilityCategory = 0; mobilityCategory <= 4; mobilityCategory++)
                     {
                         SetupModeChoice(cats, mpd, subset, mobilityCategory);
                         // if there is something here to process
                         GatherModeChoiceUtilities(zones, tempMobility[mobilityCategory], attraction, subsetRatios[subset], i);
                     }
                 }
                 toWrite.Add(ProcessUtilities(tempMobility, friction, subsetRatios[subset], i));
             }
         }
         toWrite.CompleteAdding();
         ConvertToFriction(friction, zones);
         writingTask.Wait();
         mobilityStream.Flush();
     }
     this.InteractiveModeSplit.EndInterativeModeSplit();
     CheckSaveFriction(friction);
 }
Example #7
0
 private static void SetupFrictionData(List<SparseArray<float>> productions, List<SparseArray<float>> attractions,
     List<PoRPoWGeneration> cats, MultiBlendSet multiset, float[][][] productionSet, float[][][] attractionSet,
     PoRPoWGeneration[][] multiCatSet)
 {
     int subsetIndex = -1;
     foreach(var blendSet in multiset.Subsets)
     {
         subsetIndex++;
         var set = blendSet.Set;
         var length = set.Count;
         int place = 0;
         int blendSetCount = 0;
         for(int i = 0; i < length; i++)
         {
             for(int pos = set[i].Start; pos <= set[i].Stop; pos++)
             {
                 blendSetCount++;
             }
         }
         productionSet[subsetIndex] = new float[blendSetCount][];
         attractionSet[subsetIndex] = new float[blendSetCount][];
         multiCatSet[subsetIndex] = new PoRPoWGeneration[blendSetCount];
         for(int i = 0; i < length; i++)
         {
             for(int pos = set[i].Start; pos <= set[i].Stop; pos++)
             {
                 productionSet[subsetIndex][place] = productions[pos].GetFlatData();
                 attractionSet[subsetIndex][place] = attractions[pos].GetFlatData();
                 multiCatSet[subsetIndex][place] = cats[pos];
                 place++;
             }
         }
     }
 }