View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Open Traveling Salesman Problem - Genetic Algorithm

4.2 | 4 ratings Rate this file 36 Downloads (last 30 days) File Size: 9.61 KB File ID: #21196 Version: 1.3
image thumbnail

Open Traveling Salesman Problem - Genetic Algorithm


Joseph Kirk (view profile)


23 Aug 2008 (Updated )

Finds a near-optimal solution to a "open" variation of the TSP using a GA

| Watch this File

File Information

TSPO_GA 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 to each city exactly once without returning to the starting city)
1. A single salesman travels to each of the cities but does not close
   the loop by returning to the city he started from
2. Each city is visited by the salesman exactly once

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 = tspo_ga;
resultStruct = tspo_ga(userConfig);
[...] = tspo_ga('Param1',Value1,'Param2',Value2, ...);

% Let the function create an example problem to solve

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

% Pass a random set of user-defined XY points to the solver
userConfig = struct('xy',10*rand(50,2));
resultStruct = tspo_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 = tspo_ga(userConfig);

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

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

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

Required Products MATLAB
MATLAB release MATLAB 8.3 (R2014a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (6)
02 Dec 2014 Are Losnegård

21 Sep 2014 Jack

Jack (view profile)

very good thing

11 May 2014 António

Hi Joseph Kirk, awesome job. I saw that this file was in the "File Exchange Pick of the Week" and i tried to do the same that was made there, i tried to use a map of Europe and change the code the right way and run the program, but i was not suceeded, can u help with that?

Comment only
28 May 2013 mei

mei (view profile)

what if there 10 cities, the salesman only have to go for 6 cities and no repeating cities and open?

Comment only
23 Jan 2013 Newmat

Newmat (view profile)

Thanks Joseph for your great work!

I didnt find out yet at which point the route starts and where it ends. It seems to be neither the first nor the last point in xy :)

10 Nov 2012 reza

reza (view profile)

02 Jun 2009 1.1

Added 3D capability.

07 Nov 2011 1.2

Minor cosmetic updates.

06 May 2014 1.3

Major overhaul of input/output interface.

Contact us