问题描述
我正在使用 OR-Tools 解决多站点多车辆车辆路径问题,到目前为止,我已经成功建立了一个模型,其中一辆车从每个站点离开,并且它们在它们之间的最大时间允许内访问每个节点然后通过虚拟节点 0 退出。 然而,由于这是一个现实世界的问题,在每个节点,车辆都需要停下设定的时间来执行一项服务,每个节点都有一个设定的等待时间,例如在下表中:
节点 | 持续时间 |
---|---|
4 | 9 |
38 | 8 |
... | ... |
8545 | 17 |
我不确定如何将这些添加到每个节点,尽管到目前为止节点之间的弧长表示每个节点之间的行驶时间(假设所有车辆都以相同的速度行驶)。所以我的问题是如何将这些等待时间添加到模型中,同时仍然最大限度地减少每辆车的行驶时间并将它们保持在 25200 秒的最大阈值以下(对应于 7 小时轮班)。我在下面包含了我的工作代码:
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
距离矩阵是从一个像这样布局的 csv 中输入的:
空白 | 4 | 38 | 71 | 90 | 94 | ... | 8545 |
---|---|---|---|---|---|---|---|
4 | 0 | 1280 | 1762 | 1406 | 1589 | ... | 1017 |
38 | 1280 | 0 | 681 | 202 | 385 | ... | 1433 |
71 | 1762 | 681 | 0 | 503 | 0 | ... | 0 |
90 | 1406 | 202 | 503 | 0 | 0 | ... | 1559 |
94 | 1589 | 385 | 0 | 0 | 0 | ... | 1742 |
.. | .. | ... | ... | ... | ... | 0 | ... |
8545 | 1017 | 1433 | 0 | 1559 | 1742 | ... | 0 |
Caredist_Matrix = np.loadtxt(open("Caredistances-FULL.csv","rb"),dtype=int,delimiter=",",skiprows=1)
Caredist_Matrix
模型主体如下:
def create_data_model():
"""Stores the data for the problem."""
data = {}
data['distance_matrix'] = Caredist_Matrix
data['num_vehicles'] = 6
data['starts'] = [3,7,14,104,185,220]
data['ends'] = [0,0]
return data
def print_solution(data,manager,routing,solution):
"""Prints solution on console."""
max_route_distance = 0
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
route_distance = 0
while not routing.IsEnd(index):
plan_output += ' {} -> '.format(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))
plan_output += 'distance of the route: {}m\n'.format(route_distance)
print(plan_output)
max_route_distance = max(route_distance,max_route_distance)
print('Maximum of the route distances: {}m'.format(max_route_distance))
"""Entry point of the program."""
# Instantiate the data problem.
data = create_data_model()
# Create the routing index manager.
manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),data['num_vehicles'],data['starts'],data['ends'])
# 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
25200,# 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()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)