static void Solve(int size, int forbidden, int seed) { RoutingModel routing = new RoutingModel(size, 1); // Setting first solution heuristic (cheapest addition). routing.SetFirstSolutionStrategy(RoutingModel.ROUTING_PATH_CHEAPEST_ARC); // Setting the cost function. // Put a permanent callback to the distance accessor here. The callback // has the following signature: ResultCallback2<int64, int64, int64>. // The two arguments are the from and to node inidices. RandomManhattan distances = new RandomManhattan(size, seed); routing.SetCost(distances); // Forbid node connections (randomly). Random randomizer = new Random(); long forbidden_connections = 0; while (forbidden_connections < forbidden) { long from = randomizer.Next(size - 1); long to = randomizer.Next(size - 1) + 1; if (routing.NextVar(from).Contains(to)) { Console.WriteLine("Forbidding connection {0} -> {1}", from, to); routing.NextVar(from).RemoveValue(to); ++forbidden_connections; } } // Add dummy dimension to test API. routing.AddDimension(new ConstantCallback(), size + 1, size + 1, true, "dummy"); // Solve, returns a solution if any (owned by RoutingModel). Assignment solution = routing.Solve(); if (solution != null) { // Solution cost. Console.WriteLine("Cost = {0}", solution.ObjectiveValue()); // Inspect solution. // Only one route here; otherwise iterate from 0 to routing.vehicles() - 1 int route_number = 0; for (long node = routing.Start(route_number); !routing.IsEnd(node); node = solution.Value(routing.NextVar(node))) { Console.Write("{0} -> ", node); } Console.WriteLine("0"); } }
private static NodeEvaluator2 SetCost(FeatureCollection collection, RoutingModel routing) { var coordinates = collection .Features .Select(x => x.Geometry) .Cast <Point>() .Select(x => x.Coordinates) .Cast <GeographicPosition>() .ToArray(); var nodeEvaluator = new HaversineDistanceEvaluator(coordinates); routing.SetCost(nodeEvaluator); return(nodeEvaluator); }
public override void Solve() { var pparser = new Pparser(FpatIn); int n, l; pparser.Fetch(out n, out l); var rgnode = pparser.FetchN <string>(n); rgnode.Insert(0, "X"); var model = new RoutingModel(n, 1); model.SetFirstSolutionStrategy(RoutingModel.ROUTING_GLOBAL_CHEAPEST_ARC); //model.SetMetaheuristic(RoutingModel.ROUTING_TABU_SEARCH); model.SetCost(new NodeEval(rgnode.ToArray())); model.SetDepot(0); for (int i = 0; i < n; i++) { var varI = model.NextVar(i); for (int j = 0; j < n; j++) { if (i == j) { continue; } if (!NodeEval.FMatch(rgnode[i], rgnode[j])) { varI.RemoveValue(j); } } } Console.WriteLine("solving"); Assignment solution = model.Solve(); model.UpdateTimeLimit(1000 * 60 * 3); if (solution != null) { // Solution cost. Console.WriteLine("Cost = {0}", solution.ObjectiveValue()); for (var inode = (int)model.Start(0); !model.IsEnd(inode); inode = (int)solution.Value(model.NextVar(inode))) { Console.WriteLine(rgnode[inode]); } Console.WriteLine("0"); } }
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()); }
/* * Number of Vehicles: */ public static void Solve(int vehicles, string pathToXml = null) { /* * Add custom distance function */ var dist = (pathToXml == null) ? new Distance() : new XmlDistance(pathToXml); /* * Generate constraint model */ // Third argument defines depot, i.e. start-end node for round trip. var model = new RoutingModel(dist.MapSize(), vehicles, 0); model.SetCost(dist); /* * This modification forces all Vehicles to visit at least one city. */ /*for (int i = 0; i < Vehicles; i++) { * * IntVar first = model.NextVar(model.Start(i)); * first.SetMax(dist.MapSize() - 1); * } * * /* * Solve problem and display solution */ Assignment assignment = model.Solve(); if (assignment != null) { Console.WriteLine("Total Distance: " + assignment.ObjectiveValue() + "\n"); for (int i = 0; i < vehicles; i++) { /* * Display Round Trip: */ Console.WriteLine("Round Trip for Vehicle " + i + "\n"); for (long node = model.Start(i); node < model.End(i); node = model.Next(assignment, node)) { Console.Write(node + " -> "); } Console.WriteLine(model.Start(i) + "\n"); /* * Display individual Section Distances for Verification: */ var source = (int)model.Start(i); while (source < model.End(i)) { var target = (int)model.Next(assignment, source); if (source < dist.MapSize() && target < dist.MapSize()) { Console.WriteLine("From " + source + " travel to " + target + " -> distance = " + dist.Run(source, target)); } else if (source < dist.MapSize()) { Console.WriteLine("From " + source + " travel to 0 -> distance = " + dist.Run(source, 0)); } source = target; } Console.WriteLine("\n"); } } Console.ReadKey(); }