public void OpenFile(string fileName)
        {
            //graph where the vertices and edges should be put in
            var graph = new CompoundGraph <object, IEdge <object> >();

            try
            {
                //open the file of the graph
                var reader = XmlReader.Create(fileName);

                //create the serializer
                var serializer = new GraphMLDeserializer <object, IEdge <object>, CompoundGraph <object, IEdge <object> > >();


                //deserialize the graph
                serializer.Deserialize(reader, graph,
                                       id => id, (source, target, id) => new Edge <object>(source, target)
                                       );
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            layout.Graph = graph;
            layout.UpdateLayout();
        }
        private double EvaluateEdgeLength(
            CompoundGraph <object, IEdge <object> > compoundGraph,
            CompoundFDPLayoutAlgorithm <object, IEdge <object>, CompoundGraph <object, IEdge <object> > > algorithm,
            Dictionary <object, Size> sizes,
            double idealEdgeLength,
            double nestingFactor)
        {
            double edgeLengthError = 0.0;

            foreach (var edge in compoundGraph.Edges)
            {
                var uPos  = algorithm.VertexPositions[edge.Source];
                var vPos  = algorithm.VertexPositions[edge.Target];
                var uSize = sizes[edge.Source];
                var vSize = sizes[edge.Target];

                var uPoint = LayoutUtil.GetClippingPoint(uSize, uPos, vPos);
                var vPoint = LayoutUtil.GetClippingPoint(vSize, vPos, uPos);

                double length      = (uPoint - vPoint).Length;
                bool   isInterEdge = compoundGraph.GetParent(edge.Source) != compoundGraph.GetParent(edge.Target);
                var    iel         = isInterEdge
                              ? idealEdgeLength *
                                     (algorithm.LevelOfVertex(edge.Source) + algorithm.LevelOfVertex(edge.Target) + 1) *
                                     nestingFactor
                              : idealEdgeLength;
                double err = Math.Pow(length - iel, 2);
                edgeLengthError += err;
            }
            return(edgeLengthError);
        }
Beispiel #3
0
        public TestCompoundLayout()
        {
            InitializeComponent();

            var g = new CompoundGraph <object, IEdge <object> >();

            var vertices = new string[30];

            for (int i = 0; i < 30; i++)
            {
                vertices[i] = i.ToString();
                g.AddVertex(vertices[i]);
            }

            for (int i = 6; i < 15; i++)
            {
                g.AddChildVertex(vertices[i % 5], vertices[i]);
            }
            g.AddChildVertex(vertices[5], vertices[4]);
            g.AddChildVertex(vertices[5], vertices[2]);
            g.AddChildVertex(vertices[16], vertices[0]);
            g.AddChildVertex(vertices[16], vertices[1]);
            g.AddChildVertex(vertices[16], vertices[3]);
            g.AddChildVertex(vertices[16], vertices[20]);
            g.AddChildVertex(vertices[16], vertices[21]);
            g.AddChildVertex(vertices[16], vertices[22]);
            g.AddChildVertex(vertices[16], vertices[23]);
            g.AddChildVertex(vertices[16], vertices[24]);
            g.AddChildVertex(vertices[4], vertices[25]);
            g.AddChildVertex(vertices[4], vertices[26]);
            g.AddChildVertex(vertices[4], vertices[27]);

            g.AddEdge(new Edge <object>(vertices[0], vertices[1]));
            g.AddEdge(new Edge <object>(vertices[0], vertices[2]));
            g.AddEdge(new Edge <object>(vertices[2], vertices[4]));
            g.AddEdge(new Edge <object>(vertices[0], vertices[7]));
            g.AddEdge(new Edge <object>(vertices[8], vertices[7]));
            //g.AddEdge(new Edge<object>(vertices[13], vertices[12]));
            g.AddEdge(new Edge <object>(vertices[3], vertices[20]));
            g.AddEdge(new Edge <object>(vertices[20], vertices[21]));
            g.AddEdge(new Edge <object>(vertices[20], vertices[22]));
            g.AddEdge(new Edge <object>(vertices[22], vertices[23]));
            g.AddEdge(new Edge <object>(vertices[23], vertices[24]));
            g.AddEdge(new Edge <object>(vertices[0], vertices[28]));
            g.AddEdge(new Edge <object>(vertices[0], vertices[29]));
            g.AddEdge(new Edge <object>(vertices[25], vertices[27]));
            g.AddEdge(new Edge <object>(vertices[26], vertices[25]));
            g.AddEdge(new Edge <object>(vertices[14], vertices[27]));
            g.AddEdge(new Edge <object>(vertices[14], vertices[26]));
            g.AddEdge(new Edge <object>(vertices[14], vertices[25]));
            g.AddEdge(new Edge <object>(vertices[26], vertices[27]));


            layout.LayoutMode                  = LayoutMode.Automatic;
            layout.LayoutAlgorithmType         = "CompoundFDP";
            layout.OverlapRemovalConstraint    = AlgorithmConstraints.Automatic;
            layout.OverlapRemovalAlgorithmType = "FSA";
            layout.HighlightAlgorithmType      = "Simple";
            layout.Graph = g;
        }
        public TestCompoundLayout()
        {
            InitializeComponent();

            var g = new CompoundGraph<object, IEdge<object>>();

            var vertices = new string[30];
            for (int i = 0; i < 30; i++)
            {
                vertices[i] = i.ToString();
                g.AddVertex(vertices[i]);
            }

            for (int i = 6; i < 15; i++)
            {
                g.AddChildVertex(vertices[i%5], vertices[i]);
            }
            g.AddChildVertex(vertices[5], vertices[4]);
            g.AddChildVertex(vertices[5], vertices[2]);
            g.AddChildVertex(vertices[16], vertices[0]);
            g.AddChildVertex(vertices[16], vertices[1]);
            g.AddChildVertex(vertices[16], vertices[3]);
            g.AddChildVertex(vertices[16], vertices[20]);
            g.AddChildVertex(vertices[16], vertices[21]);
            g.AddChildVertex(vertices[16], vertices[22]);
            g.AddChildVertex(vertices[16], vertices[23]);
            g.AddChildVertex(vertices[16], vertices[24]);
            g.AddChildVertex(vertices[4], vertices[25]);
            g.AddChildVertex(vertices[4], vertices[26]);
            g.AddChildVertex(vertices[4], vertices[27]);

            g.AddEdge(new Edge<object>(vertices[0], vertices[1]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[2]));
            g.AddEdge(new Edge<object>(vertices[2], vertices[4]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[7]));
            g.AddEdge(new Edge<object>(vertices[8], vertices[7]));
            //g.AddEdge(new Edge<object>(vertices[13], vertices[12]));
            g.AddEdge(new Edge<object>(vertices[3], vertices[20]));
            g.AddEdge(new Edge<object>(vertices[20], vertices[21]));
            g.AddEdge(new Edge<object>(vertices[20], vertices[22]));
            g.AddEdge(new Edge<object>(vertices[22], vertices[23]));
            g.AddEdge(new Edge<object>(vertices[23], vertices[24]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[28]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[29]));
            g.AddEdge(new Edge<object>(vertices[25], vertices[27]));
            g.AddEdge(new Edge<object>(vertices[26], vertices[25]));
            g.AddEdge(new Edge<object>(vertices[14], vertices[27]));
            g.AddEdge(new Edge<object>(vertices[14], vertices[26]));
            g.AddEdge(new Edge<object>(vertices[14], vertices[25]));
            g.AddEdge(new Edge<object>(vertices[26], vertices[27]));
            

            layout.LayoutMode = LayoutMode.Automatic;
            layout.LayoutAlgorithmType = "CompoundFDP";
            layout.OverlapRemovalConstraint = AlgorithmConstraints.Automatic;
            layout.OverlapRemovalAlgorithmType = "FSA";
            layout.HighlightAlgorithmType = "Simple";
            layout.Graph = g;
        }
Beispiel #5
0
        public static ICompoundGraph <TVertex, TEdge> CreateCompoundGraph <TVertex, TEdge>(
            int vertexCount,
            int edgeCount,
            [NotNull, InstantHandle] Func <int, TVertex> vertexFactory,
            [NotNull, InstantHandle] Func <TVertex, TVertex, TEdge> edgeFactory,
            [NotNull] Random random)
            where TEdge : IEdge <TVertex>
        {
            var graph = new CompoundGraph <TVertex, TEdge>(false, vertexCount);

            var verticesMap = new Dictionary <int, TVertex>();

            for (int i = 0; i < vertexCount; ++i)
            {
                TVertex vertex = vertexFactory(i);
                verticesMap[i] = vertex;
                graph.AddVertex(vertex);
            }

            for (int i = 0; i < vertexCount / 3; ++i)
            {
                int     vertexIndex1;
                int     vertexIndex2;
                TVertex vertex1;
                TVertex vertex2;
                do
                {
                    vertexIndex1 = random.Next(vertexCount);
                    vertexIndex2 = random.Next(vertexCount);
                    vertex1      = verticesMap[vertexIndex1];
                    vertex2      = verticesMap[vertexIndex2];
                } while (vertexIndex1 == vertexIndex2 ||
                         graph.IsChildVertex(vertex2));

                graph.AddChildVertex(vertex1, vertex2);
            }

            for (int i = 0; i < edgeCount; ++i)
            {
                int     childIndex;
                int     parentIndex;
                TVertex child;
                TVertex parent;
                do
                {
                    childIndex  = random.Next(vertexCount);
                    parentIndex = random.Next(vertexCount);
                    child       = verticesMap[childIndex];
                    parent      = verticesMap[parentIndex];
                } while (childIndex == parentIndex ||
                         graph.ContainsEdge(parent, child));

                // Create the edge between the 2 vertex
                graph.AddEdge(edgeFactory(parent, child));
            }

            return(graph);
        }
        private string[] InitVertices(CompoundGraph <object, IEdge <object> > g, int vertexCount)
        {
            var vertices = new string[vertexCount];

            for (int i = 0; i < vertexCount; i++)
            {
                vertices[i] = i.ToString();
                g.AddVertex(vertices[i]);
            }
            return(vertices);
        }
        private int EvaluateEdgeCrossing(
            CompoundGraph <object, IEdge <object> > compoundGraph,
            CompoundFDPLayoutAlgorithm <object, IEdge <object>, CompoundGraph <object, IEdge <object> > > algorithm,
            Dictionary <object, Size> sizes)
        {
            int crossings = 0;

            foreach (var edge in compoundGraph.Edges)
            {
                var uPos1  = algorithm.VertexPositions[edge.Source];
                var vPos1  = algorithm.VertexPositions[edge.Target];
                var uSize1 = sizes[edge.Source];
                var vSize1 = sizes[edge.Target];

                var uPoint1 = LayoutUtil.GetClippingPoint(uSize1, uPos1, vPos1);
                var vPoint1 = LayoutUtil.GetClippingPoint(vSize1, vPos1, uPos1);
                foreach (var edge2 in compoundGraph.Edges)
                {
                    if (edge == edge2)
                    {
                        continue;
                    }

                    var uPos2  = algorithm.VertexPositions[edge.Source];
                    var vPos2  = algorithm.VertexPositions[edge.Target];
                    var uSize2 = sizes[edge.Source];
                    var vSize2 = sizes[edge.Target];

                    var uPoint2 = LayoutUtil.GetClippingPoint(uSize2, uPos2, vPos2);
                    var vPoint2 = LayoutUtil.GetClippingPoint(vSize2, vPos2, uPos2);

                    Vector v1 = (vPoint1 - uPoint1);
                    Vector v2 = (vPoint2 - uPoint2);

                    if (v1 == v2 || v1 == -v2)
                    {
                        continue; //parallel edges
                    }
                    var t2 = (uPoint1.Y - uPoint2.Y + (uPoint2.X - uPoint1.X) * v1.Y / v1.X) / (v2.Y - v2.X * v1.Y / v1.X);
                    var t1 = (uPoint2.X - uPoint1.X + t2 * v2.X) / v1.X;

                    var p  = uPoint1 + t1 * v1;
                    var b1 = t1 > 0 && (p - uPoint1).Length < (vPoint1 - uPoint1).Length;
                    var b2 = t2 > 0 && (p - uPoint2).Length < (vPoint2 - uPoint2).Length;

                    if (b1 && b2)
                    {
                        crossings++;
                    }
                }
            }
            return(crossings);
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            CompoundGraph root = new CompoundGraph();

            root.Add(new Circle(0, 0, 0));
            root.Add(new Circle(5, 5, 5));
            root.Add(new Circle(10, 10, 10));
            root.Add(new Dot(50, 50));

            CompoundGraph comp = new CompoundGraph();

            comp.Add(new Circle(0, 0, 50));
            root.Add(comp);
            root.draw();
        }
        private IBidirectionalGraph <object, IEdge <object> > ConvertGraph(PocGraph graph)
        {
            var g = new CompoundGraph <object, IEdge <object> >();

            foreach (var item in graph.Vertices)
            {
                g.AddVertex(item.ID);
            }

            foreach (var item in graph.Edges)
            {
                g.AddEdge(new Edge <object>(item.Source.ID, item.Target.ID));
            }


            return(g);
        }
        private void EvaluateNodeDistances(
            CompoundGraph <object, IEdge <object> > compoundGraph,
            CompoundFDPLayoutAlgorithm <object, IEdge <object>, CompoundGraph <object, IEdge <object> > > algorithm,
            Dictionary <object, Size> sizes,
            out double minimalMinDistance,
            out double averageMinDistance,
            out double maximalMinDistance)
        {
            minimalMinDistance = 0.0;
            averageMinDistance = 0.0;
            maximalMinDistance = 0.0;
            double count = 0;

            foreach (var level in algorithm.Levels)
            {
                foreach (var u in level)
                {
                    double m = double.PositiveInfinity;
                    foreach (var v in level)
                    {
                        if (u == v || compoundGraph.GetParent(u) != compoundGraph.GetParent(v))
                        {
                            continue;
                        }

                        var uPoint = LayoutUtil.GetClippingPoint(sizes[u], algorithm.VertexPositions[u],
                                                                 algorithm.VertexPositions[v]);
                        var vPoint = LayoutUtil.GetClippingPoint(sizes[v], algorithm.VertexPositions[v],
                                                                 algorithm.VertexPositions[u]);
                        double distance = (uPoint - vPoint).Length;
                        m = Math.Min(m, distance);
                    }
                    if (double.IsPositiveInfinity(m))
                    {
                        continue;
                    }
                    minimalMinDistance  = Math.Min(minimalMinDistance, m);
                    averageMinDistance += m;
                    count++;
                    maximalMinDistance = Math.Max(maximalMinDistance, m);
                }
            }
        }
        private double EvaluateNodeOverlaps(
            CompoundGraph <object, IEdge <object> > compoundGraph,
            CompoundFDPLayoutAlgorithm <object, IEdge <object>, CompoundGraph <object, IEdge <object> > > algorithm,
            IDictionary <object, Size> vertexSizes)
        {
            double overlapArea = 0.0;

            foreach (var level in algorithm.Levels)
            {
                foreach (var u in level)
                {
                    foreach (var v in level)
                    {
                        if (u == v || compoundGraph.GetParent(u) != compoundGraph.GetParent(v))
                        {
                            continue;
                        }

                        var uSize     = vertexSizes[u];
                        var vSize     = vertexSizes[v];
                        var uPosition = algorithm.VertexPositions[u];
                        var vPosition = algorithm.VertexPositions[v];

                        var uRect = new Rect(uPosition, uSize);
                        var vRect = new Rect(vPosition, vSize);

                        //get the overlap size
                        uRect.Intersect(vRect);
                        if (double.IsNegativeInfinity(uRect.Width) || double.IsNegativeInfinity(uRect.Height))
                        {
                            continue;
                        }

                        overlapArea += uRect.Width * uRect.Height;
                    }
                }
            }
            return(overlapArea);
        }
        private Size EvaluateCanvasSize(
            CompoundGraph <object, IEdge <object> > compoundGraph,
            CompoundFDPLayoutAlgorithm <object, IEdge <object>, CompoundGraph <object, IEdge <object> > > algorithm,
            Dictionary <object, Size> sizes)
        {
            Point topLeft     = new Point(double.PositiveInfinity, double.PositiveInfinity);
            Point bottomRight = new Point(double.NegativeInfinity, double.NegativeInfinity);

            foreach (var v in compoundGraph.Vertices)
            {
                var pos  = algorithm.VertexPositions[v];
                var size = sizes[v];

                topLeft.X = Math.Min(topLeft.X, pos.X - size.Width / 2.0);
                topLeft.Y = Math.Min(topLeft.Y, pos.Y - size.Height / 2.0);

                bottomRight.X = Math.Max(bottomRight.X, pos.X + size.Width / 2.0);
                bottomRight.Y = Math.Max(bottomRight.Y, pos.Y + size.Height / 2.0);
            }
            var sizeVector = bottomRight - topLeft;

            return(new Size(sizeVector.X, sizeVector.Y));
        }
        private double EvaluateFitnessForGraph(CompoundFDPLayoutParameters chromosome, CompoundGraph <object, IEdge <object> > compoundGraph)
        {
            var sizes      = new Dictionary <object, Size>();
            var borders    = new Dictionary <object, Thickness>();
            var layoutType = new Dictionary <object, CompoundVertexInnerLayoutType>();

            var s = new Size(20, 20);

            foreach (var v in compoundGraph.SimpleVertices)
            {
                sizes[v] = s;
            }

            var b = new Thickness(5, 10, 5, 5);

            foreach (var v in compoundGraph.CompoundVertices)
            {
                sizes[v]      = new Size();
                borders[v]    = b;
                layoutType[v] = CompoundVertexInnerLayoutType.Automatic;
            }

            //run the compound FDP algorithm
            var algorithm =
                new CompoundFDPLayoutAlgorithm <object, IEdge <object>, CompoundGraph <object, IEdge <object> > >(
                    compoundGraph, sizes, borders, layoutType, null, chromosome);

            algorithm.Compute();

            double fitness = 0.0;

            //refresh the sizes of the compound vertices
            foreach (var v in compoundGraph.CompoundVertices)
            {
                var border          = borders[v];
                var innerCanvasSize = algorithm.InnerCanvasSizes[v];
                var size            = new Size(
                    border.Left + innerCanvasSize.Width + border.Right,
                    border.Top + innerCanvasSize.Height + border.Bottom
                    );
                sizes[v] = size;
            }

            //NODE OVERLAP
            double overlaps = EvaluateNodeOverlaps(compoundGraph, algorithm, sizes);

            /*if (overlaps > 0.0)
             *  return double.NaN;*/
            fitness += overlaps * NODE_OVERLAP_MULTIPLIER;

            //CANVAS SIZE
            Size canvasSize = EvaluateCanvasSize(compoundGraph, algorithm, sizes);

            fitness += canvasSize.Width * canvasSize.Height * CANVAS_SIZE_MULTIPLIER;

            //CANVAS RATIO
            double canvasRatio = canvasSize.Width / canvasSize.Height;

            canvasRatio = canvasRatio < 1 ? 1 / canvasRatio : canvasRatio;
            fitness    += canvasRatio * CANVAS_RATIO_MULTIPLIER;

            //NODE DISTANCES
            double minimalMinDistance, averageMinDistance, maximalMinDistance;
            double idealDistance = chromosome.IdealEdgeLength;

            EvaluateNodeDistances(compoundGraph, algorithm, sizes, out minimalMinDistance, out averageMinDistance, out maximalMinDistance);
            fitness += (averageMinDistance - idealDistance) * NODE_DISTANCE_MULTIPLIER;
            fitness += (minimalMinDistance - idealDistance) * NODE_DISTANCE_MULTIPLIER;
            fitness += (maximalMinDistance - idealDistance) * NODE_DISTANCE_MULTIPLIER;

            //EDGE LENGTH
            double edgeLength = EvaluateEdgeLength(compoundGraph, algorithm, sizes, chromosome.IdealEdgeLength, chromosome.NestingFactor);

            fitness += edgeLength * EDGE_LENGTH_MULTIPLIER;

            //EDGE CROSSING
            double edgeCrossing = EvaluateEdgeCrossing(compoundGraph, algorithm, sizes);

            fitness += edgeCrossing * EDGE_CROSSING_MULTIPLIER;

            //PHASE_LENGTH
            double phaseLength = (chromosome.Phase1Iterations + chromosome.Phase2Iterations +
                                  chromosome.Phase3Iterations) * PHASE_LENGTH_MULTIPLIER;

            fitness += phaseLength;

            return(fitness);
        }
        private void InitGraphs()
        {
            graphs = new CompoundGraph <object, IEdge <object> > [6];

            #region Big graph
            var g = new CompoundGraph <object, IEdge <object> >();

            string[] vertices = InitVertices(g, 20);

            for (int i = 6; i < 15; i++)
            {
                g.AddChildVertex(vertices[i % 5], vertices[i]);
            }
            g.AddChildVertex(vertices[5], vertices[4]);
            g.AddChildVertex(vertices[5], vertices[2]);
            g.AddChildVertex(vertices[16], vertices[0]);
            g.AddChildVertex(vertices[16], vertices[1]);
            g.AddChildVertex(vertices[16], vertices[3]);

            g.AddEdge(new Edge <object>(vertices[0], vertices[1]));
            g.AddEdge(new Edge <object>(vertices[0], vertices[2]));
            g.AddEdge(new Edge <object>(vertices[2], vertices[4]));
            g.AddEdge(new Edge <object>(vertices[0], vertices[7]));
            g.AddEdge(new Edge <object>(vertices[8], vertices[7]));
            graphs[BIG_GRAPH] = g;
            #endregion

            #region Small graph

            g        = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(g, 2);

            //add the containments

            /*g.AddChildVertex(vertices[0], vertices[1]);
            *  g.AddChildVertex(vertices[0], vertices[2]);*/

            //add the edges

            /*g.AddEdge(new Edge<object>(vertices[2], vertices[3]));
            *  g.AddEdge(new Edge<object>(vertices[3], vertices[4]));*/

            graphs[SMALL_GRAPH] = g;
            #endregion

            #region Flat graph

            g        = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(g, 10);

            g.AddEdge(new Edge <object>(vertices[0], vertices[1]));
            g.AddEdge(new Edge <object>(vertices[1], vertices[2]));
            g.AddEdge(new Edge <object>(vertices[2], vertices[3]));
            g.AddEdge(new Edge <object>(vertices[3], vertices[0]));
            g.AddEdge(new Edge <object>(vertices[1], vertices[3]));
            g.AddEdge(new Edge <object>(vertices[2], vertices[0]));

            graphs[FLAT_GRAPH] = g;
            #endregion

            #region Repulsion graph
            g        = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(g, 20);

            graphs[REP_GRAPH] = g;
            #endregion

            #region Star

            g        = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(g, 13);

            for (int i = 1; i < 13; i++)
            {
                g.AddEdge(new Edge <object>(vertices[0], vertices[i]));
            }

            for (int i = 0; i < 4; i++)
            {
                g.AddEdge(new Edge <object>(vertices[i * 3 + 1], vertices[i * 3 + 2]));
                g.AddEdge(new Edge <object>(vertices[i * 3 + 1], vertices[i * 3 + 3]));
                g.AddEdge(new Edge <object>(vertices[i * 3 + 2], vertices[i * 3 + 3]));
            }
            graphs[STAR_GRAPH] = g;
            #endregion

            #region Combined

            g        = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(g, 50);

            //add the containments
            g.AddChildVertex(vertices[0], vertices[1]);
            g.AddChildVertex(vertices[0], vertices[2]);

            //add the edges
            g.AddEdge(new Edge <object>(vertices[2], vertices[3]));
            g.AddEdge(new Edge <object>(vertices[3], vertices[4]));

            g.AddEdge(new Edge <object>(vertices[10], vertices[11]));
            g.AddEdge(new Edge <object>(vertices[11], vertices[12]));
            g.AddEdge(new Edge <object>(vertices[12], vertices[13]));
            g.AddEdge(new Edge <object>(vertices[13], vertices[10]));

            for (int i = 6; i < 15; i++)
            {
                g.AddChildVertex(vertices[i % 5 + 20], vertices[i + 20]);
            }
            g.AddChildVertex(vertices[25], vertices[24]);
            g.AddChildVertex(vertices[25], vertices[22]);
            g.AddChildVertex(vertices[36], vertices[20]);
            g.AddChildVertex(vertices[36], vertices[21]);
            g.AddChildVertex(vertices[36], vertices[23]);

            g.AddEdge(new Edge <object>(vertices[20], vertices[21]));
            g.AddEdge(new Edge <object>(vertices[20], vertices[22]));
            g.AddEdge(new Edge <object>(vertices[22], vertices[24]));
            g.AddEdge(new Edge <object>(vertices[20], vertices[27]));
            g.AddEdge(new Edge <object>(vertices[28], vertices[27]));

            graphs[COMBINED_GRAPH] = g;
            #endregion
        }
        private IBidirectionalGraph<object, IEdge<object>> ConvertGraph(PocGraph graph)
        {
            var g = new CompoundGraph<object, IEdge<object>>();

            foreach (var item in graph.Vertices)
            {
                g.AddVertex(item.ID);

            }

            foreach (var item in graph.Edges)
            {
                g.AddEdge(new Edge<object>(item.Source.ID, item.Target.ID));
            }


            return g;
        }
Beispiel #16
0
        public void Construction()
        {
            var graph = new CompoundGraph <int, Edge <int> >();

            AssertGraphProperties(graph);

            graph = new CompoundGraph <int, Edge <int> >(true);
            AssertGraphProperties(graph);

            graph = new CompoundGraph <int, Edge <int> >(false);
            AssertGraphProperties(graph, false);

            graph = new CompoundGraph <int, Edge <int> >(true, 12);
            AssertGraphProperties(graph);

            graph = new CompoundGraph <int, Edge <int> >(false, 12);
            AssertGraphProperties(graph, false);


            // From graph
            var otherGraph1 = new AdjacencyGraph <int, Edge <int> >();

            graph = new CompoundGraph <int, Edge <int> >(otherGraph1);
            AssertGraphProperties(graph);

            var otherGraph2 = new AdjacencyGraph <int, Edge <int> >(false);

            graph = new CompoundGraph <int, Edge <int> >(otherGraph2);
            AssertGraphProperties(graph, false);

            var otherNonEmptyGraph1 = new AdjacencyGraph <int, Edge <int> >();

            otherNonEmptyGraph1.AddVertexRange(new[] { 1, 2 });
            graph = new CompoundGraph <int, Edge <int> >(otherNonEmptyGraph1);
            AssertGraphProperties(graph, vertices: new[] { 1, 2 });

            var graphEdges = new[]
            {
                new Edge <int>(1, 2),
                new Edge <int>(1, 3),
                new Edge <int>(2, 3)
            };
            var otherNonEmptyGraph2 = new AdjacencyGraph <int, Edge <int> >();

            otherNonEmptyGraph2.AddVerticesAndEdgeRange(graphEdges);
            graph = new CompoundGraph <int, Edge <int> >(otherNonEmptyGraph2);
            AssertGraphProperties(graph, vertices: new[] { 1, 2, 3 }, edges: graphEdges);


            // From CompoundGraph
            var otherCompoundGraph = new CompoundGraph <int, Edge <int> >();

            graph = new CompoundGraph <int, Edge <int> >(otherCompoundGraph);
            AssertGraphProperties(graph);

            var otherNonEmptyCompoundGraph1 = new CompoundGraph <int, Edge <int> >();

            otherNonEmptyCompoundGraph1.AddVerticesAndEdgeRange(graphEdges);
            graph = new CompoundGraph <int, Edge <int> >(otherNonEmptyCompoundGraph1);
            AssertGraphProperties(graph, vertices: new[] { 1, 2, 3 }, edges: graphEdges);

            var otherNonEmptyCompoundGraph2 = new CompoundGraph <int, Edge <int> >();

            otherNonEmptyCompoundGraph2.AddVerticesAndEdgeRange(graphEdges);
            otherNonEmptyCompoundGraph2.AddChildVertex(2, 3);
            graph = new CompoundGraph <int, Edge <int> >(otherNonEmptyCompoundGraph2);
            AssertGraphProperties(graph, vertices: new[] { 1, 2, 3 }, edges: graphEdges, compoundVertices: new[] { 2 });

            var otherNonEmptyCompoundGraph3 = new CompoundGraph <int, Edge <int> >();

            otherNonEmptyCompoundGraph3.AddVerticesAndEdgeRange(graphEdges);
            otherNonEmptyCompoundGraph3.AddChildVertex(1, 2);
            otherNonEmptyCompoundGraph3.AddChildVertex(2, 3);
            graph = new CompoundGraph <int, Edge <int> >(otherNonEmptyCompoundGraph3);
            AssertGraphProperties(graph, vertices: new[] { 1, 2, 3 }, edges: graphEdges, compoundVertices: new[] { 1, 2 });

            #region Local function

            void AssertGraphProperties <TVertex, TEdge>(
                CompoundGraph <TVertex, TEdge> g,
                bool parallelEdges                     = true,
                IEnumerable <TVertex> vertices         = null,
                IEnumerable <TEdge> edges              = null,
                IEnumerable <TVertex> compoundVertices = null)
                where TEdge : IEdge <TVertex>
            {
                Assert.IsTrue(g.IsDirected);
                Assert.AreEqual(parallelEdges, g.AllowParallelEdges);

                TVertex[] verticesArray = vertices?.ToArray();
                if (vertices is null && edges is null)
                {
                    AssertEmptyGraph(g);
                }
Beispiel #17
0
        private void ShowGraph(int graphIndex)
        {
            CompoundGraph <object, IEdge <object> > g = graphs[graphIndex];

            _rectDict.Clear();
            _lineDict.Clear();
            lc.Children.Clear();
            var origo = new Ellipse();

            origo.Width       = 100;
            origo.Height      = 100;
            origo.Fill        = Brushes.Black;
            origo.OpacityMask = new RadialGradientBrush(Colors.Black, Colors.Transparent);
            Canvas.SetLeft(origo, -5);
            Canvas.SetTop(origo, -5);
            lc.Children.Add(origo);
            var sizes      = new Dictionary <object, Size>();
            var borders    = new Dictionary <object, Thickness>();
            var layoutType = new Dictionary <object, CompoundVertexInnerLayoutType>();

            var s = new Size(20, 20);

            foreach (var v in g.SimpleVertices)
            {
                sizes[v] = s;
            }

            var b = new Thickness(5, 10, 5, 5);

            foreach (var v in g.CompoundVertices)
            {
                sizes[v]      = new Size();
                borders[v]    = b;
                layoutType[v] = CompoundVertexInnerLayoutType.Automatic;
            }

            var worker = new BackgroundWorker();

            worker.WorkerReportsProgress = true;
            worker.DoWork += (sender, e) =>
            {
                //var layoutAlgorithm =
                //    new CompoundFDPLayoutAlgorithm
                //        <object, IEdge<object>, ICompoundGraph<object, IEdge<object>>>(
                //        g, sizes, borders, layoutType, null,
                //        parameters);
                //layoutAlgorithm.IterationEnded += (o, evt) =>
                //{
                //    var args = evt as TestingCompoundLayoutIterationEventArgs<object, IEdge<object>, TestingCompoundVertexInfo, object>;
                //    var positions = args.VertexPositions;
                //    var innerSizes = (args as ICompoundLayoutIterationEventArgs<object>).InnerCanvasSizes;
                //    var vertexInfos = args.VertexInfos;

                //    Dispatcher.Invoke(new Action(delegate
                //                                     {
                //                                         var pDict = new Dictionary<object, Point>();

                //                                         var compoundVerticesToCheck =
                //                                             new Queue<object>();
                //                                         var compoundVertices = new List<object>();
                //                                         var root =
                //                                             g.CompoundVertices.Where(
                //                                                 cv => g.GetParent(cv) == null);
                //                                         foreach (var r in root)
                //                                             compoundVerticesToCheck.Enqueue(r);
                //                                         while (compoundVerticesToCheck.Count > 0)
                //                                         {
                //                                             var next = compoundVerticesToCheck.Dequeue();
                //                                             if (!g.IsCompoundVertex(next))
                //                                                 continue;
                //                                             compoundVertices.Add(next);
                //                                             foreach (
                //                                                 var childrenVertex in
                //                                                     g.GetChildrenVertices(next))
                //                                             {
                //                                                 compoundVerticesToCheck.Enqueue(
                //                                                     childrenVertex);
                //                                             }
                //                                         }

                //                                         //draw the compound vertices
                //                                         foreach (var v in compoundVertices)
                //                                         {
                //                                             var size = innerSizes[v];
                //                                             size.Width += b.Left + b.Right;
                //                                             size.Height += b.Top + b.Bottom;

                //                                             var pos = positions[v];
                //                                             pDict[v] = pos;
                //                                             AddRect(v, pos, size);
                //                                         }

                //                                         //draw the simple vertices
                //                                         foreach (var v in g.SimpleVertices)
                //                                         {
                //                                             var pos = positions[v];
                //                                             pDict[v] = pos;
                //                                             AddRect(v, pos, s);
                //                                         }

                //                                         //draw the simple edges
                //                                         foreach (var edge in g.Edges)
                //                                         {
                //                                             var pos1 = pDict[edge.Source];
                //                                             var pos2 = pDict[edge.Target];
                //                                             AddLine(edge, pos1, pos2, true);
                //                                         }

                //                                         //draw the containment edges
                //                                         /*foreach (var v in g.CompoundVertices)
                //                                         {
                //                                             var pos1 = pDict[v];
                //                                             foreach (var c in g.GetChildrenVertices(v))
                //                                             {
                //                                                 var pos2 = pDict[c];
                //                                                 AddLine(c, pos1, pos2, false);
                //                                             }
                //                                         }*/

                //                                         //draw the lines of the forces
                //                                         foreach (var forceLine in _forceLines)
                //                                             lc.Children.Remove(forceLine);

                //                                         var springColor = Brushes.Orange;
                //                                         var repulsionColor = Brushes.Red;
                //                                         var gravityColor = Brushes.Green;
                //                                         var applicationColor = Brushes.Yellow;

                //                                         foreach (var kvp in vertexInfos)
                //                                         {
                //                                             var line = CreateLine(pDict[kvp.Key],
                //                                                                   kvp.Value.SpringForce,
                //                                                                   springColor);
                //                                             lc.Children.Add(line);
                //                                             _forceLines.Add(line);
                //                                             line = CreateLine(pDict[kvp.Key],
                //                                                        kvp.Value.RepulsionForce,
                //                                                        repulsionColor);
                //                                             lc.Children.Add(line);
                //                                             _forceLines.Add(line);
                //                                             line = CreateLine(pDict[kvp.Key],
                //                                                        kvp.Value.GravityForce,
                //                                                        gravityColor);
                //                                             lc.Children.Add(line);
                //                                             _forceLines.Add(line);
                //                                             line = CreateLine(pDict[kvp.Key],
                //                                                        kvp.Value.ApplicationForce,
                //                                                        applicationColor);
                //                                             lc.Children.Add(line);
                //                                             _forceLines.Add(line);
                //                                         }


                //                                         //set the position of the gravity center
                //                                         Animate(origo, Canvas.LeftProperty, args.GravitationCenter.X - origo.Width / 2.0, animationDuration);
                //                                         Animate(origo, Canvas.TopProperty, args.GravitationCenter.Y - origo.Height / 2.0, animationDuration);
                //                                         txtMessage.Text = args.Message;
                //                                     }));
                //    do
                //    {
                //        Thread.Sleep((int) animationDuration.TimeSpan.TotalMilliseconds);
                //    } while (_paused);
                //};
                //layoutAlgorithm.Compute();
            };
            worker.RunWorkerAsync();
        }
Beispiel #18
0
        private void ShowGraph(int graphIndex)
        {
            CompoundGraph <object, IEdge <object> > graph = _graphs[graphIndex];

            _rectangles.Clear();
            _lines.Clear();
            Layout.Children.Clear();

            var origo = new Ellipse
            {
                Width       = 100,
                Height      = 100,
                Fill        = System.Windows.Media.Brushes.Black,
                OpacityMask = new RadialGradientBrush(Colors.Black, Colors.Transparent)
            };

            Canvas.SetLeft(origo, -5);
            Canvas.SetTop(origo, -5);
            Layout.Children.Add(origo);

            var sizes      = new Dictionary <object, Size>();
            var borders    = new Dictionary <object, Thickness>();
            var layoutType = new Dictionary <object, CompoundVertexInnerLayoutType>();

            var size = new Size(20, 20);

            foreach (object vertex in graph.SimpleVertices)
            {
                sizes[vertex] = size;
            }

            var thickness = new Thickness(5, 10, 5, 5);

            foreach (object vertex in graph.CompoundVertices)
            {
                sizes[vertex]      = default;
                borders[vertex]    = thickness;
                layoutType[vertex] = CompoundVertexInnerLayoutType.Automatic;
            }

            var worker = new BackgroundWorker
            {
                WorkerReportsProgress = true
            };

            worker.DoWork += (sender, args) =>
            {
                var layoutAlgorithm = new CompoundFDPLayoutAlgorithm <object, IEdge <object>, ICompoundGraph <object, IEdge <object> > >(
                    graph,
                    sizes,
                    borders,
                    layoutType,
                    _parameters);

                layoutAlgorithm.IterationEnded += (s, iterationArgs) =>
                {
                    var testIterationArgs = iterationArgs as TestingCompoundLayoutIterationEventArgs <object, IEdge <object>, TestingCompoundVertexInfo, object>;

                    IDictionary <object, Point> positions = testIterationArgs?.VerticesPositions;
                    if (positions is null)
                    {
                        return;
                    }

                    IDictionary <object, Size> innerSizes = ((ICompoundLayoutIterationEventArgs <object>)testIterationArgs).InnerCanvasSizes;
                    IDictionary <object, TestingCompoundVertexInfo> verticesInfos = testIterationArgs.VerticesInfos;

                    Dispatcher.Invoke(() =>
                    {
                        var points = new Dictionary <object, Point>();

                        var compoundVerticesToCheck = new Queue <object>();
                        var compoundVertices        = new List <object>();
                        var roots = graph.CompoundVertices.Where(vertex => graph.GetParent(vertex) is null);
                        foreach (object root in roots)
                        {
                            compoundVerticesToCheck.Enqueue(root);
                        }

                        while (compoundVerticesToCheck.Count > 0)
                        {
                            object next = compoundVerticesToCheck.Dequeue();
                            if (!graph.IsCompoundVertex(next))
                            {
                                continue;
                            }

                            compoundVertices.Add(next);
                            foreach (object childrenVertex in graph.GetChildrenVertices(next))
                            {
                                compoundVerticesToCheck.Enqueue(childrenVertex);
                            }
                        }

                        // Draw the compound vertices
                        foreach (object vertex in compoundVertices)
                        {
                            Size innerSize    = innerSizes[vertex];
                            innerSize.Width  += thickness.Left + thickness.Right;
                            innerSize.Height += thickness.Top + thickness.Bottom;

                            Point pos      = positions[vertex];
                            points[vertex] = pos;
                            AddRectangle(vertex, pos, innerSize);
                        }

                        // Draw the simple vertices
                        foreach (object vertex in graph.SimpleVertices)
                        {
                            Point pos      = positions[vertex];
                            points[vertex] = pos;
                            AddRectangle(vertex, pos, size);
                        }

                        // Draw the simple edges
                        foreach (IEdge <object> edge in graph.Edges)
                        {
                            Point pos1 = points[edge.Source];
                            Point pos2 = points[edge.Target];
                            AddLine(edge, pos1, pos2, true);
                        }

                        // Draw the containment edges
                        foreach (object vertex in graph.CompoundVertices)
                        {
                            Point pos1 = points[vertex];
                            foreach (object child in graph.GetChildrenVertices(vertex))
                            {
                                Point pos2 = points[child];
                                AddLine(child, pos1, pos2, false);
                            }
                        }

                        // Draw the lines of the forces
                        foreach (Line forceLine in _forceLines)
                        {
                            Layout.Children.Remove(forceLine);
                        }

                        SolidColorBrush springColor      = System.Windows.Media.Brushes.Orange;
                        SolidColorBrush repulsionColor   = System.Windows.Media.Brushes.Red;
                        SolidColorBrush gravityColor     = System.Windows.Media.Brushes.Green;
                        SolidColorBrush applicationColor = System.Windows.Media.Brushes.Yellow;

                        foreach (KeyValuePair <object, TestingCompoundVertexInfo> pair in verticesInfos)
                        {
                            Line line = CreateLine(points[pair.Key], pair.Value.SpringForce, springColor);
                            Layout.Children.Add(line);
                            _forceLines.Add(line);

                            line = CreateLine(points[pair.Key], pair.Value.RepulsionForce, repulsionColor);
                            Layout.Children.Add(line);
                            _forceLines.Add(line);

                            line = CreateLine(points[pair.Key], pair.Value.GravityForce, gravityColor);
                            Layout.Children.Add(line);
                            _forceLines.Add(line);

                            line = CreateLine(points[pair.Key], pair.Value.ApplicationForce, applicationColor);
                            Layout.Children.Add(line);
                            _forceLines.Add(line);
                        }

                        // Set the position of the gravity center
                        Animate(
                            origo,
                            Canvas.LeftProperty,
                            testIterationArgs.GravitationCenter.X - origo.Width / 2.0,
                            _animationDuration);
                        Animate(
                            origo,
                            Canvas.TopProperty,
                            testIterationArgs.GravitationCenter.Y - origo.Height / 2.0,
                            _animationDuration);
                        TxtMessage.Text = testIterationArgs.Message;
                    });

                    do
                    {
                        Thread.Sleep((int)_animationDuration.TimeSpan.TotalMilliseconds);
                    } while (_paused);
                };

                layoutAlgorithm.Compute();
            };

            worker.RunWorkerAsync();
        }
Beispiel #19
0
        private static CompoundGraph <object, IEdge <object> >[] InitGraphs()
        {
            var graphs = new CompoundGraph <object, IEdge <object> > [6];

            #region Big graph

            var graph = new CompoundGraph <object, IEdge <object> >();

            string[] vertices = InitVertices(graph, 20);

            for (int i = 6; i < 15; ++i)
            {
                graph.AddChildVertex(vertices[i % 5], vertices[i]);
            }

            graph.AddChildVertex(vertices[5], vertices[4]);
            graph.AddChildVertex(vertices[5], vertices[2]);
            graph.AddChildVertex(vertices[16], vertices[0]);
            graph.AddChildVertex(vertices[16], vertices[1]);
            graph.AddChildVertex(vertices[16], vertices[3]);

            graph.AddEdge(new Edge <object>(vertices[0], vertices[1]));
            graph.AddEdge(new Edge <object>(vertices[0], vertices[2]));
            graph.AddEdge(new Edge <object>(vertices[2], vertices[4]));
            graph.AddEdge(new Edge <object>(vertices[0], vertices[7]));
            graph.AddEdge(new Edge <object>(vertices[8], vertices[7]));

            graphs[BigGraph] = graph;

            #endregion

            #region Small graph

            graph    = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(graph, 10);

            // Add the containements
            graph.AddChildVertex(vertices[0], vertices[1]);
            graph.AddChildVertex(vertices[0], vertices[2]);
            graph.AddChildVertex(vertices[3], vertices[4]);
            graph.AddChildVertex(vertices[3], vertices[5]);
            graph.AddChildVertex(vertices[3], vertices[6]);
            graph.AddChildVertex(vertices[3], vertices[7]);
            graph.AddChildVertex(vertices[3], vertices[8]);
            graph.AddChildVertex(vertices[3], vertices[9]);

            // Add the edges
            graph.AddEdge(new Edge <object>(vertices[2], vertices[4]));
            graph.AddEdge(new Edge <object>(vertices[1], vertices[5]));

            graphs[SmallGraph] = graph;

            #endregion

            #region Flat graph

            graph    = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(graph, 10);

            graph.AddEdge(new Edge <object>(vertices[0], vertices[1]));
            graph.AddEdge(new Edge <object>(vertices[1], vertices[2]));
            graph.AddEdge(new Edge <object>(vertices[2], vertices[3]));
            graph.AddEdge(new Edge <object>(vertices[3], vertices[0]));
            graph.AddEdge(new Edge <object>(vertices[1], vertices[3]));
            graph.AddEdge(new Edge <object>(vertices[2], vertices[0]));

            graphs[FlatGraph] = graph;

            #endregion

            #region Repulsion graph

            graph = new CompoundGraph <object, IEdge <object> >();
            InitVertices(graph, 50);

            graphs[RepGraph] = graph;

            #endregion

            #region Star

            graph    = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(graph, 13);

            for (int i = 1; i < 13; ++i)
            {
                graph.AddEdge(new Edge <object>(vertices[0], vertices[i]));
            }

            for (int i = 0; i < 4; ++i)
            {
                graph.AddEdge(new Edge <object>(vertices[i * 3 + 1], vertices[i * 3 + 2]));
                graph.AddEdge(new Edge <object>(vertices[i * 3 + 1], vertices[i * 3 + 3]));
                graph.AddEdge(new Edge <object>(vertices[i * 3 + 2], vertices[i * 3 + 3]));
            }

            graphs[StarGraph] = graph;

            #endregion

            #region Combined

            graph    = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(graph, 51);

            // Add the containements
            graph.AddChildVertex(vertices[0], vertices[1]);
            graph.AddChildVertex(vertices[0], vertices[2]);

            // Add the edges
            graph.AddEdge(new Edge <object>(vertices[2], vertices[3]));
            graph.AddEdge(new Edge <object>(vertices[3], vertices[4]));

            graph.AddEdge(new Edge <object>(vertices[10], vertices[11]));
            graph.AddEdge(new Edge <object>(vertices[11], vertices[12]));
            graph.AddEdge(new Edge <object>(vertices[12], vertices[13]));
            graph.AddEdge(new Edge <object>(vertices[13], vertices[10]));

            for (int i = 6; i < 15; ++i)
            {
                graph.AddChildVertex(vertices[i % 5 + 20], vertices[i + 20]);
            }

            graph.AddChildVertex(vertices[25], vertices[24]);
            graph.AddChildVertex(vertices[25], vertices[22]);
            graph.AddChildVertex(vertices[36], vertices[20]);
            graph.AddChildVertex(vertices[36], vertices[21]);
            graph.AddChildVertex(vertices[36], vertices[23]);

            graph.AddEdge(new Edge <object>(vertices[20], vertices[21]));
            graph.AddEdge(new Edge <object>(vertices[20], vertices[22]));
            graph.AddEdge(new Edge <object>(vertices[22], vertices[24]));
            graph.AddEdge(new Edge <object>(vertices[20], vertices[27]));
            graph.AddEdge(new Edge <object>(vertices[28], vertices[27]));

            graph.AddEdge(new Edge <object>(vertices[4], vertices[39]));
            graph.AddEdge(new Edge <object>(vertices[39], vertices[40]));
            graph.AddEdge(new Edge <object>(vertices[39], vertices[41]));
            graph.AddEdge(new Edge <object>(vertices[39], vertices[42]));
            graph.AddEdge(new Edge <object>(vertices[42], vertices[43]));
            graph.AddEdge(new Edge <object>(vertices[42], vertices[44]));

            graph.AddEdge(new Edge <object>(vertices[1], vertices[45]));
            graph.AddEdge(new Edge <object>(vertices[45], vertices[46]));
            graph.AddEdge(new Edge <object>(vertices[45], vertices[47]));
            graph.AddEdge(new Edge <object>(vertices[45], vertices[48]));
            graph.AddEdge(new Edge <object>(vertices[48], vertices[49]));
            graph.AddEdge(new Edge <object>(vertices[48], vertices[50]));

            graphs[CombinedGraph] = graph;

            #endregion

            return(graphs);
        }
 private string[] InitVertices(CompoundGraph<object, IEdge<object>> g, int vertexCount)
 {
     var vertices = new string[vertexCount];
     for (int i = 0; i < vertexCount; i++)
     {
         vertices[i] = i.ToString();
         g.AddVertex(vertices[i]);
     }
     return vertices;
 }
        private void InitGraphs()
        {
            graphs = new CompoundGraph<object, IEdge<object>>[10];

            #region Big graph
            var g = new CompoundGraph<object, IEdge<object>>();

            string[] vertices = InitVertices(g, 20);

            for (int i = 6; i < 15; i++)
            {
                g.AddChildVertex(vertices[i % 5], vertices[i]);
            }
            g.AddChildVertex(vertices[5], vertices[4]);
            g.AddChildVertex(vertices[5], vertices[2]);
            g.AddChildVertex(vertices[16], vertices[0]);
            g.AddChildVertex(vertices[16], vertices[1]);
            g.AddChildVertex(vertices[16], vertices[3]);

            g.AddEdge(new Edge<object>(vertices[0], vertices[1]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[2]));
            g.AddEdge(new Edge<object>(vertices[2], vertices[4]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[7]));
            g.AddEdge(new Edge<object>(vertices[8], vertices[7]));
            graphs[BIG_GRAPH] = g;
            #endregion

            #region Small graph

            g = new CompoundGraph<object, IEdge<object>>();
            vertices = InitVertices(g, 10);

            //add the containments
            g.AddChildVertex(vertices[0], vertices[1]);
            g.AddChildVertex(vertices[0], vertices[2]);
            g.AddChildVertex(vertices[3], vertices[4]);
            g.AddChildVertex(vertices[3], vertices[5]);
            g.AddChildVertex(vertices[3], vertices[6]);
            g.AddChildVertex(vertices[3], vertices[7]);
            g.AddChildVertex(vertices[3], vertices[8]);
            g.AddChildVertex(vertices[3], vertices[9]);

            //add the edges
            g.AddEdge(new Edge<object>(vertices[2], vertices[4]));
            g.AddEdge(new Edge<object>(vertices[1], vertices[5]));
            //g.AddEdge(new Edge<object>(vertices[0], vertices[1]));

            graphs[SMALL_GRAPH] = g;
            #endregion

            #region Flat graph

            g = new CompoundGraph<object, IEdge<object>>();
            vertices = InitVertices(g, 10);

            g.AddEdge(new Edge<object>(vertices[0], vertices[1]));
            g.AddEdge(new Edge<object>(vertices[1], vertices[2]));
            g.AddEdge(new Edge<object>(vertices[2], vertices[3]));
            g.AddEdge(new Edge<object>(vertices[3], vertices[0]));
            g.AddEdge(new Edge<object>(vertices[1], vertices[3]));
            g.AddEdge(new Edge<object>(vertices[2], vertices[0]));

            graphs[FLAT_GRAPH] = g;
            #endregion

            #region Repulsion graph
            g = new CompoundGraph<object, IEdge<object>>();
            vertices = InitVertices(g, 50);

            graphs[REP_GRAPH] = g;
            #endregion

            #region Star

            g = new CompoundGraph<object, IEdge<object>>();
            vertices = InitVertices(g, 13);

            for (int i = 1; i < 13; i++ )
                g.AddEdge(new Edge<object>(vertices[0], vertices[i]));

            for (int i = 0; i < 4; i++)
            {
                g.AddEdge(new Edge<object>(vertices[i * 3 + 1], vertices[i * 3 + 2]));
                g.AddEdge(new Edge<object>(vertices[i * 3 + 1], vertices[i * 3 + 3]));
                g.AddEdge(new Edge<object>(vertices[i * 3 + 2], vertices[i * 3 + 3]));
            }
            graphs[STAR_GRAPH] = g;
            #endregion

            #region Combined

            g = new CompoundGraph<object, IEdge<object>>();
            vertices = InitVertices(g, 51);

            //add the containments
            g.AddChildVertex(vertices[0], vertices[1]);
            g.AddChildVertex(vertices[0], vertices[2]);

            //add the edges
            g.AddEdge(new Edge<object>(vertices[2], vertices[3]));
            g.AddEdge(new Edge<object>(vertices[3], vertices[4]));

            g.AddEdge(new Edge<object>(vertices[10], vertices[11]));
            g.AddEdge(new Edge<object>(vertices[11], vertices[12]));
            g.AddEdge(new Edge<object>(vertices[12], vertices[13]));
            g.AddEdge(new Edge<object>(vertices[13], vertices[10]));

            for (int i = 6; i < 15; i++)
            {
                g.AddChildVertex(vertices[i % 5 + 20], vertices[i + 20]);
            }
            g.AddChildVertex(vertices[25], vertices[24]);
            g.AddChildVertex(vertices[25], vertices[22]);
            g.AddChildVertex(vertices[36], vertices[20]);
            g.AddChildVertex(vertices[36], vertices[21]);
            g.AddChildVertex(vertices[36], vertices[23]);

            g.AddEdge(new Edge<object>(vertices[20], vertices[21]));
            g.AddEdge(new Edge<object>(vertices[20], vertices[22]));
            g.AddEdge(new Edge<object>(vertices[22], vertices[24]));
            g.AddEdge(new Edge<object>(vertices[20], vertices[27]));
            g.AddEdge(new Edge<object>(vertices[28], vertices[27]));

            g.AddEdge(new Edge<object>(vertices[4], vertices[39]));
            g.AddEdge(new Edge<object>(vertices[39], vertices[40]));
            g.AddEdge(new Edge<object>(vertices[39], vertices[41]));
            g.AddEdge(new Edge<object>(vertices[39], vertices[42]));
            g.AddEdge(new Edge<object>(vertices[42], vertices[43]));
            g.AddEdge(new Edge<object>(vertices[42], vertices[44]));

            g.AddEdge(new Edge<object>(vertices[1], vertices[45]));
            g.AddEdge(new Edge<object>(vertices[45], vertices[46]));
            g.AddEdge(new Edge<object>(vertices[45], vertices[47]));
            g.AddEdge(new Edge<object>(vertices[45], vertices[48]));
            g.AddEdge(new Edge<object>(vertices[48], vertices[49]));
            g.AddEdge(new Edge<object>(vertices[48], vertices[50]));

            graphs[COMBINED_GRAPH] = g;
            #endregion
        }
        public ActionResult Index(float startingCaptial, float lowInterest, float highInterest, int intervals, int periods)
        {
            CompoundGraph graph = new CompoundGraph(startingCaptial, lowInterest, highInterest, intervals, periods);

            return(View(graph));
        }
        void RecGraph(MainForm frm, string file)
        {

            var g = new CompoundGraph<object, IEdge<object>>();

            var map = new Dictionary<uint, CodeBlock>();

            string[] ent = File.ReadAllText(file).Split(new string[] { "block:" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string e in ent)
            {
                string[] st = e.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                var entr = new CodeBlock();

                entr["block"] = st[0];

                for (int i = 1; i < st.Length; i++)
                {
                    if (st[i] == "{")
                        break;
                    else
                    {
                        string[] lst = st[i].Split(new char[] { ':' }, 2);
                        if (lst.Length == 2)
                        {
                            entr[lst[0]] = lst[1];
                        }
                    }
                }

                map[entr.block] = entr;
            }

            map = map.Where(x => x.Value.runs > 0).ToDictionary(x => x.Key, x => x.Value);

            long v = map.Sum(x => x.Value.cost);

            CodeBlock.gcost = v;

            double runcumsum = 0;

            var intrnode = map.Values.OrderByDescending(x => x.cost).Where(x => (runcumsum += x.cost) < 0.60 * v).Take(1000).ToList();

            var varp = intrnode.Select(x => x.hash);

            long v1 = intrnode.Sum(x => x.cost);

            foreach (var r in intrnode)
            {
                g.AddVertex(r);
            }

            var newstuff = new List<CodeBlock>();

        reloop:
            foreach (var r in intrnode)
            {
                CodeBlock va, vb;


                if (map.TryGetValue(r["pNextBlock"], out va))
                {
                    if (!g.ContainsVertex(va)) { newstuff.Add(va); g.AddVertex(va); v1 += va.cost; }

                    g.AddEdge(new Edge<object>(r, va));
                }

                if (map.TryGetValue(r["pBranchBlock"], out vb))
                {
                    if (!g.ContainsVertex(vb)) { newstuff.Add(vb); g.AddVertex(vb); v1 += vb.cost; }

                    g.AddEdge(new Edge<object>(r, vb));
                }
            }

            if (newstuff.Count != 0)
            {
                intrnode = newstuff;
                newstuff = new List<CodeBlock>();
                goto reloop;
            }

            frm.Text = "Showing " + v1 * 100 / v + "% (using " + g.VertexCount + " out of " + map.Count + ", " + g.VertexCount * 100 / map.Count + "% of blocks)";


            var ordlst = map.Values.OrderByDescending(x => x.cost).ToList();

            double rvv = 0;



            foreach (var i in ordlst)
            {
                rvv += i.cost;
                i.cumsum = rvv;
            }

            var cs = frm.chart2.Series.Add("CumSum(cost) / decrease(cost)");
            var pr = frm.chart1.Series.Add("Cost / addr");
            var bl = frm.chart1.Series.Add("gopc / addr");
            var gs = frm.chart1.Series.Add("cycl / addr");

            cs.ChartType = SeriesChartType.StepLine;

            pr.ChartType = SeriesChartType.BoxPlot;
            bl.ChartType = SeriesChartType.BoxPlot;
            gs.ChartType = SeriesChartType.BoxPlot;

            foreach (var i in ordlst)
            {
                cs.Points.AddY(i.cumsum * 100.0 / CodeBlock.gcost);
            }

            rvv = 0;
            foreach (var i in ordlst.OrderBy(x => x.pos))
            {
                rvv += i.cost;
                //if (rvv > v * 0.01)
                {
                    pr.Points.AddXY(i.pos, Math.Log(i.cost + 1));
                    bl.Points.AddXY(i.pos, Math.Log(i.guest_opcodes + 1));
                    gs.Points.AddXY(i.pos, Math.Log(i.guest_cycles / 4 + 1));
                }
            }
            /*
            var vertices = new string[30];
            
            for (int i = 0; i < 30; i++)
            {
                vertices[i] = i.ToString();
                g.AddVertex(vertices[i]);
            }
            
            for (int i = 6; i < 15; i++)
            {
                g.AddChildVertex(vertices[i % 5], vertices[i]);
            }
            g.AddChildVertex(vertices[5], vertices[4]);
            g.AddChildVertex(vertices[5], vertices[2]);
            g.AddChildVertex(vertices[16], vertices[0]);
            g.AddChildVertex(vertices[16], vertices[1]);
            g.AddChildVertex(vertices[16], vertices[3]);
            g.AddChildVertex(vertices[16], vertices[20]);
            g.AddChildVertex(vertices[16], vertices[21]);
            g.AddChildVertex(vertices[16], vertices[22]);
            g.AddChildVertex(vertices[16], vertices[23]);
            g.AddChildVertex(vertices[16], vertices[24]);
            g.AddChildVertex(vertices[4], vertices[25]);
            g.AddChildVertex(vertices[4], vertices[26]);
            g.AddChildVertex(vertices[4], vertices[27]);

            g.AddEdge(new Edge<object>(vertices[0], vertices[1]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[2]));
            g.AddEdge(new Edge<object>(vertices[2], vertices[4]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[7]));
            g.AddEdge(new Edge<object>(vertices[8], vertices[7]));
            //g.AddEdge(new Edge<object>(vertices[13], vertices[12]));
            g.AddEdge(new Edge<object>(vertices[3], vertices[20]));
            g.AddEdge(new Edge<object>(vertices[20], vertices[21]));
            g.AddEdge(new Edge<object>(vertices[20], vertices[22]));
            g.AddEdge(new Edge<object>(vertices[22], vertices[23]));
            g.AddEdge(new Edge<object>(vertices[23], vertices[24]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[28]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[29]));
            g.AddEdge(new Edge<object>(vertices[25], vertices[27]));
            g.AddEdge(new Edge<object>(vertices[26], vertices[25]));
            g.AddEdge(new Edge<object>(vertices[14], vertices[27]));
            g.AddEdge(new Edge<object>(vertices[14], vertices[26]));
            g.AddEdge(new Edge<object>(vertices[14], vertices[25]));
            g.AddEdge(new Edge<object>(vertices[26], vertices[27]));
            */

            layout.LayoutMode = LayoutMode.Automatic;
            layout.LayoutAlgorithmType = "CompoundFDP";
            layout.OverlapRemovalConstraint = AlgorithmConstraints.Automatic;
            layout.OverlapRemovalAlgorithmType = "FSA";
            layout.HighlightAlgorithmType = "Simple";

            /*
            using (FileStream w = File.Create("c:\\fail.bin"))
                g.SerializeToBinary(w);
            */

            layout.Graph = g;
        }
        public void OpenFile(string fileName)
        {
            //graph where the vertices and edges should be put in
            var graph = new CompoundGraph<object, IEdge<object>>();
        
            try
            {
                //open the file of the graph
                var reader = XmlReader.Create(fileName);

                //create the serializer
                var serializer = new GraphMLDeserializer<object, IEdge<object>, CompoundGraph<object, IEdge<object>>>();


                //deserialize the graph
                serializer.Deserialize(reader, graph,
                                       id => id, (source, target, id) => new Edge<object>(source, target)
                    );

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            layout.Graph = graph;
            layout.UpdateLayout();

        }
        private static double EvaluateFitnessForGraph(
            [NotNull] CompoundFDPLayoutParameters chromosome,
            [NotNull] CompoundGraph <object, IEdge <object> > compoundGraph)
        {
            var verticesSizes   = new Dictionary <object, Size>();
            var verticesBorders = new Dictionary <object, Thickness>();
            var layoutTypes     = new Dictionary <object, CompoundVertexInnerLayoutType>();

            var size = new Size(20, 20);

            foreach (object v in compoundGraph.SimpleVertices)
            {
                verticesSizes[v] = size;
            }

            var border = new Thickness(5, 10, 5, 5);

            foreach (object v in compoundGraph.CompoundVertices)
            {
                verticesSizes[v]   = new Size();
                verticesBorders[v] = border;
                layoutTypes[v]     = CompoundVertexInnerLayoutType.Automatic;
            }

            // Run the compound FDP algorithm
            var algorithm = new CompoundFDPLayoutAlgorithm <object, IEdge <object>, CompoundGraph <object, IEdge <object> > >(
                compoundGraph,
                verticesSizes,
                verticesBorders,
                layoutTypes,
                chromosome);

            algorithm.Compute();

            double fitness = 0.0;

            // Refresh the sizes of the compound vertices
            foreach (object v in compoundGraph.CompoundVertices)
            {
                Thickness b = verticesBorders[v];
                Size      innerCanvasSize = algorithm.InnerCanvasSizes[v];
                var       s = new Size(
                    b.Left + innerCanvasSize.Width + b.Right,
                    b.Top + innerCanvasSize.Height + b.Bottom);
                verticesSizes[v] = s;
            }

            // NODE OVERLAP
            double overlaps = EvaluateNodeOverlaps(compoundGraph, algorithm, verticesSizes);

            fitness += overlaps * NodeOverlapMultiplier;

            // CANVAS SIZE
            Size canvasSize = EvaluateCanvasSize(compoundGraph, algorithm, verticesSizes);

            fitness += canvasSize.Width * canvasSize.Height * CanvasSizeMultiplier;

            // CANVAS RATIO
            double canvasRatio = canvasSize.Width / canvasSize.Height;

            canvasRatio = canvasRatio < 1 ? 1 / canvasRatio : canvasRatio;
            fitness    += canvasRatio * CanvasRatioMultiplier;

            // NODE DISTANCES
            double idealDistance = chromosome.IdealEdgeLength;

            EvaluateNodeDistances(
                compoundGraph,
                algorithm,
                verticesSizes,
                out double minimalMinDistance,
                out double averageMinDistance,
                out double maximalMinDistance);
            fitness += (averageMinDistance - idealDistance) * NodeDistanceMultiplier;
            fitness += (minimalMinDistance - idealDistance) * NodeDistanceMultiplier;
            fitness += (maximalMinDistance - idealDistance) * NodeDistanceMultiplier;

            // EDGE LENGTH
            double edgeLength = EvaluateEdgeLength(
                compoundGraph,
                algorithm,
                verticesSizes,
                chromosome.IdealEdgeLength,
                chromosome.NestingFactor);

            fitness += edgeLength * EdgeLengthMultiplier;

            // EDGE CROSSING
            double edgeCrossing = EvaluateEdgeCrossing(compoundGraph, algorithm, verticesSizes);

            fitness += edgeCrossing * EdgeCrossingMultiplier;

            // PHASE_LENGTH
            double phaseLength = chromosome.Phase1Iterations + chromosome.Phase2Iterations + chromosome.Phase3Iterations;

            phaseLength *= PhaseLengthMultiplier;
            fitness     += phaseLength;

            return(fitness);
        }
        void RecGraph(MainForm frm, string file)
        {
            var g = new CompoundGraph <object, IEdge <object> >();

            var map = new Dictionary <uint, CodeBlock>();

            string[] ent = File.ReadAllText(file).Split(new string[] { "block:" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string e in ent)
            {
                string[] st = e.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                var entr = new CodeBlock();

                entr["block"] = st[0];

                for (int i = 1; i < st.Length; i++)
                {
                    if (st[i] == "{")
                    {
                        break;
                    }
                    else
                    {
                        string[] lst = st[i].Split(new char[] { ':' }, 2);
                        if (lst.Length == 2)
                        {
                            entr[lst[0]] = lst[1];
                        }
                    }
                }

                map[entr.block] = entr;
            }

            map = map.Where(x => x.Value.runs > 0).ToDictionary(x => x.Key, x => x.Value);

            long v = map.Sum(x => x.Value.cost);

            CodeBlock.gcost = v;

            double runcumsum = 0;

            var intrnode = map.Values.OrderByDescending(x => x.cost).Where(x => (runcumsum += x.cost) < 0.60 * v).Take(1000).ToList();

            var varp = intrnode.Select(x => x.hash);

            long v1 = intrnode.Sum(x => x.cost);

            foreach (var r in intrnode)
            {
                g.AddVertex(r);
            }

            var newstuff = new List <CodeBlock>();

reloop:
            foreach (var r in intrnode)
            {
                CodeBlock va, vb;


                if (map.TryGetValue(r["pNextBlock"], out va))
                {
                    if (!g.ContainsVertex(va))
                    {
                        newstuff.Add(va); g.AddVertex(va); v1 += va.cost;
                    }

                    g.AddEdge(new Edge <object>(r, va));
                }

                if (map.TryGetValue(r["pBranchBlock"], out vb))
                {
                    if (!g.ContainsVertex(vb))
                    {
                        newstuff.Add(vb); g.AddVertex(vb); v1 += vb.cost;
                    }

                    g.AddEdge(new Edge <object>(r, vb));
                }
            }

            if (newstuff.Count != 0)
            {
                intrnode = newstuff;
                newstuff = new List <CodeBlock>();
                goto reloop;
            }

            frm.Text = "Showing " + v1 * 100 / v + "% (using " + g.VertexCount + " out of " + map.Count + ", " + g.VertexCount * 100 / map.Count + "% of blocks)";


            var ordlst = map.Values.OrderByDescending(x => x.cost).ToList();

            double rvv = 0;



            foreach (var i in ordlst)
            {
                rvv     += i.cost;
                i.cumsum = rvv;
            }

            var cs = frm.chart2.Series.Add("CumSum(cost) / decrease(cost)");
            var pr = frm.chart1.Series.Add("Cost / addr");
            var bl = frm.chart1.Series.Add("gopc / addr");
            var gs = frm.chart1.Series.Add("cycl / addr");

            cs.ChartType = SeriesChartType.StepLine;

            pr.ChartType = SeriesChartType.BoxPlot;
            bl.ChartType = SeriesChartType.BoxPlot;
            gs.ChartType = SeriesChartType.BoxPlot;

            foreach (var i in ordlst)
            {
                cs.Points.AddY(i.cumsum * 100.0 / CodeBlock.gcost);
            }

            rvv = 0;
            foreach (var i in ordlst.OrderBy(x => x.pos))
            {
                rvv += i.cost;
                //if (rvv > v * 0.01)
                {
                    pr.Points.AddXY(i.pos, Math.Log(i.cost + 1));
                    bl.Points.AddXY(i.pos, Math.Log(i.guest_opcodes + 1));
                    gs.Points.AddXY(i.pos, Math.Log(i.guest_cycles / 4 + 1));
                }
            }

            /*
             * var vertices = new string[30];
             *
             * for (int i = 0; i < 30; i++)
             * {
             *  vertices[i] = i.ToString();
             *  g.AddVertex(vertices[i]);
             * }
             *
             * for (int i = 6; i < 15; i++)
             * {
             *  g.AddChildVertex(vertices[i % 5], vertices[i]);
             * }
             * g.AddChildVertex(vertices[5], vertices[4]);
             * g.AddChildVertex(vertices[5], vertices[2]);
             * g.AddChildVertex(vertices[16], vertices[0]);
             * g.AddChildVertex(vertices[16], vertices[1]);
             * g.AddChildVertex(vertices[16], vertices[3]);
             * g.AddChildVertex(vertices[16], vertices[20]);
             * g.AddChildVertex(vertices[16], vertices[21]);
             * g.AddChildVertex(vertices[16], vertices[22]);
             * g.AddChildVertex(vertices[16], vertices[23]);
             * g.AddChildVertex(vertices[16], vertices[24]);
             * g.AddChildVertex(vertices[4], vertices[25]);
             * g.AddChildVertex(vertices[4], vertices[26]);
             * g.AddChildVertex(vertices[4], vertices[27]);
             *
             * g.AddEdge(new Edge<object>(vertices[0], vertices[1]));
             * g.AddEdge(new Edge<object>(vertices[0], vertices[2]));
             * g.AddEdge(new Edge<object>(vertices[2], vertices[4]));
             * g.AddEdge(new Edge<object>(vertices[0], vertices[7]));
             * g.AddEdge(new Edge<object>(vertices[8], vertices[7]));
             * //g.AddEdge(new Edge<object>(vertices[13], vertices[12]));
             * g.AddEdge(new Edge<object>(vertices[3], vertices[20]));
             * g.AddEdge(new Edge<object>(vertices[20], vertices[21]));
             * g.AddEdge(new Edge<object>(vertices[20], vertices[22]));
             * g.AddEdge(new Edge<object>(vertices[22], vertices[23]));
             * g.AddEdge(new Edge<object>(vertices[23], vertices[24]));
             * g.AddEdge(new Edge<object>(vertices[0], vertices[28]));
             * g.AddEdge(new Edge<object>(vertices[0], vertices[29]));
             * g.AddEdge(new Edge<object>(vertices[25], vertices[27]));
             * g.AddEdge(new Edge<object>(vertices[26], vertices[25]));
             * g.AddEdge(new Edge<object>(vertices[14], vertices[27]));
             * g.AddEdge(new Edge<object>(vertices[14], vertices[26]));
             * g.AddEdge(new Edge<object>(vertices[14], vertices[25]));
             * g.AddEdge(new Edge<object>(vertices[26], vertices[27]));
             */

            layout.LayoutMode                  = LayoutMode.Automatic;
            layout.LayoutAlgorithmType         = "CompoundFDP";
            layout.OverlapRemovalConstraint    = AlgorithmConstraints.Automatic;
            layout.OverlapRemovalAlgorithmType = "FSA";
            layout.HighlightAlgorithmType      = "Simple";

            /*
             * using (FileStream w = File.Create("c:\\fail.bin"))
             *  g.SerializeToBinary(w);
             */

            layout.Graph = g;
        }