我正在尝试使用或工具解决车辆路线问题,并且我想修改以下解决方案,以使每辆车在行驶至下一辆车之前至少覆盖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,
0, # 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, manager, routing, 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)
条件,但这没有用。
需要帮助!
答案 0 :(得分:1)
这是一个坏主意。您将很容易产生不可行的问题。 您需要获取每辆车的结束变量,并在该车辆上添加一个软的下限。
答案 1 :(得分:1)
在您的示例中,您已使用:
100, # vehicle maximum travel distance
即每辆车的硬上限是100,那么您如何期望车辆行驶超过其极限?
您还应该注释掉GlobalSpan
系数,该系数当前可激励求解器限制最大路径长度(这是此处的主要因素)...