Advertisement
agmike

Sparse Scheme Matrix Generator Code

Nov 7th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | None | 0 0
  1.         string PrintStructure(bool[,] s, out int nonZeroCount)
  2.         {
  3.             var output = new StringBuilder();
  4.             int nzCount = 0;
  5.             int n = (int) System.Math.Sqrt(s.Length);
  6.             for (int i = 0; i < n; i++)
  7.             {
  8.                 for (int j = 0; j < n; j++)
  9.                 {
  10.                     if (s[i, j])
  11.                     {
  12.                         output.Append("(X)");
  13.                         ++nzCount;
  14.                     }
  15.                     else
  16.                         output.Append(" - ");
  17.                 }
  18.                 output.AppendLine();
  19.             }
  20.  
  21.             int upperTriangularSize = s.Length * (s.Length - 1) / 2 + s.Length;
  22.             output.AppendFormat("n: {2}, nz: {0} ({3} bytes), sparse: {1:F10}", nzCount, nzCount / (decimal) upperTriangularSize, n * n, nzCount * 4);
  23.  
  24.             nonZeroCount = nzCount;
  25.             return output.ToString();
  26.         }
  27.  
  28.         void FillRandom(int[] a, int max, Random rng)
  29.         {
  30.             for (int i = 0; i < a.Length; i++)
  31.                 a[i] = rng.Next(0, max);
  32.         }
  33.  
  34.         void Shuffle(int[] a, Random rng)
  35.         {
  36.             for (int i = 0; i < a.Length; ++i)
  37.             {
  38.                 var j = rng.Next(i, a.Length - 1);
  39.                 var tmp = a[i];
  40.                 a[i] = a[j];
  41.                 a[j] = tmp;
  42.             }
  43.         }
  44.        
  45.        
  46.             var rng = new Random();
  47.             var nodes = 50;
  48.             var links = 100;
  49.             var ind = new int[nodes];
  50.             var isrc = new int[links];
  51.             var idst = new int[links];
  52.             var structure = new bool[nodes, nodes];
  53.  
  54.             for (int i = 0; i < nodes; i++)
  55.                 ind[i] = i;
  56.  
  57.             FillRandom(isrc, nodes, rng);
  58.             FillRandom(idst, nodes, rng);
  59.  
  60.             int nzCountMin = int.MaxValue;
  61.             int nzCountMax = 0;
  62.             int nzTries = 0;
  63.             long nzCountSum = 0;
  64.             double nzCountSqSum = 0;
  65.             var log = new StringBuilder();
  66.             int nzCount;
  67.  
  68.             for (int i = 0; i < 100; ++i)
  69.             {
  70.                 Shuffle(ind, rng);
  71.                 var scheme = new SPAirScheme();
  72.                 scheme.Init(null);
  73.                 CreateScheme(scheme, ind, isrc, idst);
  74.                 scheme.SchemeMatrixStructure(structure);
  75.                 var schemeStructure = PrintStructure(structure, out nzCount);
  76.                 scheme.SolutionMatrixStructure(structure);
  77.                 var solutionStructure = PrintStructure(structure, out nzCount);
  78.  
  79.                 nzCountMin = System.Math.Min(nzCount, nzCountMin);
  80.                 nzCountMax = System.Math.Max(nzCount, nzCountMax);
  81.                 nzCountSum += nzCount;
  82.                 nzCountSqSum += nzCount * nzCount;
  83.                 ++nzTries;
  84.  
  85.                 if (nzCount < nzCountSum / (double) nzTries)
  86.                 {
  87.                     log.Append(schemeStructure);
  88.                     log.Append("\n\n");
  89.                     log.Append(solutionStructure);
  90.                     log.Append("\n\n");
  91.                 }
  92.             }
  93.  
  94.             log.AppendFormat("NZ stats ({0} probes): min: {1}, max: {2}, avg: {3}, squared average: {4}",
  95.                              nzTries,
  96.                              nzCountMin,
  97.                              nzCountMax,
  98.                              nzCountSum / nzTries,
  99.                              (int) System.Math.Sqrt(nzCountSqSum / nzTries));
  100.  
  101.             mainText.Text = log.ToString();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement