예제 #1
0
        public int PrintStatus(RoutingModel routing)
        {
            switch (routing.GetStatus())
            {
            case 0:
                Console.WriteLine("Problem is not solved yet.");
                return(0);

            case 1:
                Console.WriteLine("Problem is solved");
                return(1);

            case 2:
                Console.WriteLine("No solution found to the problem.");
                return(2);

            case 3:
                Console.WriteLine("Time limit reached before finding a solution.");
                return(3);

            case 4:
                Console.WriteLine("Model, model parameters, or flags are not valid.");
                return(4);

            default:
                return(-1);
            }
        }
예제 #2
0
    static void Solve(int size, int forbidden, int seed)
    {
        RoutingIndexManager manager = new RoutingIndexManager(size, 1, 0);
        RoutingModel        routing = new RoutingModel(manager);

        // Setting the cost function.
        // Put a permanent callback to the distance accessor here. The callback
        // has the following signature: ResultCallback2<int64_t, int64_t, int64_t>.
        // The two arguments are the from and to node inidices.
        RandomManhattan distances = new RandomManhattan(manager, size, seed);

        routing.SetArcCostEvaluatorOfAllVehicles(routing.RegisterTransitCallback(distances.Call));

        // 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(routing.RegisterUnaryTransitCallback((long index) =>
                                                                  { return(1); }),
                             size + 1, size + 1, true, "dummy");

        // Solve, returns a solution if any (owned by RoutingModel).
        RoutingSearchParameters search_parameters =
            operations_research_constraint_solver.DefaultRoutingSearchParameters();

        // Setting first solution heuristic (cheapest addition).
        search_parameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;

        Assignment solution = routing.SolveWithParameters(search_parameters);

        Console.WriteLine("Status = {0}", routing.GetStatus());
        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");
        }
    }
예제 #3
0
        public string GetSolverStatus()
        {
            string status       = "";
            int    solverStatus = RoutingModel.GetStatus();

            switch (solverStatus)
            {
            case 0: status = "ROUTING_NOT_SOLVED";     //Problem not solved yet
                break;

            case 1: status = "ROUTING_SUCCESS";     //Problem solved successfully.
                break;

            case 2: status = "ROUTING_FAIL";     //No solution found to the problem
                break;

            case 3: status = "ROUTING_FAIL_TIMEOUT";     //Time limit reached before finding the solution
                break;

            case 4: status = "ROUTING_INVALID";     //Model, parameter or flags are not valid
                break;
            }
            return(status);
        }