public void BuildLocationChoiceCacheHome(float[,] p, IZone[] flatZones) { string temp = Path.GetTempFileName(); //string temp = Directory.GetCurrentDirectory() + "\\" + "test2.csv"; Console.WriteLine( temp ); StreamWriter writer = new StreamWriter( temp ); //fire up the cache ZoneCache<IZone>.CacheSize = 8000; StringBuilder line = null; Console.WriteLine( "Building LocationChoiceHomeCache" ); 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 ) continue; else { sum++; } double distkm = 0; //sum EXP for G--------------------------------------------------- double sumExp = 0; double empP = 0; double empG = 0; double empR = 0; double sh0 = 0.0; double sh1 = 0.0; double sh2 = 0.0; double empT = 0; 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( ( (double)kz.TotalEmployment / 1000.0 ) + 1.0 ); //distance in KM distkm = this.ZoneSystem.Distances[iz.ZoneNumber, kz.ZoneNumber] / 1000; if ( distkm >= 0 && distkm <= p[0, 4] ) { 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[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( ( (double)kz.TotalEmployment / 1000.0 ) + 1.0 ); distkm = this.ZoneSystem.Distances[iz.ZoneNumber, kz.ZoneNumber] / 1000; if ( distkm >= 0 && distkm <= p[0, 4] && 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[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( ( (double)kz.TotalEmployment / 1000.0 ) + 1.0 ); distkm = this.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 = this.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( ( (double)kz.TotalEmployment / 1000.0 ) + 1.0 ); double pop = Math.Log( ( (double)kz.Population / 1000.0 ) + 1.0 ); distkm = this.ZoneSystem.Distances[iz.ZoneNumber, kz.ZoneNumber] / 1000; if ( distkm >= 0 && distkm <= p[2, 4] ) { empP = ( kz.ProfessionalEmployment / 1000 ) + 0.001; empG = ( kz.GeneralEmployment / 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; 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( ( (double)kz.TotalEmployment / 1000.0 ) + 1.0 ); double pop = Math.Log( ( (double)kz.Population / 1000.0 ) + 1.0 ); distkm = this.ZoneSystem.Distances[iz.ZoneNumber, kz.ZoneNumber] / 1000; if ( distkm >= 0 && distkm <= p[2, 4] && 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[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( ( (double)kz.RetailEmployment / 1000.0 ) + 1.0 ); distkm = this.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( ( (double)kz.RetailEmployment / 1000.0 ) + 1.0 ); distkm = this.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( this.LocatonChoiceModelHomeCache ); File.Delete( temp ); }
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 ); }