需求:
给定需要经过的点,可以花费的总天数,生成行程。需要满足每天不超过 x h , 每个点需要留下一定长度的停留时间,总的路线合理不绕路
思路:
通过选择合适的模型,将需要的场景转化为模型对应的变量,在优化一个总的点的情况下添加合适的约束,从而利用模型实现需要的算法场景
思路解析:
只能优化一个方向,但是可以在多个约束条件下
比如,总花费时间最少的条件下,满足各运输车辆的载重总量符合要求,在每个点放下足够的货物or需要停留一定的时间
可以将不同需求转化为相同的维度,比如都转化为对时间的优化。总距离最短相当于一定的速度下总时间最少;每个地点需要停留一定的时间可以转化为有多少“时间的货物”需要分配给每个地点;每天有多少时间可以分配可以转化为每辆车可以运输多少“时间的货物”; 需要几天去完成这些工作,可以转化为需要几辆车来运输。这样问题就全部转化到了时间的维度了,向时间最少的角度优化即可。
可能适用的模型:
- 带时间窗车辆路由问题
- 多车场车辆路由问题
- Vehicle Routing Problems with Time Windows (VRPTW), 时间限制,车辆必须在一定时间内开始或完成任务。
基础知识:
路径规划的综述博客:https://blog.csdn.net/dazhushenxu/article/details/77833023
TSP旅行商问题: http://baijiahao.baidu.com/s?id=1642936131231091670&wfr=spider&for=pc
曼哈顿距离(Manhattan Distance):是种使用在几何度量空间的几何学用语,用以标明两个点在标准坐标系上的绝对轴距总和。
运筹优化工具Google Ortools 解决TSP问题
OR-Tools
官方文档https://developers.google.cn/optimization/routing
目录https://zhuanlan.zhihu.com/c_157167393
运筹优化工具google ortools简介https://zhuanlan.zhihu.com/p/55089642
安装 https://developers.google.cn/optimization/install
1. 路由问题
https://zhuanlan.zhihu.com/p/55867181
https://developers.google.cn/optimization/routing/tsp
2. VPR问题
https://zhuanlan.zhihu.com/p/55868067
https://developers.google.cn/optimization/routing/vrp
3.容量约束VRP问题 CVRP
https://zhuanlan.zhihu.com/p/55868853
https://developers.google.cn/optimization/routing/cvrp
4.时间窗约束VRP问题 VRPTWs
https://zhuanlan.zhihu.com/p/55869275
https://developers.google.cn/optimization/routing/vrptw
5.维度:
https://developers.google.cn/optimization/routing/dimensions
6.资源约束VRP问题 VRRC
https://zhuanlan.zhihu.com/p/55869490
https://developers.google.cn/optimization/routing/cvrptw_resources
7.VRP中的一些常见问题
https://zhuanlan.zhihu.com/p/55869908
https://developers.google.cn/optimization/routing/routing_tasks
实现方法:
ortools
以Vehicles Routing Problem (VRP) with Resource Constraints为基础
代码逻辑:
1. 输入需要的参数,并创建参数字典data
2.创建路线索引管理器
3.创建路线模型
4.注册各种限制和约束条件
注册主优化方向
①时间窗口约束限制 (根据距离换算出的时间矩阵)
time_callback_index = routing.RegisterTransitCallback(time_callback)
routing.SetArcCostEvaluatorOfAllVehicles(time_callback_index)
添加各种维度的限制
②添加时间变量 每天允许的时间
time_dimension = routing.GetDimensionOrDie(time)
③添加时间变量的累积限制 时间窗限制
time_dimension.CumulVar(index).SetRange(time_window[0], time_window[1])
routing.AddVariableMinimizedByFinalizer(
time_dimension.CumulVar(routing.Start(i)))
routing.AddVariableMinimizedByFinalizer(
time_dimension.CumulVar(routing.End(i)))
注册车载容量限制
④添加车载容量限制
demand_callback_index = routing.RegisterUnaryTransitCallback(
demand_callback)
routing.AddDimensionWithVehicleCapacity(
demand_callback_index,
0, # null capacity slack
data['vehicle_capacities'], # vehicle maximum capacities
True, # start cumul to zero
'Capacity')
5.设置解决方案策略的参数
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.time_limit.seconds = 10
search_parameters.solution_limit = 30
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
6.计算解决方案
assignment = routing.SolveWithParameters(search_parameters)
7.整理结果为字典形式
print_solution_capacity_time(data, manager, routing, assignment)
8.输出结果
如果有结果assignment,则返回字典;
否则返回空字典
参考文献:
https://wenku.baidu.com/view/44df9c72f71fb7360b4c2e3f5727a5e9856a27f1.html