% % ( ) % dist - % route - % openclose - %------------------------------------------------------------------------ function [tour, distance] = all_brute_best(dist,route,n) global_diff = -0.1; best_dist = inf; % , while global_diff < 0 global_diff = 0; p1 = route(n); % for i = 1:n-2 t1 = p1; p1 = route(i); t2 = route(i+1); spd_var = dist(t1,p1); for j = i+2:n p2 = t2; t2 = route(j); diff = (dist(t1,p2) - dist(p2,t2))... + (dist(p1,t2) - spd_var); % if global_diff > diff global_diff = diff; % "" imin = i; jmin = j; end end end % if global_diff < 0 route(imin:jmin-1) = route(jmin-1:-1:imin); end end %-------------------------------------------------------------------- % cur_dist = 0; for i = 1:n-1 cur_dist = cur_dist + dist(route(i),route(i+1)); end % cur_dist = cur_dist + dist(route(1),route(end)); %-------------------------------------------------------------------- % if cur_dist < best_dist best_route = route; best_dist = cur_dist; end % - distance = best_dist; % tour = best_route; end
% % ( ) % dist - % route - % openclose - %------------------------------------------------------------------------- function [tour, distance] = all_brute_first(dist,route,n) global_diff = -0.1; best_dist = inf; % , while global_diff < 0 global_diff = 0; p1 = route(n); % for i = 1:n-2 t1 = p1; p1 = route(i); t2 = route(i+1); spd_var = dist(t1,p1); for j = i+2:n p2 = t2; t2 = route(j); diff = (dist(t1,p2) - dist(p2,t2))... + (dist(p1,t2) - spd_var); % if diff < 0 global_diff = diff; % "" imin = i; jmin = j; break end end % if diff < 0 break end end % if global_diff < 0 route(imin:jmin-1) = route(jmin-1:-1:imin); end end %-------------------------------------------------------------------- % cur_dist = 0; for i = 1:n-1 cur_dist = cur_dist + dist(route(i),route(i+1)); end % cur_dist = cur_dist + dist(route(1),route(end)); %-------------------------------------------------------------------- % if cur_dist < best_dist best_route = route; best_dist = cur_dist; end % - distance = best_dist; % tour = best_route; end
parfor k = 1:n % ( ) dist_temp = dist; % route = zeros(1,n); % next_point = start_route(k); % route(1) = next_point; % for e = 2:n % dist_temp(next_point,:) = nan; % [~,next_point] = min(dist_temp(:,next_point)); % route(e) = next_point; end % 1- if check_routes == true route = check_tour(route); end % arr_route_in(k,:) = route; end
% (probability several route) % , % dist - % tour_sev - - % n - - % tour_select - - % check_routes - % p_change - %------------------------------------------------------------------------- function [arr_several_route] = psr(dist,tour_sev,n,tour_select,cr,p_change) % () arr_several_route = zeros(tour_sev,n); parfor k = 1:tour_sev % ( ) dist_temp = dist; % route = zeros(1,n); % ( ) next_point = randi([1,n]); % route(1) = next_point; % count_var = 0; % for e = 2:n % dist_temp(next_point,:) = nan; % count_var = count_var + 1; % if rand(1) > p_change % k- % [~,next_point] = min(dist_temp(:,next_point)); else % % () arr_next = 1./(dist_temp(:,next_point)); % % , "habrahabr_vis" habrahabr_vis = (1:1:n)'; % arr_next = [arr_next, habrahabr_vis]; % arr_next_sort = sortrows(arr_next,1); % rand if (n - count_var) < tour_select tour_select_fun = (n - count_var); else tour_select_fun = tour_select; end % P = arr_next_sort(1:tour_select_fun,:); P(:,1) = P(:,1)./sum(P(:,1)); randNumb = rand; next_point = P(find(cumsum(P) >= randNumb, 1, 'first'),2); end % route(e) = next_point; end % 1- if cr == true route = check_tour(route); end % ( ) arr_several_route(k,:) = route; end
% TSP() ( ) %------------------------------------------------------------------------% if exist('cities','var') == 1 clearvars -except cities n = length(cities(:,1)); else clearvars intvar = inputdlg({'- '}... ,'TSP',1); n = str2double(intvar(1)); if isnan(n) || n < 0 msgbox ' ' return else cities = rand(n,2)*100; end end % tic %---------------------------------------------------------------% % ( x y ) start_tour = 1; % , % ( ) check_routes = false; % roulete_method = false; % (- ) if roulete_method == true crrm = 1000; end % "ts" top_roulete_method = true; if top_roulete_method == true % - " " ctrrm = 1000; % ts = 10; % p_change = 0.05; end %------------------------------------------------------------------% % dist = zeros(n,n); % () best_dist = inf; % brute_edge arr_route_in = zeros(n,n); % - ( ) % b = 1; %------------------------------------------------------------------------% % for i = 1:n for j = 1:n dist(i,j) = sqrt((cities(i,1) - cities(j,1))^2 + ... (cities(i,2) - cities(j,2))^2); end end % dist = round(dist); % returndist = 1./dist; % ( ) start_route = linspace(1,n,n); % if start_tour > n || start_tour < 1 start_tour = 1; end % "start_tour" start_route(start_tour) = 1; start_route(1) = start_tour; % % % parfor - parfor k = 1:n % ( ) dist_temp = dist; % route = zeros(1,n); % next_point = start_route(k); % route(1) = next_point; % for e = 2:n % dist_temp(next_point,:) = nan; % [~,next_point] = min(dist_temp(:,next_point)); % route(e) = next_point; end % 1- if check_routes == true route = check_tour(route); end % arr_route_in(k,:) = route; end % ( ) % if top_roulete_method == true arr_several = psr(dist,ctrrm,n,ts,check_routes,p_change); arr_route_in = [arr_route_in; arr_several]; end % % if roulete_method == true % , [randRoute] = prr(returndist,crrm,n,check_routes); % arr_route_in = [arr_route_in;randRoute]; end % % % parfor r = 1:size(arr_route_in, 1); % temp_route = arr_route_in(r,:); % [~,numb_ix] = find(temp_route == 1); % if numb_ix ~= 1 % temp_route = [temp_route(numb_ix:end),temp_route(1:numb_ix-1)]; % elseif numb_ix == n % temp_route = fliplr(temp_route); % end % arr_route_in(r,:) = temp_route; % end % , arr_route_in = unique(arr_route_in,'rows'); %------------------------------------------------------------------% % () arr_tour_out = zeros(size(arr_route_in,1),n); % () arr_result = zeros(size(arr_route_in,1),1); %------------------------------------------------------------------------% % ( ) spdvar = size(arr_route_in, 1); parfor i = 1:spdvar % [tour, distance] = all_brute_first(dist,arr_route_in(i,:),n); [tour, distance] = all_brute_best(dist,arr_route_in(i,:),n); arr_tour_out(i,:) = tour; arr_result(i,1) = distance; end % toc % [best_length,best_indx] = min(arr_result); % 1- best_route = arr_tour_out(best_indx,:); % % % mean(arr_result) %-----------------------------------------------------------------% tsp_plot = figure(1); set(tsp_plot,'name','TSP','numbertitle','off'... ,'color','w','menubar','none') set(gcf,'position',[2391 410 476 409]); opt_tour(:,[1,2]) = cities(best_route,[1,2]); plot([opt_tour(:,1);opt_tour(1,1)],[opt_tour(:,2);opt_tour(1,2)],'-g.', ... 'linewidth',1.5) set(gca,'color','k') title(['TSP best distance = ',num2str(round(best_length,2))]) xlabel('cities'); ylabel('cities') %------------------------------------------------------------------------% fprintf('%s %d\n',' ',round(best_length)); clearvars -except cities arr_result arr_tour_out arr_route_in ... n best_length opt_tour dist openclose
Source: https://habr.com/ru/post/335016/
All Articles