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:
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 <= N)
 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.
Output:
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
Usage:
tsp_nn
or
tsp_nn(userConfig)
or
resultStruct = tsp_nn;
or
resultStruct = tsp_nn(userConfig);
or
[...] = tsp_nn('Param1',Value1,'Param2',Value2, ...);
Example:
% Let the function create an example problem to solve
tsp_nn;
Example:
% Request the output structure from the solver
resultStruct = tsp_nn;
Example:
% Pass a random set of userdefined XY points to the solver
userConfig = struct('xy',10*rand(50,2));
resultStruct = tsp_nn(userConfig);
Example:
% Pass a more interesting set of XY points to the solver
n = 100;
phi = (sqrt(5)1)/2;
theta = 2*pi*phi*(0:n1);
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 = tsp_nn(userConfig);
Example:
% Pass a random set of 3D (XYZ) points to the solver
xyz = 10*rand(50,3);
userConfig = struct('xy',xyz);
resultStruct = tsp_nn(userConfig);
Example:
% Turn off the plots but show a waitbar
userConfig = struct('showProg',false,'showResult',false,'showWaitbar',true);
resultStruct = tsp_nn(userConfig);
