在谷歌或求解器中没有给出距离矩阵的最近距离

问题描述

我正在使用谷歌或工具从距离矩阵中获取根,但我的限制是从
开始 最近的距离,但求解器给出了意想不到的路线。低于我的距离矩阵。我的预期输出是根据距离按顺序 1 到 10 个。

        data['distance_matrix']=[[1000.    451.13  508.64  543.41  577.64  611.88  646.12  672.58  689.2
              1231.78 1246.69]  
             [ 451.13 1000.    484.56  519.33  553.56  587.8   622.04  648.5   665.12
              1207.7  1222.61]  
             [ 508.64  460.8  1000.    485.58  519.81  554.05  588.29  614.75  631.37
              1173.95 1188.86]  
             [ 543.41  483.77  473.78 1000.    496.84  531.08  565.32  591.78  608.4
              1150.98 1165.89]  
             [ 577.64  506.5   496.51  485.34 1000.    508.35  542.59  569.05  585.67
              1128.25 1143.16]  
             [ 611.88  529.23  519.24  508.07  496.84 1000.    519.86  546.32  562.94
              1105.52 1120.43]  
             [ 646.12  551.96  541.97  530.8   519.57  508.35 1000.    523.59  540.21
              1082.79 1097.7 ]  
             [ 672.58  571.36  561.37  550.2   538.97  527.75  516.53 1000.    520.81
              1063.39 1078.3 ]  
             [ 689.2   587.98  577.99  566.82  555.59  544.37  533.15  520.81 1000.
              1046.77 1061.68]    
             [1231.78 1154.62 1144.63 1133.46 1122.23 1111.01 1099.79 1087.45 1070.83
              1000.    495.04]  
             [1246.69 1171.32 1161.33 1150.16 1138.93 1127.71 1116.49 1104.15 1087.53
               496.83   10.  ]]    
        manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),data['num_vehicles'],data['depot'])
                        routing = pywrapcp.RoutingModel(manager)  
            ...  
            transit_callback_index = routing.RegisterTransitCallback(distance_callback)  
            
            routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)  
            search_parameters = pywrapcp.DefaultRoutingSearchParameters()  
            search_parameters.first_solution_strategy = (  
            routing_enums_pb2.FirstSolutionStrategy.PATH_CHEApest_ARC)  
            solution= routing.solveWithParameters(search_parameters)

... 有没有办法向求解器添加约束以根据距离值顺序获取路线。如果距离值是 4、3、2,那么我的路线点应该是 4、3、2。?

解决方法

如果 j 是 nextVar(i) 然后 transit(i-1,i) >= transit(i,j),你的意思是所有的 i 吗?

solver = routing.solver()
for index in range(routing.size()):
  if routing.IsEnd(index):
    continue
  node = manager.IndexToNode(index)
  for next_index in range(routing.size()):
    if routing.IsEnd(next_index) or routing.IsStart(next_index):
      continue
    next_node = manager.IndexToNode(next_index)
    first_cond = routing.NextVar(index) == next_index
    for next_next_index in range(routing.size()):
      if routing.IsStart(next_next_index):
        continue
      next_next_node = manager.IndexToNode(next_next_index)
      second_cond = routing.NextVar(next_index) == next_next_index
      sovler.Add(first_cond * second_cond * distance_matrix[node][next_node] >= first_cond * second_cond * distance_matrix[next_node][next_next_node])