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);
        }
        public void Start()
        {
            ZoneSystem.LoadData();
            ActivityDistribution.LoadDistributions(ActivityLevels, ZoneSystem.ZoneArray);

            //G = office
            //M = Manufacturing
            //S = Retail
            //P = Professional

            char[] separators = { ',' };

            string[] BGP = BGPString.Split(separators);
            string[] BMP = BMPString.Split(separators);
            string[] BSP = BSPString.Split(separators);
            string[] BPP = BPPString.Split(separators);
            string[] MAP = MAPString.Split(separators);
            string[] MBP = MBPString.Split(separators);
            string[] MOP = MOPString.Split(separators);
            string[] MMP = MMPString.Split(separators);

            string[][] workParams = { BGP, BMP, BSP, BPP };

            float[,] parArray  = new float[4, 7];
            float[,] parArray2 = new float[4, 19];

            ///////////////////////////WORK PARAMS/////////////////////////

            for (int i = 0; i < workParams.Length; i++)
            {
                if (i <= 1)
                {
                    for (int j = 0; j <= 6; j++)
                    {
                        parArray[i, j] = float.Parse(workParams[i][j]);
                    }
                }
                else
                {
                    for (int j = 0; j <= 5; j++)
                    {
                        parArray[i, j] = float.Parse(workParams[i][j]);
                    }
                }
            }

            /////////////////////////////HOMEPARAMS//////////////////

            for (int j = 0; j < 5; j++)
            {
                parArray2[0, j] = float.Parse(MAP[j]);
                parArray2[2, j] = float.Parse(MOP[j]);
            }

            for (int j = 0; j < 6; j++)
            {
                parArray2[1, j] = float.Parse(MBP[j]);
            }

            for (int j = 0; j < MMP.Length; j++)
            {
                parArray2[3, j] = float.Parse(MMP[j]);
            }

            parArray2[3, 16] = MMMaxDist1;
            parArray2[3, 17] = MMMaxDist2;
            parArray2[3, 18] = MMMaxDist3;

            IZone[] flatZones = ZoneSystem.ZoneArray.GetFlatData();

            BuildLocationChoiceCache(parArray, flatZones);
            BuildLocationChoiceCacheHome(parArray2, flatZones);
            ZoneSystem.UnloadData();
        }
示例#3
0
 /// <summary>
 /// Loads all of the scheduler distributions
 /// </summary>
 private void LoadDistributions()
 {
     ActivityDistribution.LoadDistributions(this.ActivityLevelsLocal, this.TashaRuntime.ZoneSystem.ZoneArray);
     Distribution.InitializeDistributions();
 }