具有累积成本最小化的 VRP 的 OR 工具

问题描述

我正在使用 OR 工具来解决 VRP,没有任何限制。这是源代码

def create_data_model():
    """Stores the data for the problem."""
    data = {}
    data['distance_matrix'] = [
        [0,20079,2613,8005,19277,12468,13701],[0,21285,16012,32574,35394,28806],18233,5392,19965,19650,13064],15013,5639,22883,22570,15982],32991,19256,21815,18414,9112],34348,16976,23122,15678,14647],27652,13917,16476,8043,14820,0]
    ]
    data['num_vehicles'] = 6
    data['depot'] = 0
    return data

def test(request):
    # 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)
    
    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)
    
    routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)

    dimension_name = 'distance'
    routing.AddDimension(
        transit_callback_index,# no slack
        1000000000,# vehicle maximum travel distance
        True,# start cumul to zero
        dimension_name)
    distance_dimension = routing.GetDimensionorDie(dimension_name)
    distance_dimension.SetGlobalSpanCostCoefficient(1)
    
    # 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,manager,routing,solution)

    return HttpResponse('')

这就像一个魅力,除了在我的情况下,每单位距离的成本不是线性的。说 0-5 公里,它的成本是 10 卢比/公里。然后 5-10 公里,费用为 25 卢比/公里。

这意味着单个距离矩阵不再适用,我必须做其他事情。

有什么办法可以做到这一点吗?提前致谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)