/// <summary>
        /// Utility method, for creating new layer in the multilayer network.
        /// </summary>
        /// <param name="mnet">Multilayer network.</param>
        /// <param name="newLayerName">Name of the new layer.</param>
        /// <param name="layers">Layers of the network.</param>
        /// <param name="forceDirected">True if new edges should be directed, false if not.</param>
        /// <param name="forceActors">True if all actors should be on the new layer, false if not.</param>
        /// <returns>Newly created layer.</returns>
        private Layer createLayer(MultilayerNetwork mnet, string newLayerName, HashSet <Layer> layers,
                                  bool forceDirected, bool forceActors)
        {
            var directed = forceDirected;

            // Check if there are any directed layers, if YES, then new layer will be directed as well.
            if (!directed)
            {
                foreach (var layer1 in layers)
                {
                    foreach (var layer2 in layers)
                    {
                        if (mnet.IsDirected(layer1, layer2))
                        {
                            directed = true;
                            break;
                        }
                    }

                    if (directed)
                    {
                        break;
                    }
                }
            }

            var dir      = directed ? EdgeDirectionality.Directed : EdgeDirectionality.Undirected;
            var newLayer = mnet.AddLayer(newLayerName, dir);

            if (newLayer == null)
            {
                throw new ArgumentException("Layer " + newLayerName + " already exists.");
            }

            if (forceActors)
            {
                foreach (var actor in mnet.GetActors())
                {
                    mnet.AddNode(actor, newLayer);
                }
            }
            else
            {
                foreach (var layer in layers)
                {
                    foreach (var node in mnet.GetNodes(layer))
                    {
                        mnet.AddNode(node.Actor, newLayer);
                    }
                }
            }

            return(newLayer);
        }
Example #2
0
        /// <summary>
        /// Returns density of the layer in the network.
        /// </summary>
        /// <param name="mnet">Multilayer network model.</param>
        /// <param name="layer">Layer to calculate density on.</param>
        /// <returns>Density of the given layer.</returns>
        public double Density(MultilayerNetwork mnet, Layer layer)
        {
            // Had to use long, because we will be calculating max. potential connections.
            // And integer wasn't enough.

            // Get all nodes on the layer.
            long nodesCount = mnet.NodesByLayer[layer.Id].Count;
            // Get all edges on the layer.
            long edgesCount = 0;

            // Check if there is this layer.
            if (mnet.EdgesByLayerPair.ContainsKey(layer.Id) && mnet.EdgesByLayerPair[layer.Id].ContainsKey(layer.Id))
            {
                edgesCount = mnet.EdgesByLayerPair[layer.Id][layer.Id].Count;
            }

            long potentialConnections = 0;

            if (mnet.IsDirected(layer, layer))
            {
                potentialConnections = nodesCount * (nodesCount - 1);
            }
            else
            {
                potentialConnections = (nodesCount * (nodesCount - 1)) / 2;
            }

            long actualConnections = edgesCount;

            if (actualConnections == 0)
            {
                return(0);
            }

            return((double)actualConnections / potentialConnections);
        }
        /// <summary>
        /// Flatten layers into single layer.
        /// </summary>
        /// <param name="mnet">Multilayer network.</param>
        /// <param name="newLayerName">Name of the new layer.</param>
        /// <param name="layers">Layers to flatten.</param>
        /// <param name="forceDirected">True if new edges should be directed, false if not.</param>
        /// <param name="forceActors">True if all actors should be on the new layer, false if not.</param>
        /// <returns>Flattened layer.</returns>
        public Layer FlattenUnweighted(MultilayerNetwork mnet, string newLayerName, HashSet <Layer> layers,
                                       bool forceDirected = false, bool forceActors = false)
        {
            var newLayer = createLayer(mnet, newLayerName, layers, forceDirected, forceActors);
            var directed = mnet.IsDirected(newLayer, newLayer);

            foreach (var layer1 in layers)
            {
                foreach (var layer2 in layers)
                {
                    foreach (var edge in mnet.GetEdges(layer1, layer2))
                    {
                        var node1   = mnet.GetNode(edge.V1.Actor, newLayer);
                        var node2   = mnet.GetNode(edge.V2.Actor, newLayer);
                        var newEdge = mnet.GetEdge(node1, node2);

                        if (newEdge == null)
                        {
                            newEdge = mnet.AddEdge(node1, node2);
                        }

                        // If new layer is directed, undirected edges must be added twice in both directions.
                        if (directed && edge.Directionality == EdgeDirectionality.Undirected)
                        {
                            newEdge = mnet.GetEdge(node2, node1);
                            if (newEdge == null)
                            {
                                newEdge = mnet.AddEdge(node2, node1);
                            }
                        }
                    }
                }
            }

            return(newLayer);
        }
Example #4
0
        private IO io = IO.Instance; //new IO();
        public void TransformationTest()
        {
            var transform = new Transformation.Transformation();

            Console.WriteLine("Transformation test begin...");
            Console.WriteLine("Creating the network...");

            var mnet   = new MultilayerNetwork("Transformation Test");
            var a1     = mnet.AddActor("a1");
            var a2     = mnet.AddActor("a2");
            var a3     = mnet.AddActor("a3");
            var a4     = mnet.AddActor("a4");
            var a5     = mnet.AddActor("a5");
            var a6     = mnet.AddActor("a6");
            var i1     = mnet.AddActor("I1");
            var i2     = mnet.AddActor("I2");
            var i3     = mnet.AddActor("I3");
            var L1     = mnet.AddLayer("People 1", EdgeDirectionality.Undirected);
            var L2     = mnet.AddLayer("People 2", EdgeDirectionality.Undirected);
            var people = new HashSet <Layer> {
                L1, L2
            };
            var L3   = mnet.AddLayer("Institutions", EdgeDirectionality.Undirected);
            var a1l1 = mnet.AddNode(a1, L1);
            var a2l1 = mnet.AddNode(a2, L1);
            var a3l1 = mnet.AddNode(a3, L1);
            var a4l1 = mnet.AddNode(a4, L1);
            var a5l1 = mnet.AddNode(a5, L1);
            var a1l2 = mnet.AddNode(a1, L2);
            var a2l2 = mnet.AddNode(a2, L2);
            var a3l2 = mnet.AddNode(a3, L2);
            var a4l2 = mnet.AddNode(a4, L2);
            var a5l2 = mnet.AddNode(a5, L2);
            var i1l3 = mnet.AddNode(i1, L3);
            var i2l3 = mnet.AddNode(i2, L3);
            var i3l3 = mnet.AddNode(i3, L3);

            mnet.AddEdge(a1l1, a2l1);
            mnet.AddEdge(a1l1, a3l1);
            mnet.AddEdge(a1l2, a2l2);
            mnet.AddEdge(a3l2, a4l2);
            mnet.AddEdge(a1l2, i1l3);
            mnet.AddEdge(a2l2, i1l3);
            mnet.AddEdge(a2l2, i2l3);
            mnet.AddEdge(a3l2, i2l3);
            mnet.AddEdge(a4l2, i2l3);
            mnet.AddEdge(a5l2, i3l3);
            Console.WriteLine("Creating network end.");


            Console.WriteLine("Flattening L1 and L2 (unweighted, only existing actors)...");
            var f1 = transform.FlattenUnweighted(mnet, "flattened1", people, false, false);

            if (mnet.IsDirected(f1, f1))
            {
                Console.WriteLine("Layer should be undirected");
            }
            if (mnet.GetNodes(f1).Count != 5)
            {
                Console.WriteLine("Wrong number of nodes");
            }
            if (mnet.GetEdges(f1, f1).Count != 3)
            {
                Console.WriteLine("Wrong number of edges");
            }
            Console.WriteLine("Done! " + mnet.ToString());
            Console.WriteLine();

            Console.WriteLine("Flattening L1 and L2 (unweighted, all actors)...");
            var f2 = transform.FlattenUnweighted(mnet, "flattened2", people, false, true);

            if (mnet.IsDirected(f2, f2))
            {
                Console.WriteLine("Layer should be undirected");
            }
            if (mnet.GetNodes(f2).Count != 9)
            {
                Console.WriteLine("Wrong number of nodes, {0}\t != {1}", mnet.GetNodes(f2).Count, 9);
            }
            if (mnet.GetEdges(f2, f2).Count != 3)
            {
                Console.WriteLine("Wrong number of edges");
            }
            Console.WriteLine("Done! " + mnet.ToString());
            Console.WriteLine();



            Console.WriteLine("Transformation test end.");
            Console.ReadLine();
        }