Example #1
0
        static void TimeWindowInit(Day day,
                                   RoutingModel routing,
                                   RoutingIndexManager manager)
        {
            RoutingDimension timeDimension = routing.GetMutableDimension("Time");

            timeDimension.SetGlobalSpanCostCoefficient(100);

            // Add time window constraints for each location except depot.
            for (int i = 1; i < day.TimeWindows.GetLength(0); i++)
            {
                long index = manager.NodeToIndex(i);
                timeDimension.CumulVar(index).SetRange(
                    day.TimeWindows[i, 0],
                    day.TimeWindows[i, 1]);
            }

            // Add time window constraints for each vehicle start node.
            for (int i = 0; i < day.Vehicles.Count; i++)
            {
                long index = routing.Start(i);
                timeDimension.CumulVar(index).SetRange(
                    day.TimeWindows[0, 0],
                    day.TimeWindows[0, 1]);

                routing.AddVariableMinimizedByFinalizer(
                    timeDimension.CumulVar(routing.Start(i)));
                routing.AddVariableMinimizedByFinalizer(
                    timeDimension.CumulVar(routing.End(i)));
            }
        }
Example #2
0
        /// <summary>
        ///   Print the solution.
        /// </summary>
        static void VRP_LongestSingleRoute()
        {
            Console.WriteLine("VRP_LongestSingleRoute!");
            // Instantiate the data problem.
            ORDataModel data = new ORDataModel();

            // public RoutingIndexManager(int num_nodes, int num_vehicles, int depot);
            //public RoutingIndexManager(int num_nodes, int num_vehicles, int[] starts, int[] ends);

            // Create Routing Index Manager
            RoutingIndexManager manager = new RoutingIndexManager(
                data.DistanceMatrix.GetLength(0),
                data.VehicleNumber,
                data.Depot);


            // Create Routing Model.
            RoutingModel routing = new RoutingModel(manager);

            // Create and register a transit callback.
            int transitCallbackIndex = routing.RegisterTransitCallback(
                (long fromIndex, long toIndex) => {
                // Convert from routing variable Index to distance matrix NodeIndex.
                var fromNode = manager.IndexToNode(fromIndex);
                var toNode   = manager.IndexToNode(toIndex);
                return(data.DistanceMatrix[fromNode, toNode]);
            }
                );

            // Define cost of each arc.
            routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);

            // Add Distance constraint.
            routing.AddDimension(transitCallbackIndex, 0, 3000,
                                 true, // start cumul to zero
                                 "Distance");
            RoutingDimension distanceDimension = routing.GetMutableDimension("Distance");

            distanceDimension.SetGlobalSpanCostCoefficient(100);

            // Setting first solution heuristic.
            RoutingSearchParameters searchParameters =
                operations_research_constraint_solver.DefaultRoutingSearchParameters();

            searchParameters.FirstSolutionStrategy =
                FirstSolutionStrategy.Types.Value.PathCheapestArc;

            // Solve the problem.
            Assignment solution = routing.SolveWithParameters(searchParameters);

            // Print solution on console.
            PrintSolutionPathCheapest(data, routing, manager, solution);
        }
Example #3
0
    public static void Main(String[] args)
    {
        // Instantiate the data problem.
        DataModel data = new DataModel();

        // Create Routing Index Manager
        RoutingIndexManager manager = new RoutingIndexManager(
            data.DistanceMatrix.GetLength(0),
            data.VehicleNumber,
            data.Depot);

        // Create Routing Model.
        RoutingModel routing = new RoutingModel(manager);

        // Create and register a transit callback.
        int transitCallbackIndex = routing.RegisterTransitCallback(
            (long fromIndex, long toIndex) =>
        {
            // Convert from routing variable Index to distance matrix NodeIndex.
            var fromNode = manager.IndexToNode(fromIndex);
            var toNode   = manager.IndexToNode(toIndex);
            return(data.DistanceMatrix[fromNode, toNode]);
        }
            );

        // Define cost of each arc.
        routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);

        // Add Distance constraint.
        routing.AddDimension(
            transitCallbackIndex,
            0,
            3000,
            true, // start cumul to zero
            "Distance");
        RoutingDimension distanceDimension = routing.GetMutableDimension("Distance");

        distanceDimension.SetGlobalSpanCostCoefficient(100);

        // Define Transportation Requests.
        Solver solver = routing.solver();

        for (int i = 0; i < data.PickupsDeliveries.GetLength(0); i++)
        {
            long pickupIndex   = manager.NodeToIndex(data.PickupsDeliveries[i][0]);
            long deliveryIndex = manager.NodeToIndex(data.PickupsDeliveries[i][1]);
            routing.AddPickupAndDelivery(pickupIndex, deliveryIndex);
            solver.Add(solver.MakeEquality(
                           routing.VehicleVar(pickupIndex),
                           routing.VehicleVar(deliveryIndex)));
            solver.Add(solver.MakeLessOrEqual(
                           distanceDimension.CumulVar(pickupIndex),
                           distanceDimension.CumulVar(deliveryIndex)));
        }

        // Setting first solution heuristic.
        RoutingSearchParameters searchParameters =
            operations_research_constraint_solver.DefaultRoutingSearchParameters();

        searchParameters.FirstSolutionStrategy =
            FirstSolutionStrategy.Types.Value.PathCheapestArc;

        // Solve the problem.
        Assignment solution = routing.SolveWithParameters(searchParameters);

        // Print solution on console.
        PrintSolution(data, routing, manager, solution);
    }
Example #4
0
        //IntVar x;
        //IntVar y;//Reduntant

        public void SolveVrpProblem(Day day, ConfigParams cfg, VrpProblem vrpProblem, IDataOutput dataOutput, int[] VCMinMax)
        {
            this.day = day;
            this.cfg = cfg;
            //Google Distance Matrix API (Duration matrix)


            // Create Routing Index Manager
            manager = new RoutingIndexManager(
                day.TimeMatrix.GetLength(0),
                day.Vehicles.Count,
                day.Depot);


            // Create Routing Model.
            routing = new RoutingModel(manager);

            // Create and register a transit callback.
            int transitCallbackIndex = routing.RegisterTransitCallback(
                (long fromIndex, long toIndex) =>
            {
                // Convert from routing variable Index to distance matrix NodeIndex.
                var fromNode = manager.IndexToNode(fromIndex);
                var toNode   = manager.IndexToNode(toIndex);
                return(day.TimeMatrix[fromNode, toNode]);
            }
                );

            // Define cost of each arc.
            routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);

            // Add Distance constraint.

            if (day.TimeWindowsActive != true)
            {
                routing.AddDimension(transitCallbackIndex, 0, 700000,
                                     true, // start cumul to zero
                                     "Distance");
                RoutingDimension distanceDimension = routing.GetMutableDimension("Distance");
                distanceDimension.SetGlobalSpanCostCoefficient(100);
            }
            else
            {
                routing.AddDimension(
                    transitCallbackIndex, // transit callback
                    1000,                 // allow waiting time
                    600,                  // vehicle maximum capacities
                    false,                // start cumul to zero
                    "Time");

                TimeWindowInit(day, routing, manager);//Set Time Window Constraints
            }
            if (day.MaxVisitsActive != 0)
            {
                int demandCallbackIndex = routing.RegisterUnaryTransitCallback(
                    (long fromIndex) => {
                    // Convert from routing variable Index to demand NodeIndex.
                    var fromNode = manager.IndexToNode(fromIndex);
                    return(day.Demands[fromNode]);
                }
                    );

                routing.AddDimensionWithVehicleCapacity(
                    demandCallbackIndex, 0,  // null capacity slack
                    day.VehicleCapacities,   // vehicle maximum capacities
                    true,                    // start cumul to zero
                    "Capacity");
            }

            // Allow to drop nodes.
            for (int i = 1; i < day.TimeMatrix.GetLength(0); ++i)
            {
                routing.AddDisjunction(
                    new long[] { manager.NodeToIndex(i) }, day.Locations.ElementAt(i).Penalty);
            }

            // Setting first solution heuristic.
            RoutingSearchParameters searchParameters =
                operations_research_constraint_solver.DefaultRoutingSearchParameters();


            searchParameters.FirstSolutionStrategy =
                FirstSolutionStrategy.Types.Value.PathMostConstrainedArc;

            //metaheuristic
            searchParameters.LocalSearchMetaheuristic = LocalSearchMetaheuristic.Types.Value.GuidedLocalSearch;
            searchParameters.TimeLimit = new Duration {
                Seconds = cfg.SolutionDuration
            };
            searchParameters.LogSearch = true;

            solver = routing.solver();

            //TODO
            //Some location must be on same route.
            //solver.Add(routing.VehicleVar(manager.NodeToIndex(2)) == routing.VehicleVar(manager.NodeToIndex(5)));
            //One node takes precedence over the another.
            //routing.AddPickupAndDelivery(manager.NodeToIndex(2), manager.NodeToIndex(5));

            //Constraint variable
            //x = solver.MakeIntVar(day.Vehicles.Count, day.Vehicles.Count, "x");

            //Number of vehicle restriction - old version
            //solver.Add(x < 7);

            //Number of vehicle restriction -new version
            //y = solver.MakeIntVar(routing.Vehicles(), routing.Vehicles(), "y");
            //solver.Add(y <= VCMinMax[1]);//Reduntant

            // Solve the problem.
            solution = routing.SolveWithParameters(searchParameters);



            day.LocationDropped = false;

            // Display dropped nodes.
            List <int> droppedNodes = new List <int>();

            for (int index = 0; index < routing.Size(); ++index)
            {
                if (routing.IsStart(index) || routing.IsEnd(index))
                {
                    continue;
                }
                if (solution.Value(routing.NextVar(index)) == index)
                {
                    droppedNodes.Add(manager.IndexToNode((int)index));
                    day.LocationDropped = true;
                }
            }
            day.DroppedLocations.Clear();
            Console.WriteLine(day.Locations.ElementAt(0).Name);
            if (droppedNodes != null)
            {
                foreach (var item in droppedNodes)
                {
                    Location location = LocationDB.Locations.Where(x => x.Position.strPos_ == day.Addresses[item]).ToList().ElementAt(0);

                    if (location != null)
                    {
                        Console.WriteLine(location.Name);
                        day.DroppedLocations.Add(location);
                    }
                }
            }
            List <int> AssignedNodes = new List <int>();

            Console.WriteLine(manager.GetNumberOfNodes());


            //Inspect Infeasable Nodes
            for (int i = 0; i < day.Vehicles.Count; i++)
            {
                var index = routing.Start(i);
                int j     = 0;
                while (routing.IsEnd(index) == false)
                {
                    j++;

                    index = solution.Value(routing.NextVar(index));
                }
                if (j == 1)
                {
                    day.InfeasibleNodes = true;
                    foreach (var item in day.DroppedLocations)
                    {
                        LocationDB.Locations.Where(x => x.ClientRef == item.ClientRef).ToList().ElementAt(0).Infeasible = true;
                    }
                    if (day.Vehicles.Count - 1 >= 1)
                    {
                        day.SetVehicleNumber(day.Vehicles.Count - 1);
                        day.ResetResults();

                        vrpProblem.SolveVrpProblem(day, cfg, vrpProblem, dataOutput, VCMinMax);
                    }
                    return;
                }
            }

            // Inspect solution.
            day.TotalDur = 0;
            day.MinDur   = 100000;
            for (int i = 0; i < day.Vehicles.Count; i++)
            {
                long routeDuration = 0;

                var index = routing.Start(i);

                while (routing.IsEnd(index) == false)
                {
                    var previousIndex = index;

                    index = solution.Value(routing.NextVar(index));

                    routeDuration += routing.GetArcCostForVehicle(previousIndex, index, 0);
                }
                day.TotalDur += routeDuration;
                day.MaxDur    = Math.Max(routeDuration, day.MaxDur);
                day.MinDur    = Math.Min(routeDuration, day.MinDur);
            }
            day.AvgDur = day.TotalDur / day.Vehicles.Count;
        }