public void GetUncorrelatedBetweennessPrefMatrixTest()
        {
            TemporalNetwork          temp_net   = ExampleData.GetTestNetwork();
            Dictionary <string, int> index_pred = null;
            Dictionary <string, int> index_succ = null;

            double[,] actual = null;
            actual           = BetweennessPref.GetUncorrelatedBetweennessPrefMatrix(temp_net, "e", out index_pred, out index_succ);

            Assert.IsNotNull(index_pred);
            Assert.IsNotNull(index_succ);
            Assert.IsNotNull(actual);

            // Check whether all entries in the normalized betweenness preference matrix returned match the expected values
            Assert.AreEqual(actual[index_pred["a"], index_succ["b"]], (2d / 11d) * (1d / 10d));
            Assert.AreEqual(actual[index_pred["b"], index_succ["b"]], (1d / 11d) * (1d / 10d));
            Assert.AreEqual(actual[index_pred["c"], index_succ["b"]], (6d / 11d) * (1d / 10d));
            Assert.AreEqual(actual[index_pred["f"], index_succ["b"]], (1d / 11d) * (1d / 10d));
            Assert.AreEqual(actual[index_pred["g"], index_succ["b"]], (1d / 11d) * (1d / 10d));

            Assert.AreEqual(actual[index_pred["a"], index_succ["f"]], (2d / 11d) * (6d / 10d));
            Assert.AreEqual(actual[index_pred["b"], index_succ["f"]], (1d / 11d) * (6d / 10d));
            Assert.AreEqual(actual[index_pred["c"], index_succ["f"]], (6d / 11d) * (6d / 10d));
            Assert.AreEqual(actual[index_pred["f"], index_succ["f"]], (1d / 11d) * (6d / 10d));
            Assert.AreEqual(actual[index_pred["g"], index_succ["f"]], (1d / 11d) * (6d / 10d));

            Assert.AreEqual(actual[index_pred["a"], index_succ["g"]], (2d / 11d) * (3d / 10d));
            Assert.AreEqual(actual[index_pred["b"], index_succ["g"]], (1d / 11d) * (3d / 10d));
            Assert.AreEqual(actual[index_pred["c"], index_succ["g"]], (6d / 11d) * (3d / 10d));
            Assert.AreEqual(actual[index_pred["f"], index_succ["g"]], (1d / 11d) * (3d / 10d));
            Assert.AreEqual(actual[index_pred["g"], index_succ["g"]], (1d / 11d) * (3d / 10d));
        }
        public void BWPTest()
        {
            TemporalNetwork net = ExampleData.GetTestNetwork6();

            Dictionary <string, int> index_pred = null;
            Dictionary <string, int> index_succ = null;

            double[,] actual = null;
            actual           = BetweennessPref.GetBetweennessPrefMatrix(net, "B", out index_pred, out index_succ);

            double bwp = BetweennessPref.GetBetweennessPref(net, "B");
        }
        public void GetBetweennessPrefMatrixTest()
        {
            TemporalNetwork          temp_net   = ExampleData.GetTestNetwork();
            Dictionary <string, int> index_pred = null;
            Dictionary <string, int> index_succ = null;

            double[,] actual;
            actual = BetweennessPref.GetBetweennessPrefMatrix(temp_net, "e", out index_pred, out index_succ);

            Assert.IsNotNull(index_pred);
            Assert.IsNotNull(index_succ);
            Assert.IsNotNull(actual);

            // Check whether all entries in the normalized betweenness preference matrix returned match the expected values
            Assert.AreEqual(0d, actual[index_pred["a"], index_succ["b"]]);
            Assert.AreEqual(0d, actual[index_pred["b"], index_succ["b"]]);
            Assert.AreEqual(0d, actual[index_pred["c"], index_succ["b"]]);
            Assert.AreEqual(1d / 10d, actual[index_pred["f"], index_succ["b"]]);
            Assert.AreEqual(0d, actual[index_pred["g"], index_succ["b"]]);

            Assert.AreEqual(actual[index_pred["a"], index_succ["f"]], 0d);
            Assert.AreEqual(actual[index_pred["b"], index_succ["f"]], 0d);
            Assert.AreEqual(actual[index_pred["c"], index_succ["f"]], 11d / 20d);
            Assert.AreEqual(actual[index_pred["f"], index_succ["f"]], 0d);
            Assert.AreEqual(actual[index_pred["g"], index_succ["f"]], 1d / 20d);

            Assert.AreEqual(actual[index_pred["a"], index_succ["g"]], 2d / 10d);
            Assert.AreEqual(actual[index_pred["b"], index_succ["g"]], 1d / 10d);
            Assert.AreEqual(actual[index_pred["c"], index_succ["g"]], 0d);
            Assert.AreEqual(actual[index_pred["f"], index_succ["g"]], 0d);
            Assert.AreEqual(actual[index_pred["g"], index_succ["g"]], 0d);

            Assert.AreEqual(BetweennessPref.GetBetweennessPref(temp_net, "e"), 1.2954618442383219d);

            actual = BetweennessPref.GetBetweennessPrefMatrix(temp_net, "f", out index_pred, out index_succ);

            Assert.IsNotNull(index_pred);
            Assert.IsNotNull(index_succ);
            Assert.IsNotNull(actual);

            Assert.AreEqual(actual[index_pred["e"], index_succ["e"]], 1d);
        }
        /// <summary>
        /// Parallely computes the betweenness preference distribution of a given temporal network
        /// </summary>
        /// <param name="args"></param>
        public static void Run(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: TempNet distribution [temporal_network_file] [output_file] [aggregationWndow=1] [undirected=false]");
                return;
            }
            string out_file = args[2];

            if (!CmdlTools.PromptExistingFile(out_file))
            {
                Console.WriteLine("User opted to exit.");
                return;
            }

            bool undirected = false;
            int aggregationWindow = 1;            
            if (args.Length >= 4)
                aggregationWindow = int.Parse(args[3]);
            if (args.Length >= 5)
                undirected = Boolean.Parse(args[4]);


            Console.Write("Reading temporal network as {0} network...", undirected ? "undirected" : "directed");
            TemporalNetwork temp_net = TemporalNetwork.ReadFromFile(args[1], undirected:undirected);
            Console.WriteLine(" done.");

            Console.WriteLine("Applying aggregation window, length = {0}, time steps before = {1}", aggregationWindow, temp_net.Length);
            temp_net.AggregateTime(aggregationWindow);
            Console.WriteLine("done, time steps after = {0}", temp_net.Length);

            Console.Write("Preparing temporal network ...");
            temp_net.ReduceToTwoPaths();
            Console.WriteLine(" done.");

            double nodeNum = temp_net.AggregateNetwork.VertexCount;
            double current = 0d;
            double last_perc = 0d;

            Console.WriteLine("Computing betweenness preference for {0} nodes ...", nodeNum);

            // Parallely compute betweenness preference for all nodes
#if DEBUG 
            foreach(string v in temp_net.AggregateNetwork.Vertices)
#else
            Parallel.ForEach<string>(temp_net.AggregateNetwork.Vertices, v =>
#endif
            {
                double betweennessPref = BetweennessPref.GetBetweennessPref(temp_net, v);

                // Synchronized access to file and to counters ... 
                if(temp_net.AggregateNetwork.GetIndeg(v)>0 && temp_net.AggregateNetwork.GetOutdeg(v)>0)
                    lock (out_file)
                    {
                        System.IO.File.AppendAllText(out_file, v + " " + string.Format(System.Globalization.CultureInfo.GetCultureInfo("en-US").NumberFormat, "{0:0.000000}\n", betweennessPref));
                        current++;
                        if (100 * current / nodeNum >= last_perc + 5d)
                        {
                            last_perc = 100 * current / nodeNum;
                            Console.WriteLine("Completed for {0} nodes [{1:0.0} %]", current, last_perc);
                        }
                    }
            }
#if !DEBUG
                );
#endif
            Console.WriteLine("done.");
        }
示例#5
0
        static void Main(string[] args)
        {
            /// Create a simple temporal network consisting of 11 repeated two-paths (this network corresponds two Fig. 2 (right part) in the paper)
            TemporalNetwork temporal_net = new TemporalNetwork();

            int k = 0;

            for (int i = 0; i < 100; i++)
            {
                // Add edges according to two paths
                temporal_net.AddTemporalEdge(k++, "c", "e");
                temporal_net.AddTemporalEdge(k++, "e", "f");

                temporal_net.AddTemporalEdge(k++, "a", "e");
                temporal_net.AddTemporalEdge(k++, "e", "g");

                temporal_net.AddTemporalEdge(k++, "c", "e");
                temporal_net.AddTemporalEdge(k++, "e", "f");

                temporal_net.AddTemporalEdge(k++, "a", "e");
                temporal_net.AddTemporalEdge(k++, "e", "g");

                temporal_net.AddTemporalEdge(k++, "c", "e");
                temporal_net.AddTemporalEdge(k++, "e", "f");

                // Note that the next added edge additionally continues a two-path e -> f -> e
                temporal_net.AddTemporalEdge(k++, "f", "e");
                temporal_net.AddTemporalEdge(k++, "e", "b");

                // An additional edge that should be filtered during preprocessing ...
                temporal_net.AddTemporalEdge(k++, "e", "b");

                // And one case where we have multiple edges in a single time step
                temporal_net.AddTemporalEdge(k, "g", "e");
                temporal_net.AddTemporalEdge(k++, "c", "e");
                temporal_net.AddTemporalEdge(k++, "e", "f");

                temporal_net.AddTemporalEdge(k++, "b", "e");
                temporal_net.AddTemporalEdge(k++, "e", "g");

                temporal_net.AddTemporalEdge(k++, "c", "e");
                temporal_net.AddTemporalEdge(k++, "e", "f");

                temporal_net.AddTemporalEdge(k++, "c", "e");
                temporal_net.AddTemporalEdge(k++, "e", "f");
            }

            // Preprocess two-paths and the aggregate network of the temporal network, if this is skipped it will be done automatically when the aggregate network is computed for the first time
            Console.Write("Preparing temporal network...");
            temporal_net.ReduceToTwoPaths();
            Console.WriteLine(" done.");

            // Aggregate the temporal network
            WeightedNetwork aggregate_net = temporal_net.AggregateNetwork;

            // Compute and output betweenness preference of v
            Console.WriteLine("Betw. pref. of e in empirical network = \t\t{0:0.00000}",
                              BetweennessPref.GetBetweennessPref(temporal_net, "e"));

            // Create a random temporal network which only preserves the aggregate network (and destroys bet. pref.)
            TemporalNetwork microstate_random = TemporalNetworkEnsemble.ShuffleEdges(temporal_net, temporal_net.Length);

            microstate_random.ReduceToTwoPaths();

            // Create a random temporal network that preserves both the aggregate network and betw. pref.
            TemporalNetwork microstate_betweennessPref = TemporalNetworkEnsemble.ShuffleTwoPaths(temporal_net, temporal_net.Length);

            microstate_betweennessPref.ReduceToTwoPaths();

            // Compute and output betweenness preference of v in random temporal network
            Console.WriteLine("Betw. pref. of e with shuffled edges = \t\t\t{0:0.00000}",
                              BetweennessPref.GetBetweennessPref(microstate_random, "e"));

            // Compute and output betweenness preference of v in temporal network preserving betw. pref.
            Console.WriteLine("Betw. pref. of e with shuffled two paths = \t\t{0:0.00000}",
                              BetweennessPref.GetBetweennessPref(microstate_betweennessPref, "e"));

            // Compute the betweenness preference matrices of the networks
            Dictionary <string, int> ind_pred;
            Dictionary <string, int> ind_succ;

            double[,] m1 = BetweennessPref.GetBetweennessPrefMatrix(temporal_net, "e", out ind_pred, out ind_succ, normalized: false);

            double[,] m2 = BetweennessPref.GetBetweennessPrefMatrix(microstate_betweennessPref, "e", out ind_pred, out ind_succ, normalized: false);

            // Get the betweenness preference distribution
            IEnumerable <double> dist = BetweennessPref.GetBetweennessPrefDist(temporal_net);
        }