首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Or-Tools Routing - Solution为空

Google Or-Tools Routing - Solution为空
EN

Stack Overflow用户
提问于 2020-07-06 18:44:31
回答 1查看 266关注 0票数 0

我正在尝试实现谷歌或工具的VRPTW。但我正面临着一个问题。当我传递动态时间矩阵时,解决方案对象为空,但是当我传递示例中给出的时间矩阵时,它就起作用了。

以下是我的代码

代码语言:javascript
复制
 public class DataModel
    {
        public long[,] DistanceMatrix { get; set; }
        public long[,] TimeMatrix = {
            //commented matrix is dynamic generated
  // {0,5,20,10,0,5},  
  //{5,0,25,10,5,5},
  //{20,25,0,30,20,20},
  //{10,10,30,0,10,15},
  //{0,5,20,10,0,5},
  //{5,5,20,15,5,0},
    {0, 6, 9, 8, 7, 3},
  {6, 0, 8, 3, 2, 6},
  {9, 8, 0, 11, 10, 6},
  {8, 3, 11, 0, 1, 7},
  {7, 2, 10, 1, 0, 6},
  {3, 6, 6, 7, 6, 0},
};
        public long[,] TimeWindows = {
  {0, 5},    // depot
  {7, 12},   // 1
  {10, 15},  // 2
  {16, 18},  // 3
  {10, 13},  // 4
  {0, 5},    // 5
};
        public int VehicleNumber = 3;
        public int Depot = 0;
    };

下面是主要的函数代码

代码语言:javascript
复制
DataModel data = new DataModel();
            // data.TimeMatrix = TimeMatrix;
            // Create Routing Index Manager
            RoutingIndexManager manager = new RoutingIndexManager(
                data.TimeMatrix.GetLength(0),
                data.VehicleNumber,
                data.Depot);

            // Create a 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.TimeMatrix[fromNode, toNode];
                }
                );

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

            // Add Distance constraint.
            routing.AddDimension(
                transitCallbackIndex, // transit callback
                30, // allow waiting time
                30, // vehicle maximum capacities
                false,  // start cumul to zero
                "Time");
            RoutingDimension timeDimension = routing.GetMutableDimension("Time");
            // Add time window constraints for each location except depot.
            for (int i = 1; i < data.TimeWindows.GetLength(0); ++i)
            {
                long index = manager.NodeToIndex(i);
                timeDimension.CumulVar(index).SetRange(
                    data.TimeWindows[i, 0],
                    data.TimeWindows[i, 1]);
            }
            // Add time window constraints for each vehicle start node.
            for (int i = 0; i < data.VehicleNumber; ++i)
            {
                long index = routing.Start(i);
                timeDimension.CumulVar(index).SetRange(
                    data.TimeWindows[0, 0],
                    data.TimeWindows[0, 1]);
            }

            // Instantiate route start and end times to produce feasible times.
            for (int i = 0; i < data.VehicleNumber; ++i)
            {
                routing.AddVariableMinimizedByFinalizer(
                    timeDimension.CumulVar(routing.Start(i)));
                routing.AddVariableMinimizedByFinalizer(
                    timeDimension.CumulVar(routing.End(i)));
            }

            // 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);  //it is null when dynamic time matrix is used, but it is not null when time matrix mentioned in example is used.

问题似乎出在AddDimension方法中。我对此感到震惊,但找不到任何解决方案。请提出任何解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-06 19:28:15

solution is null表示求解器找不到任何可行的解。很可能你的时间窗口太紧了。

要么试着放松你的时间窗

或者确保节点是可选的(使用addDisjunction)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62754399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档