예제 #1
0
        private void Generate(int node_count, int edge_multiplier)
        {
            Multiplier = edge_multiplier;
            Edges      = new List <Edge>();

            // Generating LCD diagram
            int m = node_count * edge_multiplier,
                l = 2 * m;                          // Length of lcd to create G(n, k)
            List <int> alphabet = new List <int>(); // Alphabet to get random nonreccuring integers from it

            for (int i = 0; i < l; i++)
            {
                alphabet.Add(i);
            }
            int[] lcd = Enumerable.Repeat(-1, l).ToArray(); // The main lcd diagram array
            for (int i = 0; i < m; i++)
            {
                // Searching random values from the alphabet for both starting and ending of lcd-edge
                int r1 = alphabet[ilRandom.Next(0, alphabet.Count)];
                alphabet.Remove(r1);
                //ilRandom.Next(); // Do not rly need, but w/o it lcd diagrams seems too similar
                int r2 = alphabet[ilRandom.Next(0, alphabet.Count)];
                alphabet.Remove(r2);
                lcd[Math.Max(r1, r2)] = Math.Min(r1, r2);
            }

            // Corresponding lcd-nodes to network-nodes
            int[] nodeMarker = new int[l];
            int   kCnt       = 0,
                  globalNode = 0;

            for (int i = 0; i < l; i++)
            {
                nodeMarker[i] = globalNode;
                if (lcd[i] > -1 && ++kCnt == edge_multiplier)
                {
                    globalNode++;
                    kCnt = 0;
                }
            }

            // Corresponding lcd-edges to network-edges
            for (int i = 0; i < l; i++)
            {
                if (lcd[i] == -1)
                {
                    continue;
                }
                Edge edg = new Edge(nodeMarker[i], nodeMarker[lcd[i]], 1);
                if (!Edges.Any(x => { return(x.From == edg.From && x.To == edg.To); }))
                {
                    Edges.Add(edg);
                }
                else
                {
                    Edges.Find(x => { return(x.From == edg.From && x.To == edg.To); }).Weight++;
                }
            }
            NodesRecalculate();
        }
 private void GenerateTestNetwork()
 {
     while (progress + inProgress < tests && !cancel.IsCancellationRequested)
     {
         inProgress++;
         SFNetwork             nw   = new SFNetwork(nodes, mlt, rnd.Next());
         Dictionary <int, int> stat = new Dictionary <int, int>();
         for (int i = 0; i < nw.Edges.Count; i++)
         {
             Edge edg = nw.Edges[i];
             if (!stat.ContainsKey(edg.From))
             {
                 stat.Add(edg.From, 0);
             }
             stat[edg.From] += edg.Weight;
             if (edg.From != edg.To)
             {
                 if (!stat.ContainsKey(edg.To))
                 {
                     stat.Add(edg.To, 0);
                 }
                 stat[edg.To] += edg.Weight;
             }
         }
         lock (stats)
         {
             for (int i = 0; i < stats.Count; i++)
             {
                 stats[i] = (stats[i] * nwCounter + stat[i]) / (nwCounter + 1);
             }
             nwCounter++;
             progress++;
             inProgress--;
             //Console.WriteLine("{0}/{1}\tTime:\t{2}", progress, tests, sw.Elapsed);
         }
         timeRem = TimeSpan.FromMilliseconds(sw.ElapsedMilliseconds / progress * (tests - progress));
     }
     //Console.WriteLine("Finished!");
 }