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(); }
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(); }