如何解决具有累积成本最小化的 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 举报,一经查实,本站将立刻删除。