Exemple #1
0
        public void GetEdge_Throws()
        {
            var graph  = new AdjacencyGraph <int, Edge <int> >();
            var graph2 = new AdjacencyGraph <TestVertex, Edge <TestVertex> >();
            var random = new Random();

            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            // ReSharper disable AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(null, random));
            Assert.Throws <ArgumentNullException>(() => RandomGraphFactory.GetEdge(graph2, null));
            Assert.Throws <ArgumentNullException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(null, null));

            Assert.Throws <ArgumentNullException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(null, 1, random));
            Assert.Throws <ArgumentNullException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(Enumerable.Empty <Edge <int> >(), 1, null));
            Assert.Throws <ArgumentNullException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(null, 1, null));
            // ReSharper restore AssignNullToNotNullAttribute
            Assert.Throws <ArgumentOutOfRangeException>(() => RandomGraphFactory.GetVertex(graph, random));

            Assert.Throws <ArgumentOutOfRangeException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(Enumerable.Empty <Edge <int> >(), -1, random));
            Assert.Throws <ArgumentOutOfRangeException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(Enumerable.Empty <Edge <int> >(), 0, random));
            Assert.Throws <InvalidOperationException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(Enumerable.Empty <Edge <int> >(), 1, random));
            Assert.Throws <InvalidOperationException>(
                () => RandomGraphFactory.GetEdge <int, Edge <int> >(
                    new[] { new Edge <int>(1, 2), new Edge <int>(1, 3) },
                    10,
                    new Random(123456)));
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
Exemple #2
0
        private static IEnumerable <(Graph <int, double, double>, Graph <int, double, double>, IHeuristic <int, double>)> generateTestCases()
        {
            var densityCount = 6;

            foreach (var graph1nodeCount in FibonacciGenerator())
            {
                foreach (var graph2nodeCount in FibonacciGenerator().TakeWhile(number => number <= graph1nodeCount))
                {
                    foreach (var density1 in Enumerable.Range(1, densityCount).Select(integer => integer * 1d / densityCount))
                    {
                        foreach (var density2 in Enumerable.Range(1, densityCount).Select(integer => integer * 1d / densityCount))
                        {
                            foreach (var heuristic in generateAllHeuristics())
                            {
                                var           random1 = new Random(graph2nodeCount + graph1nodeCount * graph1nodeCount + (int)(density1 * 1000) + (int)(density2 * 100000));
                                var           random2 = new Random(random1.Next());
                                Func <double> vertexAttributeGenerator1 = () => random1.NextDouble();
                                Func <double> vertexAttributeGenerator2 = () => random2.NextDouble();
                                Func <double> edgeAttributeGenerator1   = () => random1.NextDouble();
                                Func <double> edgeAttributeGenerator2   = () => random2.NextDouble();

                                var randomTemporalGraph1 = RandomGraphFactory.GenerateRandomInstance(graph1nodeCount, density1, true, vertexAttributeGenerator1, edgeAttributeGenerator1, random1);
                                var randomTemporalGraph2 = RandomGraphFactory.GenerateRandomInstance(graph2nodeCount, density2, true, vertexAttributeGenerator2, edgeAttributeGenerator2, random2, graph1nodeCount);

                                yield return(randomTemporalGraph1, randomTemporalGraph2, heuristic);
                            }
                        }
                    }
                }
            }
        }
Exemple #3
0
        public void GetEdge()
        {
            var graph  = new AdjacencyGraph <int, Edge <int> >();
            var edge12 = new Edge <int>(1, 2);
            var edge13 = new Edge <int>(1, 3);
            var edge23 = new Edge <int>(2, 3);
            var edge24 = new Edge <int>(2, 4);
            var edge35 = new Edge <int>(3, 5);

            graph.AddVerticesAndEdgeRange(new[]
            {
                edge12, edge13, edge23, edge24, edge35
            });

            Edge <int> edge = RandomGraphFactory.GetEdge(graph, new Random(123456));

            Assert.AreSame(edge13, edge);

            edge = RandomGraphFactory.GetEdge(graph, new Random(456789));
            Assert.AreSame(edge35, edge);

            edge = RandomGraphFactory.GetEdge <int, Edge <int> >(graph.Edges, graph.VertexCount, new Random(123456));
            Assert.AreSame(edge13, edge);

            edge = RandomGraphFactory.GetEdge <int, Edge <int> >(graph.Edges, graph.VertexCount, new Random(456789));
            Assert.AreSame(edge35, edge);

            edge = RandomGraphFactory.GetEdge <int, Edge <int> >(graph.Edges, 3, new Random(123));
            Assert.AreSame(edge23, edge);
        }
        public void GraphWithSelfEdges(
            [UsingLinear(2, 5)] int i,
            [UsingLinear(0, 25)] int j
            )
        {
            if (i == 0 && j == 0)
            {
                return;
            }

            Random rnd = new Random();

            var next = new IntFactory();

            g = new AdjacencyGraph <int, Edge <int> >(true);
            RandomGraphFactory.Create <int, Edge <int> >(g,
                                                         next.Next,
                                                         (s, t) => new Edge <int>(s, t),
                                                         rnd, i, j, true);

            foreach (var sv in g.Vertices)
            {
                this.sourceVertex = sv;
                RunBfs();
            }
        }
        public static IEnumerable <object[]> SubgraphSupergraphCases(string name)
        {
            var densityCount  = 8;
            var verticesCount = 8;
            var offset        = verticesCount;

            foreach (var heuristic in generateAllHeuristics())
            {
                foreach (var supergraphVertexCount in Enumerable.Range(2, verticesCount))
                {
                    foreach (var subgraphVertexCount in Enumerable.Range(0, supergraphVertexCount))
                    {
                        foreach (var density in Enumerable.Range(1, densityCount).Select(integer => integer * 1d / densityCount))
                        {
                            var           random  = new Random(subgraphVertexCount + verticesCount * supergraphVertexCount + (int)(density * 10000));
                            var           random2 = new Random(random.Next());
                            var           random3 = new Random(random.Next());
                            Func <double> edgeAttributeGenerator = () => random.NextDouble();
                            var(subgraph, supergraph) = RandomGraphFactory.GenerateRandomInstanceWithASubinstance(subgraphVertexCount, supergraphVertexCount, density, true, () => 0, edgeAttributeGenerator, random2, allowLoops: false);
                            var subgraphPermuted = Transform.PermuteClone(subgraph, random3, (id, attr) => (id + offset, attr), (pair, attr) => ((pair.Item1 + offset, pair.Item2 + offset), attr));

                            yield return(new object[] { $"SubgraphVC: {subgraphVertexCount}, supergraphVC: {supergraphVertexCount}, density: {density:0.00}", heuristic, subgraphPermuted, supergraph });
                        }
                    }
                }
            }
        }
        public static IEnumerable <object[]> RandomCases(string name)
        {
            var densityCount  = 6;
            var verticesCount = 7;
            var offset        = verticesCount;

            foreach (var vertexCount1 in Enumerable.Range(0, verticesCount))
            {
                foreach (var vertexCount2 in Enumerable.Range(0, vertexCount1))
                {
                    foreach (var density1 in Enumerable.Range(1, densityCount).Select(integer => integer * 1d / densityCount))
                    {
                        foreach (var density2 in Enumerable.Range(1, densityCount).Select(integer => integer * 1d / densityCount))
                        {
                            var           random  = new Random(vertexCount1 + verticesCount * vertexCount2 + (int)(density1 * 1000) + (int)(density2 * 100000));
                            var           random2 = new Random(random.Next());
                            var           random3 = new Random(random.Next());
                            Func <double> edgeAttributeGenerator = () => random.NextDouble();
                            var           graph1 = RandomGraphFactory.GenerateRandomInstance(vertexCount1, density1, true, () => 0, edgeAttributeGenerator, random2, allowLoops: false);
                            var           graph2 = RandomGraphFactory.GenerateRandomInstance(vertexCount1, density2, true, () => 0, edgeAttributeGenerator, random2, allowLoops: false, vertexOffset: offset);

                            yield return(new object[] { $"VertexCount1: {vertexCount1}, density1: {density1:0.00}, vertexCount2: {vertexCount2}, density2: {density2:0.00}", graph1, graph2 });
                        }
                    }
                }
            }
        }
Exemple #7
0
        public void GraphWithSelfEdges(
            [UsingLinear(2, 9)] int i,
            [UsingLinear(0, 10)] int j
            )
        {
            if (i == 0 && j == 0)
            {
                return;
            }

            Random rnd = new Random();

            g = new AdjacencyGraph <int, Edge <int> >(true);
            RandomGraphFactory.Create <int, Edge <int> >(g,
                                                         new IntVertexFactory(),
                                                         FactoryCompiler.GetEdgeFactory <int, Edge <int> >(),
                                                         rnd, i, j, true);

            algo = new BreadthFirstSearchAlgorithm <int, Edge <int> >(g);
            try
            {
                algo.InitializeVertex += new VertexEventHandler <int>(this.InitializeVertex);
                algo.DiscoverVertex   += new VertexEventHandler <int>(this.DiscoverVertex);
                algo.ExamineEdge      += new EdgeEventHandler <int, Edge <int> >(this.ExamineEdge);
                algo.ExamineVertex    += new VertexEventHandler <int>(this.ExamineVertex);
                algo.TreeEdge         += new EdgeEventHandler <int, Edge <int> >(this.TreeEdge);
                algo.NonTreeEdge      += new EdgeEventHandler <int, Edge <int> >(this.NonTreeEdge);
                algo.GrayTarget       += new EdgeEventHandler <int, Edge <int> >(this.GrayTarget);
                algo.BlackTarget      += new EdgeEventHandler <int, Edge <int> >(this.BlackTarget);
                algo.FinishVertex     += new VertexEventHandler <int>(this.FinishVertex);

                parents.Clear();
                distances.Clear();
                currentDistance = 0;
                sourceVertex    = RandomGraphFactory.GetVertex(g, rnd);

                foreach (int v in g.Vertices)
                {
                    distances[v] = int.MaxValue;
                    parents[v]   = v;
                }
                distances[sourceVertex] = 0;
                algo.Compute(sourceVertex);

                CheckBfs();
            }
            finally
            {
                algo.InitializeVertex -= new VertexEventHandler <int>(this.InitializeVertex);
                algo.DiscoverVertex   -= new VertexEventHandler <int>(this.DiscoverVertex);
                algo.ExamineEdge      -= new EdgeEventHandler <int, Edge <int> >(this.ExamineEdge);
                algo.ExamineVertex    -= new VertexEventHandler <int>(this.ExamineVertex);
                algo.TreeEdge         -= new EdgeEventHandler <int, Edge <int> >(this.TreeEdge);
                algo.NonTreeEdge      -= new EdgeEventHandler <int, Edge <int> >(this.NonTreeEdge);
                algo.GrayTarget       -= new EdgeEventHandler <int, Edge <int> >(this.GrayTarget);
                algo.BlackTarget      -= new EdgeEventHandler <int, Edge <int> >(this.BlackTarget);
                algo.FinishVertex     -= new VertexEventHandler <int>(this.FinishVertex);
            }
        }
        public void GetVertex_Throws()
        {
            var graph  = new AdjacencyGraph <int, Edge <int> >();
            var graph2 = new AdjacencyGraph <TestVertex, Edge <TestVertex> >();
            var random = new Random();

            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.GetVertex <int>(null, random)
                );

            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.GetVertex(graph2, null)
                );

            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.GetVertex <int>(null, null)
                );

            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.GetVertex <int>(null, 1, random)
                );

            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.GetVertex(Enumerable.Empty <int>(), 1, null)
                );

            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.GetVertex <int>(null, 1, null)
                );

            // ReSharper restore AssignNullToNotNullAttribute
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.GetVertex(graph, random)
                );

            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.GetVertex(Enumerable.Empty <int>(), -1, random)
                );

            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.GetVertex(Enumerable.Empty <int>(), 0, random)
                );

            Assert.Throws <InvalidOperationException>(
                () => RandomGraphFactory.GetVertex(Enumerable.Empty <int>(), 1, random)
                );

            Assert.Throws <InvalidOperationException>(
                () => RandomGraphFactory.GetVertex(
                    new[] { 1, 2 },
                    10,
                    new Random(123456)
                    )
                );
        }
        public static BidirectionalGraph <int, IEdge <int> > QuickGraphRandomGraph(int vertexCount = 200, int edgeCount = 400)
        {
            var graph = new BidirectionalGraph <int, IEdge <int> >();

            int currentVertexIndex = 0;

            RandomGraphFactory.Create(
                g: graph,
                vertexFactory: () => currentVertexIndex++,
                edgeFactory: (v1, v2) => new Edge <int>(v1, v2),
                rnd: new Random(),
                vertexCount: vertexCount,
                edgeCount: edgeCount,
                selfEdges: false);

            return(graph);
        }
        public void GraphWithSelfEdgesBig()
        {
            TestConsole.WriteLine("processors: {0}", TaskManager.Current.Policy.IdealProcessors);
            Random rnd = new Random();

            g = new AdjacencyGraph <int, Edge <int> >(true);
            var next = new IntFactory();

            RandomGraphFactory.Create <int, Edge <int> >(g,
                                                         next.Next,
                                                         (s, t) => new Edge <int>(s, t),
                                                         rnd, 5000, 20000, false);

            var sv = g.Vertices.FirstOrDefault();

            this.sourceVertex = sv;
            RunBfs();
        }
        public void GetVertex()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();

            graph.AddVertexRange(new[] { 1, 2, 3, 4, 5 });

            int vertex = RandomGraphFactory.GetVertex(
                graph,
                new Random(123456)
                );

            Assert.AreEqual(2, vertex);

            vertex = RandomGraphFactory.GetVertex(
                graph,
                new Random(456789)
                );
            Assert.AreEqual(5, vertex);

            vertex = RandomGraphFactory.GetVertex(
                graph.Vertices,
                graph.VertexCount,
                new Random(123456)
                );
            Assert.AreEqual(2, vertex);

            vertex = RandomGraphFactory.GetVertex(
                graph.Vertices,
                graph.VertexCount,
                new Random(456789)
                );
            Assert.AreEqual(5, vertex);

            vertex = RandomGraphFactory.GetVertex(
                graph.Vertices,
                3,
                new Random(123)
                );
            Assert.AreEqual(3, vertex);
        }
        public static IEnumerable <object[]> IsomorphicGraphCases(string name)
        {
            var densityCount  = 9;
            var verticesCount = 9;
            var offset        = verticesCount;

            foreach (var heuristic in generateAllHeuristics())
            {
                foreach (var density in Enumerable.Range(1, densityCount).Select(integer => integer * 1d / densityCount))
                {
                    foreach (var vertexCount in Enumerable.Range(2, verticesCount))
                    {
                        var           random  = new Random(vertexCount + (int)(density * 10000));
                        var           random2 = new Random(random.Next());
                        var           random3 = new Random(random.Next());
                        Func <double> edgeAttributeGenerator = () => random.NextDouble();
                        var           graph1 = RandomGraphFactory.GenerateRandomInstance(vertexCount, density, true, () => 0, edgeAttributeGenerator, random2, allowLoops: false);
                        var           graph2 = Transform.PermuteClone(graph1, random3, (id, attr) => (id + offset, attr), (pair, attr) => ((pair.Item1 + offset, pair.Item2 + offset), attr));

                        yield return(new object[] { $"VertexCount: {vertexCount}, density: {density:0.00}", heuristic, graph1, graph2 });
                    }
                }
            }
        }
Exemple #13
0
        public void Create_Undirected_Throws()
        {
            var graph  = new UndirectedGraph <int, Edge <int> >();
            var random = new Random();

            // ReSharper disable AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    () => 1,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    null,
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    null,
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    () => 1,
                    null,
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    null,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    null,
                    null,
                    null,
                    1, 1, false));
            // ReSharper restore AssignNullToNotNullAttribute
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    -1, 1, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    0, 1, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, -1, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 0, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    0, 0, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    -1, -1, false));
        }
Exemple #14
0
        public void Create_Undirected()
        {
            var graph = new UndirectedGraph <int, EquatableEdge <int> >();

            // With self edge
            int v = 0;

            RandomGraphFactory.Create(
                graph,
                () => ++ v,
                (source, target) => new EquatableEdge <int>(source, target),
                new Random(123456),
                5,
                10,
                true);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4, 5 });
            AssertHasEdges(
                graph,
                new[]
            {
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(2, 1),
                new EquatableEdge <int>(3, 5),
                new EquatableEdge <int>(3, 1),
                new EquatableEdge <int>(4, 5),
                new EquatableEdge <int>(4, 4),
                new EquatableEdge <int>(4, 1),
                new EquatableEdge <int>(5, 3)
            });

            // Without self edge
            graph.Clear();
            v = 0;
            RandomGraphFactory.Create(
                graph,
                () => ++ v,
                (source, target) => new EquatableEdge <int>(source, target),
                new Random(123456),
                5,
                10,
                false);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4, 5 });
            AssertHasEdges(
                graph,
                new[]
            {
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(2, 1),
                new EquatableEdge <int>(3, 5),
                new EquatableEdge <int>(3, 1),
                new EquatableEdge <int>(3, 1),
                new EquatableEdge <int>(4, 5),
                new EquatableEdge <int>(4, 1),
                new EquatableEdge <int>(5, 3)
            });

            // Different seed change generated graph
            graph.Clear();
            v = 0;
            RandomGraphFactory.Create(
                graph,
                () => ++ v,
                (source, target) => new EquatableEdge <int>(source, target),
                new Random(456789),
                5,
                10,
                true);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4, 5 });
            AssertHasEdges(
                graph,
                new[]
            {
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(2, 2),
                new EquatableEdge <int>(2, 5),
                new EquatableEdge <int>(3, 4),
                new EquatableEdge <int>(3, 2),
                new EquatableEdge <int>(4, 5),
                new EquatableEdge <int>(4, 2),
                new EquatableEdge <int>(4, 2),
                new EquatableEdge <int>(5, 2),
                new EquatableEdge <int>(5, 3)
            });

            // On non empty graph, keep existing stuff
            graph.Clear();
            graph.AddVerticesAndEdge(new EquatableEdge <int>(6, 7));
            v = 0;
            RandomGraphFactory.Create(
                graph,
                () => ++ v,
                (source, target) => new EquatableEdge <int>(source, target),
                new Random(123456),
                5,
                10,
                true);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4, 5, 6, 7 });
            AssertHasEdges(
                graph,
                new[]
            {
                new EquatableEdge <int>(6, 7),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(2, 1),
                new EquatableEdge <int>(3, 5),
                new EquatableEdge <int>(3, 1),
                new EquatableEdge <int>(4, 5),
                new EquatableEdge <int>(4, 4),
                new EquatableEdge <int>(4, 1),
                new EquatableEdge <int>(5, 3)
            });
        }
        public void Create_Throws()
        {
            var graph  = new AdjacencyGraph <int, Edge <int> >();
            var random = new QuikGraph.Utils.CryptoRandom();

            // ReSharper disable AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    () => 1,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    null,
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    null,
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    () => 1,
                    null,
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    null,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    null,
                    null,
                    null,
                    1, 1, false));
            // ReSharper restore AssignNullToNotNullAttribute
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    -1, 1, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    0, 1, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, -1, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 0, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    0, 0, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    -1, -1, false));
        }
Exemple #16
0
        static void Main(string[] args)
        {
            var           random = new Random(3_14159265);
            Func <double> vertexAttributeGenerator = () =>
            {
                return(random.NextDouble());
            };
            Func <double> edgeAttributeGenerator = () =>
            {
                return(random.NextDouble());
            };

            double vertexBound = 2;
            double edgeBound   = 3;
            double vertexDecay = 1;
            double edgeDecay   = 1;

            Func <double, double, double, double> boundMetric = (a, bound, decay) => bound * a / (1 / decay + a);

            Func <double, double, double> vertexRelabel = (a1, a2) =>
            {
                return(boundMetric(Math.Abs(a1 - a2), vertexBound, vertexDecay));
            };
            Func <double, double> vertexAdd    = a => boundMetric(Math.Abs(a), vertexBound, vertexDecay);
            Func <double, double> vertexRemove = vertexAdd;

            Func <double, double, double> edgeRelabel = (a1, a2) =>
            {
                return(boundMetric(Math.Abs(a1 - a2), edgeBound, edgeDecay));
            };
            Func <double, double> edgeAdd    = a => boundMetric(Math.Abs(a), edgeBound, edgeDecay);
            Func <double, double> edgeRemove = edgeAdd;



            // Func<double, double, double> vertexRelabel = (a1, a2) =>
            // {
            //     return Math.Abs(a1 - a2);
            // };
            // Func<double, double> vertexAdd = a => Math.Abs(a);
            // Func<double, double> vertexRemove = vertexAdd;

            // Func<double, double, double> edgeRelabel = (a1, a2) =>
            // {
            //     return Math.Abs(a1 - a2);
            // };
            // Func<double, double> edgeAdd = a => Math.Abs(a);
            // Func<double, double> edgeRemove = edgeAdd;

            var a = new List <double>()
            {
                0, 1, .5, 1d / 3, 2d / 3, -1, 2, 10, -10, 100, -100, 1000, -1000
            };
            var b = new List <double>()
            {
                0, 1, .5, 1d / 3, 2d / 3, -1, 2, 10, -10, 100, -100, 1000, -1000
            };
            var RiesenBunke2009AB = (1, 1);



            for (int gVertices = 8; gVertices < 12; gVertices++)
            {
                var hVertices = gVertices;
                // for (int hVertices = gVertices; hVertices > 0; hVertices-=1)
                {
                    var measurements = 0;
                    var results      = new Dictionary <(double, double), double>();
                    foreach (var aElement in a)
                    {
                        foreach (var bElement in b)
                        {
                            results.Add((aElement, bElement), 0);
                        }
                    }
                    for (double gDensity = 1.0; gDensity > 0; gDensity -= 0.1)
                    {
                        for (double hDensity = gDensity; hDensity > 0; hDensity -= 0.1)
                        {
                            for (int iter = 0; iter < 5; iter++)
                            {
                                var G = RandomGraphFactory.GenerateRandomInstance(
                                    vertices: gVertices,
                                    density: gDensity,
                                    directed: true,
                                    vertexAttributeGenerator: vertexAttributeGenerator,
                                    edgeAttributeGenerator: edgeAttributeGenerator
                                    );
                                var H = RandomGraphFactory.GenerateRandomInstance(
                                    vertices: hVertices,
                                    density: hDensity,
                                    directed: true,
                                    vertexAttributeGenerator: vertexAttributeGenerator,
                                    edgeAttributeGenerator: edgeAttributeGenerator
                                    );

                                var matchingParameters = new GraphMatchingParameters <int, double, double>
                                {
                                    aCollection    = a,
                                    bCollection    = b,
                                    edgeAdd        = edgeAdd,
                                    vertexAdd      = vertexAdd,
                                    edgeRelabel    = edgeRelabel,
                                    edgeRemove     = edgeRemove,
                                    vertexRemove   = vertexRemove,
                                    vertexRelabel  = vertexRelabel,
                                    encodingMethod = GraphEncodingMethod.Wojciechowski
                                };
                                var matching = new VertexPartialMatchingNode <int, double, double>(
                                    G,
                                    H,
                                    matchingParameters
                                    );

                                var myRelativeError    = (matching.BestUpperBound - matching.BestLowerBound) / matching.BestLowerBound;
                                var theirRelativeError = (matching.BestUpperBound - matching.abLowerBounds[RiesenBunke2009AB]) / matching.abLowerBounds[RiesenBunke2009AB];
                                var eps = 1e-12;
                                // if (theirRelativeError > eps)
                                // {
                                //     System.Console.WriteLine($"|Vg|={gVertices}, |Eg|={gDensity:f2}, |Vh|={hVertices}, |Eh|={hDensity:f2}. My estimate / theirs {matching.LowerBound / matching.abLowerBounds[RiesenBunke2009AB]:f2}.");

                                // }

                                // var theirLowerBound = matching.abLowerBounds[(2d/3, .5)];
                                // if (theirLowerBound > eps)
                                {
                                    measurements += 1;
                                    foreach (var kvp in matching.abLowerBounds)
                                    {
                                        // var score = (matching.UpperBound - kvp.Value) / matching.UpperBound;
                                        var score = kvp.Value > Math.Max(Math.Max(
                                                                             matching.abLowerBounds[(.5, .5)],