Code covered by the BSD License  

Highlights from
Traveling Salesman Problem - Nearest Neighbor

4.66667

4.7 | 3 ratings Rate this file 37 Downloads (last 30 days) File Size: 2.73 KB File ID: #21297
image thumbnail

Traveling Salesman Problem - Nearest Neighbor

by Joseph Kirk

 

02 Sep 2008 (Updated 07 Nov 2011)

Finds a near-optimal solution to a TSP using Nearest Neighbor (NN)

| Watch this File

File Information
Description

TSP_NN Traveling Salesman Problem (TSP) Nearest Neighbor (NN) Algorithm
The Nearest Neighbor algorithm produces different results depending on which city is selected as the starting point. This function determines the Nearest Neighbor routes for multiple starting points and returns the best of those routes

Summary:
1. A single salesman travels to each of the cities and completes the route by returning to the city he started from
2. Each city is visited by the salesman exactly once

Input:
XY (float) is an Nx2 (or Nx3) matrix 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 <= N)
SHOWPROG (scalar logical) shows the GA progress if true
SHOWRESULT (scalar logical) shows the GA results if true

Output:
OPTRTE (integer array) is the best route found by the algorithm
MINDIST (scalar float) is the cost of the best route

Example:
n = 50;
xy = 10*rand(n,2);
popSize = n;
showProg = 1;
showResult = 1;
a = meshgrid(1:n);
dmat = reshape(sqrt(sum((xy(a,:)-xy(a',:)).^2,2)),n,n);
[optRoute,minDist] = tsp_nn(xy,dmat,popSize,showProg,showResult);

Example:
n = 100;
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]));
popSize = n;
showProg = 1;
showResult = 1;
a = meshgrid(1:n);
dmat = reshape(sqrt(sum((xy(a,:)-xy(a',:)).^2,2)),n,n);
[optRoute,minDist] = tsp_nn(xy,dmat,popSize,showProg,showResult);

Example:
n = 50;
xyz = 10*rand(n,3);
popSize = n;
showProg = 1;
showResult = 1;
a = meshgrid(1:n);
dmat = reshape(sqrt(sum((xyz(a,:)-xyz(a',:)).^2,2)),n,n);
[optRoute,minDist] = tsp_nn(xyz,dmat,popSize,showProg,showResult);

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
Traveling Salesman Problem - Genetic Algorithm

Required Products MATLAB
MATLAB release MATLAB 7.12 (2011a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
16 Oct 2008 Kururunfa Goju

Very nice. It could benefit from the fact of working with TSPLIB files...

18 Jan 2009 Sandip Vijay  
01 Aug 2010 Maroag

Pretty nice program. However, there is a small bug in line 103 when by any chance two points are at the same distance min_d. Easily solvable though by just adding a line to choose the first element of J in case length(J)>1. Otherwise, works flawlessly.

Please login to add a comment or rating.
Updates
02 Jun 2009

Added 3D capability.

07 Nov 2011

Minor cosmetic updates.

Tag Activity for this File
Tag Applied By Date/Time
optimization Joseph Kirk 22 Oct 2008 10:17:04
traveling salesman problem Joseph Kirk 22 Oct 2008 10:17:04
tsp Joseph Kirk 22 Oct 2008 10:17:04
nn Joseph Kirk 22 Oct 2008 10:17:04
nearest neighbor Joseph Kirk 22 Oct 2008 10:17:04
nearest neighbor Jesus Valencia 10 Nov 2011 21:58:06

Contact us at files@mathworks.com