/// <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); }
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(); }