Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string PrintStructure(bool[,] s, out int nonZeroCount)
- {
- var output = new StringBuilder();
- int nzCount = 0;
- int n = (int) System.Math.Sqrt(s.Length);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (s[i, j])
- {
- output.Append("(X)");
- ++nzCount;
- }
- else
- output.Append(" - ");
- }
- output.AppendLine();
- }
- int upperTriangularSize = s.Length * (s.Length - 1) / 2 + s.Length;
- output.AppendFormat("n: {2}, nz: {0} ({3} bytes), sparse: {1:F10}", nzCount, nzCount / (decimal) upperTriangularSize, n * n, nzCount * 4);
- nonZeroCount = nzCount;
- return output.ToString();
- }
- void FillRandom(int[] a, int max, Random rng)
- {
- for (int i = 0; i < a.Length; i++)
- a[i] = rng.Next(0, max);
- }
- void Shuffle(int[] a, Random rng)
- {
- for (int i = 0; i < a.Length; ++i)
- {
- var j = rng.Next(i, a.Length - 1);
- var tmp = a[i];
- a[i] = a[j];
- a[j] = tmp;
- }
- }
- var rng = new Random();
- var nodes = 50;
- var links = 100;
- var ind = new int[nodes];
- var isrc = new int[links];
- var idst = new int[links];
- var structure = new bool[nodes, nodes];
- for (int i = 0; i < nodes; i++)
- ind[i] = i;
- FillRandom(isrc, nodes, rng);
- FillRandom(idst, nodes, rng);
- int nzCountMin = int.MaxValue;
- int nzCountMax = 0;
- int nzTries = 0;
- long nzCountSum = 0;
- double nzCountSqSum = 0;
- var log = new StringBuilder();
- int nzCount;
- for (int i = 0; i < 100; ++i)
- {
- Shuffle(ind, rng);
- var scheme = new SPAirScheme();
- scheme.Init(null);
- CreateScheme(scheme, ind, isrc, idst);
- scheme.SchemeMatrixStructure(structure);
- var schemeStructure = PrintStructure(structure, out nzCount);
- scheme.SolutionMatrixStructure(structure);
- var solutionStructure = PrintStructure(structure, out nzCount);
- nzCountMin = System.Math.Min(nzCount, nzCountMin);
- nzCountMax = System.Math.Max(nzCount, nzCountMax);
- nzCountSum += nzCount;
- nzCountSqSum += nzCount * nzCount;
- ++nzTries;
- if (nzCount < nzCountSum / (double) nzTries)
- {
- log.Append(schemeStructure);
- log.Append("\n\n");
- log.Append(solutionStructure);
- log.Append("\n\n");
- }
- }
- log.AppendFormat("NZ stats ({0} probes): min: {1}, max: {2}, avg: {3}, squared average: {4}",
- nzTries,
- nzCountMin,
- nzCountMax,
- nzCountSum / nzTries,
- (int) System.Math.Sqrt(nzCountSqSum / nzTries));
- mainText.Text = log.ToString();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement