微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用或工具在车辆路线选择中的最小距离约束

如何解决使用或工具在车辆路线选择中的最小距离约束

我正在尝试使用或工具解决车辆路线问题,并且我想修改以下解决方案,以使每辆车在行驶至下一辆车之前至少覆盖100个单位距离。

到目前为止,以下是我的代码: 距离矩阵作为变量数据传递。

def print_solution(data,manager,routing,solution):
    """Prints solution on console."""
    max_route_distance = 0
    
    for vehicle_id in range(data['num_vehicles']):
        sap_index = []
        index = routing.Start(vehicle_id)
        print(routing.IsEnd(index))
        plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
        route_distance = 0
        while not routing.IsEnd(index):
            plan_output += ' {} -> '.format(manager.IndexToNode(index))
            sap_index.append(manager.IndexToNode(index))
            prevIoUs_index = index
            index = solution.Value(routing.Nextvar(index))
            route_distance += routing.GetArcCostForVehicle(
                prevIoUs_index,index,vehicle_id)
        plan_output += '{}\n'.format(manager.IndexToNode(index))
        sap_index.append(manager.IndexToNode(index))
        plan_output += 'distance of the route: {}\n'.format(route_distance)
        print(plan_output)
        for z in sap_index:
            print(sapids[z],end=" -> ")
        print("\n")
        max_route_distance = max(route_distance,max_route_distance)
    print('Maximum of the route distances: {}'.format(max_route_distance))




def main():
    """Solve the CVRP problem."""
    # Instantiate the data problem.
    data = create_data_model()

    # Create the routing index manager.
    manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),data['num_vehicles'],data['depot'])

    # Create Routing Model.
    routing = pywrapcp.RoutingModel(manager)


    # Create and register a transit callback.
    def distance_callback(from_index,to_index):
        """Returns the distance between the two nodes."""
        # Convert from routing variable Index to distance matrix NodeIndex.
        from_node = manager.IndexToNode(from_index)
        to_node = manager.IndexToNode(to_index)
        return data['distance_matrix'][from_node][to_node]

    transit_callback_index = routing.RegisterTransitCallback(distance_callback)

    # Define cost of each arc.
    routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)

    # Add distance constraint.
    dimension_name = 'distance'
    routing.AddDimension(
        transit_callback_index,# no slack
        100,# vehicle maximum travel distance
        True,# start cumul to zero
        dimension_name)
    distance_dimension = routing.GetDimensionorDie(dimension_name)
    distance_dimension.SetGlobalSpanCostCoefficient(100)

    # Setting first solution heuristic.
    search_parameters = pywrapcp.DefaultRoutingSearchParameters()
    search_parameters.first_solution_strategy = (
        routing_enums_pb2.FirstSolutionStrategy.PATH_CHEApest_ARC)

    # Solve the problem.
    solution = routing.solveWithParameters(search_parameters)

    # Print solution on console.
    if solution:
        print_solution(data,solution)


if __name__ == '__main__':
    main()

50个站点和4辆车的答案如下:

Route for vehicle 0:
 0 ->  22 ->  11 ->  21 ->  39 ->  49 ->  24 ->  41 ->  35 -> 0
distance of the route: 13

Route for vehicle 1:
 0 -> 0
distance of the route: 0

Route for vehicle 2:
 0 ->  10 ->  43 ->  38 ->  6 ->  17 ->  36 ->  37 ->  14 ->  19 ->  15 ->  20 ->  40 ->  18 ->  16 ->  34 ->  12 ->  13 ->  5 ->  7 ->  8 ->  42
-> 0
distance of the route: 20

Route for vehicle 3:
 0 ->  23 ->  27 ->  26 ->  1 ->  48 ->  46 ->  47 ->  45 ->  30 ->  2 ->  33 ->  32 ->  31 ->  9 ->  28 ->  25 ->  29 ->  3 ->  44 ->  4 -> 0
distance of the route: 25

Maximum of the route distances: 25

def print_solution函数中,我尝试在while循环中与route_distance < 100一起赋予not routing.IsEnd(index)条件,但这没有用。

需要帮助!

解决方法

这是一个坏主意。您将很容易产生不可行的问题。 您需要获取每辆车的结束变量,并在该车辆上添加一个软的下限。

请参见this doc entry

,

在您的示例中,您已使用:

       100,# vehicle maximum travel distance

即每辆车的硬上限是100,那么您如何期望车辆行驶超过其极限?

您还应该注释掉GlobalSpan系数,该系数当前可激励求解器限制最大路径长度(这是此处的主要因素)...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。