+ Reply to Thread
Results 1 to 7 of 7

Driving Distance Matrix calculation

  1. #1
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    263

    Driving Distance Matrix calculation

    I used the Microsoft Bing API to return the driving distances between various addresses in a matrix.

    My next step is to create a formula to return the sequence of addresses in the order I should goto based on shortest distance between each point (also referred to as the traveling salesman problem). Also looking to return the distance between each point.

    See attached spreadsheet, with a "before" and "after" section. Address1 is my starting address.

    Any ideas what formula to use in Excel 365 to get the output? Thanks.
    Attached Files Attached Files
    Last edited by Brawnystaff; 10-04-2021 at 01:44 PM.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    16,154

    Re: Driving Distance Matrix calculation

    I'm not aware of any formula (or combination of formulas) that can efficiently solve the traveling salesman problem. From what I understand of the TSP, its complexity is NP-hard or similar, so that one would not expect to find a simple string of calculations that solves the problem.

    Most Excel based solutions that I see proposed involve using Excel's Solver utility (or similar solver engine). The overall solution involves:

    1) Formulas to compute "total distance" based on visiting the nodes in a given order.
    2) Other formulas to compute any constraints on the problem
    3) Call the solver engine and tell it to minimize the total distance by trying different sequences of nodes. Keep trying until the solver engine decides it is done.

    Here's one tutorial I found: http://blog.excelmasterseries.com/20...blem-with.html There are several others out there by putting something like "traveling salesman Excel" into your favorite search engine.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    263

    Re: Driving Distance Matrix calculation

    Interesting. I saw the Solver addin before, but thought with Excel 365 dynamic arrays or Power Query/PowerPivot, it could achieved. Thanks.

  4. #4
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: Driving Distance Matrix calculation

    I know you've marked this as solved but I've only just started playing with solver and thought this was a good opportunity to try it out. I changed the names of the destinations to numbers just so I could set solver parameters around them but you can still link the numbers to named destinations. I put the solver parameters as an image into the spreadsheet too. The requirement for the destinations to add up to 15 is not required as I also have settings that they are integers between 1 and 5 and must all be different so you could remove the sum = 15 if you wanted.

    EDIT: I am curious though why your matrix isn't symmetrical. It implies some directions are longer in one direction thana the other which might account for one way streets and the like I guess.
    Attached Files Attached Files
    Last edited by Crooza; 10-04-2021 at 10:49 PM.
    Happy with my advice? Click on the * reputation button below

  5. #5
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,778

    Re: Driving Distance Matrix calculation

    If this problem should be treated as a "Travelling salesman" problem the salesman should end up in the same city (address) as he started from.

    This is easily fixed in the uploaded solver solution add a formula to cell A16 i.e. "A16=A11" and copy the formulas in "B15:C15" to "B16".

    And the shortest distance for the travelling salesman is 61.1 as opposed to 63.2 if he travels 1, 2, 3, 4, 5.

    The setting "AllDifferent" is an interesting one. FrontlineSolvers Excel Online Help has this comment about it:

    A constraint such as*A1:A5 = alldifferent, where A1:A5 are decision variable cells, requires that these cells must be integers in the range 1 to N
    (N = 5 in this example), with each variable*different*from all the others at the solution.* Hence, A1:A5 will contain a*permutation*of integers, such as 1,2,3,4,5
    or 1,3,5,2,4.* An alldifferent constraint can be used to model problems involving ordering or sequencing of choices, such as the Traveling Salesman Problem.
    So using AllDifferent for range A11:A15 removes the need for an integer constraint as well as the upper and lower limits.

    Alf
    Last edited by Alf; 10-06-2021 at 04:22 PM.

  6. #6
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: Driving Distance Matrix calculation

    Thanks Alf,

    I tried removing the constraint that the sum = 15 (as that seemed superfluous in this instance) but interestingly I got some weird results.

    I agree with your solution too on the returning to home.

  7. #7
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,778

    Re: Driving Distance Matrix calculation

    You are welcome.

    I tried removing the constraint that the sum = 15 (as that seemed superfluous in this instance) but interestingly I got some weird results.
    Strange as your file ran whiteout any problems after my small modifications. Tested with and without "Require Bounds on Variables"

    Alf
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Using VBA to pull driving distance between two zio codes
    By billfinnjr in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-13-2019, 02:17 PM
  2. Driving distance using google API
    By AV114 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-11-2018, 07:51 AM
  3. [SOLVED] Google Distance Matrix API - Distance and Time
    By rtcwlomax in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-18-2014, 08:42 PM
  4. Use Google Maps to return driving time and distance
    By KevanC in forum Excel General
    Replies: 2
    Last Post: 12-22-2013, 11:16 PM
  5. Help With VBA code for getting driving distance.
    By anfdrew in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-22-2013, 11:06 PM
  6. Use mapquest or google maps to get driving distance?
    By DonkeyOte in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-19-2010, 10:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1