Beispiel #1
0
        private void Distancia()
        {
            long routeDistance = 0;
            RoutingSearchParameters searchParameters =
                operations_research_constraint_solver.DefaultRoutingSearchParameters();

            searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;

            DataModel           data    = new DataModel(Distancias, 1, 0);
            RoutingIndexManager manager = new RoutingIndexManager(
                data.DistanceMatrix.GetLength(0),
                data.VehicleNumber,
                data.Depot);
            RoutingModel routing = new RoutingModel(manager);

            int transitCallbackIndex = routing.RegisterTransitCallback(
                (long fromIndex, long toIndex) => {
                // Convert from routing variable Index to distance matrix NodeIndex.
                var fromNode = manager.IndexToNode(fromIndex);
                var toNode   = manager.IndexToNode(toIndex);
                return(data.DistanceMatrix[fromNode, toNode]);
            });

            routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);

            Assignment solution = routing.SolveWithParameters(searchParameters);


            var    index   = routing.Start(0);
            int    indice  = 1;
            string CadenaR = "";
            string c       = "";

            while (routing.IsEnd(index) == false)
            {
                //Recorrido[indice] = manager.IndexToNode((int)index);
                c       = manager.IndexToNode((int)index).ToString();
                CadenaR = CadenaR + " - " + c;
                var previousIndex = index;
                index          = solution.Value(routing.NextVar(index));
                routeDistance += routing.GetArcCostForVehicle(previousIndex, index, 0);
                indice++;
            }
            //Recorrido[indice] = manager.IndexToNode((int)index);
            c       = manager.IndexToNode((int)index).ToString();
            CadenaR = CadenaR + " - " + c;

            Km = routeDistance;
            CadenaRecorrido = CadenaR;
        }
Beispiel #2
0
        public static void Solve(int vehicles, int capacity, Distance distances, Demands demands)
        {
            /*
             * Generate constraint model
             */

            // Third argument defines depot, i.e. start-end node for round trip.
            var model = new RoutingModel(distances.MapSize(), vehicles, 0);

            // Node costs vs. Arc Costs
            model.SetArcCostEvaluatorOfAllVehicles(distances);

            /*
             * Capacity Constraints
             */

            if (distances.MapSize() != demands.MapSize())
            {
                throw new ArgumentException("Define demand for each city.");
            }

            model.AddDimension(demands, 0, capacity, true, "capacity");

            /*
             * Solve problem and display solution
             */

            Assignment assignment = model.Solve();

            if (assignment != null)
            {
                Console.WriteLine("Depot: " + model.GetDepot());
                Console.WriteLine("Total Distance: " + assignment.ObjectiveValue() + "\n");

                for (int i = 0; i < vehicles; i++)
                {
                    /*
                     * Display Trips:
                     */

                    Console.WriteLine("Round Trip for Vehicle " + i + ":");

                    long total  = 0;
                    var  source = model.Start(i);

                    while (!model.IsEnd(source))
                    {
                        var s = model.IndexToNode(source);
                        var t = model.IndexToNode(model.Next(assignment, source));

                        total += distances.Run(s, t);

                        Console.WriteLine(String.Format(" - From {0,-2} (demand: {1,-1}) travel to {2,-2} (demand: {3,-1}) with distance: {4,-2}",
                                                        distances.ToString(s),
                                                        demands.Demand(s),
                                                        distances.ToString(t),
                                                        demands.Demand(t),
                                                        distances.Run(s, t)));
                        source = model.Next(assignment, source);
                    }

                    Console.WriteLine("Total Distance for Vehicle " + i + ": " + total + "\n");
                }
            }

            Console.ReadKey();
        }