If you’re anything like me, the excruciating boredom of a trip to the supermarket is enough to get you thinking about anything else, in a desperate attempt to avoid a conversation on the best value lettuce on offer. I’ll do you a favour then by presenting some brain-fodder suitable for the next grocery hell-ride.

Supermarkets are, plainly, big business, and the companies running them take great pains to ensure their expensive buildings are utilised as effectively as possible (see, e.g., this oddly single-use blog). Though in general this is a complex problem, let’s have a look at a simple example of such analysis.

Lets set up a model of a shop, with an entrance and exit on one side with shelves and aisles running perpendicularly away. Allow for a number of gaps between the aisles, which help shoppers get around the shop.

To begin with, insert a shopper at the entrance, assign them an item to walk to, and then direct them to the exit. The shopper should walk as directly as possible towards their next goal, though we all know that in reality this is rarely the case. Unfortunately computer science has yet to catch up with the warped social dynamics at play here.

The algorithm I use for this is the A* search algorithm, a simple pathfinding algorithm widely used in computer games, for example. Being far too busy (lazy) to write my own implementation, I borrow once again from the File Exchange to save some time. I’m only interested in representing the supermarket as superficially as possible, so here I assume all aisles and walkways are one dimensional. With this assumption the supermarket is represented as a small 2D grid, and plotted below are the paths calculated by the A* algorithm for a few randomly-placed items.

These look about right, so we can be satisfied the algorithm is probably doing its job correctly. As the shopper is confined to a rigid system of aisles and walkways the distance they travel is actually controlled by the Manhattan Metric, rather than the usual 2D Euclidean metric.

Now we’d like to see where people tend to walk, so lets inject 1000 shoppers who enter the shop, walk to a single item, then wander out again. The item is randomly distributed throughout the supermarket – this isn’t that realistic, but is an illustrative example. Plotted below is an animation showing the accumulation of the first 127 paths so generated, with small circles marking the position of the item.

What is immediately clear is how important the middle walkway is, an optimum path relies on being able to walk between aisles this way. Supermarket managers are well aware of this fact, as my formative years spent at Sainsburys stacking the promotional ‘plinth’ shelves attest to. We can sum this distribution horizontally to see how far shoppers penetrate into the store. Sorry for the bar chart, I know I’ve spoken before about my irrational hatred of them.

All shoppers visit the entrance and exit, so the number of shoppers making it to layer ‘1’ is 1000. This drops off towards the central walkway, until it suddenly spikes to over 50% of shoppers. The poor aisles farther on barely get a look in, so any self-respecting store manager would surely want to encourage customers farther into the supermarket. Well customers love a walkway, how about another?

Adding a second row of aisles doesn’t cause customers to venture farther into the shop, and there is still a significant drop-off in footfall after each walkway. What this shop needs is something to pull customers to the back and more evenly distribute footfall. To explore this situation, I’ll make things slightly more realistic by giving each customer 5 items to head for rather than 1. I didn’t go full travelling salesman on every customer, but they do at least visit the items in order from left to right to help alleviate any backtracking. I also force a varying percentage of items to be right on the back wall, simulating the location of a critical element of the supermarket (e.g. a bakery) that lots of people will visit every time they shop.

Obviously if everything was on the back wall, people just walk in straight to the back and then head out again. For a small proportion, around 10-20% it seems, total customer distribution is evened out somewhat between the front and back of the supermarket.

Next time you’re shopping then and you notice the all-important hard spirits section has been hidden away at the back, you can feel smugly superior about uncovering the hidden tactics of the crafty manager store while you buy your Tuesday afternoon vodka.