Ejemplo n.º 1
0
 public IEnumerable <ODData <float> > Read()
 {
     if (OriginOnly)
     {
         ODData <float> temp;
         var            data = DataResource.AcquireResource <SparseArray <float> >();
         temp.D = 0;
         var validIndexes = data.ValidIndexArray();
         var flatData     = data.GetFlatData();
         for (int i = 0; i < validIndexes.Length; i++)
         {
             temp.O    = validIndexes[i];
             temp.Data = flatData[i];
             yield return(temp);
         }
     }
     else
     {
         ODData <float> temp;
         var            data     = DataResource.AcquireResource <SparseTwinIndex <float> >();
         var            flatData = data.GetFlatData();
         for (int i = 0; i < flatData.Length; i++)
         {
             temp.O = data.GetSparseIndex(i);
             var row = flatData[i];
             for (int j = 0; j < row.Length; j++)
             {
                 temp.D    = data.GetSparseIndex(i, j);
                 temp.Data = flatData[i][j];
                 yield return(temp);
             }
         }
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Get the log of employment per zone.
 /// </summary>
 /// <returns>Log of Employment by zone</returns>
 private float[] GetEmploymentData()
 {
     if (EmploymentHasLogApplied)
     {
         return(Employment.AcquireResource <SparseArray <float> >().GetFlatData().Select(x => x).ToArray());
     }
     return(Employment.AcquireResource <SparseArray <float> >().GetFlatData().Select(x => (float)Math.Log(x + 1)).ToArray());
 }
 public void IterationStarting(int iterationNumber, int maxIterations)
 {
     if (!AccessStationChoiceLoaded | UnloadAccessStationModelEachIteration)
     {
         AccessStationModel.Load();
         AccessStationChoiceLoaded = true;
     }
     // We do this here instead of the RuntimeValidation so that we don't run into issues with estimation
     AgeUtilLookup = new float[16];
     for (int i = 0; i < AgeUtilLookup.Length; i++)
     {
         AgeUtilLookup[i] = (float)Math.Log(i + 1, Math.E) * LogOfAgeFactor;
     }
     //build the region constants
     for (int i = 0; i < TimePeriodConstants.Length; i++)
     {
         TimePeriodConstants[i].BuildMatrix();
     }
     ZonalDensityForActivitiesArray = ZonalDensityForActivities.AcquireResource <SparseArray <float> >().GetFlatData().Clone() as float[];
     ZonalDensityForHomeArray       = ZonalDensityForHome.AcquireResource <SparseArray <float> >().GetFlatData().Clone() as float[];
     for (int i = 0; i < ZonalDensityForActivitiesArray.Length; i++)
     {
         ZonalDensityForActivitiesArray[i] *= ToActivityDensityFactor;
         ZonalDensityForHomeArray[i]       *= ToHomeDensityFactor;
     }
 }
Ejemplo n.º 4
0
        public void Start()
        {
            SparseArray <float> data;

            data = Data.AcquireResource <SparseArray <float> >();
            TMG.Functions.SaveData.SaveVector(data, OutputTo.GetFilePath());
        }
Ejemplo n.º 5
0
        private void ComputeProduction(float[] flatProduction, float[] flatAttraction)
        {
            var zones      = Root.ZoneSystem.ZoneArray.GetFlatData();
            int emp        = EmploymentStatusCategory[0].Start;
            var rateEmp    = emp;
            var mob        = Mobility[0].Start;
            var age        = AgeCategoryRange[0].Start;
            var occ        = OccupationCategory[0].Start;
            var workerData = WorkerData.AcquireResource <SparseArray <SparseTriIndex <float> > >().GetFlatData();
            var test       = workerData[0];

            if (!test.GetFlatIndex(ref emp, ref mob, ref age))
            {
                throw new XTMFRuntimeException(this, "In " + Name + " we were unable to find a place to store our data (" + emp + "," + mob + "," + age + ")");
            }
            for (int i = 0; i < flatProduction.Length; i++)
            {
                var population = zones[i].Population;
                var zoneNumber = UsesPlanningDistricts ? zones[i].PlanningDistrict : zones[i].ZoneNumber;
                // production is the generation rate
                if (population <= 0 | zones[i].RegionNumber == 0)
                {
                    flatProduction[i] = 0;
                    flatAttraction[i] = 0;
                    continue;
                }
                var workersOfCatInZone = workerData[i].GetFlatData()[emp][mob][age];
                flatProduction[i] = workersOfCatInZone * DailyRates[zoneNumber, rateEmp, occ] * TimeOfDayRates[zoneNumber, rateEmp, occ];
                flatAttraction[i] = workersOfCatInZone;
            }
        }
Ejemplo n.º 6
0
        public void Start()
        {
            SparseTwinIndex <float> matrix;

            if (ODMatrix != null)
            {
                matrix = ODMatrix.AcquireResource <SparseTwinIndex <float> >();
            }
            else
            {
                var previouslyLoaded = ODMatixRaw.Loaded;
                if (!previouslyLoaded)
                {
                    ODMatixRaw.LoadData();
                }
                matrix = ODMatixRaw.GiveData();
                if (!previouslyLoaded)
                {
                    ODMatixRaw.UnloadData();
                }
            }
            if (ThirdNormalized)
            {
                TMG.Functions.SaveData.SaveMatrixThirdNormalized(matrix, SaveLocation);
            }
            else
            {
                TMG.Functions.SaveData.SaveMatrix(matrix, SaveLocation);
            }
        }
Ejemplo n.º 7
0
        public void LoadData()
        {
            var zoneArray  = Root.ZoneSystem.ZoneArray;
            var zones      = zoneArray.GetFlatData();
            var firstRate  = FirstRateToApply.AcquireResource <SparseArray <float> >();
            var secondRate = SecondRateToApply.AcquireResource <SparseArray <float> >();
            SparseArray <float> data;

            if (SaveRatesBasedOnPD)
            {
                data = TMG.Functions.ZoneSystemHelper.CreatePdArray <float>(zoneArray);
                var pds = data.ValidIndexArray();
                for (int i = 0; i < pds.Length; i++)
                {
                    var pd = pds[i];
                    data[pd] = firstRate[pd] * secondRate[pd];
                }
            }
            else
            {
                // then we are outputting by zone
                data = zoneArray.CreateSimilarArray <float>();
                var flatData = data.GetFlatData();
                for (int i = 0; i < flatData.Length; i++)
                {
                    var pd   = zones[i].PlanningDistrict;
                    var zone = zones[i].ZoneNumber;
                    flatData[i] = firstRate[FirstRateBasedOnPD ? pd : zone] * secondRate[SecondRateBasedOnPD ? pd : zone];
                }
            }
            Data = data;
        }
Ejemplo n.º 8
0
        public void LoadData()
        {
            // Get totals by row
            var totalToNormalizeTo = GetRowTotalsFromResource(DataToNormalizeTo);
            var inputMatrix        = DataToNormalize.AcquireResource <SparseTwinIndex <float> >();
            var ourMatrix          = inputMatrix.GetFlatData();
            var ourTotalByRow      = GetRowTotalsFromResource(DataToNormalize);

            // create inverse
            VectorHelper.Divide(ourTotalByRow, 0, totalToNormalizeTo, 0, ourTotalByRow, 0, ourTotalByRow.Length);
            // apply inverse
            var data     = inputMatrix.CreateSimilarArray <float>();
            var flatData = data.GetFlatData();

            for (int i = 0; i < ourTotalByRow.Length; i++)
            {
                // if it is infinity or NAN, that means that we had zero elements
                // thusly we can just leave the matrix alone to its default value of zero.
                if (!(float.IsInfinity(ourTotalByRow[i]) || float.IsNaN(ourTotalByRow[i])))
                {
                    VectorHelper.Multiply(flatData[i], 0, ourMatrix[i], 0, ourTotalByRow[i], flatData[i].Length);
                }
            }
            Data   = data;
            Loaded = true;
        }
        private float ComputeFitness()
        {
            var fitness = 0.0;
            var auto    = AutoProbabilites.AcquireResource <SparseArray <float> >().GetFlatData();

            // we only need the auto probabilities since auto + transit = 1 always.
            for (int i = 0; i < AutoProbabilities.Length; i++)
            {
                var observedTrips = TotalTrips[i];
                var pModel        = auto[i];
                var pTruth        = AutoProbabilities[i];
                if (observedTrips > 0 & pTruth > 0)
                {
                    double cellError;
                    if (pModel > pTruth)
                    {
                        // y - deltaXY <=> 2y-x
                        cellError = pTruth * Math.Log(Math.Min((Math.Max((pTruth + pTruth - pModel), 0)
                                                                + (pTruth * 0.0015)) / (pTruth * 1.0015), 1));
                    }
                    else
                    {
                        cellError = pTruth * Math.Log(Math.Min((pModel + (pTruth * 0.0015)) / (pTruth * 1.0015), 1));
                    }
                    fitness += cellError;
                }
            }
            return((float)fitness);
        }
Ejemplo n.º 10
0
        private void StoreProductionData(SparseArray <float> production)
        {
            var age            = AgeCategoryRange[0].Start;
            var mob            = Mobility[0].Start;
            var emp            = EmploymentStatusCategory[0].Start;
            var data           = WorkerData.AcquireResource <SparseArray <SparseTriIndex <float> > >();
            var flatData       = data.GetFlatData();
            var test           = flatData[0];
            var flatProduction = production.GetFlatData();

            if (!test.GetFlatIndex(ref emp, ref mob, ref age))
            {
                throw new XTMFRuntimeException("In " + Name + " we were unable to find a place to store our data (" + emp + "," + mob + "," + age + ")");
            }
            int i = 0;

            try
            {
                for (; i < flatProduction.Length; i++)
                {
                    flatData[i].GetFlatData()[emp][mob][age] = flatProduction[i];
                }
            }
            catch
            {
                throw new XTMFRuntimeException("Failed Yo!");
            }
        }
Ejemplo n.º 11
0
        public void IterationStarting(int iterationNumber, int maxIterations)
        {
            var zoneSystem = Root.ZoneSystem;

            ZoneArray = zoneSystem.ZoneArray;
            // We do this here instead of the RuntimeValidation so that we don't run into issues with estimation
            for (int i = 0; i < TimePeriodConstants.Length; i++)
            {
                TimePeriodConstants[i].BuildMatrix();
            }
            ZonalDensityForActivitiesArray = ZonalDensityForActivities.AcquireResource <SparseArray <float> >().GetFlatData().Clone() as float[];
            ZonalDensityForHomeArray       = ZonalDensityForHome.AcquireResource <SparseArray <float> >().GetFlatData().Clone() as float[];
            for (int i = 0; i < ZonalDensityForActivitiesArray.Length; i++)
            {
                ZonalDensityForActivitiesArray[i] *= ToActivityDensityFactor;
                ZonalDensityForHomeArray[i]       *= ToHomeDensityFactor;
            }

            ProfessionalCost     = ConvertCostFactor(ProfessionalCostFactor, ProfessionalTimeFactor);
            GeneralCost          = ConvertCostFactor(GeneralCostFactor, GeneralTimeFactor);
            SalesCost            = ConvertCostFactor(SalesCostFactor, SalesTimeFactor);
            ManufacturingCost    = ConvertCostFactor(ManufacturingCostFactor, ManufacturingTimeFactor);
            StudentCost          = ConvertCostFactor(StudentCostFactor, StudentTimeFactor);
            NonWorkerStudentCost = ConvertCostFactor(NonWorkerStudentCostFactor, NonWorkerStudentTimeFactor);
        }
Ejemplo n.º 12
0
        private void RecordData(int iterationNumber)
        {
            var   first  = FirstMatrix.AcquireResource <SparseTwinIndex <float> >().GetFlatData();
            var   second = SecondMatrix.AcquireResource <SparseTwinIndex <float> >().GetFlatData();
            float value  = 0.0f;

            switch (AnalysisToRun)
            {
            case AnalysisType.Average:
                value = GetAverage(first, second);
                break;

            case AnalysisType.Max:
                value = GetMax(first, second);
                break;
            }
            Writer.Write(iterationNumber + 1);
            Writer.Write(',');
            Writer.Write(value);
            if (SumFirst)
            {
                var sum = 0.0f;
                for (int i = 0; i < first.Length; i++)
                {
                    sum += VectorHelper.Sum(first[i], 0, first[i].Length);
                }
                Writer.Write(',');
                Writer.Write(sum);
            }
            Writer.WriteLine();
        }
Ejemplo n.º 13
0
        public void Start()
        {
            LoadMapping(out string[] aggregationHeaders, out List <int>[] zoneToAggregationMap);
            var data    = AnalysisTarget.AcquireResource <SparseTwinIndex <float> >();
            var aggData = AggregationToApply.ApplyAggregation(data.GetFlatData(), zoneToAggregationMap, aggregationHeaders);

            SaveData(aggData, aggregationHeaders);
        }
Ejemplo n.º 14
0
 public T GiveData()
 {
     if (Linked == null)
     {
         Link();
     }
     return(Linked.AcquireResource <T>());
 }
        public void Start()
        {
            var dbConnection = DatabaseConnection.AcquireResource <IDbConnection>();

            if (dbConnection == null)
            {
                throw new XTMFRuntimeException(this, "In '" + Name + "' we were unable to get a database connection!");
            }
        }
Ejemplo n.º 16
0
 public void IterationStarting(int iterationNumber, int maxIterations)
 {
     if (iterationNumber > 0)
     {
         AccessStationUtilities.ReleaseResource();
     }
     // each iteration reload the utilities
     AccessUtilities = AccessStationUtilities.AcquireResource <SparseTwinIndex <Tuple <IZone[], IZone[], float[]> > >();
 }
Ejemplo n.º 17
0
 private float[] GetRowTotalsFromResource(IResource resource)
 {
     float[] totalByRow;
     if (resource.CheckResourceType <SparseArray <float> >())
     {
         totalByRow = resource.AcquireResource <SparseArray <float> >().GetFlatData();
     }
     else
     {
         var matrix = resource.AcquireResource <SparseTwinIndex <float> >().GetFlatData();
         totalByRow = new float[matrix.Length];
         for (int i = 0; i < totalByRow.Length; i++)
         {
             totalByRow[i] = VectorHelper.Sum(matrix[i], 0, matrix[i].Length);
         }
     }
     return(totalByRow);
 }
Ejemplo n.º 18
0
            internal void Execute()
            {
                Console.WriteLine("Processing '" + Name + "'");
                var results = Results.AcquireResource <SparseTwinIndex <float> >();
                var data    = results.GetFlatData();

                float[] o        = StudentsByZone.GiveData().GetFlatData();
                var     baseData = BaseYearData.GiveData().GetFlatData();
                var     factors  = new ConcurrentDictionary <int, float>();
                var     zones    = Root.ZoneSystem.ZoneArray.GetFlatData();

                Parallel.For(0, baseData.Length, i =>
                {
                    var baseYearRow        = baseData[i];
                    float baseYearStudents = VectorHelper.Sum(baseYearRow, 0, baseYearRow.Length);
                    var resultRow          = data[i];
                    if (baseYearStudents > 0)
                    {
                        var factor = o[i] / baseYearStudents;
                        factors[i] = factor;
                        VectorHelper.Multiply(resultRow, 0, baseYearRow, 0, factor, baseYearRow.Length);
                    }
                    else
                    {
                        // if there were no students then we need to ensure the results are zero
                        Array.Clear(resultRow, 0, resultRow.Length);
                        if (o[i] == 0.0f)
                        {
                            factors[i] = 1.0f;
                        }
                        else
                        {
                            factors[i] = float.PositiveInfinity;
                        }
                    }
                });
                if (SaveResults != null)
                {
                    SaveData.SaveMatrix(results, SaveResults.GetFilePath());
                }
                if (SaveFactors != null)
                {
                    using (var writer = new StreamWriter(SaveFactors))
                    {
                        writer.WriteLine("Zone,Factor");
                        foreach (var zoneFactor in from element in factors
                                 orderby element.Key ascending
                                 select new { Zone = zones[element.Key].ZoneNumber, Factor = element.Value })
                        {
                            writer.Write(zoneFactor.Zone);
                            writer.Write(',');
                            writer.WriteLine(zoneFactor.Factor);
                        }
                    }
                }
            }
Ejemplo n.º 19
0
        public void LoadData()
        {
            var firstRate            = FirstRateToApply.AcquireResource <SparseArray <float> >();
            var secondRate           = SecondRateToApply.AcquireResource <SparseArray <float> >();
            SparseArray <float> data = firstRate.CreateSimilarArray <float>();
            var flatFirst            = firstRate.GetFlatData();
            var flatSecond           = secondRate.GetFlatData();
            var flat = data.GetFlatData();

            VectorHelper.Divide(flat, 0, flatFirst, 0, flatSecond, 0, flat.Length);
            Data = data;
        }
Ejemplo n.º 20
0
 public void IterationStarting(int iterationNumber, int maxIterations)
 {
     if (iterationNumber > 0)
     {
         AccessStationUtilities.ReleaseResource();
     }
     // each iteration reload the utilities
     if ((AccessUtilities = AccessStationUtilities.AcquireResource <SparseTwinIndex <Tuple <IZone[], IZone[], float[]> > >()) == null)
     {
         throw new XTMFRuntimeException(this, "In '" + Name + "' we were unable to gather our Access Station Utilities!");
     }
 }
Ejemplo n.º 21
0
        public void Execute(int iterationNumber, int totalIterations)
        {
            if (Writer == null)
            {
                Writer = new StreamWriter(ReportFile);
                switch (AnalysisToRun)
                {
                case AnalysisType.Average:
                    Writer.Write("Iteration,Average");
                    break;

                case AnalysisType.Max:
                    Writer.Write("Iteration,Max");
                    break;
                }
                Writer.WriteLine(SumFirst ? ",SumOfFirst" : "");
            }
            var   first  = FirstMatrix.AcquireResource <SparseTwinIndex <float> >().GetFlatData();
            var   second = SecondMatrix.AcquireResource <SparseTwinIndex <float> >().GetFlatData();
            float value  = 0.0f;

            switch (AnalysisToRun)
            {
            case AnalysisType.Average:
                value = GetAverage(first, second);
                break;

            case AnalysisType.Max:
                value = GetMax(first, second);
                break;
            }
            Writer.Write(iterationNumber + 1);
            Writer.Write(',');
            Writer.Write(value);
            if (SumFirst)
            {
                var sum = 0.0f;
                for (int i = 0; i < first.Length; i++)
                {
                    sum += VectorHelper.Sum(first[i], 0, first[i].Length);
                }
                Writer.Write(',');
                Writer.Write(sum);
            }
            Writer.WriteLine();
            // if this is the last iteration dispose
            if (iterationNumber >= totalIterations - 1)
            {
                Writer.Dispose();
                Writer = null;
            }
        }
Ejemplo n.º 22
0
        public void Start()
        {
            float[] population;
            var     zoneSystem = ZoneSystem.AcquireResource <IZoneSystem>();
            var     connection = DatabaseConnection.AcquireResource <IDbConnection>();

            using (var command = connection.CreateCommand())
            {
                population = ExtractPopulation(command, zoneSystem.ZoneArray);
            }

            WritePopulation(population, zoneSystem.ZoneArray);
        }
Ejemplo n.º 23
0
        public void Start()
        {
            var matrix = ODMatrix.AcquireResource <SparseTwinIndex <float> >();

            if (ThirdNormalized)
            {
                TMG.Functions.SaveData.SaveMatrixThirdNormalized(matrix, SaveLocation);
            }
            else
            {
                TMG.Functions.SaveData.SaveMatrix(matrix, SaveLocation);
            }
        }
Ejemplo n.º 24
0
 public void LoadData()
 {
     var firstRate = FirstRateToApply.AcquireResource<SparseTwinIndex<float>>();
     var secondRate = SecondRateToApply.AcquireResource<SparseTwinIndex<float>>();
     SparseTwinIndex<float> data = firstRate.CreateSimilarArray<float>();
     var flatFirst = firstRate.GetFlatData();
     var flatSecond = secondRate.GetFlatData();
     var flat = data.GetFlatData();
     for (int i = 0; i < flat.Length; i++)
     {
         VectorHelper.Divide(flat[i], 0, flatFirst[i], 0, flatSecond[i], 0, flat[i].Length);
     }
     Data = data;
 }
Ejemplo n.º 25
0
        public void Start()
        {
            var zones         = ZoneSystem.AcquireResource <IZoneSystem>().ZoneArray;
            var numberOfZones = zones.GetFlatData().Length;
            var connection    = DatabaseConnection.AcquireResource <IDbConnection>();

            float[][] populationByAge;
            using (var command = connection.CreateCommand())
            {
                populationByAge = new float[AgeSets.Count][];
                FillInPopulationByZone(zones, numberOfZones, command, populationByAge);
            }
            WriteOutData(populationByAge, zones);
        }
        public void Start()
        {
            var modeller = EmmeModeller.AcquireResource <ModellerController>();
            var tools    = Tools;
            int i        = 0;

            // ReSharper disable AccessToModifiedClosure
            _Progress = () => (((float)i / tools.Length) + tools[i].Progress * (1.0f / tools.Length));
            Status    = () => tools[i].ToString();
            for (; i < tools.Length; i++)
            {
                tools[i].Execute(modeller);
            }
            _Progress = () => 0f;
        }
Ejemplo n.º 27
0
        public void Load()
        {
            // Get our resources
            ElementarySchoolProbabilities = ElementarySchoolProbabilitiesResource.AcquireResource <SparseTwinIndex <float> >();
            HighSchoolProbabilities       = HighschoolProbabilitiesResource.AcquireResource <SparseTwinIndex <float> >();
            UniversityProbabilities       = UniversityProbabilitiesResource.AcquireResource <SparseTwinIndex <float> >();

            // create replicated versions for our per iteration needs
            CurrentElementarySchoolProbabilities = Replicate(ElementarySchoolProbabilities);
            CurrentHighSchoolProbabilities       = Replicate(HighSchoolProbabilities);
            CurrentUniversityProbabilities       = Replicate(UniversityProbabilities);

            // Gather the zone system for use from the root module.
            Zones = Root.ZoneSystem.ZoneArray;
        }
        public void LoadData()
        {
            var employmentForZone   = EmploymentByZoneForOccEmpStat.AcquireResource <SparseArray <float> >().GetFlatData();
            var workAtHomeRates     = WorkAtHomeRateByZoneForOccEmpStat.AcquireResource <SparseArray <float> >().GetFlatData();
            var externalWorkerRates = ExternalWorkerRateByZoneForByOccEmpStat.AcquireResource <SparseArray <float> >().GetFlatData();
            var data = Root.ZoneSystem.ZoneArray.CreateSimilarArray <float>();
            var flat = data.GetFlatData();

            for (int i = 0; i < flat.Length; i++)
            {
                var postWaHEmployment = employmentForZone[i] * (1.0f - workAtHomeRates[i]);
                flat[i] = postWaHEmployment * (1.0f - externalWorkerRates[i]);
            }
            Data = data;
        }
Ejemplo n.º 29
0
 public void Load()
 {
     ZoneSystem = Root.ZoneSystem.ZoneArray;
     Zones      = ZoneSystem.GetFlatData();
     if (SaveWorkerCategory != null)
     {
         WorkerResults = new float[3][];
         for (int i = 0; i < WorkerResults.Length; i++)
         {
             WorkerResults[i] = new float[Zones.Length];
         }
     }
     Probabilities = Linkages.AcquireResource <SparseTriIndex <float> >();
     ConvertToProbabilities(Probabilities.GetFlatData());
     Linkages.ReleaseResource();
 }
        public void Execute()
        {
            var sparse = ToOutput.AcquireResource <SparseArray <float> >();
            var data   = sparse.GetFlatData();

            using (StreamWriter writer = new StreamWriter(OutputFile))
            {
                writer.WriteLine("SparseIndex,Value");
                for (int i = 0; i < data.Length; i++)
                {
                    writer.Write(sparse.GetSparseIndex(i));
                    writer.Write(',');
                    writer.WriteLine(data[i]);
                }
            }
        }