File Exchange

image thumbnail

Fixed Endpoints Open Traveling Salesman Problem - Genetic Algorithm

version (10.3 KB) by Joseph Kirk
Finds a near-optimal solution to a "open" variation of the TSP with fixed endpoints using a GA


Updated 06 May 2014

View License

TSPOF_GA Fixed Open Traveling Salesman Problem (TSP) Genetic Algorithm (GA)
Finds a (near) optimal solution to a variation of the TSP by setting up
a GA to search for the shortest route (least distance for the salesman
to travel from a FIXED START to a FIXED END while visiting the other
cities exactly once)
1. A single salesman starts at the first point, ends at the last
point, and travels to each of the remaining cities in between, but
does not close the loop by returning to the city he started from
2. Each city is visited by the salesman exactly once

Note: The Fixed Start is taken to be the first XY point, and the Fixed
End is taken to be the last XY point

USERCONFIG (structure) with zero or more of the following fields:
- XY (float) is an Nx2 matrix of city locations, where N is the number of cities
- DMAT (float) is an NxN matrix of point to point distances/costs
- POPSIZE (scalar integer) is the size of the population (should be divisible by 4)
- NUMITER (scalar integer) is the number of desired iterations for the algorithm to run
- SHOWPROG (scalar logical) shows the GA progress if true
- SHOWRESULT (scalar logical) shows the GA results if true
- SHOWWAITBAR (scalar logical) shows a waitbar if true

Input Notes:
1. Rather than passing in a structure containing these fields, any/all of
these inputs can be passed in as parameter/value pairs in any order instead.
2. Field/parameter names are case insensitive but must match exactly otherwise.

RESULTSTRUCT (structure) with the following fields:
(in addition to a record of the algorithm configuration)
- OPTROUTE (integer array) is the best route found by the algorithm
- MINDIST (scalar float) is the cost of the best route

resultStruct = tspof_ga;
resultStruct = tspof_ga(userConfig);
[...] = tspof_ga('Param1',Value1,'Param2',Value2, ...);

% Let the function create an example problem to solve

% Request the output structure from the solver
resultStruct = tspof_ga;

% Pass a random set of user-defined XY points to the solver
userConfig = struct('xy',10*rand(50,2));
resultStruct = tspof_ga(userConfig);

% Pass a more interesting set of XY points to the solver
n = 50;
phi = (sqrt(5)-1)/2;
theta = 2*pi*phi*(0:n-1);
rho = (1:n).^phi;
[x,y] = pol2cart(theta(:),rho(:));
xy = 10*([x y]-min([x;y]))/(max([x;y])-min([x;y]));
userConfig = struct('xy',xy);
resultStruct = tspof_ga(userConfig);

% Pass a random set of 3D (XYZ) points to the solver
xyz = 10*rand(50,3);
userConfig = struct('xy',xyz);
resultStruct = tspof_ga(userConfig);

% Change the defaults for GA population size and number of iterations
userConfig = struct('popSize',200,'numIter',1e4);
resultStruct = tspof_ga(userConfig);

% Turn off the plots but show a waitbar
userConfig = struct('showProg',false,'showResult',false,'showWaitbar',true);
resultStruct = tspof_ga(userConfig);

Cite As

Joseph Kirk (2020). Fixed Endpoints Open Traveling Salesman Problem - Genetic Algorithm (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (2)

There is an error which arises when only placing the input as a dmat. How can this be solved?

Very nice work!


Major overhaul of input/output interface.

Bug fix. Minor cosmetic updates.

Added 3D capability.

updated help notes, description

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: Traveling Salesman Problem - Genetic Algorithm