%%{init: {"theme": "neutral"}}%%
flowchart TB
C["Cafes<br/>OpenStreetMap"] -->|Ch 1| S["Exact loop<br/>one SQL query"]
O["Streets<br/>OpenStreetMap"] -->|Ch 2| N["Street network<br/>with one-way rules"]
C -->|Ch 2| M["Cost matrix<br/>distance and time"]
N -->|Ch 2| M
M -->|Ch 2| P["Road tours<br/>exact query and pgr_TSP"]
N -->|Ch 3| G["Road tour<br/>v.net.salesman"]
S -->|Ch 4| B["Scoreboard"]
P -->|Ch 4| B
G -->|Ch 4| B
H["Puskesmas<br/>healthsites.io"] -->|Ch 5| D["Day plan<br/>HiGHS"]
N -->|Ch 5| D
The Traveling Salesman Problem
The Traveling Salesman Problem
Fifteen cafes in Bandung, one loop: in which order do you visit them? This is the Traveling Salesman Problem. Many tools will give you an answer. This tutorial also asks the next question: how good is that answer?
You find the exact best loop with a single SQL query in PostgreSQL and PostGIS, then solve the same tour on real streets with pgRouting and with GRASS, and score every result against the exact one. The last chapter uses the same steps for a working day: a technician visiting twenty puskesmas (community health centres).
Who it is for. Readers who know some SQL or Python and have used a GIS. No experience with routing or optimisation is needed.
What this tutorial is, and is not
This is a tutorial about solving a routing puzzle with free and open-source geospatial software, and about measuring the answers. It is not a route planner, and its tours are not travel advice.
- The travel times rest on assumed speeds, one for each class of road, without traffic. Use them to compare tours, not to predict when you will arrive. Chapter 5 tests what happens when the speeds are wrong.
- The stops are examples. The fifteen cafes are not a ranking, and nobody checked whether they are open today. The list of puskesmas is incomplete and is not the official one.
- One vehicle, one tour. Several vehicles, loads and appointments at fixed hours belong to a larger family of problems, vehicle routing, which the chapters do not cover.
- The scoreboard is about these sets of stops on the streets of Bandung. Other cities and longer lists can give another order. The way of comparing is what carries over.
PostGIS and GRASS at the centre, not alone. Each program has its own part:
| Software | What it does here |
|---|---|
| PostgreSQL | finds the exact best loop with a recursive query, and holds every table |
| PostGIS | measures distances, cuts streets into a network, draws tours, spots loops that cross themselves |
| pgRouting | computes costs and routes on the streets, and solves tours with pgr_TSP |
| GRASS | solves the same tours on a vector network with v.net.salesman |
| HiGHS, through SciPy | solves longer tours exactly, and chooses which stops to leave for another day |
| Python | sends the queries, fills in the parts that need NumPy or pandas, and draws the maps and figures with Matplotlib |
The workflow
Each arrow carries the chapter that does that step.
Chapters
| Chapter | What it covers | |
|---|---|---|
| 1 | The best tour in one SQL query | Try every loop through nine cafes in SQL, then find the exact best loop through fifteen with one recursive query. |
| 2 | Real roads with pgRouting | Build a street network from OpenStreetMap, compute road distances and times, and see why the direction of a loop matters. |
| 3 | The same roads in GRASS | Solve the same tour with vector topology: v.net and v.net.salesman, with one-way costs, from the command line and from Python. |
| 4 | How good is your tour? | Score every tour against the exact best one, on your fifteen cafes and on many random sets. |
| 5 | Twenty puskesmas in one day? | Plan a technician’s round of twenty health centres, solve it exactly with HiGHS, and ask whether it fits in one working day. |
Each chapter opens with “At a glance”: what it builds, what it needs from the chapters before it, and how long it took to run when it was tested.
Before you start
- Software: all of it is free and open source. PostgreSQL with PostGIS and pgRouting is installed separately, as Chapter 1 explains. GRASS and the Python packages come with the environment in
environment.yml. The database connection and the GRASS folder are set inconfig.yml. - Tested on: Windows 11 with PostgreSQL 18, PostGIS 3.7, pgRouting 4.0 and GRASS 8.5 (ledger L31). The chapters have not been run on Linux or macOS yet. On Windows, one section of Chapter 3 needs a GRASS release newer than 8.5.0; the chapter says what to do until then.
- Data: every file is in the repository, so nothing has to be downloaded while you work. Streets and cafes come from OpenStreetMap, health facilities from healthsites.io, and kecamatan borders from Badan Informasi Geospasial (BIG), which offers them for download for the whole country; its portal is Ina-Geoportal.
sample-data/README.mddescribes each file. - Space and time: the tutorial database grows to about 150 MB, and the five chapters together ran in about six minutes on the author’s computer (ledger L31).
- The notebooks: each chapter is shown with the outputs of the author’s run. The “View source” link of each page opens it on GitHub, where you can download it and run it yourself. Each chapter ends with a checkpoint that stops with an error if its results are not what the chapter expects.
Where the numbers come from
Every number in the chapters comes from a cell output in the same chapter or from a row of the sources page, which says how it was measured or where it was read.
Bahasa Indonesia
The whole series is also available in Bahasa Indonesia.
Repository
Source, issues and history: github.com/osgeoid/traveling-salesman-problem. Corrections, new chapters and translations are welcome: see how to contribute. Maintainer: Benny Istanto. Part of the tutorials of OSGeo Indonesia.
Licence and citation
Code under the Mozilla Public License 2.0, text and figures under CC BY 4.0. The data keep their own terms: the cafes, streets and puskesmas are © OpenStreetMap contributors under the Open Database License, and the kecamatan borders are public data of BIG. To cite the tutorial, use the citation file of the repository.