Exemplo n.º 1
0
        public void ToDotFile(string filename = "tmp.dot", TspTour tour = null)
        {
            using (var writer = new StreamWriter(filename))
            {
                writer.WriteLine($"digraph G {{{Environment.NewLine}penwidth=2;");
                if (tour?.Length > 1)
                {
                    for (int i = 0; i < tour.Length; i++)
                    {
                        var n = tour[i];
                        var m = tour[(i + 1) % tour.Length];
                        writer.WriteLine($"{n} -> {m} [label=\"{_dists[n, m]}\",color=red,penwidth=3];");
                    }
                }

                for (int n = 0; n < NumNodes; n++)
                {
                    for (int m = n + 1; m < NumNodes; m++)
                    {
                        if (_dists[n, m] == NoEdge)
                        {
                            continue;
                        }
                        if (tour == null || !tour.VisitsEdge(n, m))
                        {
                            writer.WriteLine($"{n} -> {m} [dir=none,label=\"{_dists[n, m]}\"];");
                        }
                    }
                }
                writer.WriteLine("}");
            }
        }
Exemplo n.º 2
0
        private static void Plot(Graph g, string file, TspTour t = null)
        {
            g.ToDotFile($"{file}.dot", t);
            var p = Process.Start("dot", $"-Tpng -o {file}.png {file}.dot");

            p.WaitForExit();
            // only on OS X
            if (p.ExitCode == 0)
            {
                Process.Start("open", $"{file}.png");
            }
        }
Exemplo n.º 3
0
 private void Exhaustive_internal(TspTour t)
 {
     if (t.AllVisiteed && _graph.HasEdge(t.LastNode, 0))
     {
         _tours.Add(t.VisitNext(0));
         return;
     }
     foreach (var next in t.UnvisitedNodes.Where(x => _graph.HasEdge(t.LastNode, x)))
     {
         // Assuming symmetric distances, we only want one
         // directed traversal of a TspTour-Slope
         if (_graph.NumNodes > 3 && next == 2 && !t.IsVisited(1))
         {
             continue;
         }
         Exhaustive_internal(t.VisitNext(next));
     }
 }