Implementing Nearest neighbor heuristics in Matlab

6 views (last 30 days)
Nearest-neighbor heuristics (go to the nearest unvisited city). Write an algorithm in which chooses the next city in the path to be the closest city that have not already visited. Once all cities have been visited, the salesman return home.
Hint: Use the matlab codes provided below to perform this task.
I am not getting accurate result, it shows error. Can someone kindly check my code and fix it?? Thanks in advance. :)
% This is a script which I wrote to encapsulate the state capital location data %
clear
close all
function City = Capitals()
% Creates a length 48 structure array where each component
% has three fields:
% Name a length 24 string with the state and capital city's name
% Lat latitude in degress
% Long longitude in degrees
A = [...
'AL Montgomery 32.38 86.37';...
'AZ Phoenix 33.43 112.22';...
'AR Little Rock 34.73 92.23';...
'CA Sacramento 38.52 121.50';...
'CO Denver 39.75 104.87';...
'CT Hartford 41.78 72.78';...
'DE Dover 39.17 75.53';...
'FL Tallahassee 30.38 84.37';...
'GA Atlanta 33.75 84.38';...
'ID Boise 43.57 116.22';...
'IL Springfield 39.83 89.67';...
'IN Indianapolis 39.73 86.28';...
'IA Des Moines 41.53 93.65';...
'KS Topeka 39.77 95.63';...
'KY Frankfurt 38.20 84.88';...
'LA Baton Rouge 30.53 91.15';...
'ME Augusta 44.30 69.77';...
'MA Boston 42.37 71.33';...
'MD Annapolis 38.97 76.49';...
'MI Lansing 42.78 84.60';...
'MN St.Paul 44.88 93.22';...
'MS Jackson 32.32 90.88';...
'MO Jefferson City 38.57 92.18';...
'MT Helena 46.60 112.00';...
'NE Lincoln 40.85 96.75';...
'NV Carson City 39.17 119.77';...
'NH Concord 43.20 71.50';...
'NJ Trenton 40.22 74.77';...
'NM Santa Fe 35.62 106.88';...
'NY Albany 42.75 73.80';...
'NC Raleigh 35.87 78.78';...
'ND Bismarck 46.77 100.75';...
'OH Columbus 40.00 82.88';...
'OK Oklahoma City 35.40 97.60';...
'OR Salem 44.92 123.33';...
'PA Harrisburg 40.20 76.77';...
'RI Providence 41.73 71.43';...
'SC Columbia 34.00 81.55';...
'SD Pierre 44.38 100.28';...
'TN Knoxville 35.82 83.98';...
'TX Austin 30.30 97.70';...
'UT Salt Lake City 40.77 111.97';...
'VT Montpelier 44.25 72.58';...
'VA Richmond 37.50 77.33';...
'WA Olympia 46.97 122.90';...
'WI Madison 43.77 89.40';...
'WV Charleston 38.37 81.60';...
'WY Cheyenne 41.15 104.80'];
for k=1:48
Lat = str2num(A(k,25:29));
Long = str2num(A(k,34:39));
City(k) = struct('Name',A(k,1:24),'Lat',Lat,'Long',Long);
end
end
% 2nd script: This will compute the great circle distance between the two cities given their respective lattitudes and longitudes %
function d = Dist(C1,C2)
% Great circle distance between city C1 and city C2.
theta1 = C1.Lat*pi/180;
phi1 = C1.Long*pi/180;
theta2 = C2.Lat*pi/180;
phi2 = C2.Long*pi/180;
xDiff = cos(theta1)*cos(phi1) - cos(theta2)*cos(phi2);
yDiff = cos(theta1)*sin(phi1) - cos(theta2)*sin(phi2);
zDiff = sin(theta1) - sin(theta2);
d = 7926*asin(sqrt(xDiff^2 + yDiff^2 + zDiff^2)/2);
% 3rd script: This will set up the distance matrix given a set of input cities and their locations %
function D = CityDistTable(City)
% City is a length n structure array where the name, latitude, and
% longitude of the kth city is housed in City(k).Name, City(k).Lat,
% and City(k).Long.
% D is an n-by-n matrix with D(i,j) being the great circle distance
% from City(i) to City(j).
% Set up the city-to-city distance matrix...
n = length(City);
D = zeros(n,n);
for i=1:n
for j=i+1:n
D(i,j) = Dist(City(i),City(j));
D(j,i) = D(i,j);
end
end
% 4th script: This script is going to create a route from city i to city j %
function [S,Odom] = Route(StartCity,D)
% D is an n-by-n matrix whose (i,j) entry is the distance
% between city i and city j.
% StartCity is an index with 1 <= StartCity <= n
% S and Odom are column vectors of length n+1. S(k) is the index of
% the kth stop and Odom(k) is the accumulated mileage at that point.
% Note that S(1) = StartCity , Odom(1) = 0, S(n+1) = StartCity, and
% S(n+1) is the total distance of the journey.
[n] = size(D);
% Get set at the starting city...
S = zeros(n+1,1);
Odom = zeros(n+1,1);
Here = StartCity;
S(1) = Here;
% Remember the distance to the starting city...
dStart = D(StartCity,:);
for j=1:n-1
% Figure out where to go next, i.e., stop j+1...
D(:,Here) = inf;
[Leg,Next] = min(D(Here,:));
% Update the odometer and move on...
Odom(j+1) = Odom(j) + Leg;
S(j+1) = Next;
Here = Next;
end
% Return to the starting city...
S(n+1) = StartCity;
Odom(n+1) = Odom(n) + dStart(Here);
% 5th and last script: This route applies 48 times to calculate the heuristic solution of TSP %
% Script Eg15_1
% Heuristic solution of the traveling salesperson problem.
clc
close all
% Get the data and set up the distance matrix...
City = Capitals();
D = CityDistTable(City);
Shortest = inf;
% Examine the route obtained for each possible starting city...
for StartCity = 1:48
[S,Odom] = Route(StartCity,D);
if Odom(49) < Shortest
% A new best route discovered. Display...
Shortest = Odom(49);
clc
disp(' Stop Odometer')
disp('---------------------------------')
for k=1:49
disp([City(S(k)).Name sprintf(' %6.0f',Odom(k)) ])
end
pause
end
end
% End of all scripts %

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!