public string[] Solve(string[] nodes) { var routing = new RoutingModel(nodes.Length, 1, 0); routing.SetCost(new HaversineDistanceEvaluator(nodes, _maps)); var parameters = RoutingModel.DefaultSearchParameters(); parameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc; var solution = routing.SolveWithParameters(parameters); int routeNumber = 0; var results = new List <string>(); for (long node = routing.Start(routeNumber); !routing.IsEnd(node); node = solution.Value(routing.NextVar(node))) { results.Add(nodes[routing.IndexToNode(node)]); } return(results.ToArray()); }
public static void Solve(int vehicles, Distance distances) { /* * 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); /* * A vehicle must not visit more than 7 cities */ //model.AddConstantDimension(1, 7, true, "count"); /* * A vehicle must visit at least 3 cities */ /*model.AddConstantDimension(1, Int32.MaxValue, true, "count"); * var count = model.GetDimensionOrDie("count"); * for (int i = 0; i < vehicles; i++) { * count.CumulVar(model.End(i)).SetMin(3); * }*/ /* * City 3 and 5 must NOT be visited by the same vehicle */ //model.solver().Add(model.VehicleVar(3) != model.VehicleVar(5)); /* * City 3 must be visited before city 5 (not necessarily by the same vehicle) */ /*model.AddConstantDimension(1, Int32.MaxValue, true, "time"); * var time = model.GetDimensionOrDie("time"); * model.solver().Add(time.CumulVar(3) < time.CumulVar(5));*/ /* * City 3 must be visited right after city 5 (not necessarily by the same vhicle) */ /*model.AddConstantDimension(1, Int32.MaxValue, true, "time"); * var time = model.GetDimensionOrDie("time"); * model.solver().Add(time.CumulVar(5) + 1 == time.CumulVar(3));*/ /* * 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 target = model.Next(assignment, source); var from = model.IndexToNode(source); var to = model.IndexToNode(target); total += distances.Run(from, to); Console.WriteLine( $" - From {distances.ToString(@from),-3} travel to {distances.ToString(to),-3} with distance: {distances.Run(@from, to),-3}"); source = target; } Console.WriteLine("Total Distance for Vehicle " + i + ": " + total + "\n"); } } Console.ReadKey(); }
private static void VRP(int[,] locations, int[] demands, int num_vehicles) { int num_locations = locations.GetLength(0); int depot = 0; // The depot is the start and end point of each route. // Create routing model if (locations.Length > 0) { RoutingModel routing = new RoutingModel(num_locations, num_vehicles, depot); var search_parameters = RoutingModel.DefaultSearchParameters(); // Setting first solution heuristic: the // method for finding a first solution to the problem. search_parameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc; var distanceCallback = new DistanceLocationsCallback(locations); // Display(distanceCallback.matrix); routing.SetArcCostEvaluatorOfAllVehicles(distanceCallback); // Add a dimension for demand. long slack_max = 0; long vehicle_capacity = 100; bool fix_start_cumul_to_zero = true; string demand = "Demand"; routing.AddDimension(new DemandCallback(demands), slack_max, vehicle_capacity, fix_start_cumul_to_zero, demand); // Solve, displays a solution if any. var assignment = routing.SolveWithParameters(search_parameters); Console.WriteLine("Status = {0}", routing.Status()); if (assignment != null) { // Solution cost. Console.WriteLine("Total distance of all routes: {0}", assignment.ObjectiveValue()); // Inspect solution. // Only one route here; otherwise iterate from 0 to routing.vehicles() - 1 for (int vehicle_nbr = 0; vehicle_nbr < num_vehicles; vehicle_nbr++) { long index = routing.Start(vehicle_nbr); long index_next = assignment.Value(routing.NextVar(index)); string route = string.Empty; long route_dist = 0; long route_demand = 0; int node_index; int node_index_next; while (!routing.IsEnd(index_next)) { // Convert variable indices to node indices in the displayed route. node_index = routing.IndexToNode(index); node_index_next = routing.IndexToNode(index_next); route += node_index + " -> "; // Add the distance to the next node. route_dist += distanceCallback.Run(node_index, node_index_next); // # Add demand. route_demand += demands[node_index_next]; index = index_next; index_next = assignment.Value(routing.NextVar(index)); } node_index = routing.IndexToNode(index); node_index_next = routing.IndexToNode(index_next); route += node_index + " -> " + node_index_next; route_dist += distanceCallback.Run(node_index, node_index_next); Console.WriteLine("Route for vehicle " + vehicle_nbr + ":\n\n" + route + "\n"); Console.WriteLine("Distance of route " + vehicle_nbr + ": " + route_dist); Console.WriteLine("Demand met by vehicle " + vehicle_nbr + ": " + route_demand + "\n"); // Console.WriteLine($"Route: {route}"); } } else { Console.WriteLine("No solution found."); } } }
private static void Test() { string[] city_names = { "New York", "Los Angeles", "Chicago", "Minneapolis", "Denver", "Dallas", "Seattle", "Boston", "San Francisco", "St. Louis", "Houston", "Phoenix", "Salt Lake City" }; int tsp_size = city_names.Length; // The number of routes, which is 1 in the TSP. int num_routes = 1; // The depot is the starting node of the route. int depot = 0; // Create routing model if (tsp_size > 0) { RoutingModel routing = new RoutingModel(tsp_size, num_routes, depot); var search_parameters = RoutingModel.DefaultSearchParameters(); // Setting first solution heuristic: the // method for finding a first solution to the problem. search_parameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc; // Setting the cost function // Create the distance callback, which takes two arguments (the from and to node indices) // and returns the distance between these nodes. // routing.SetCost(new ConstantCallback()); routing.SetArcCostEvaluatorOfAllVehicles(new DistanceCallback()); var assignment = routing.SolveWithParameters(search_parameters); Console.WriteLine("Status = {0}", routing.Status()); if (assignment != null) { // Solution cost. Console.WriteLine("Total distance: = {0}", assignment.ObjectiveValue()); // Inspect solution. // Only one route here; otherwise iterate from 0 to routing.vehicles() - 1 int route_number = 0; long index = routing.Start(route_number); // Index of the variable for the starting node. string route = string.Empty; while (!routing.IsEnd(index)) { // Convert variable indices to node indices in the displayed route. route += city_names[routing.IndexToNode(index)] + " -> "; index = assignment.Value(routing.NextVar(index)); route += city_names[routing.IndexToNode(index)]; } Console.WriteLine($"Route: {route}"); } else { Console.WriteLine("No solution found."); } } }
public IList <string> GetShortestOptimizedRoute([FromBody] GeographicViewModel geographicVM) { // double[] latitude = { 13.121329, 13.065150, 13.024346, 13.027691, 12.913887, 12.915754, 12.962431, 12.890461, 12.907220, 12.954234, 13.026996, 13.041044, 13.001573 }; //double[] longitude = { 80.029049, 80.128613, 79.909573, 80.259762, 80.253067, 80.192041, 80.253839, 80.097198, 80.142088, 80.188437, 80.107756, 80.234957, 80.257616 }; string[] city_names = { "Thirunindravur", "Thiruverkadu", "Senkadu", "Milapore", "VGP Golden", "Medavakkam", "Palavakkam", "Vandalur", "Selaiyur", "Kelkattalai", "Mangadu", "TNagar", "Adyar" }; double[] latitudeandLongitude; var sCoordlatitudeandLongitude = new List <string>(); var finallatitudeandLongitude = new List <string>(); distanceArray = new double[geographicVM.Latitude.Length, geographicVM.Longitude.Length]; var k = 0; for (int i = 0; i < geographicVM.Latitude.Length; i++) { for (int j = 0; j < geographicVM.Longitude.Length; j++) { var sCoord = new GeoCoordinate(geographicVM.Latitude[i], geographicVM.Longitude[i]); var eCoord = new GeoCoordinate(geographicVM.Latitude[j], geographicVM.Longitude[j]); if (i == j) { if (i % 2 == 0) { sCoordlatitudeandLongitude.Add(sCoord.ToString() + ",D" + "," + k++); } else { sCoordlatitudeandLongitude.Add(sCoord.ToString() + ",P" + "," + k); } } var text = sCoord.GetDistanceTo(eCoord) / 1609.344; distanceArray[i, j] = Math.Round(text); } } double[,] costs = distanceArray; int num_locations = city_names.Length; RoutingModel routingModel = new RoutingModel(geographicVM.Latitude.Length, 1, 0); Solver solver = routingModel.solver(); string rank_name = "rank"; routingModel.AddConstantDimension(1, geographicVM.Latitude.Length, true, rank_name); var rank_dimension = routingModel.GetDimensionOrDie(rank_name); //Constraint MinneapolisBeforeNewYork = solver.MakeLess(routingModel.CumulVar(highPriorityVarIndex, rank_name), routingModel.CumulVar(lowPriorityVarIndex, rank_name)); /* Later needs to be worked on the Constraint for the Multiple Pickups before Delivery */ //Constraint test = routingModel.CumulVar(3, rank_name) < routingModel.CumulVar(13, rank_name); //solver.Add(test); RoutingSearchParameters search_parameters = RoutingModel.DefaultSearchParameters(); search_parameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.LocalCheapestArc; NodeEvaluator2 cost_between_nodes = new Cost(costs); routingModel.SetArcCostEvaluatorOfAllVehicles(cost_between_nodes); //oder SetVehicleCost wenn Fahrzeuge unterschiedliche Kostenmatrix haben StringBuilder route = new StringBuilder(); Assignment assignment = routingModel.SolveWithParameters(search_parameters); if (assignment != null) { Console.WriteLine("Total distance: " + assignment.ObjectiveValue().ToString() + "miles"); long index = routingModel.Start(0); //vehicle 0 route.Append("Route: "); do { //route.Append(city_names[routingModel.IndexToNode(index)] + " -> "); finallatitudeandLongitude.Add(sCoordlatitudeandLongitude[routingModel.IndexToNode(index)]); index = assignment.Value(routingModel.NextVar(index)); }while (!routingModel.IsEnd(index)); // route.Append(city_names[routingModel.IndexToNode(index)]); finallatitudeandLongitude.Add(sCoordlatitudeandLongitude[routingModel.IndexToNode(index)]); } return(finallatitudeandLongitude); }
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(); }