为您的物流提供免费、智能的路线选择–现在在python上
elasticroute的Python项目详细描述
Python的ElasticRoute
用于解决大规模旅行推销员/车队路线问题的api
你的车队只有10辆车,可以为这个城市的500个景点服务。有些车辆只在白天可用。有些车站只能在晚上服务。你将如何解决这个问题?
你不需要。只要给我们一份停车场、车辆和停车场的清单,我们就可以为你做重担了。作为服务路由!
beta relase:ElasticRoute在2020年4月30日前完全免费使用!
快速入门指南
使用pip安装:
pip install elasticroute
在代码中,设置默认的api密钥(可以从web应用程序的仪表板中检索):
importelasticrouteaserer.defaults.API_KEY="my_super_secret_key"
创建一个新的Plan
对象并给它一个名称/id:
plan=er.Plan()plan.id="my_first_plan"
给我们一系列的站:
plan.stops=[{"name":"Changi Airport","address":"80 Airport Boulevard (S)819642",},{"name":"Gardens By the Bay","lat":"1.281407","lng":"103.865770",},# add more stops!# both human-readable addresses and machine-friendly coordinates work!]
为我们提供一系列可用车辆:
plan.vehicles=[{"name":"Van 1"},{"name":"Van 2"},]
给我们一系列仓库:
plan.depots=[{"name":"Main Warehouse","address":"61 Kaki Bukit Ave 1 #04-34, Shun Li Ind Park Singapore 417943",},]
设置您的国家/地区和时区(用于精确的地理编码):
plan.generalSettings["country"]="SG"plan.generalSettings["timezone"]="Asia/Singapore"
调用solve()
并将结果保存到变量:
solution=plan.solve()
检查溶液!
forstopinsolution.stops:print("Stop {} will be served by {} at time {}".format(stop["name"],stop["assign_to"],stop["eta"]))
简要说明:
- 各个站点、车辆和站点可以分别作为字典或实例传入
elasticroute.Stop
、elasticroute.Vehicle
和elasticroute.Depot
。各自的属性与字典键相同。 - 解算一个计划将返回一个
elasticroute.Solution
的实例,该实例的属性与elasticroute.Plan
基本相同,但函数不同(请参阅高级用法) - 与创建
Plan
不同,Solution.stops|vehicles|depots
相应地返回elasticroute.Stop
、elasticroute.Vehicle
和elasticroute.Depot
的实例,而不是字典。
高级用法
设置时间限制
可使用elasticroute.Stop
和elasticroute.Vehicle
键的from
和till
设置停车和车辆的时间限制:
morning_only_stop=er.Stop()morning_only_stop["name"]="Morning Delivery 1"morning_only_stop["from"]=900morning_only_stop["till"]=1200# add address and add to plan...morning_shift_van=er.Vehicle()morning_shift_van["name"]="Morning Shift 1"morning_shift_van["from"]=900morning_shift_van["till"]-1200# add to plan and solve...
如果不指定这两个类的from
和till
键,则会导致它在elasticroute.defaults.generalSettings
字典中被默认为avail_from
和avail_to
键,而字典又默认为500
和1700
。
设置家庭站点
停车场和车辆都可以设置“家得宝”。停车场表示车辆在到达前必须在何处提取停车场的货物,而车辆停车场表示车辆行程的起点和终点(这意味着分配了车辆可以从事的可能工作)。 默认情况下,对于每个站点和车辆,如果未指定Depot字段,我们将假定它是第一个Depot。
common_stop=er.Stop()common_stop["name"]="Normal Delivery 1"common_stop["depot"]="Main Warehouse"# set stop address and add to plan...rare_stop=er.Stop()rare_stop["name"]="Uncommon Delivery 1"rare_stop["depot"]="Auxillary Warehouse"# set stop address and add to plan...plan.vehicles=[{"name":"Main Warehouse Van","depot":"Main Warehouse"},{"name":"Auxillary Warehouse Van","depot":"Auxillary Warehouse"}]plan.depots=[{"name":"Main Warehouse","address":"Somewhere"},{"name":"Auxillary Warehouse","address":"Somewhere else"}]# solve and get results...
重要信息:字段的值必须与同名计划中的匹配值对应!
设置负载限制
每个车辆都可以设置成累积最大重量、体积和(非累积)座位容量,它可以用来确定在它必须返回到仓库之前能停多少站。相反,每个站点也可以分配重量、体积和座位载荷。
这些键是weight_load
、volume_load
、seating_load
用于停止,以及weight_capacity
、volume_capacity
和seating_capacity
用于车辆。
替代连接类型(用于大型数据集)
默认情况下,所有请求都以synchronous方式发出。大多数中小型数据集可以在10秒内解决,但对于生产应用,您可能需要先关闭http连接,然后按以下方式轮询更新:
importtimeplan=er.Plan()plan.connection_type="poll";# do the usual stuffsolution=plan.solve()whilesolution.status!="planned":solution.refresh()time.sleep(2)# or do some threading or promise
将connection_type
设置为"poll"
将导致服务器在分析请求数据后立即返回响应。您可以使用status
和progress
属性监视状态,同时使用refresh()
方法获取更新。
此外,将connectionType
设置为"webhook"
还将导致服务器向您所说的webhook发送响应的副本。确切的位置可以使用Plan
对象的webhook
属性指定webhook的on。