GRASS logo

NAME - Creates a cycle connecting given nodes (Traveling salesman problem).
Note that TSP is NP-hard, heuristic algorithm is used by this module and created cycle may be suboptimal


vector, network, salesman

SYNOPSIS --help [-g] input=name output=name [type=string[,string,...]] [alayer=string] [nlayer=string] [afcolumn=string] [abcolumn=string] [sequence=name] ccats=range [method=string] [--overwrite] [--help] [--verbose] [--quiet] [--ui]


Use geodesic calculation for longitude-latitude locations
Allow output files to overwrite existing files
Print usage summary
Verbose module output
Quiet module output
Force launching GUI dialog


input=name [required]
Name of input vector map
Or data source for direct OGR access
output=name [required]
Name for output vector map
Arc type
Options: line, boundary
Default: line,boundary
Layer number or name
Arc layer
Default: 1
Layer number or name
Node layer (used for cities)
Default: 2
Arc forward/both direction(s) cost column (number)
EXPERIMENTAL: Arc backward direction cost column (number)
Name for output file holding node sequence ("-" for stdout)
ccats=range [required]
Category values
Categories of points ('cities') on nodes (layer is specified by nlayer)
Optimization method
Options: bs, ga
bs: bootstrapping
ga: genetic algorithm

Table of contents

DESCRIPTION estimates the optimal route to visit nodes on a vector network and optionally tries to improve the result.

Costs may be either line lengths, or attributes saved in a database table. These attribute values are taken as costs of whole segments, not as costs to traverse a length unit (e.g. meter) of the segment. For example, if the speed limit is 100 km / h, the cost to traverse a 10 km long road segment must be calculated as length / speed = 10 km / (100 km/h) = 0.1 h. Supported are cost assignments for arcs, and also different costs for both directions of a vector line. For areas, costs will be calculated along boundary lines.

The input vector needs to be prepared with operation=connect in order to connect points representing center nodes to the network.

Points specified by category must be exactly on network nodes, and the input vector map needs to be prepared with operation=connect.


For less than 10 nodes, the initial result is usually very close to the shortest possible tour and further optimization will have no effect. uses the same heuristics like to find a good tour which is not necessarily the optimal tour. The tour can be optimized with two methods: bootstrapping and genetic algorithm (GA). The bootstrapping method removes a subtour from the tour and reinserts the nodes resulting in a shorter tour. This is applied on intermediate tours and the final tour.

The genetic algorithm first creates several initial tours. From these tours nearly identical toursare eliminated. The remaining tours are recombined to create new, better tours. All tours are now mutated. The sequence of Selection, Recombination, Mutation is repeated until there is no better solution. Finally, the best tour is optimized with bootstrapping.


Arcs can be closed using cost = -1.


Visiting all 167 schools (North Carolina):
# North Carolina

# prepare network by connecting schools to streets -c input=streets_wake points=schools_wake output=streets_schools \
  operation=connect alayer=1 nlayer=2 thresh=1000

# verify data preparation
v.category in=streets_schools op=report
# type       count        min        max
# point          6          1          6

# find the shortest path in=streets_schools ccats=1-167 out=schools_tour

# Resulting tour length: 551551.662

# find the shortest path, optimize with genetic algorithm in=streets_schools ccats=1-167 out=schools_tour_ga method=ga

# Resulting tour length: 521191.083
# The original tour was 5.8% longer

# find the shortest path, optimize with bootstrapping in=streets_schools ccats=1-167 out=schools_tour_bs method=bs

# Resulting tour length: 510693.965
# The original tour was 8.0% longer




Radim Blazek, ITC-Irst, Trento, Italy
Markus Metz
Documentation: Markus Neteler, Markus Metz

Last changed: $Date: 2012-05-14 16:37:56 +0200 (Mon, 14 May 2012) $


Available at: source code (history)

Main index | Vector index | Topics index | Keywords index | Graphical index | Full index

© 2003-2017 GRASS Development Team, GRASS GIS 7.2.3svn Reference Manual