楼主: casey_c
1443 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-7-28 10:17:22 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币



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


1、建立公路旅游路线的列表
第一步是决定在公路旅行时,你想在哪里停。


确保您首先查看 Google Maps 上的所有位置,以便你具有正确的地址、城市、州等。如果用于查看位置的名字在 Google Maps 上无法起效,则它在这里也无法起效。


将所有路标添加到下面的列表中,确保它们的格式与下面的示例相同。


技术说明:由于Google Maps API的日常使用限制,您最多只能有70个路标。如果您想添加更多的路标,您将不得不为了增加的API限制值,向Google支付费用



  1. all_waypoints = ["USS Alabama, Battleship Parkway, Mobile, AL",
  2.                  "Grand Canyon National Park, Arizona",
  3.                  "Toltec Mounds, Scott, AR",
  4.                  "San Andreas Fault, San Benito County, CA",
  5.                  "Cable Car Museum, 94108, 1201 Mason St, San Francisco, CA 94108",
  6.                  "Pikes Peak, Colorado",
  7.                  "The Mark Twain House & Museum, Farmington Avenue, Hartford, CT",
  8.                  "New Castle Historic District, Delaware",
  9.                  "White House, Pennsylvania Avenue Northwest, Washington, DC",
  10.                  "Cape Canaveral, FL",
  11.                  "Okefenokee Swamp Park, Okefenokee Swamp Park Road, Waycross, GA",
  12.                  "Craters of the Moon National Monument & Preserve, Arco, ID",
  13.                  "Lincoln Home National Historic Site Visitor Center, 426 South 7th Street, Springfield, IL",
  14.                  "West Baden Springs Hotel, West Baden Avenue, West Baden Springs, IN",
  15.                  "Terrace Hill, Grand Avenue, Des Moines, IA",
  16.                  "C. W. Parker Carousel Museum, South Esplanade Street, Leavenworth, KS",
  17.                  "Mammoth Cave National Park, Mammoth Cave Pkwy, Mammoth Cave, KY",
  18.                  "French Quarter, New Orleans, LA",
  19.                  "Acadia National Park, Maine",
  20.                  "Maryland State House, 100 State Cir, Annapolis, MD 21401",
  21.                  "USS Constitution, Boston, MA",
  22.                  "Olympia Entertainment, Woodward Avenue, Detroit, MI",
  23.                  "Fort Snelling, Tower Avenue, Saint Paul, MN",
  24.                  "Vicksburg National Military Park, Clay Street, Vicksburg, MS",
  25.                  "Gateway Arch, Washington Avenue, St Louis, MO",
  26.                  "Glacier National Park, West Glacier, MT",
  27.                  "Ashfall Fossil Bed, Royal, NE",
  28.                  "Hoover Dam, NV",
  29.                  "Omni Mount Washington Resort, Mount Washington Hotel Road, Bretton Woods, NH",
  30.                  "Congress Hall, Congress Place, Cape May, NJ 08204",
  31.                  "Carlsbad Caverns National Park, Carlsbad, NM",
  32.                  "Statue of Liberty, Liberty Island, NYC, NY",
  33.                  "Wright Brothers National Memorial Visitor Center, Manteo, NC",
  34.                  "Fort Union Trading Post National Historic Site, Williston, North Dakota 1804, ND",
  35.                  "Spring Grove Cemetery, Spring Grove Avenue, Cincinnati, OH",
  36.                  "Chickasaw National Recreation Area, 1008 W 2nd St, Sulphur, OK 73086",
  37.                  "Columbia River Gorge National Scenic Area, Oregon",
  38.                  "Liberty Bell, 6th Street, Philadelphia, PA",
  39.                  "The Breakers, Ochre Point Avenue, Newport, RI",
  40.                  "Fort Sumter National Monument, Sullivan's Island, SC",
  41.                  "Mount Rushmore National Memorial, South Dakota 244, Keystone, SD",
  42.                  "Graceland, Elvis Presley Boulevard, Memphis, TN",
  43.                  "The Alamo, Alamo Plaza, San Antonio, TX",
  44.                  "Bryce Canyon National Park, Hwy 63, Bryce, UT",
  45.                  "Shelburne Farms, Harbor Road, Shelburne, VT",
  46.                  "Mount Vernon, Fairfax County, Virginia",
  47.                  "Hanford Site, Benton County, WA",
  48.                  "Lost World Caverns, Lewisburg, WV",
  49.                  "Taliesin, County Road C, Spring Green, Wisconsin",
  50.                  "Yellowstone National Park, WY 82190"]
复制代码


接下来,你必须使用 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="PASTE YOUR API KEY HERE")
复制代码

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

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

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

2、收集所有路标之间最短路线的距离

  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 %s and %s." % (waypoint1, waypoint2))
复制代码

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

  1. ith 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 constitution

沙发
casey_c 发表于 2017-8-1 10:50:24

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

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