1

I'm new to python programming please bear with me,

I have calculated a distance matrix (300x300) using real data (lat & log) through a OSRM running in dockers in a windows machine. it takes about 400 secs to complete the calculation.

because the Matrix will increase in size as customers locations are added, I need to calculate the distance matrix for the "same" matrix with an additional node (additional lat & log)

What is the fastest way to determine the distance matrix without recalculating the whole matrix?

my current code:

#setup OSRM 
from routingpy import OSRM
from routingpy.routers import options
options.default_timeout=None

client = OSRM(base_url="")


#take lat&log from dataframe to list

import numpy as np
from boltons import iterutils

lat_= locations_DCA_df['Latitude'].tolist()
log_= locations_DCA_df['Longitude'].tolist()
distance_list = list()


for i in locations_DCA_df.index:
    for j in locations_DCA_df.index:
        orig_node = [log_[i],lat_[i]]
        dest_node = [log_[j],lat_[j]]
        orig_dest = [orig_node,dest_node]
        distance_list.append(client.directions(locations=orig_dest,profile='car').distance)

#convert distance_list to distance_ format required for OrTools
distance = iterutils.chunked(distance_list, len(lat_))


mj_davis
  • 21
  • 3

1 Answers1

0

I had a similar issue with my team. What we decided to do is to get all unique (client_id,lat & lng) from all clients in the matrix and then calculate the matrix for new clients against all clients in the matrix but not matrix vs matrix clients.

Let's say my matrix looks like this:

client1 | lat1 | lng1 | client2 | lat2 | lng2 | distance
---------------------------------------------------------
   a    | 13.4 | -2.3 |    b    | 45.3 | -4.6 |   12
   a    | 13.4 | -2.3 |    c    | 22.5 | 23.4 |   16
   b    | 45.3 | -4.6 |    a    | 13.4 | -2.3 |   9
   b    | 45.3 | -4.6 |    c    | 22.5 | 23.4 |   7
   c    | 22.5 | 23.4 |    a    | 13.4 | -2.3 |   22
   c    | 22.5 | 23.4 |    b    | 45.3 | -4.6 |   10

And let's say a new client d should be added. Instead of collecting lat and lng from your original table (or pandas dataframe) and sending the request to OSRM to recalculate everything, you can select all unique lats and lngs from either lat1 & lng1 or lat2 & lng2 (from your matrix), and then select the new client's latitude and longitude to send that info to OSRM to get distance between matrix clients and new clients but not between matrix clients and matrix clients.

It is important to point out that it shouldn't matter if you take the latitudes and longitudes from either lat1 & lng1 or lat2 & lng2 since both columns should have the same data (same number of clients) but not in the same order.

OSRM should return 6 numbers (or distances) which should be:

  • new client vs a
  • new client vs b
  • new client vs c
  • a vs new client
  • b vs new client
  • c vs new client

Finally, you will only need to append these 6 values into your original matrix.

brenda
  • 656
  • 8
  • 24