public void HeuristicComparisonForRandomCases(string name, Graph <int, int, double> graph1, Graph <int, int, double> graph2)
        {
            // Arrange
            var algorithms = new List <AStarAlgorithm <TemporalMatchingNode <int, int, double> > >();

            foreach (var heuristic in generateAllHeuristics())
            {
                var initialNode = new TemporalMatchingNode <int, int, double>(graph1, graph2, heuristic, false);
                algorithms.Add(new AStarAlgorithm <TemporalMatchingNode <int, int, double> >(initialNode));
            }
            var temporalMatchings = new TemporalMatchingNode <int, int, double> [algorithms.Count];

            // Act
            for (int i = 0; i < algorithms.Count; i++)
            {
                temporalMatchings[i] = algorithms[i].ExpandRecursively();
            }

            // Assert
            var referenceMathcing = temporalMatchings[0];

            for (int i = 1; i < temporalMatchings.Length; i++)
            {
                var currentMatching = temporalMatchings[i];
                Assert.Equal(referenceMathcing.DistanceFromSource(), currentMatching.DistanceFromSource());
            }
        }
        public void SubgraphsSupergraphs(string name, IHeuristic <int, double> heuristic, Graph <int, int, double> graph1, Graph <int, int, double> graph2)
        {
            // Arrange
            var initialNode = new TemporalMatchingNode <int, int, double>(graph1, graph2, heuristic, false);
            var algorithm   = new AStarAlgorithm <TemporalMatchingNode <int, int, double> >(initialNode);

            // Act
            var temporalMatching = algorithm.ExpandRecursively();

            // Assert
            Assert.True(graph1.VertexCount <= graph2.VertexCount);
            Assert.Equal(graph1.VertexCount, -1 * temporalMatching.DistanceFromSource());
        }
        public void ExampleFromArxiv()
        {
            // Arrange
            // Graph from first page of https://arxiv.org/pdf/1801.08098.pdf
            var graph1 = new Graph <string, int, int>(directed: true);

            foreach (var vertex in new[] { "A", "B", "C", "D", "E", "F" })
            {
                graph1.AddVertex(vertex);
            }
            graph1.AddEdge(("A", "B"), 3);
            graph1.AddEdge(("B", "C"), 2);
            graph1.AddEdge(("B", "D"), 6);
            graph1.AddEdge(("C", "A"), 5);
            graph1.AddEdge(("C", "E"), 4);
            graph1.AddEdge(("D", "E"), 1);
            graph1.AddEdge(("E", "B"), 7);
            graph1.AddEdge(("E", "F"), 9);
            graph1.AddEdge(("F", "C"), 8);

            var graph2 = new Graph <string, int, int>(directed: true);

            graph2.AddVertex("1");
            graph2.AddVertex("2");
            graph2.AddVertex("3");
            graph2.AddEdge(("1", "2"), 1);
            graph2.AddEdge(("2", "3"), 2);
            graph2.AddEdge(("3", "1"), 3);

            var heuristic = new TrivialHeuristic <string, int>();

            var initialNode = new TemporalMatchingNode <string, int, int>(graph1, graph2, heuristic);
            var algorithm   = new AStarAlgorithm <TemporalMatchingNode <string, int, int> >(initialNode);

            // Act
            var temporalMatching = algorithm.ExpandRecursively();

            // Assert
            Assert.Equal("1", temporalMatching.Matching("B"));
            Assert.Equal("2", temporalMatching.Matching("C"));
            Assert.Equal("3", temporalMatching.Matching("E"));
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            File.WriteAllText(csvExactPath, string.Empty);
            File.WriteAllText(texExactPath, string.Empty);

            foreach (var(graph1, graph2, heuristic) in generateTestCases())
            {
                var initialNode = new TemporalMatchingNode <int, double, double>(graph1, graph2, heuristic, false);
                var benchmark   = new Benchmark <string>();
                initialNode.Benchmark = benchmark;

                var algorithm = new AStarAlgorithm <TemporalMatchingNode <int, double, double> >(initialNode, benchmark);

                Console.WriteLine("Computing...");

                benchmark.StartBenchmark("AStar");
                var temporalMatching = algorithm.ExpandRecursively();
                benchmark.StopBenchmark("AStar");

                Console.WriteLine("Computed.");

                var graph1density       = graph1.EdgeCount * 1d / (graph1.VertexCount * graph1.VertexCount);
                var graph2density       = graph2.EdgeCount * 1d / (graph2.VertexCount * graph2.VertexCount);
                var subgraphVertexCount = -temporalMatching.DistanceFromSource();
                var subgraphDensity     = temporalMatching.alreadyMatchedEdges.Count * 1d / (temporalMatching.alreadyMatchedVertices.Count * temporalMatching.alreadyMatchedVertices.Count);
                var computationTime     = benchmark.GetIntermittentResult("AStar").TotalSeconds;
                var expansionCount      = benchmark.GetIntermittentCount("Expand outer");
                var removalCount        = benchmark.GetIntermittentCount("Removed worst node");

                // size of graph1
                // size of graph2
                // density of graph1
                // density of graph2
                // type of heuristic
                // size of maximum common subgraph
                // density of subgraph
                // total computation time in miliseconds
                // number of nodes expanded by A* algorithm
                // number of nodes automatically pruned

                using (var csvWriter = File.AppendText(csvExactPath))
                    csvWriter.WriteLine($"{graph1.VertexCount},{graph2.VertexCount},{graph1density:0.00},{graph2density:0.00},{heuristic.Name},{subgraphVertexCount},{subgraphDensity:0.00},{computationTime:0.00},{expansionCount},{removalCount}");
                using (var texWriter = File.AppendText(texExactPath))
                    texWriter.WriteLine($"{graph1.VertexCount} & {graph2.VertexCount} & {graph1density:0.00} & {graph2density:0.00} & {heuristic.Name} & {subgraphVertexCount} & {subgraphDensity:0.00} & {computationTime:0.00} & {expansionCount} & {removalCount} \\\\");

                Console.WriteLine("Saved.");
            }

            //var random1 = new Random(3_14159265);
            //var random2 = new Random(2_71828182);
            //double vertexAttributeGenerator() => random1.NextDouble();
            //double edgeAttributeGenerator() => random1.NextDouble();
            //var nodes = 100;
            //var density = .9d;
            //var randomTemporalGraph1 = RandomGraphFactory.GenerateRandomInstance(nodes, density, true, vertexAttributeGenerator, edgeAttributeGenerator, random1);
            //var randomTemporalGraph2 = RandomGraphFactory.GenerateRandomInstance(nodes, density, true, vertexAttributeGenerator, edgeAttributeGenerator, random2, nodes);

            ////var counter = -100;
            ////var heuristic = new ExactHeuristic<int, double>(() => counter--);
            //var heuristic = new TrivialHeuristic<int, double>();

            //var initialNode = new TemporalMatchingNode<int, double, double>(randomTemporalGraph1, randomTemporalGraph2, heuristic);

            //var benchmark = new Benchmark<string>();
            //initialNode.Benchmark = benchmark;

            //var aStarAlgorithm = new AStarAlgorithm<TemporalMatchingNode<int, double, double>>(initialNode, benchmark);

            //var padRight = 12;
            //aStarAlgorithm.Logger += (message) => Console.WriteLine($"{benchmark.GetIntermittentResult("AStar").TotalMilliseconds.ToString().PadRight(padRight)}ms: {message}");
            //aStarAlgorithm.Logger += (message) => Console.WriteLine(benchmark.ExportResults());

            //benchmark.StartBenchmark("AStar");
            //var solution = aStarAlgorithm.ExpandRecursively();
            //benchmark.StopBenchmark("AStar");

            //var benchmarkTextSummary = benchmark.ExportResults();

            //Console.WriteLine($"Graph1 size: {randomTemporalGraph1.VertexCount} vertices");
            //Console.WriteLine($"Graph2 size: {randomTemporalGraph2.VertexCount} vertices");
            //Console.WriteLine($"Maximum temporal subgraph size: {-solution.DistanceFromSource()}");

            //Console.WriteLine("Performance benchmarking results:");

            //Console.WriteLine(benchmarkTextSummary);
        }