强迫症会根治吗:基于遗传算法(GA)的多变量旅行商问题(TSP)
来源:百度文库 编辑:偶看新闻 时间:2024/10/05 12:07:19
MTSPV_GA可变多个旅行商问题(M - TSP)遗传算法(GA)
找到了(附近)的M - TSP的变化(即具有最佳的解决方案
可变数量的推销员)设立GA在搜索
的最短路径(最小距离为推销员需要前往
每个城市一次,并返回其起始位置)
摘要:
1。每个业务员一套独特的城市,并完成
回城的路线,他开始从
2。每个城市访问过的一个业务员
输入:
XY(浮动)是一个城市位置的NX2矩阵,其中N是城市数量
DMAT(浮动)是NxN的矩阵点的距离或成本点
MIN_TOUR(标量整数)是任何推销员的最低游览长度
POP_SIZE(标量整数)人口规模(应该是能被4整除)
NUM_ITER(标量整数)是算法运行所需的迭代数
如果真正SHOW_PROG(标量逻辑)显示GA进展
SHOW_RES(标量逻辑)的GA结果显示,如果为true
输出:
OPT_RTE(整型数组)是由算法发现的最佳途径
OPT_BRK(整型数组)是路线破发点的名单(这些指定的指数
到用来获取个人业务员路线路线)
MIN_DIST(标浮动)由推销员走过的总距离
路由/断点详情:
如果有10个城市和3个推销员,一个可能的途径/休息
组合可能是:RTE = [5 6 9 1 4 2 8 10 3 7],brks = [3 7]
两者合计,这些代表的解决方案[5 6 9] [1 4 2 8] [10 3 7]
指定为如下3推销员路线:
。业务员1旅游城市5到6日至9日,回到5
。业务员2旅游城市1至4日至2日至8回1
。业务员3旅游城市10至3日至7回10
function varargout = mtspv_ga(xy,dmat,min_tour,pop_size,num_iter,show_prog,show_res)
% MTSPV_GA Variable Multiple Traveling Salesman Problem (M-TSP) Genetic Algorithm (GA)
% Finds a (near) optimal solution to a variation of the M-TSP (that has a
% variable number of salesmen) by setting up a GA to search for the
% shortest route (least distance needed for the salesmen to travel to
% each city exactly once and return to their starting locations)
%
% Summary:
% 1. Each salesman travels to a unique set of cities and completes the
% route by returning to the city he started from
% 2. Each city is visited by exactly one salesman
%
% Input:
% 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 or costs
% MIN_TOUR (scalar integer) is the minimum tour length for any of the salesmen
% POP_SIZE (scalar integer) is the size of the population (should be divisible by 4)
% NUM_ITER (scalar integer) is the number of desired iterations for the algorithm to run
% SHOW_PROG (scalar logical) shows the GA progress if true
% SHOW_RES (scalar logical) shows the GA results if true
%
% Output:
% OPT_RTE (integer array) is the best route found by the algorithm
% OPT_BRK (integer array) is the list of route break points (these specify the indices
% into the route used to obtain the individual salesman routes)
% MIN_DIST (scalar float) is the total distance traveled by the salesmen
%
% Route/Breakpoint Details:
% If there are 10 cities and 3 salesmen, a possible route/break
% combination might be: rte = [5 6 9 1 4 2 8 10 3 7], brks = [3 7]
% Taken together, these represent the solution [5 6 9][1 4 2 8][10 3 7],
% which designates the routes for the 3 salesmen as follows:
% . Salesman 1 travels from city 5 to 6 to 9 and back to 5
% . Salesman 2 travels from city 1 to 4 to 2 to 8 and back to 1
% . Salesman 3 travels from city 10 to 3 to 7 and back to 10
%
% Example:
% n = 35;
% xy = 10*rand(n,2);
% min_tour = 3;
% pop_size = 40;
% num_iter = 5e3;
% a = meshgrid(1:n);
% dmat = reshape(sqrt(sum((xy(a,:)-xy(a',:)).^2,2)),n,n);
% [opt_rte,opt_brk,min_dist] = mtspv_ga(xy,dmat,min_tour,pop_size,num_iter,1,1);
%
% Author: Joseph Kirk
% Email: jdkirk630@gmail.com
% Release: 1.1
% Release Date: 9/21/08% Process Inputs and Initialize Defaults
nargs = 7;
for k = nargin:nargs-1
switch k
case 0
xy = 10*rand(40,2);
case 1
N = size(xy,1);
a = meshgrid(1:N);
dmat = reshape(sqrt(sum((xy(a,:)-xy(a',:)).^2,2)),N,N);
case 2
min_tour = 3;
case 3
pop_size = 80;
case 4
num_iter = 5e3;
case 5
show_prog = 1;
case 6
show_res = 1;
otherwise
end
end% Verify Inputs
N = size(xy,1);
[nr,nc] = size(dmat);
if N ~= nr || N ~= nc
error('Invalid XY or DMAT inputs!')
end
n = N;% Sanity Checks
min_tour = max(1,min(n,round(real(min_tour(1)))));
pop_size = max(8,8*ceil(pop_size(1)/8));
num_iter = max(1,round(real(num_iter(1))));
show_prog = logical(show_prog(1));
show_res = logical(show_res(1));% Initialize the Populations
pop_rte = zeros(pop_size,n); % population of routes
pop_brk = cell(pop_size,1); % population of breaks
for k = 1:pop_size
pop_rte(k,:) = randperm(n);
pop_brk{k} = randbreak();
end
tmp_pop_rte = zeros(8,n);
tmp_pop_brk = cell(8,1);
new_pop_rte = zeros(pop_size,n);
new_pop_brk = cell(pop_size,1);% Select the Colors for the Plotted Routes
clr = hsv(floor(n/min_tour));% Run the GA
global_min = Inf;
total_dist = zeros(1,pop_size);
dist_history = zeros(1,num_iter);
if show_prog
pfig = figure('Name','MTSPV_GA | Current Best Solution','Numbertitle','off');
end
for iter = 1:num_iter
% Evaluate Each Population Member (Calculate Total Distance)
for p = 1:pop_size
d = 0;
p_rte = pop_rte(p,:);
p_brk = pop_brk{p};
salesmen = length(p_brk)+1;
rng = [[1 p_brk+1];[p_brk n]]';
for s = 1:salesmen
d = d + dmat(p_rte(rng(s,2)),p_rte(rng(s,1)));
for k = rng(s,1):rng(s,2)-1
d = d + dmat(p_rte(k),p_rte(k+1));
end
end
total_dist(p) = d;
end % Find the Best Route in the Population
[min_dist,index] = min(total_dist);
dist_history(iter) = min_dist;
if min_dist < global_min
global_min = min_dist;
opt_rte = pop_rte(index,:);
opt_brk = pop_brk{index};
salesmen = length(opt_brk)+1;
rng = [[1 opt_brk+1];[opt_brk n]]';
if show_prog
% Plot the Best Route
figure(pfig);
for s = 1:salesmen
rte = opt_rte([rng(s,1):rng(s,2) rng(s,1)]);
plot(xy(rte,1),xy(rte,2),'.-','Color',clr(s,:));
title(sprintf(['Total Distance = %1.4f, Salesmen = %d, ' ...
'Iterations = %d'],min_dist,salesmen,iter));
hold on
end
hold off
end
end % Genetic Algorithm Operators
rand_grouping = randperm(pop_size);
for p = 8:8:pop_size
rtes = pop_rte(rand_grouping(p-7:p),:);
brks = pop_brk(rand_grouping(p-7:p));
dists = total_dist(rand_grouping(p-7:p));
[ignore,idx] = min(dists);
best_of_8_rte = rtes(idx,:);
best_of_8_brk = brks{idx};
rte_ins_pts = sort(ceil(n*rand(1,2)));
I = rte_ins_pts(1);
J = rte_ins_pts(2);
for k = 1:8 % Generate New Solutions
tmp_pop_rte(k,:) = best_of_8_rte;
tmp_pop_brk{k} = best_of_8_brk;
switch k
case 2 % Flip
tmp_pop_rte(k,I:J) = fliplr(tmp_pop_rte(k,I:J));
case 3 % Swap
tmp_pop_rte(k,[I J]) = tmp_pop_rte(k,[J I]);
case 4 % Slide
tmp_pop_rte(k,I:J) = tmp_pop_rte(k,[I+1:J I]);
case 5 % Change Breaks
tmp_pop_brk{k} = randbreak();
case 6 % Flip, Change Breaks
tmp_pop_rte(k,I:J) = fliplr(tmp_pop_rte(k,I:J));
tmp_pop_brk{k} = randbreak();
case 7 % Swap, Change Breaks
tmp_pop_rte(k,[I J]) = tmp_pop_rte(k,[J I]);
tmp_pop_brk{k} = randbreak();
case 8 % Slide, Change Breaks
tmp_pop_rte(k,I:J) = tmp_pop_rte(k,[I+1:J I]);
tmp_pop_brk{k} = randbreak();
otherwise % Do Nothing
end
end
new_pop_rte(p-7:p,:) = tmp_pop_rte;
new_pop_brk(p-7:p) = tmp_pop_brk;
end
pop_rte = new_pop_rte;
pop_brk = new_pop_brk;
endif show_res
% Plots
figure('Name','MTSPV_GA | Results','Numbertitle','off');
subplot(2,2,1);
plot(xy(:,1),xy(:,2),'k.');
title('City Locations');
subplot(2,2,2);
imagesc(dmat(opt_rte,opt_rte));
title('Distance Matrix');
salesmen = length(opt_brk)+1;
subplot(2,2,3);
rng = [[1 opt_brk+1];[opt_brk n]]';
for s = 1:salesmen
rte = opt_rte([rng(s,1):rng(s,2) rng(s,1)]);
plot(xy(rte,1),xy(rte,2),'.-','Color',clr(s,:));
title(sprintf('Total Distance = %1.4f',min_dist));
hold on;
end
subplot(2,2,4);
plot(dist_history,'b','LineWidth',2)
title('Best Solution History');
set(gca,'XLim',[0 num_iter+1],'YLim',[0 1.1*max([1 dist_history])]);
end% Return Outputs
if nargout
varargout{1} = opt_rte;
varargout{2} = opt_brk;
varargout{3} = min_dist;
end % Generate Random Set of Breaks
function breaks = randbreak()
salesmen = ceil(floor(n/min_tour)*rand);
num_brks = salesmen - 1;
dof = n - min_tour*salesmen; % degrees of freedom
addto = ones(1,dof+1);
for kk = 2:num_brks
addto = cumsum(addto);
end
cum_prob = cumsum(addto)/sum(addto);
num_adjust = find(rand < cum_prob,1)-1;
spaces = ceil(num_brks*rand(1,num_adjust));
adjust = zeros(1,num_brks);
for kk = 1:num_brks
adjust(kk) = sum(spaces == kk);
end
breaks = min_tour*(1:num_brks) + cumsum(adjust);
end
end
找到了(附近)的M - TSP的变化(即具有最佳的解决方案
可变数量的推销员)设立GA在搜索
的最短路径(最小距离为推销员需要前往
每个城市一次,并返回其起始位置)
摘要:
1。每个业务员一套独特的城市,并完成
回城的路线,他开始从
2。每个城市访问过的一个业务员
输入:
XY(浮动)是一个城市位置的NX2矩阵,其中N是城市数量
DMAT(浮动)是NxN的矩阵点的距离或成本点
MIN_TOUR(标量整数)是任何推销员的最低游览长度
POP_SIZE(标量整数)人口规模(应该是能被4整除)
NUM_ITER(标量整数)是算法运行所需的迭代数
如果真正SHOW_PROG(标量逻辑)显示GA进展
SHOW_RES(标量逻辑)的GA结果显示,如果为true
输出:
OPT_RTE(整型数组)是由算法发现的最佳途径
OPT_BRK(整型数组)是路线破发点的名单(这些指定的指数
到用来获取个人业务员路线路线)
MIN_DIST(标浮动)由推销员走过的总距离
路由/断点详情:
如果有10个城市和3个推销员,一个可能的途径/休息
组合可能是:RTE = [5 6 9 1 4 2 8 10 3 7],brks = [3 7]
两者合计,这些代表的解决方案[5 6 9] [1 4 2 8] [10 3 7]
指定为如下3推销员路线:
。业务员1旅游城市5到6日至9日,回到5
。业务员2旅游城市1至4日至2日至8回1
。业务员3旅游城市10至3日至7回10
function varargout = mtspv_ga(xy,dmat,min_tour,pop_size,num_iter,show_prog,show_res)
% MTSPV_GA Variable Multiple Traveling Salesman Problem (M-TSP) Genetic Algorithm (GA)
% Finds a (near) optimal solution to a variation of the M-TSP (that has a
% variable number of salesmen) by setting up a GA to search for the
% shortest route (least distance needed for the salesmen to travel to
% each city exactly once and return to their starting locations)
%
% Summary:
% 1. Each salesman travels to a unique set of cities and completes the
% route by returning to the city he started from
% 2. Each city is visited by exactly one salesman
%
% Input:
% 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 or costs
% MIN_TOUR (scalar integer) is the minimum tour length for any of the salesmen
% POP_SIZE (scalar integer) is the size of the population (should be divisible by 4)
% NUM_ITER (scalar integer) is the number of desired iterations for the algorithm to run
% SHOW_PROG (scalar logical) shows the GA progress if true
% SHOW_RES (scalar logical) shows the GA results if true
%
% Output:
% OPT_RTE (integer array) is the best route found by the algorithm
% OPT_BRK (integer array) is the list of route break points (these specify the indices
% into the route used to obtain the individual salesman routes)
% MIN_DIST (scalar float) is the total distance traveled by the salesmen
%
% Route/Breakpoint Details:
% If there are 10 cities and 3 salesmen, a possible route/break
% combination might be: rte = [5 6 9 1 4 2 8 10 3 7], brks = [3 7]
% Taken together, these represent the solution [5 6 9][1 4 2 8][10 3 7],
% which designates the routes for the 3 salesmen as follows:
% . Salesman 1 travels from city 5 to 6 to 9 and back to 5
% . Salesman 2 travels from city 1 to 4 to 2 to 8 and back to 1
% . Salesman 3 travels from city 10 to 3 to 7 and back to 10
%
% Example:
% n = 35;
% xy = 10*rand(n,2);
% min_tour = 3;
% pop_size = 40;
% num_iter = 5e3;
% a = meshgrid(1:n);
% dmat = reshape(sqrt(sum((xy(a,:)-xy(a',:)).^2,2)),n,n);
% [opt_rte,opt_brk,min_dist] = mtspv_ga(xy,dmat,min_tour,pop_size,num_iter,1,1);
%
% Author: Joseph Kirk
% Email: jdkirk630@gmail.com
% Release: 1.1
% Release Date: 9/21/08% Process Inputs and Initialize Defaults
nargs = 7;
for k = nargin:nargs-1
switch k
case 0
xy = 10*rand(40,2);
case 1
N = size(xy,1);
a = meshgrid(1:N);
dmat = reshape(sqrt(sum((xy(a,:)-xy(a',:)).^2,2)),N,N);
case 2
min_tour = 3;
case 3
pop_size = 80;
case 4
num_iter = 5e3;
case 5
show_prog = 1;
case 6
show_res = 1;
otherwise
end
end% Verify Inputs
N = size(xy,1);
[nr,nc] = size(dmat);
if N ~= nr || N ~= nc
error('Invalid XY or DMAT inputs!')
end
n = N;% Sanity Checks
min_tour = max(1,min(n,round(real(min_tour(1)))));
pop_size = max(8,8*ceil(pop_size(1)/8));
num_iter = max(1,round(real(num_iter(1))));
show_prog = logical(show_prog(1));
show_res = logical(show_res(1));% Initialize the Populations
pop_rte = zeros(pop_size,n); % population of routes
pop_brk = cell(pop_size,1); % population of breaks
for k = 1:pop_size
pop_rte(k,:) = randperm(n);
pop_brk{k} = randbreak();
end
tmp_pop_rte = zeros(8,n);
tmp_pop_brk = cell(8,1);
new_pop_rte = zeros(pop_size,n);
new_pop_brk = cell(pop_size,1);% Select the Colors for the Plotted Routes
clr = hsv(floor(n/min_tour));% Run the GA
global_min = Inf;
total_dist = zeros(1,pop_size);
dist_history = zeros(1,num_iter);
if show_prog
pfig = figure('Name','MTSPV_GA | Current Best Solution','Numbertitle','off');
end
for iter = 1:num_iter
% Evaluate Each Population Member (Calculate Total Distance)
for p = 1:pop_size
d = 0;
p_rte = pop_rte(p,:);
p_brk = pop_brk{p};
salesmen = length(p_brk)+1;
rng = [[1 p_brk+1];[p_brk n]]';
for s = 1:salesmen
d = d + dmat(p_rte(rng(s,2)),p_rte(rng(s,1)));
for k = rng(s,1):rng(s,2)-1
d = d + dmat(p_rte(k),p_rte(k+1));
end
end
total_dist(p) = d;
end % Find the Best Route in the Population
[min_dist,index] = min(total_dist);
dist_history(iter) = min_dist;
if min_dist < global_min
global_min = min_dist;
opt_rte = pop_rte(index,:);
opt_brk = pop_brk{index};
salesmen = length(opt_brk)+1;
rng = [[1 opt_brk+1];[opt_brk n]]';
if show_prog
% Plot the Best Route
figure(pfig);
for s = 1:salesmen
rte = opt_rte([rng(s,1):rng(s,2) rng(s,1)]);
plot(xy(rte,1),xy(rte,2),'.-','Color',clr(s,:));
title(sprintf(['Total Distance = %1.4f, Salesmen = %d, ' ...
'Iterations = %d'],min_dist,salesmen,iter));
hold on
end
hold off
end
end % Genetic Algorithm Operators
rand_grouping = randperm(pop_size);
for p = 8:8:pop_size
rtes = pop_rte(rand_grouping(p-7:p),:);
brks = pop_brk(rand_grouping(p-7:p));
dists = total_dist(rand_grouping(p-7:p));
[ignore,idx] = min(dists);
best_of_8_rte = rtes(idx,:);
best_of_8_brk = brks{idx};
rte_ins_pts = sort(ceil(n*rand(1,2)));
I = rte_ins_pts(1);
J = rte_ins_pts(2);
for k = 1:8 % Generate New Solutions
tmp_pop_rte(k,:) = best_of_8_rte;
tmp_pop_brk{k} = best_of_8_brk;
switch k
case 2 % Flip
tmp_pop_rte(k,I:J) = fliplr(tmp_pop_rte(k,I:J));
case 3 % Swap
tmp_pop_rte(k,[I J]) = tmp_pop_rte(k,[J I]);
case 4 % Slide
tmp_pop_rte(k,I:J) = tmp_pop_rte(k,[I+1:J I]);
case 5 % Change Breaks
tmp_pop_brk{k} = randbreak();
case 6 % Flip, Change Breaks
tmp_pop_rte(k,I:J) = fliplr(tmp_pop_rte(k,I:J));
tmp_pop_brk{k} = randbreak();
case 7 % Swap, Change Breaks
tmp_pop_rte(k,[I J]) = tmp_pop_rte(k,[J I]);
tmp_pop_brk{k} = randbreak();
case 8 % Slide, Change Breaks
tmp_pop_rte(k,I:J) = tmp_pop_rte(k,[I+1:J I]);
tmp_pop_brk{k} = randbreak();
otherwise % Do Nothing
end
end
new_pop_rte(p-7:p,:) = tmp_pop_rte;
new_pop_brk(p-7:p) = tmp_pop_brk;
end
pop_rte = new_pop_rte;
pop_brk = new_pop_brk;
endif show_res
% Plots
figure('Name','MTSPV_GA | Results','Numbertitle','off');
subplot(2,2,1);
plot(xy(:,1),xy(:,2),'k.');
title('City Locations');
subplot(2,2,2);
imagesc(dmat(opt_rte,opt_rte));
title('Distance Matrix');
salesmen = length(opt_brk)+1;
subplot(2,2,3);
rng = [[1 opt_brk+1];[opt_brk n]]';
for s = 1:salesmen
rte = opt_rte([rng(s,1):rng(s,2) rng(s,1)]);
plot(xy(rte,1),xy(rte,2),'.-','Color',clr(s,:));
title(sprintf('Total Distance = %1.4f',min_dist));
hold on;
end
subplot(2,2,4);
plot(dist_history,'b','LineWidth',2)
title('Best Solution History');
set(gca,'XLim',[0 num_iter+1],'YLim',[0 1.1*max([1 dist_history])]);
end% Return Outputs
if nargout
varargout{1} = opt_rte;
varargout{2} = opt_brk;
varargout{3} = min_dist;
end % Generate Random Set of Breaks
function breaks = randbreak()
salesmen = ceil(floor(n/min_tour)*rand);
num_brks = salesmen - 1;
dof = n - min_tour*salesmen; % degrees of freedom
addto = ones(1,dof+1);
for kk = 2:num_brks
addto = cumsum(addto);
end
cum_prob = cumsum(addto)/sum(addto);
num_adjust = find(rand < cum_prob,1)-1;
spaces = ceil(num_brks*rand(1,num_adjust));
adjust = zeros(1,num_brks);
for kk = 1:num_brks
adjust(kk) = sum(spaces == kk);
end
breaks = min_tour*(1:num_brks) + cumsum(adjust);
end
end
matlab遗传算法工具箱中ga函数的用法
有人做基于遗传算法的资源调度吗?
基于Matlab的函数优化遗传算法程序
遗传算法的一个问题
请解释一下“多变量内插问题”的数学原理?
哪能下到基于遗传-bp算法的图象边缘检测的原代码
谁有"基于遗传算法的图象分割"MFC应用程序
遗传算法的改进
基于禁忌搜索算法的TSP 问题的分析
那位好心人有模拟退火算法的旅行商问题的程序???
一个最优化问题:求多变量函数在一定区间的最小值!
八皇后问题的遗传算法解法,c语言编写
遗传算法工具箱的使用方法
遗传算法
量旅行箱的长度
关于C语言多变量全局变量的消除
介绍几种基于原根和离散对数问题的算法
电感量的计算法
遗传算法的编程 (有算法步骤) 有重赏
求电梯调度算法与遗传算法的PASCAL资料
遗传算法的汇编编程 有重赏
那位打侠有遗传算法的C源代码?
遗传算法怎么用的啊!
遗传算法在数学上的应用