楼主: casey_c
892 1

[程序分享] 在有限预算下规划最佳公路旅行路线 [推广有奖]

  • 0关注
  • 10粉丝

博士生

92%

还不是VIP/贵宾

-

威望
0
论坛币
96 个
通用积分
2.1003
学术水平
2 点
热心指数
15 点
信用等级
2 点
经验
11502 点
帖子
278
精华
0
在线时间
94 小时
注册时间
2016-11-22
最后登录
2022-5-2

楼主
casey_c 发表于 2017-11-7 15:25:45 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币




以下内容转自 数析学院,只节选了部分,有需要的同学可以直接查看原文



一、构建公路旅行路线列表

第一步:在公路旅行过程中,你需要决定在哪停下来。 确保先查看 Google Maps 上的所有位置,以便掌握正确的地址、城市、州等。如果用于查看位置的名称在 Google Maps 上无法起效,那么它在本节代码中也无法起效。 将所有路标添加到下面的列表中,确保它们的格式与下面的示例相同。
技术说明:由于 Google Maps API 的日常使用限制,您最多只能有70个路标,如果您想添加更多的路标,则需要向 Google 支付费用增加 API 限制值。


  1. # https://en.wikipedia.org/wiki/List_of_state_capitols_in_the_United_States

  2. all_waypoints = ['Alabama State Capitol, 600 Dexter Avenue, Montgomery, AL 36130',
  3.                  #'Alaska State Capitol, Juneau, AK',
  4.                  'Arizona State Capitol, 1700 W Washington St, Phoenix, AZ 85007',
  5.                  'Arkansas State Capitol, 500 Woodlane Street, Little Rock, AR 72201',
  6.                  'L St & 10th St, Sacramento, CA 95814',
  7.                  '200 E Colfax Ave, Denver, CO 80203',
  8.                  'Connecticut State Capitol, 210 Capitol Ave, Hartford, CT 06106',
  9.                  'Legislative Hall: The State Capitol, Legislative Avenue, Dover, DE 19901',
  10.                  '402 S Monroe St, Tallahassee, FL 32301',
  11.                  'Georgia State Capitol, Atlanta, GA 30334',
  12.                  #'Hawaii State Capitol, 415 S Beretania St, Honolulu, HI 96813'
  13.                  '700 W Jefferson St, Boise, ID 83720',
  14.                  'Illinois State Capitol, Springfield, IL 62756',
  15.                  'Indiana State Capitol, Indianapolis, IN 46204',
  16.                  'Iowa State Capitol, 1007 E Grand Ave, Des Moines, IA 50319',
  17.                  '300 SW 10th Ave, Topeka, KS 66612',
  18.                  'Kentucky State Capitol Building, 700 Capitol Avenue, Frankfort, KY 40601',
  19.                  'Louisiana State Capitol, Baton Rouge, LA 70802',
  20.                  'Maine State House, Augusta, ME 04330',
  21.                  'Maryland State House, 100 State Cir, Annapolis, MD 21401',
  22.                  'Massachusetts State House, Boston, MA 02108',
  23.                  'Michigan State Capitol, Lansing, MI 48933',
  24.                  'Minnesota State Capitol, St Paul, MN 55155',
  25.                  '400-498 N West St, Jackson, MS 39201',
  26.                  'Missouri State Capitol, Jefferson City, MO 65101',
  27.                  'Montana State Capitol, 1301 E 6th Ave, Helena, MT 59601',
  28.                  'Nebraska State Capitol, 1445 K Street, Lincoln, NE 68509',
  29.                  'Nevada State Capitol, Carson City, NV 89701',
  30.                  'State House, 107 North Main Street, Concord, NH 03303',
  31.                  'New Jersey State House, Trenton, NJ 08608',
  32.                  'New Mexico State Capitol, Santa Fe, NM 87501',
  33.                  'New York State Capitol, State St. and Washington Ave, Albany, NY 12224',
  34.                  'North Carolina State Capitol, Raleigh, NC 27601',
  35.                  'North Dakota State Capitol, Bismarck, ND 58501',
  36.                  'Ohio State Capitol, 1 Capitol Square, Columbus, OH 43215',
  37.                  'Oklahoma State Capitol, Oklahoma City, OK 73105',
  38.                  'Oregon State Capitol, 900 Court St NE, Salem, OR 97301',
  39.                  'Pennsylvania State Capitol Building, North 3rd Street, Harrisburg, PA 17120',
  40.                  'Rhode Island State House, 82 Smith Street, Providence, RI 02903',
  41.                  'South Carolina State House, 1100 Gervais Street, Columbia, SC 29201',
  42.                  '500 E Capitol Ave, Pierre, SD 57501',
  43.                  'Tennessee State Capitol, 600 Charlotte Avenue, Nashville, TN 37243',
  44.                  'Texas Capitol, 1100 Congress Avenue, Austin, TX 78701',
  45.                  'Utah State Capitol, Salt Lake City, UT 84103',
  46.                  'Vermont State House, 115 State Street, Montpelier, VT 05633',
  47.                  'Virginia State Capitol, Richmond, VA 23219',
  48.                  'Washington State Capitol Bldg, 416 Sid Snyder Ave SW, Olympia, WA 98504',
  49.                  'West Virginia State Capitol, Charleston, WV 25317',
  50.                  '2 E Main St, Madison, WI 53703',
  51.                  'Wyoming State Capitol, Cheyenne, WY 82001']

  52. len(all_waypoints)
复制代码


接下来,必须使用 Google Maps API 注册此脚本,以便他们知道谁在用数百个 Google Maps 路由请求冲击他们的服务器。
1) 用你的 Google 账户使激活 Google Maps Distance Matrix API,Google 给出了操作说明 here
2) 复制生成的 API key 粘贴到下面的代码中


  1. import googlemaps

  2. gmaps = googlemaps.Client(key='ENTER YOUR GOOGLE MAPS KEY HERE')
复制代码

接下来,我们将查询 Google Maps API,寻找所有路标之间的最短路线。

这相当于在 Google 地图网站上进行 Google 地图路线查询,只是现在我们正在使用代码自动执行数百次查询。

如果您在此部分遇到错误,那么很有可能意味着在 Google Maps 上找不到你输入的路标。另一个可能导致错误的原因是,如果在这些点之间不可能开车,例如,寻找夏威夷和佛罗里达之间的行车路线将会返回错误,除非我们发明飞行汽车。

二、收集所有路标之间最短路线

  1. from itertools import combinations

  2. waypoint_distances = {}
  3. waypoint_durations = {}

  4. for (waypoint1, waypoint2) in combinations(all_waypoints, 2):
  5.     try:
  6.         route = gmaps.distance_matrix(origins=[waypoint1],
  7.                                       destinations=[waypoint2],
  8.                                       mode='driving', # 如果是步行导航,请改为 'walking'
  9.                                                       # 骑行则改为'bicycling',以此类推
  10.                                       language='English',
  11.                                       units='metric')

  12.         # 距离'distance' 单位是米
  13.         distance = route['rows'][0]['elements'][0]['distance']['value']

  14.         # 时间'duration' 单位是秒
  15.         duration = route['rows'][0]['elements'][0]['duration']['value']

  16.         waypoint_distances[frozenset([waypoint1, waypoint2])] = distance
  17.         waypoint_durations[frozenset([waypoint1, waypoint2])] = duration
  18.    
  19.     except Exception as e:
  20.         print('Error with finding the route between {} and {}.'.format(waypoint1, waypoint2))
复制代码

现在我们有所有路标之间的路线,让我们将它们保存到一个文本文件中,这样我们也不用再麻烦 Google 了。

  1. with open('my-waypoints-dist-dur.tsv', 'w') as out_file:
  2.     out_file.write('\t'.join(['waypoint1',
  3.                               'waypoint2',
  4.                               'distance_m',
  5.                               'duration_s']))
  6.    
  7.     for (waypoint1, waypoint2) in waypoint_distances.keys():
  8.         out_file.write('\n' +
  9.                        '\t'.join([waypoint1,
  10.                                   waypoint2,
  11.                                   str(waypoint_distances[frozenset([waypoint1, waypoint2])]),
  12.                                   str(waypoint_durations[frozenset([waypoint1, waypoint2])])]))
复制代码

以上内容转自 数析学院,如需完整内容可以直接查看原文



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Combinations Pennsylvania Indianapolis combination Connecticut

沙发
casey_c 发表于 2017-11-10 11:27:54

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-21 11:24