private static void GenerateActivityLevels(string fileName, SparseArray <IZone> zoneArray)
        {
            var    zones       = zoneArray.GetFlatData();
            string csvFileName = Path.GetTempFileName();

            using (StreamWriter writer = new StreamWriter(csvFileName))
            {
                writer.WriteLine("Zone,Retail Level,Other Level,Work Level");
                for (int i = 0; i < zones.Length; i++)
                {
                    writer.Write(zones[i].ZoneNumber);
                    writer.Write(',');
                    writer.Write(zones[i].RetailActivityLevel);
                    writer.Write(',');
                    writer.Write(zones[i].OtherActivityLevel);
                    writer.Write(',');
                    writer.WriteLine(zones[i].WorkActivityLevel);
                }
            }
            SparseZoneCreator creator = new SparseZoneCreator(zones.Last().ZoneNumber + 1, 3);

            creator.LoadCSV(csvFileName, true);
            creator.Save(fileName);
            File.Delete(csvFileName);
        }
Beispiel #2
0
        public void Generate()
        {
            SparseZoneCreator creator = new SparseZoneCreator(HighestZoneNumber + 1, 22);

            creator.LoadCsv(GetFullPath(ZoneFileName), true);
            creator.Save(GetFullPath(ZoneCacheFile));
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        private void CreateFrequencyDistroCache()
        {
            if (!GenerateIfExists && File.Exists(FrequencyLevelsZFC))
            {
                return;
            }
            string       temp = Path.GetTempFileName();
            StreamReader readFrequency;
            StreamReader readStartTimeFrequency;
            StreamReader readStartTime;
            StreamWriter writer;

            FailIfNotExists(DurationDistribution);
            FailIfNotExists(StartTimeFrequency);
            FailIfNotExists(Frequency);
            using (writer = new StreamWriter(temp))
                using (readStartTime = new StreamReader(DurationDistribution))
                    using (readStartTimeFrequency = new StreamReader(StartTimeFrequency))
                        using (readFrequency = new StreamReader(Frequency))
                        {
                            // get rid of the headers
                            RemoveHeaders(readFrequency, readStartTime, readStartTimeFrequency);

                            StringBuilder line;
                            for (int dist = 0; dist < NumberOfDistributions; dist++)
                            {
                                line = new StringBuilder(20000);
                                line.Append(dist);
                                line.Append(',');

                                WriteFrequencies(line, readFrequency);
                                WriteDurations(line, readStartTime);
                                WriteStartTimeFrequencies(line, readStartTimeFrequency);
                                writer.WriteLine(line.ToString(0, line.Length - 1));
                            }
                        }

            var numberOfFrequencies = HighFrequency + 1; // it was inclusive
            // there are actually StartTimeQuantums + 1 durations
            var numberOfDurations = (StartTimeQuantums) * (StartTimeQuantums + 1);

            int types = numberOfFrequencies + numberOfFrequencies * StartTimeQuantums + numberOfDurations;

            if (File.Exists(FrequencyLevelsZFC))
            {
                try
                {
                    File.Delete(FrequencyLevelsZFC);
                }
                catch (IOException)
                { }
            }
            SparseZoneCreator zc = new SparseZoneCreator(NumberOfDistributions, types);

            zc.LoadCsv(temp, false);
            zc.Save(FrequencyLevelsZFC);
            File.Delete(temp);
        }
Beispiel #4
0
        public void BuildLocationChoiceCache(float[,] p, IZone[] flatZones)
        {
            string temp = Path.GetTempFileName();

            //string temp = Directory.GetCurrentDirectory() + "\\" + "test.csv";
            Console.WriteLine(temp);
            StreamWriter writer = new StreamWriter(temp);

            //fire up the cache

            ZoneCache <IZone> .CacheSize = 8000;
            //each row comparing one zone to another zone
            //column 1 zone ID, column 2 zone(comparing zone),column 3 distance,
            //column 4 sumExp1,
            //column5 exponentExpression1: exp( (p1*distkm) + (p2*log(empP)) + (p3*log(empG)) + (p4*sh0) + (p5*sh1) + (p6*sh2) )
            //column 6 sumExp1,
            //column7 exponentExpression1: exp( (p1*distkm) + (p2*log(empP)) + (p3*log(empG)) + (p4*sh0) + (p5*sh1) + (p6*sh2) )
            //column 8 sumExp1,
            //column9 exponentExpression1: exp( (p1*distkm) + (p2*log(empP)) + (p3*log(empG)) + (p4*sh0) + (p5*sh1) + (p6*sh2) )
            //column 10 sumExp1,
            //column11 exponentExpression1: exp( (p1*distkm) + (p2*log(empP)) + (p3*log(empG)) + (p4*sh0) + (p5*sh1) + (p6*sh2) )
            //n = number of zones, so n*n number of rows
            StringBuilder line = null;
            int           sum  = 0;

            for (int i = 0; i < flatZones.Length; i++)
            {
                IZone iz = flatZones[i];
                if ((iz.Population == 0 &&
                     iz.X == 0 && iz.Y == 0) || iz.InternalDistance == 0)
                {
                    continue;
                }
                else
                {
                    sum++;
                }

                line = new StringBuilder(100000);
                line.Append(i);

                BuildOfficeWorkCache(line, iz, p, flatZones);
                BuildManufacturingWorkCache(line, iz, p, flatZones);
                BuildRetailWorkCache(line, iz, p, flatZones);
                BuildProfessionalWorkCache(line, iz, p, flatZones);

                writer.WriteLine(line.ToString(0, line.Length - 1));
            }
            writer.Close();

            //IConfigurationDirectory directory =
            //    TashaConfiguration.GetDirectory("LocationChoiceModelParameters");
            SparseZoneCreator creator = new SparseZoneCreator(3, (4 * flatZones.Last().ZoneNumber) + 1);

            creator.LoadCSV(temp, false);
            creator.Save(LocatonChoiceModelWorkCache);
            File.Delete(temp);
        }
Beispiel #5
0
        private void CreateAdultFrequencyDistroCache()
        {
            if (!GenerateIfExists && File.Exists(AdultDistributionsZFC))
            {
                return;
            }
            string temp = Path.GetTempFileName();

            using (StreamReader reader = new StreamReader(AdultIn))
            {
                using (StreamWriter writer = new StreamWriter(temp))
                {
                    //0...num of distributions
                    for (int i = 0; i < AdultDistributions; i++)
                    {
                        StringBuilder sb = new StringBuilder(1000000);
                        //convert the data to one line

                        sb.Append(i);
                        for (int j = 0; j < NumberOfAdultFrequencies; j++)
                        {
                            string line = reader.ReadLine();
                            if (line != null)
                            {
                                //skip the first 3 values (they are implied) based on index
                                string[] values = line.Split(',');
                                sb.Append(",");
                                sb.Append(values[3]);
                            }
                        }
                        writer.WriteLine(sb);
                    }
                }
            }
            //  ZoneCreator.CsvToZFC(temp, Zone.GetNumberOfZones, 4 * numInternalZones, TashaConfiguration.GetInputFile(directory, "LocationChoiceModelWorkCache"), false);
            if (File.Exists(AdultDistributionsZFC))
            {
                try
                {
                    File.Delete(AdultDistributionsZFC);
                }
                catch (IOException)
                { }
            }
            SparseZoneCreator zc = new SparseZoneCreator(AdultDistributions, NumberOfAdultFrequencies);

            zc.LoadCsv(temp, false);
            zc.Save(AdultDistributionsZFC);
            File.Delete(temp);
        }
        public void BuildLocationChoiceCacheHome(float[,] p, IZone[] flatZones)
        {
            string temp = Path.GetTempFileName();
            //string temp = Directory.GetCurrentDirectory() + "\\" + "test2.csv";
            StreamWriter writer = new StreamWriter(temp);
            //fire up the cache

            StringBuilder line;

            Console.WriteLine("Building LocationChoiceHomeCache");
            for (int i = 0; i < flatZones.Length; i++)
            {
                IZone iz = flatZones[i];
                if (iz.Population == 0 &&
                    iz.X == 0 && iz.Y == 0)
                {
                    continue;
                }

                double distkm;
                //sum EXP for G---------------------------------------------------
                double sumExp = 0;
                double empR, sh0, sh1, sh2, empT;
                line = new StringBuilder(100000);
                line.Append(i);

                //h=0

                for (int k = 0; k < flatZones.Length; k++)
                {
                    IZone kz = flatZones[k];

                    if (kz.InternalDistance == 0)
                    {
                        continue;
                    }

                    if (kz.TotalEmployment > 0)
                    {
                        empT = Math.Log((kz.TotalEmployment / 1000.0) + 1.0);

                        //distance in KM
                        distkm = ZoneSystem.Distances[iz.ZoneNumber, kz.ZoneNumber] / 1000;
                        if (distkm >= 0 && distkm <= p[0, 4])
                        {
                            sh0 = 0.0;
                            sh1 = 0.0;
                            if (distkm >= 0 && distkm < 1)
                            {
                                sh0 = 1;
                            }
                            else if (distkm >= 1 && distkm < 2)
                            {
                                sh1 = 1;
                            }

                            sumExp += Math.Exp((p[0, 0] * distkm) + (p[0, 1] * empT) + (p[0, 2] * sh0) + (p[0, 3] * sh1));
                        }
                    }
                }
                //now calculate CDF sums from ... k .. num InternalZones

                double cdf = 0.0;
                for (int k = 0; k < flatZones.Length; k++)
                {
                    IZone kz = flatZones[k];

                    if (kz.TotalEmployment > 0)
                    {
                        empT = Math.Log((kz.TotalEmployment / 1000.0) + 1.0);

                        distkm = ZoneSystem.Distances[iz.ZoneNumber, kz.ZoneNumber] / 1000;
                        if (distkm >= 0 && distkm <= p[0, 4] && kz.InternalDistance > 0)
                        {
                            sh0 = 0.0;
                            sh1 = 0.0;
                            if (distkm >= 0 && distkm < 1)
                            {
                                sh0 = 1;
                            }
                            else if (distkm >= 1 && distkm < 2)
                            {
                                sh1 = 1;
                            }

                            cdf += (Math.Exp((p[0, 0] * distkm) + (p[0, 1] * empT) + (p[0, 2] * sh0) + (p[0, 3] * sh1)) / sumExp);
                        }
                    }
                    line.Append(",");
                    line.Append(cdf);
                }

                sumExp = 0;
                for (int k = 0; k < flatZones.Length; k++)
                {
                    IZone kz = flatZones[k];

                    if (kz.InternalDistance == 0)
                    {
                        continue;
                    }
                    if (kz.TotalEmployment > 0)
                    {
                        empT   = Math.Log((kz.TotalEmployment / 1000.0) + 1.0);
                        distkm = ZoneSystem.Distances[iz.ZoneNumber, kz.ZoneNumber] / 1000;
                        if (distkm >= 0 && distkm <= p[1, 5])
                        {
                            sh0 = 0.0;
                            sh1 = 0.0;
                            sh2 = 0.0;
                            if (distkm >= 0 && distkm < 1)
                            {
                                sh0 = 1;
                            }
                            else if (distkm >= 1 && distkm < 2)
                            {
                                sh1 = 1;
                            }
                            else if (distkm >= 2 && distkm < 3)
                            {
                                sh2 = 1;
                            }

                            sumExp += Math.Exp(((p[1, 0] * distkm) + (p[1, 1] * empT) + (p[1, 2] * sh0) + (p[1, 3] * sh1) + (p[1, 4] * sh2)));
                        }
                    }
                }

                cdf = 0.0;
                for (int k = 0; k < flatZones.Length; k++)
                {
                    IZone kz = flatZones[k];

                    if (kz.TotalEmployment > 0)
                    {
                        empT = Math.Log(((double)kz.TotalEmployment / 1000) + 1.0);

                        distkm = ZoneSystem.Distances[iz.ZoneNumber, kz.ZoneNumber] / 1000;
                        if (distkm >= 0 && distkm <= p[1, 5] && kz.InternalDistance > 0)
                        {
                            sh0 = 0.0;
                            sh1 = 0.0;
                            sh2 = 0.0;
                            if (distkm >= 0 && distkm < 1)
                            {
                                sh0 = 1;
                            }
                            else if (distkm >= 1 && distkm < 2)
                            {
                                sh1 = 1;
                            }
                            else if (distkm >= 2 && distkm < 3)
                            {
                                sh2 = 1;
                            }

                            cdf += Math.Exp(((p[1, 0] * distkm) + (p[1, 1] * empT) + (p[1, 2] * sh0) + (p[1, 3] * sh1) + (p[1, 4] * sh2))) / sumExp;
                        }
                    }
                    line.Append(",");
                    line.Append(cdf);
                }

                sumExp = 0;
                for (int k = 0; k < flatZones.Length; k++)
                {
                    IZone kz = flatZones[k];

                    if (kz.InternalDistance == 0 || kz.TotalEmployment <= 0)
                    {
                        continue;
                    }

                    empT = Math.Log((kz.TotalEmployment / 1000.0) + 1.0);
                    double pop = Math.Log((kz.Population / 1000.0) + 1.0);
                    distkm = ZoneSystem.Distances[iz.ZoneNumber, kz.ZoneNumber] / 1000;
                    if (distkm >= 0 && distkm <= p[2, 4])
                    {
                        sh0 = 0.0;
                        if (distkm >= 0 && distkm < 1)
                        {
                            sh0 = 1;
                        }

                        sumExp += Math.Exp((p[2, 0] * distkm) + (p[2, 1] * empT) + (p[2, 2] * pop) + (p[2, 3] * sh0));
                    }
                }

                //line.Append(sumExp);
                cdf = 0.0;
                for (int k = 0; k < flatZones.Length; k++)
                {
                    IZone kz = flatZones[k];

                    if (kz.TotalEmployment > 0)
                    {
                        empT = Math.Log((kz.TotalEmployment / 1000.0) + 1.0);
                        double pop = Math.Log((kz.Population / 1000.0) + 1.0);

                        distkm = ZoneSystem.Distances[iz.ZoneNumber, kz.ZoneNumber] / 1000;
                        if (distkm >= 0 && distkm <= p[2, 4] && kz.InternalDistance > 0)
                        {
                            sh0 = 0.0;
                            if (distkm >= 0 && distkm < 1)
                            {
                                sh0 = 1;
                            }

                            cdf += Math.Exp((p[2, 0] * distkm) + (p[2, 1] * empT) + (p[2, 2] * pop) + (p[2, 3] * sh0)) / sumExp;
                        }
                    }
                    line.Append(",");
                    line.Append(cdf);
                }

                sumExp = 0;
                for (int k = 0; k < flatZones.Length; k++)
                {
                    IZone kz = flatZones[k];

                    if (kz.InternalDistance == 0 || kz.TotalEmployment <= 0)
                    {
                        continue;
                    }

                    empR   = Math.Log((kz.RetailEmployment / 1000.0) + 1.0);
                    distkm = ZoneSystem.Distances[iz.ZoneNumber, kz.ZoneNumber] / 1000;

                    double maxDist;
                    int    index;
                    if (kz.PlanningDistrict == 1)
                    {
                        maxDist = p[3, 16]; //config <MMaxDist1>
                        index   = 16;
                    }
                    //if retail activity > 3
                    else if (ActivityDistribution.GetDistribution(kz, 0) >= 3)
                    {
                        maxDist = p[3, 17];
                        index   = 17;
                    }
                    else
                    {
                        maxDist = p[3, 18];
                        index   = 18;
                    }

                    if (distkm >= 0 && distkm <= maxDist)
                    {
                        //empR = ( kz.RetailActivityLevel / 1000 ) + 0.001;
                        sh0 = 0.0;
                        sh1 = 0.0;
                        sh2 = 0.0;
                        if (distkm >= 0 && distkm < 1)
                        {
                            sh0 = 1;
                        }
                        else if (distkm >= 1 && distkm < 2)
                        {
                            sh1 = 1;
                        }

                        switch (index)
                        {
                        case 16:
                            sumExp += Math.Exp((p[3, 0] * distkm) + (p[3, 1] * empR) + (p[3, 2] * sh0) + (p[3, 3] * sh1) + (p[3, 4] * sh2));
                            break;

                        case 17:
                            sumExp += Math.Exp(p[3, 6] + (p[3, 5] * distkm) + (p[3, 7] * empR) + (p[3, 8] * sh0) + (p[4, 9] * sh1) + (p[3, 10] * sh2));
                            break;

                        case 18:
                            sumExp += Math.Exp(p[3, 12] + (p[3, 11] * distkm) + (p[3, 13] * empR) + (p[3, 14] * sh0) + (p[3, 15] * sh1));
                            break;
                        }
                    }
                }

                //line.Append(sumExp);
                cdf = 0.0;
                for (int k = 0; k < flatZones.Length; k++)
                {
                    IZone kz = flatZones[k];

                    if (kz.TotalEmployment > 0)
                    {
                        empR   = Math.Log((kz.RetailEmployment / 1000.0) + 1.0);
                        distkm = ZoneSystem.Distances[iz.ZoneNumber, kz.ZoneNumber] / 1000;

                        double maxDist;
                        int    index;
                        if (kz.PlanningDistrict == 1)
                        {
                            maxDist = p[3, 16]; //config <MMaxDist1>
                            index   = 16;
                        }
                        //if retail activity > 3
                        else if (ActivityDistribution.GetDistribution(kz, 0) >= 3)
                        {
                            maxDist = p[3, 17];
                            index   = 17;
                        }
                        else
                        {
                            maxDist = p[3, 18];
                            index   = 18;
                        }

                        if (distkm >= 0 && distkm <= maxDist && kz.InternalDistance > 0)
                        {
                            // empR = ( kz.RetailActivityLevel / 1000 ) + 0.001;
                            sh0 = 0.0;
                            sh1 = 0.0;
                            sh2 = 0.0;
                            if (distkm >= 0 && distkm < 1)
                            {
                                sh0 = 1;
                            }
                            else if (distkm >= 1 && distkm < 2)
                            {
                                sh1 = 1;
                            }

                            switch (index)
                            {
                            case 16:

                                cdf += Math.Exp((p[3, 0] * distkm) + (p[3, 1] * empR) + (p[3, 2] * sh0) + (p[3, 3] * sh1) + (p[3, 4] * sh2)) / sumExp;
                                break;

                            case 17:
                                cdf += Math.Exp(p[3, 6] + (p[3, 5] * distkm) + (p[3, 7] * empR) + (p[3, 8] * sh0) + (p[4, 9] * sh1) + (p[3, 10] * sh2)) / sumExp;
                                break;

                            case 18:
                                cdf += Math.Exp(p[3, 12] + (p[3, 11] * distkm) + (p[3, 13] * empR) + (p[3, 14] * sh0) + (p[3, 15] * sh1)) / sumExp;
                                break;
                            }
                        }
                    }
                    line.Append(",");
                    line.Append(cdf);
                }

                writer.WriteLine(line.ToString(0, line.Length - 1));
            }
            writer.Close();
            //IConfigurationDirectory directory =
            //    TashaConfiguration.GetDirectory("LocationChoiceModelParameters");
            SparseZoneCreator creator = new SparseZoneCreator(3, (4 * flatZones.Last().ZoneNumber) + 1);

            creator.LoadCsv(temp, false);
            creator.Save(LocatonChoiceModelHomeCache);
            File.Delete(temp);
        }