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