Exemple #1
0
        private void Run()
        {
            Func <Node, Node, double> cost = (u, v) => (Graph.Arcs(u, v, ArcFilter.Forward).Any() ? 1 : 10);
            IEnumerable <Node>        tour = null;
            double minimumTourCost         = Graph.NodeCount();

            // Use the insertion tsp combined with 2-OPT heuristic.
            InsertionTsp <Node> insertionTsp = new InsertionTsp <Node>(Graph.Nodes(), cost);

            insertionTsp.Run();
            if (insertionTsp.TourCost == minimumTourCost)
            {
                tour = insertionTsp.Tour;
            }
            else
            {
                Opt2Tsp <Node> opt2Tsp = new Opt2Tsp <Node>(cost, insertionTsp.Tour, insertionTsp.TourCost);
                opt2Tsp.Run();
                if (opt2Tsp.TourCost == minimumTourCost)
                {
                    tour = opt2Tsp.Tour;
                }
            }

            // convert the tour (node sequence) into a path (arc sequence connecting two nodes)
            if (tour == null)
            {
                Cycle = null;
            }
            else
            {
                var cycle = new Path(Graph);
                if (tour.Any())
                {
                    Node prev = Node.Invalid;
                    foreach (var n in tour)
                    {
                        if (prev == Node.Invalid)
                        {
                            cycle.Begin(n);
                        }
                        else
                        {
                            cycle.AddLast(Graph.Arcs(prev, n, ArcFilter.Forward).First());
                        }

                        prev = n;
                    }
                    cycle.AddLast(Graph.Arcs(prev, tour.First(), ArcFilter.Forward).First());
                }                 // if tour is not empty
                Cycle = cycle;
            }
        }
Exemple #2
0
        private void Run()
        {
            Func <Node, Node, double> cost       = (Node u, Node v) => (double)(Graph.Arcs(u, v, ArcFilter.Forward).Any() ? 1 : 10);
            IEnumerable <Node>        enumerable = null;
            double num = (double)Graph.NodeCount();
            InsertionTsp <Node> insertionTsp = new InsertionTsp <Node>(Graph.Nodes(), cost, TspSelectionRule.Farthest);

            insertionTsp.Run();
            if (insertionTsp.TourCost == num)
            {
                enumerable = insertionTsp.Tour;
            }
            else
            {
                Opt2Tsp <Node> opt2Tsp = new Opt2Tsp <Node>(cost, insertionTsp.Tour, insertionTsp.TourCost);
                opt2Tsp.Run();
                if (opt2Tsp.TourCost == num)
                {
                    enumerable = opt2Tsp.Tour;
                }
            }
            if (enumerable == null)
            {
                Cycle = null;
            }
            else
            {
                Path path = new Path(Graph);
                if (enumerable.Any())
                {
                    Node node = Node.Invalid;
                    foreach (Node item in enumerable)
                    {
                        if (node == Node.Invalid)
                        {
                            path.Begin(item);
                        }
                        else
                        {
                            path.AddLast(Graph.Arcs(node, item, ArcFilter.Forward).First());
                        }
                        node = item;
                    }
                    path.AddLast(Graph.Arcs(node, enumerable.First(), ArcFilter.Forward).First());
                }
                Cycle = path;
            }
        }