问题描述
我正在使用ortools优化路线,并且在尝试针对某些特定条件进行优化并在计算出vrp问题的解决方案后为用户提供输出选项时遇到问题。
要做到这一点,我想出的解决方案是解决最佳路线问题,并保留所有解决方案,这样我以后便可以仔细研究并检查其中的任何一个,并满足其中的某些特定条件并将其分开。所以我的问题是,如何保存所有在python列表中得到的解决方案?
我尝试使用收集器,但没有成功,该部分在这里:
def main():
"""Solve the VRP with time windows."""
data = create_data_model()
# Create the routing index manager.
manager = pywrapcp.RoutingIndexManager(len(data['time_matrix']),data['num_vehicles'],data['starts'],data['ends'])
# Create Routing Model.
routing = pywrapcp.RoutingModel(manager)
solver = routing.solver()
collector = solver.AllSolutionCollector()
routing.AddSearchMonitor(collector)
我的期望是,有了这个收集器对象,我可以访问所有解决方案并将它们放在类似以下的列表中:
解决方案= [[0,3,1,2],[0,2,3,1],[0,1,3,2]]
BestSolution = [0,1,3,2]
让我知道你们需要我发布些什么来帮助我。 感谢您的提前帮助。
完整代码:
def main():
"""Solve the VRP with time windows."""
#start = timee.clock()
# Instantiate the data problem.
data = create_data_model()
# Create the routing index manager.
manager = pywrapcp.RoutingIndexManager(len(data['time_matrix']),data['ends'])
# Create Routing Model.
routing = pywrapcp.RoutingModel(manager)
solver = routing.solver()
collector = solver.AllSolutionCollector()
routing.AddSearchMonitor(collector)
#sm = pywrapcp.SearchMonitor(solver)
#collector = pywrapcp.solutionCollector(sm)
# Create and register a transit callback.
def time_callback(from_index,to_index):
"""Returns the travel time between the two nodes."""
# Convert from routing variable Index to time matrix NodeIndex.
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['time_matrix'][from_node][to_node]
transit_callback_index = routing.RegisterTransitCallback(time_callback)
# Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
start_time = timee()
solution_curve = list()
COST_MULTIPLIER = 1e4
def record_solution():
t = timee() - start_time
solution_curve.append((t,routing.Costvar().Max() / COST_MULTIPLIER))
routing.AddAtSolutionCallback(record_solution)
# Add Time Windows constraint.
time = 'Time'
routing.AddDimension(
transit_callback_index,# allow waiting time
15000,# maximum time per vehicle
False,# Don't force start cumul to zero.
time)
time_dimension = routing.GetDimensionorDie(time)
time_dimension.SetGlobalSpanCostCoefficient(1)
# Instantiate route start and end times to produce feasible times.
for i in range(data['num_vehicles']):
routing.AddVariableMinimizedByFinalizer(
time_dimension.CumulVar(routing.Start(i)))
routing.AddVariableMinimizedByFinalizer(
time_dimension.CumulVar(routing.End(i)))
# 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(f'COLLECTION = {collector.solution(0)}')
if solution:
for i in range(collector.solutionCount()):
solution_n = collector.solution(i)
print_solution(data,manager,routing,solution_n)
routes = get_routes(solution_n,manager)
# display the routes.
for i,route in enumerate(routes):
print('Route',i,route)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)