%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         My_TSP%           by%       John Monahan%        322-577% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Purpose:% To allow a user to guess an optimal path given “m” number of random% points.  The code will generate between 4 and 9 random points and tell% the user to click the points to guess a path.  The user will click m% number of points and the script will calculate the minimum distance and% display the optimal path.%% Code: Generate Pointsclear allclcclose allm = 9;xy = rand(m,2)*10;scatter(xy(:,1),xy(:,2),’filled’,’LineWidth’,5);hold ona = (1:m)’;b = num2str(a);c = cellstr(b);dx = 0.25;dy = 0.25;text(xy(:,1)+dx,xy(:,2)+dy,c);axis off;set(gca,’Box’,’off’);set(gcf,’color’,’w’);xpos,ypos = ginput(m);G = graph;userP = plot(xpos,ypos,’k’,’linewidth’,2);plot(xpos(1) xpos(m),ypos(1) ypos(m),’k’,’linewidth’,2);userDist = 0;for ii = 1:m    if ii~=m        userDist = userDist + sqrt((xpos(ii+1)-xpos(ii))^2+(ypos(ii+1)-ypos(ii))^2);    else        userDist = userDist + sqrt((xpos(1)-xpos(ii))^2+(ypos(1)-ypos(ii))^2);    endenddisp(‘User Calculated Distance: ‘ num2str(userDist));m = size(xy,1);distances = pdist(xy);distances = squareform(distances);distances(distances==0) = realmax;sPL = realmax;for ii = 1:m        startPt = ii;    path = startPt;        distanceTraveled = 0;    distNew = distances;        currentPt = startPt;         for jj = 1:m-1                minDist,nextPt = min(distNew(:,currentPt));        if (length(nextPt) > 1)            nextPt = nextPt(1);        end        path(end+1,1) = nextPt;        distanceTraveled = distanceTraveled + distances(currentPt,nextPt);                distNew(currentPt,:) = realmax;                currentPt = nextPt;            end        path(end+1,1) = startPt;    distanceTraveled = distanceTraveled + distances(currentPt,startPt);        if (distanceTraveled < sPL)        sPL = distanceTraveled;        shortestPath = path;         end  endfor ii = 1:m+1    if ii~=m+1        bestP = plot(xy(shortestPath(ii),1) xy(shortestPath(ii+1),1),xy(shortestPath(ii),2) xy(shortestPath(ii+1),2),'r--','linewidth',2);    endendlegend(userP,bestP,'Guessed Path','Optimal Path','Location','Best');disp('Best Calculated Distance: ' num2str(sPL));