Shortest-path grocery shopping

What’s the shortest path thru the grocery store to get my items?

Justin Pearson
Apr 19, 2020

In[1]:=

index_1.gif

Summary

Given an image of the store’s aisles, we mesh it and generate a graph from the mesh. Then we use various shortest-path algorithms on the graph to find the shortest tour that visits all the items.

We are careful to build a graph whose edge-weights equal the euclidean distance between adjacent vertices, so that that shortest-path is shortest with respect to actual space, not just “number of hops on the graph”.

In[3]:=

index_2.gif

In[4]:=

index_3.gif

index_4.gif

Triangularize the mesh. Pick a small “max cell size” to get small triangles.

index_5.gif

index_6.gif

index_7.gif

index_8.gif

Convert the triangularized-mesh to a graph, using the degree-0 elements of the mesh -- the mesh points -- as vertices. (Degree-1 is the triangle edges, degree-2 is the triangles themselves.)

index_9.gif

index_10.gif

A slick UI for showing shortest-paths between any two vertices.

index_11.gif

index_12.gif

We extend this idea to build a shortest-path thru the store that visits all your items:

index_13.gif

index_14.gif

- This is a real map of my local Albertsons grocery store.
- The thin pink line is the shortest tour, ignoring occlusions like the (black) aisles.
- The colored paths visit the items in the order of the shortest tour.
- To find the shortest tour thru the items along the triangulated mesh, I first found the shortest pairwise distance between every pair of items on the triangulated-mesh graph. Then I made a new complete graph with only 40 vertices -- the items -- whose edge weights were the pairwise distances. Then I asked MMA to find the shortest tour of those 40 vertices. (It would have been simpler to ask MMA to find the shortest path on the triangulated-mesh graph from vertex 1 to vertex 40 that visits all 40 vertices, but I didn’t know how to do that.)

Build graph of the grocery store

index_15.gif

index_16.gif

index_17.gif

index_18.gif

index_19.gif

index_20.gif

index_21.gif

index_22.gif

index_23.gif

index_24.gif

index_25.gif

index_26.gif

Get the graph vertices.
Notice that the vertices are not integers, but rather of the form {0,3}. Read this as “Point number 3”. (Because vertices correspond to points on the mesh, not lines or faces, and points are “degree 0”.)

index_27.gif

index_28.gif

index_29.gif

index_30.gif

index_31.gif

index_32.gif

Here are the euclidean coordinates of each vertex.

index_33.gif

index_34.gif

Map vertices to their coordinates.

index_35.gif

index_36.gif

Build a new graph with the same vertices and edges, but whose edges are weighted by the euclidean distance between the vertices.

index_37.gif

index_38.gif

The weighted graph has edges that are weighted by the euclidean distance between the vertices.
This is important for distance-finding. Here are two adjacent vertices:

index_39.gif

index_40.gif

In the raw mesh graph, they are only a distance “1” apart:

index_41.gif

index_42.gif

In the weighted graph, their graph distance is the euclidean distance:

index_43.gif

index_44.gif

index_45.gif

index_46.gif

index_47.gif

index_48.gif

Shopping trip

index_49.gif

index_50.gif

index_51.gif

index_52.gif

index_53.gif

index_54.gif

index_55.gif

index_56.gif

index_57.gif

index_58.gif

index_59.gif

index_60.gif

index_61.gif

index_62.gif

index_63.gif

index_64.gif

index_65.gif

index_66.gif

index_67.gif

index_68.gif

index_69.gif

index_70.gif

Example

Find a path from the first item (“entrance”)to the last item (“exit”):

index_71.gif

index_72.gif

index_73.gif

index_74.gif

index_75.gif

Find best order to visit the items

Idea: make a NEW graph, (a complete graph): vertices are the items, edges are weighted by shortest-dist from the mesh graph.
Then find a tour of this new graph, and map it back to the mesh graph.

index_76.gif

index_77.gif

index_78.gif

Here’s the shortest path thru all the items:

index_79.gif

index_80.gif

index_81.gif

index_82.gif

This should be the same distance as traversing associated vertices on the weighted mesh graph:

index_83.gif

index_84.gif

index_85.gif

index_86.gif

index_87.gif

index_88.gif

index_89.gif

index_90.gif

Here’s the shortest path thru the items, if you get to move through the walls:

index_91.gif

index_92.gif

However, if you account for the boundaries, then visiting the items in that order is longer than the shortest path:

index_93.gif

index_94.gif

(The optimal one is less:)

index_95.gif

index_96.gif

Show the optimal path thru the items:

index_97.gif

index_98.gif

index_99.gif

index_100.gif

index_101.gif

index_102.gif

index_103.gif

index_104.gif

index_105.gif

Created with the Wolfram Language